-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathALWindowController.m
More file actions
337 lines (291 loc) · 9.87 KB
/
ALWindowController.m
File metadata and controls
337 lines (291 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// ALWindowController.m
// ActiveLog
//
// Created by Uncle MiF on 9/10/10.
// Copyright 2010 Deep IT. All rights reserved.
//
#import "ALWindowController.h"
#import "DINSLogFilter+Remote.h"
#import "DINSLogObserver.h"
#import "DIContextualMenu.h"
@implementation ALWindowController
@synthesize registeredApps, enabledZones, persistentCheck, addZonePanel, zoneNameField;
-(void)awakeFromNib
{
rApps = [NSMutableArray new];
eAppZones = [NSMutableDictionary new];
detectedModules = [NSMutableDictionary new];
[self.window setFrameAutosaveName:@"ALMainWindow"];
[self.window setTitle:[[self.window title] stringByAppendingFormat:@" (%i)",getpid()]];
[self refresh:self];
}
-(IBAction)refresh:(id)sender
{
NSLogDebugMethod;
[self willChangeValueForKey:@"eZones"];
[eZones autorelease], eZones = nil;
[self didChangeValueForKey:@"eZones"];
[enabledZones reloadData];
[self setEZoneSelected:NO];
[eAppZones removeAllObjects];
[rApps removeAllObjects];
[self registeredAppsReloadData];
[DINSLogObserver sendCommand:LOG_MODULE_CMD_REFRESH forTarget:nil withOptions:nil];
}
-(IBAction)persistentChanged:(id)sender
{
NSLogDebugMethod;
NSInteger row = [registeredApps selectedRow];
if (row != -1)
{
NSString * task = [rApps objectAtIndex:row];
NSNumber * persistent = [NSNumber numberWithBool:[persistentCheck state]];
id info = [eAppZones objectForKey:task];
[info setObject:persistent forKey:LOG_MODULE_PERSISTENT_STATE];
[DINSLogObserver sendCommand:LOG_MODULE_CMD_PERSISTENT
forTarget:[info objectForKey:LOG_MODULE_PID]
withOptions:[NSDictionary dictionaryWithObject:persistent forKey:LOG_MODULE_PERSISTENT_STATE]];
}
}
-(void)addKnownZone:(id)sender
{
[zoneNameField setStringValue:@""];
[addZonePanel makeKeyAndOrderFront:self];
}
-(void)addDetectedZone:(id)sender
{
[zoneNameField setStringValue:[sender title]];
[self addZoneFinalize:self];
}
-(NSArray*)orderedObjects:(NSArray*)strArr
{
if (!strArr)
return strArr;
return [strArr sortedArrayUsingSelector:@selector(compare:)];
}
-(IBAction)addZone:(id)sender
{
NSLogDebug(NSPF(@"detectedModules: %@",detectedModules));
if (!eZones)
return;
if (sender)
return [self performSelector:_cmd withObject:nil afterDelay:0.0];
NSInteger selectedRow = [registeredApps selectedRow];
if (selectedRow == -1)
return;
NSString * task = [rApps objectAtIndex:selectedRow];
if (!task)
return;
NSMenuItem * detectedZonesItem = [DIContextualMenu itemWithTitle:@"Known Zone" action:@selector(addKnownZone:) keyEquivalent:@""];
DIContextualMenu* contextualMenu =
[DIContextualMenu contextualMenuWithDelegate:self andItems:
[NSArray arrayWithObjects:
detectedZonesItem,
nil
]
];
DIContextualMenu * detectedZones = [DIContextualMenu contextualMenuWithDelegate:self];
NSArray * modules = [self orderedObjects:[[detectedModules objectForKey:task] allKeys]];
for (NSString* module in modules)
{
if ([eZones containsObject:module])
continue;
NSMenuItem * moduleItem = [DIContextualMenu itemWithTitle:module action:@selector(addDetectedZone:) keyEquivalent:@""];
[detectedZones addItem:moduleItem];
NSArray * zones = [self orderedObjects:[[[detectedModules objectForKey:task] objectForKey:module] allObjects]];
if (zones)
{
DIContextualMenu * deepZones = [DIContextualMenu contextualMenuWithDelegate:self];
for (NSString* zone in zones)
{
if ([eZones containsObject:zone])
continue;
NSMenuItem * zoneItem = [DIContextualMenu itemWithTitle:zone action:@selector(addDetectedZone:) keyEquivalent:@""];
[deepZones addItem:zoneItem];
}
if ([deepZones numberOfItems])
[detectedZones setSubmenu:[deepZones menu] forItem:moduleItem];
}
}
if ([detectedZones numberOfItems])
{
[contextualMenu setSubmenu:[detectedZones menu] forItem:detectedZonesItem];
[contextualMenu showMenuInWindow:self.window];
}
else
[self addKnownZone:self];
}
-(IBAction)addZoneFinalize:(id)sender
{
NSLogDebugMethod;
[addZonePanel orderOut:self];
NSString * zone = [zoneNameField stringValue];
if (![eZones containsObject:zone])
{
NSInteger row = [registeredApps selectedRow];
if (row != -1)
{
NSTask * task = [rApps objectAtIndex:row];
NSMutableDictionary * info = [eAppZones objectForKey:task];
NSString * target = [info objectForKey:LOG_MODULE_PID];
id announceObject = [info objectForKey:LOG_MODULE_ANNOUNCE_OBJECT];
if ([announceObject isKindOfClass:[NSString class]])
[info setObject:(announceObject = [NSMutableSet set]) forKey:LOG_MODULE_ANNOUNCE_OBJECT];
if (![announceObject isKindOfClass:[NSMutableSet class]])
[info setObject:(announceObject = [[announceObject mutableCopy] autorelease]) forKey:LOG_MODULE_ANNOUNCE_OBJECT];
[announceObject addObject:zone];// module set
[eZones addObject:zone];
[enabledZones reloadData];
[DINSLogObserver sendCommand:LOG_MODULE_CMD_ENABLE forTarget:target
withOptions:[NSDictionary dictionaryWithObject:zone forKey:LOG_MODULE_NAME]];
}
}
}
-(IBAction)removeZone:(id)sender
{
NSLogDebugMethod;
if (!eZones)
return;
NSInteger row = [enabledZones selectedRow];
if (row != -1)
{
NSString * zone = [eZones objectAtIndex:row];
row = [registeredApps selectedRow];
if (row != -1)
{
NSTask * task = [rApps objectAtIndex:row];
NSMutableDictionary * info = [eAppZones objectForKey:task];
NSString * target = [info objectForKey:LOG_MODULE_PID];
[DINSLogObserver sendCommand:LOG_MODULE_CMD_DISABLE forTarget:target
withOptions:[NSDictionary dictionaryWithObject:zone forKey:LOG_MODULE_NAME]];
id announceObject = [info objectForKey:LOG_MODULE_ANNOUNCE_OBJECT];
if (![announceObject isKindOfClass:[NSMutableSet set]])
[info setObject:(announceObject = [[announceObject mutableCopy] autorelease]) forKey:LOG_MODULE_ANNOUNCE_OBJECT];
[announceObject removeObject:zone];// module set
[eZones removeObject:zone];
[enabledZones reloadData];
if ([eZones count])
{
if (row >= [eZones count])
row = [eZones count] - 1;
[enabledZones selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
}
}
}
}
-(NSString*)processKey:(NSDictionary*)info
{
return [NSString stringWithFormat:@"%@ (%@)",[[info objectForKey:LOG_MODULE_PATH] lastPathComponent],[info objectForKey:LOG_MODULE_PID]];
}
-(void)registeredAppsReloadData
{
NSLogDebugMethod;
[registeredApps reloadData];
NSInteger selectedRow = [registeredApps selectedRow];
if (selectedRow == -1)
{
if ([rApps count])
[registeredApps selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
}
else
if (!eZones)
[self tableViewSelectionDidChange:[NSNotification notificationWithName:NSTableViewSelectionDidChangeNotification object:registeredApps]];
}
-(BOOL)processAnnounce:(NSDictionary*)info
{
NSLogZoneDebug(ActiveLog,NSPF(@"%@",info));
id announceObject = [info objectForKey:LOG_MODULE_ANNOUNCE_OBJECT];
if ([[info objectForKey:LOG_MODULE_ANNOUNCE_NAME] isEqualToString:LOG_MODULE_REGISTER])
{
NSLogZoneDebug(ActiveLog,NSPF(@"register: %@",announceObject));
NSString * task = [self processKey:info];
[eAppZones setValue:info forKey:task];
if (![rApps containsObject:task])
[rApps addObject:task];
[detectedModules setObject:[info objectForKey:LOG_MODULE_ZONES] forKey:task];
[self registeredAppsReloadData];
}
else
if ([[info objectForKey:LOG_MODULE_ANNOUNCE_NAME] isEqualToString:LOG_MODULE_UNREGISTER])
{
NSLogZoneDebug(ActiveLog,NSPF(@"unregister: %@",announceObject));
NSString * task = [self processKey:info];
if ([registeredApps selectedRow] == -1 || [registeredApps selectedRow] == [rApps indexOfObject:task])
{
[self willChangeValueForKey:@"eZones"];
[eZones autorelease], eZones = nil;
[self didChangeValueForKey:@"eZones"];
[enabledZones reloadData];
[self setEZoneSelected:NO];
}
[rApps removeObject:task];
[eAppZones removeObjectForKey:task];
[detectedModules removeObjectForKey:task];
[self registeredAppsReloadData];
}
else
if ([[info objectForKey:LOG_MODULE_ANNOUNCE_NAME] isEqualToString:LOG_MODULE_ZONES])
{
NSString * task = [self processKey:info];
[detectedModules setObject:announceObject forKey:task];
}
return NO;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
if (aTableView == registeredApps)
return [rApps count];
if (aTableView == enabledZones)
return [eZones count];
NSLogDebug(NSPF(@"unknown table %@",aTableView));
return 0;
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
if (aTableView == registeredApps)
{
return [rApps objectAtIndex:rowIndex];
}
if (aTableView == enabledZones)
{
return [eZones objectAtIndex:rowIndex];
}
NSLogDebug(NSPF(@"unknown table %@",aTableView));
return nil;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
NSLogDebugMethod;
if ([aNotification object] == registeredApps)
{
NSInteger row = [registeredApps selectedRow];
if (row != -1)
{
NSString * task = [rApps objectAtIndex:row];
NSDictionary * info = [eAppZones objectForKey:task];
id infoObject = [info objectForKey:LOG_MODULE_ANNOUNCE_OBJECT];
[self willChangeValueForKey:@"eZones"];
[eZones autorelease];
eZones = [infoObject isKindOfClass:[NSSet class]] ?
[[infoObject allObjects] mutableCopy] : [NSMutableArray new];
[self didChangeValueForKey:@"eZones"];
[enabledZones reloadData];
[self setEZoneSelected:[enabledZones selectedRow] != -1];
[persistentCheck setState:[[info objectForKey:LOG_MODULE_PERSISTENT_STATE] boolValue]];
}
}
else if ([aNotification object] == enabledZones)
{
[self setEZoneSelected:[enabledZones selectedRow] != -1];
}
}
-(void)setEZoneSelected:(BOOL)flag;// auto KVO
{
eZoneSelected = flag;
}
-(IBAction)help:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://deepitpro.com/en/articles/ActiveLog/info/"]];
}
@end