-
Notifications
You must be signed in to change notification settings - Fork 283
/
QSTrigger.m
358 lines (303 loc) · 9.19 KB
/
QSTrigger.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//
// QSTrigger.m
// Quicksilver
//
// Created by Alcor on 6/19/05.
// Copyright 2005 Blacktree, Inc. All rights reserved.
//
// Modified by p_j_r on 24/04/2011
#import "QSTriggersPrefPane.h"
#import "QSTrigger.h"
#import "QSTriggerCenter.h"
#import "QSCommand.h"
#import "QSRegistry.h"
@implementation QSTrigger
// KVO
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([key isEqualToString:@"name"]) {
keyPaths = [keyPaths setByAddingObject:@"command"];
} else if ([key isEqualToString:@"imageAndText"]) {
keyPaths = [keyPaths setByAddingObjectsFromSet:[NSSet setWithObjects:@"name",@"icon",nil]];
}
return keyPaths;
}
+ (id)triggerWithInfo:(NSDictionary *)dict {
return [self triggerWithDictionary:dict];
}
+ (id)triggerWithDictionary:(NSDictionary *)dict {
return [[self alloc] initWithDictionary:dict];
}
- (id)initWithInfo:(NSDictionary *)dict {
return [self initWithDictionary:dict];
}
- (id)initWithDictionary:(NSDictionary *)dict {
self = [super init];
if (self != nil) {
info = [dict mutableCopy];
}
return self;
}
- (id)init {
self = [super init];
if (self != nil) {
info = [[NSMutableDictionary alloc] init];
activated = YES;
}
return self;
}
- (void)dealloc {
#ifdef DEBUG
NSLog(@"dealloc %@", self);
#endif
}
- (NSString *)identifier {
return [info objectForKey:kItemID];
}
- (BOOL)isGroup {
return [[self type] isEqualToString:@"QSGroupTrigger"];
}
- (NSImage *)smallIcon {
if ([[self type] isEqualToString:@"QSGroupTrigger"]) {
return [[self manager] image];
} else {
[[self command] loadIcon];
NSImage *icon = [[self command] icon];
[icon setFlipped:NO];
[icon setSize:QSSize16];
return icon;
}
}
- (NSString *)name {
NSString *name = [info objectForKey:@"name"];
if (!name)
name = [[self command] name];
return name;
}
- (BOOL)hasCustomName {
if ([self isPreset]) return NO;
return [info objectForKey:@"name"] != nil;
}
- (void)setName:(NSString *)name {
if (![name length]) {
[info removeObjectForKey:@"name"];
} else if (name) {
[info setObject:name forKey:@"name"];
}
}
- (NSString *)type {
NSString *type = [info objectForKey:@"type"];
if (type)
return type;
[self setType:@"QSHotKeyTrigger"];
return @"QSHotKeyTrigger";
}
- (void)setType:(NSString *)type {
BOOL wasEnabled = [self enabled];
if (wasEnabled)
[self setEnabled:NO];
[info setObject:type forKey:@"type"];
[self initializeTrigger];
[[QSTriggerCenter sharedInstance] triggerChanged:self];
if (wasEnabled)
[self setEnabled:YES];
}
- (void)initializeTrigger {
[[self manager] initializeTrigger:self];
}
// On app change, checks all triggers to see if they should be enabled/disabled based on scope in prefs
- (void)rescope:(NSString *)ident {
// If the trigger's disabled there's no point rescoping
if([info objectForKey:@"enabled"] && ![[info objectForKey:@"enabled"] boolValue]) return;
// Scoped is 0 for unscoped triggers, -1 for 'disabled in application xxx' and +1 for 'enabled in application xxx'
NSInteger scoped = [[info objectForKey:@"applicationScopeType"] integerValue];
if (!scoped) return;
NSArray *apps = [info objectForKey:@"applicationScope"];
BOOL shouldActivate = [apps containsObject:ident];
if (scoped<0) shouldActivate = !shouldActivate;
[self setActivated:shouldActivate];
}
- (BOOL)execute {
if(!activated) {
return NO;
}
void (^block)(void) = ^{
[[self command] executeIgnoringModifiers];
if ([info objectForKey:@"oneshot"]) {
[self setEnabled:NO];
}
};
if (defaultBool(kExecuteInThread)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), block);
} else {
block();
}
return YES;
}
- (void)setCommand:(QSCommand*)newCommand {
if (newCommand != command) {
command = newCommand;
}
}
- (QSCommand *)command {
if (command)
return command;
id archivedCommand = [info objectForKey:@"command"];
if (archivedCommand)
command = [QSCommand commandWithInfo:archivedCommand];
return command;
}
- (NSArray *)commands {
NSArray * array = nil;
QSCommand * aCommand = [self command];
if (aCommand != nil)
array = [NSArray arrayWithObject:aCommand];
return array;
}
- (BOOL)isPreset {
return [[info objectForKey:kItemID] hasPrefix:@"QS"];
}
// if the command has a preset (e.g. a built in command/trigger for a plugin: performs a single action),
// it is a single string as opposed to an NSDict with an 'action' and 'direct object' (or 'indirect object')
- (BOOL)usesPresetCommand {
return ([[info objectForKey:@"command"] isKindOfClass:[NSString class]]);
}
- (NSDictionary *)dictionaryRepresentation {
NSMutableDictionary *dict = [info mutableCopy];
id rep = nil;
if ([self usesPresetCommand]) {
rep = [[self command] identifier];
} else {
rep = [[self command] objectForType:QSCommandType];
}
if (rep)
[dict setObject:rep forKey:@"command"];
else
[dict removeObjectForKey:@"command"];
return dict;
}
- (NSString *)triggerDescription {
return [[self manager] descriptionForTrigger:self];
}
- (void)setTriggerDescription:(NSString *)description {
[[self manager] trigger:self setTriggerDescription:description];
}
- (NSString *)description {
return [[self command] description];
}
- (id)manager {
return [QSReg instanceForKey:[info objectForKey:@"type"] inTable:QSTriggerManagers];
}
- (void)reactivate {
[self setEnabled:[self enabled]];
activated = [self enabled];
}
- (BOOL)activated {
return activated;
}
- (void)setActivated:(BOOL)flag {
if (![[info objectForKey:@"enabled"] boolValue])
return;
activated = flag;
QSHotKeyEvent *hotKeyEvent = [QSHotKeyEvent hotKeyWithIdentifier:[[self info] objectForKey:kItemID]];
// list of triggers with the same hotkey (i.e. the same ID)
NSArray *triggersWithSameID = [[QSTriggerCenter sharedInstance] triggersWithIDs:[hotKeyEvent identifiers]];
// get indexes of any triggers that are activated
NSIndexSet *ind = [triggersWithSameID indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(QSTrigger *trig, NSUInteger idx, BOOL *stop) {
return [trig activated];
}];
// set whether or not the hotkey is enabled (Quicksilver 'grabs' the hotkey) based on whether or not any triggers are active
if (flag) {
[hotKeyEvent setEnabled:([ind count] >= 1)];
} else {
[hotKeyEvent setEnabled:([ind count])];
}
}
- (BOOL)enabled {
return [[info objectForKey:@"enabled"] boolValue];
}
- (void)disable {
[self setEnabled:NO];
}
- (void)setEnabled:(BOOL)enabled {
[info setObject:[NSNumber numberWithBool:enabled] forKey:@"enabled"];
enabled ? [[self manager] enableTrigger:self] : [[self manager] disableTrigger:self];
[[QSTriggerCenter sharedInstance] triggerChanged:self];
}
// Fix for issue 47, http://github.com/tiennou/blacktree-alchemy/issues#issue/47
// Enable/Disable the trigger based on the enabled flag.
// Allows the flag to be changed without notifing the QSTriggerCenter, avoiding
// endless recursive calls and blowing out the stack.
- (void)setEnabledDoNotNotify:(BOOL)enabled {
[info setObject:[NSNumber numberWithBool:enabled] forKey:@"enabled"];
enabled ? [[self manager] enableTrigger:self] : [[self manager] disableTrigger:self];
activated = enabled;
}
- (id)objectForKey:(NSString *)key {
return [info objectForKey:key];
}
- (void)setObject:(id)object forKey:(NSString *)key {
[info setObject:object forKey:key];
}
- (id)valueForUndefinedKey:(NSString *)key {
return [info objectForKey:key];
}
//- (void)triggerChanged {
// [self disableTrigger:trigger];
// [self enableTrigger:trigger];
// [self writeTriggers];
// [[NSNotificationCenter defaultCenter] postNotificationName:QSTriggerChangedNotification object:trigger];
//}
- (NSMutableDictionary *)info {
return info;
}
//- (QSCommand *)command {
// return [[command retain] autorelease];
//}
//- (void)setCommand:(QSCommand *)value {
// if (command != value) {
// [command release];
// command = [value retain];
// }
//}
- (NSString *)triggerSet {
return [info valueForKey:@"set"];
}
// Tree methods
- (NSString *)parentID {
return [info valueForKey:@"parent"];
}
- (void)setParentID:(NSString *)ident {
if (!ident)
[info removeObjectForKey:@"parent"];
else
[info setObject:ident forKey:@"parent"];
}
- (NSString *)path {
if ([self parent])
return [[[self parent] path] stringByAppendingPathComponent:[self identifier]];
else
return [self identifier];
}
- (QSTrigger *)parent {
return [[QSTriggerCenter sharedInstance] triggerWithID:[self parentID]];
}
- (NSComparisonResult) compare:(QSTrigger *)compareObject {
return [[self name] compare:[compareObject name]];
}
- (BOOL)isLeaf {
if ([[self type] isEqualToString:@"QSGroupTrigger"]) return NO;
return YES;
}
- (NSArray *)children {
if (![[self type] isEqualToString:@"QSGroupTrigger"]) return nil;
return [[QSTriggerCenter sharedInstance] triggersWithParentID:[self identifier]];
}
// Image and text cell methods
- (id)imageAndText {return self;}
- (void)setImageAndText:(id)value {
[self setName:value];
}
- (NSString *)text {return [self name];}
- (NSImage *)image {return [self smallIcon];}
@end