/
RunningApplicationEvidenceSource.m
153 lines (120 loc) · 3.35 KB
/
RunningApplicationEvidenceSource.m
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
//
// RunningApplicationEvidenceSource.m
// ControlPlane
//
// Created by David Symonds on 23/5/07.
//
#import "RunningApplicationEvidenceSource.h"
#import "DSLogger.h"
@implementation RunningApplicationEvidenceSource
- (id)init
{
if (!(self = [super init]))
return nil;
applications = [[NSMutableArray alloc] init];
return self;
}
- (void)dealloc
{
[applications release];
[super dealloc];
}
- (void)doFullUpdate
{
NSArray *runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
NSMutableArray *apps = [[NSMutableArray alloc] initWithCapacity:[runningApps count]];
for (NSRunningApplication *runningApp in runningApps) {
NSString *identifier = [runningApp bundleIdentifier];
NSString *name = [runningApp localizedName];
// some programs, like mdworker, don't have a bundleIdentifier
if ([identifier length] == 0)
identifier = [runningApp localizedName];
if ([identifier length] != 0)
[apps addObject:[NSDictionary dictionaryWithObjectsAndKeys:
identifier, @"identifier", name, @"name", nil]];
}
[lock lock];
[applications setArray:apps];
[self setDataCollected:[applications count] > 0];
#ifdef DEBUG_MODE
DSLog(@"Running apps:\n%@", applications);
#endif
[lock unlock];
[apps release];
}
- (void)start
{
if (running)
return;
// register for notifications
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(doFullUpdate)
name:NSWorkspaceDidLaunchApplicationNotification
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(doFullUpdate)
name:NSWorkspaceDidTerminateApplicationNotification
object:nil];
[self doFullUpdate];
running = YES;
}
- (void)stop
{
if (!running)
return;
// remove notifications
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self
name:nil
object:nil];
[lock lock];
[applications removeAllObjects];
[self setDataCollected:NO];
[lock unlock];
running = NO;
}
- (NSString *)name
{
return @"RunningApplication";
}
- (BOOL)doesRuleMatch:(NSDictionary *)rule
{
NSString *param = [rule valueForKey:@"parameter"];
BOOL match = NO;
[lock lock];
NSEnumerator *en = [applications objectEnumerator];
NSDictionary *dict;
while ((dict = [en nextObject])) {
if ([[dict valueForKey:@"identifier"] isEqualToString:param]) {
match = YES;
break;
}
}
[lock unlock];
return match;
}
- (NSString *)getSuggestionLeadText:(NSString *)type
{
return NSLocalizedString(@"The following application running", @"In rule-adding dialog");
}
- (NSArray *)getSuggestions
{
[lock lock];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[applications count]];
NSEnumerator *en = [applications objectEnumerator];
NSDictionary *dict;
while ((dict = [en nextObject])) {
NSString *identifier = [dict valueForKey:@"identifier"];
NSString *desc = [NSString stringWithFormat:@"%@ (%@)", [dict valueForKey:@"name"], identifier];
[array addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
@"RunningApplication", @"type",
identifier, @"parameter",
desc, @"description", nil]];
}
[lock unlock];
return array;
}
- (NSString *) friendlyName {
return NSLocalizedString(@"Running Application", @"");
}
@end