forked from jigish/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperation.m
296 lines (272 loc) · 11.4 KB
/
Operation.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
//
// Operation.m
// Slate
//
// Created by Jigish Patel on 5/18/11.
// Copyright 2011 Jigish Patel. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses
#import "Operation.h"
#import "MoveOperation.h"
#import "ResizeOperation.h"
#import "ChainOperation.h"
#import "LayoutOperation.h"
#import "FocusOperation.h"
#import "SnapshotOperation.h"
#import "ActivateSnapshotOperation.h"
#import "DeleteSnapshotOperation.h"
#import "HintOperation.h"
#import "StringTokenizer.h"
#import "Constants.h"
#import "SlateLogger.h"
#import "SwitchOperation.h"
#import "GridOperation.h"
#import "SequenceOperation.h"
#import "VisibilityOperation.h"
#import "RelaunchOperation.h"
#import "ShellOperation.h"
#import "UndoOperation.h"
#import "SlateConfig.h"
#import <WebKit/WebKit.h>
#import "JSController.h"
#import "CornerOperation.h"
#import "ThrowOperation.h"
#import "NudgeOperation.h"
#import "PushOperation.h"
#import "JSInfoWrapper.h"
@implementation Operation
@synthesize opName;
@synthesize options;
@synthesize dynamicOptions;
- (id)init {
self = [super init];
if (self) {
[self setOpName:nil];
[self setOptions:[NSMutableDictionary dictionary]];
[self setDynamicOptions:[NSMutableDictionary dictionary]];
}
return self;
}
- (BOOL)doOperation {
return YES;
}
- (BOOL)doOperationWithAccessibilityWrapper:(AccessibilityWrapper *)aw screenWrapper:(ScreenWrapper *)sw {
return YES;
}
- (BOOL)testOperation {
return YES;
}
- (BOOL)shouldTakeUndoSnapshot {
return [[[SlateConfig getInstance] getConfig:UNDO_OPS] rangeOfString:[self opName]].location != NSNotFound;
}
- (NSArray *)requiredOptions {
return [NSArray array];
}
- (NSString *)checkRequiredOptions:(NSDictionary *)_options {
for (NSString *key in [self requiredOptions]) {
id opt = [_options objectForKey:key];
if (opt == nil) {
return key;
}
}
return nil;
}
- (void)initOptions:(NSDictionary *)_options {
NSString *missing = [self checkRequiredOptions:_options];
if (missing != nil) {
SlateLogger(@"ERROR: Missing Option in %@", [self opName]);
@throw([NSException exceptionWithName:@"Missing Option" reason:[NSString stringWithFormat:@"Missing option '%@' in '%@'", missing, [self opName]] userInfo:nil]);
return;
}
[self beforeInitOptions];
for (NSString *key in [_options allKeys]) {
id opt = [_options objectForKey:key];
if (opt == nil) { continue; }
if ([opt isKindOfClass:[NSString class]] || [opt isKindOfClass:[NSValue class]] || [opt isKindOfClass:[NSNumber class]] ||
[opt isKindOfClass:[NSDictionary class]] || [opt isKindOfClass:[NSArray class]]) {
[self.options setObject:opt forKey:key];
[self parseOption:key value:[[self options] objectForKey:key]];
} else if ([opt isKindOfClass:[WebScriptObject class]]) {
// assume this is a function (otherwise it would have been converted)
NSString *jsKey = [[JSController getInstance] addCallableFunction:opt];
[self.dynamicOptions setObject:jsKey forKey:key];
} else {
[self.options setObject:[NSString stringWithFormat:@"%@", opt] forKey:key];
[self parseOption:key value:[[self options] objectForKey:key]];
}
}
if ([[self dynamicOptions] count] == 0) { [self afterEvalOptions]; };
}
- (void)beforeInitOptions {
// OVERRIDE - runs before any options are set
}
- (void)parseOption:(NSString *)name value:(id)value {
// OVERRIDE - runs while setting options (both normal and dynamic)
}
- (void)afterEvalOptions {
// OVERRIDE - runs after all options are set
}
- (BOOL)testOperationWithAccessibilityWrapper:(AccessibilityWrapper *)aw screenWrapper:(ScreenWrapper *)sw {
[self evalOptionsWithAccessibilityWrapper:aw screenWrapper:sw];
return [self testOperation];
}
- (void)evalOptionsWithAccessibilityWrapper:(AccessibilityWrapper *)aw screenWrapper:(ScreenWrapper *)sw {
if ([[self dynamicOptions] count] == 0) { return; }
if (aw == nil) {
[[JSInfoWrapper getInstance] setAw:[[AccessibilityWrapper alloc] init]];
} else {
[[JSInfoWrapper getInstance] setAw:aw];
}
if (sw == nil) {
[[JSInfoWrapper getInstance] setSw:[[ScreenWrapper alloc] init]];
} else {
[[JSInfoWrapper getInstance] setSw:sw];
}
for (NSString *key in [[self dynamicOptions] allKeys]) {
id result = [[JSController getInstance] runCallableFunction:[[self dynamicOptions] objectForKey:key]];
if (result == nil) { continue; }
[self.options setObject:result forKey:key];
[self parseOption:key value:[[self options] objectForKey:key]];
}
[self afterEvalOptions];
}
- (id)dup:(NSDictionary *)_options {
NSMutableDictionary *newOptions = [NSMutableDictionary dictionaryWithDictionary:[self options]];
[newOptions addEntriesFromDictionary:[self dynamicOptions]];
[newOptions addEntriesFromDictionary:_options];
return [Operation operationWithName:[self opName] options:newOptions];
}
+ (BOOL)doOperation:(NSString *)op options:(NSDictionary *)options aw:(AccessibilityWrapper *)aw sw:(ScreenWrapper *)sw {
Operation *operation = [Operation operationWithName:op options:options];
BOOL success = NO;
if (operation != nil) {
[operation doOperationWithAccessibilityWrapper:aw screenWrapper:sw];
operation = nil; // force release of operation object
}
return success;
}
+ (id)operationFromString:(NSString *)opString {
NSMutableArray *tokens = [[NSMutableArray alloc] initWithCapacity:10];
[StringTokenizer tokenize:opString into:tokens maxTokens:2];
NSString *op = [tokens objectAtIndex:0];
Operation *operation = nil;
if ([op isEqualToString:MOVE]) {
operation = [MoveOperation moveOperationFromString:opString];
} else if ([op isEqualToString:RESIZE]) {
operation = [ResizeOperation resizeOperationFromString:opString];
} else if ([op isEqualToString:PUSH]) {
operation = [PushOperation pushOperationFromString:opString];
} else if ([op isEqualToString:NUDGE]) {
operation = [NudgeOperation nudgeOperationFromString:opString];
} else if ([op isEqualToString:THROW]) {
operation = [ThrowOperation throwOperationFromString:opString];
} else if ([op isEqualToString:CORNER]) {
operation = [CornerOperation cornerOperationFromString:opString];
} else if ([op isEqualToString:CHAIN]) {
operation = [ChainOperation chainOperationFromString:opString];
} else if ([op isEqualToString:LAYOUT]) {
operation = [LayoutOperation layoutOperationFromString:opString];
} else if ([op isEqualToString:FOCUS]) {
operation = [FocusOperation focusOperationFromString:opString];
} else if ([op isEqualToString:SNAPSHOT]) {
operation = [SnapshotOperation snapshotOperationFromString:opString];
} else if ([op isEqualToString:ACTIVATE_SNAPSHOT]) {
operation = [ActivateSnapshotOperation activateSnapshotOperationFromString:opString];
} else if ([op isEqualToString:DELETE_SNAPSHOT]) {
operation = [DeleteSnapshotOperation deleteSnapshotOperationFromString:opString];
} else if ([op isEqualToString:HINT]) {
operation = [HintOperation hintOperationFromString:opString];
} else if ([op isEqualToString:SWITCH]) {
operation = [SwitchOperation switchOperationFromString:opString];
} else if ([op isEqualToString:GRID]) {
operation = [GridOperation gridOperationFromString:opString];
} else if ([op isEqualToString:SEQUENCE]) {
operation = [SequenceOperation sequenceOperationFromString:opString];
} else if ([op isEqualToString:TOGGLE] || [op isEqualToString:SHOW] || [op isEqualToString:HIDE]) {
operation = [VisibilityOperation visibilityOperationFromString:opString];
} else if ([op isEqualToString:RELAUNCH]) {
operation = [RelaunchOperation relaunchOperationFromString:opString];
} else if ([op isEqualToString:SHELL]) {
operation = [ShellOperation shellOperationFromString:opString];
} else if ([op isEqualToString:UNDO]) {
operation = [UndoOperation undoOperationFromString:opString];
} else {
SlateLogger(@"ERROR: Unrecognized operation '%@'", opString);
@throw([NSException exceptionWithName:@"Unrecognized Operation" reason:[NSString stringWithFormat:@"Unrecognized operation '%@' in '%@'", op, opString] userInfo:nil]);
}
if (operation != nil) { [operation setOpName:op]; }
return operation;
}
+ (id)operationWithName:(NSString *)op options:(NSDictionary *)options {
Operation *operation = nil;
if ([op isEqualToString:MOVE]) {
operation = [MoveOperation moveOperation];
} else if ([op isEqualToString:RESIZE]) {
operation = [ResizeOperation resizeOperation];
} else if ([op isEqualToString:PUSH]) {
operation = [PushOperation pushOperation];
} else if ([op isEqualToString:NUDGE]) {
operation = [NudgeOperation nudgeOperation];
} else if ([op isEqualToString:THROW]) {
operation = [ThrowOperation throwOperation];
} else if ([op isEqualToString:CORNER]) {
operation = [CornerOperation cornerOperation];
} else if ([op isEqualToString:CHAIN]) {
operation = [ChainOperation chainOperation];
} else if ([op isEqualToString:LAYOUT]) {
operation = [LayoutOperation layoutOperation];
} else if ([op isEqualToString:FOCUS]) {
operation = [FocusOperation focusOperation];
} else if ([op isEqualToString:SNAPSHOT]) {
operation = [SnapshotOperation snapshotOperation];
} else if ([op isEqualToString:ACTIVATE_SNAPSHOT]) {
operation = [ActivateSnapshotOperation activateSnapshotOperation];
} else if ([op isEqualToString:DELETE_SNAPSHOT]) {
operation = [DeleteSnapshotOperation deleteSnapshotOperation];
} else if ([op isEqualToString:HINT]) {
operation = [HintOperation hintOperation];
} else if ([op isEqualToString:SWITCH]) {
operation = [SwitchOperation switchOperation];
} else if ([op isEqualToString:GRID]) {
operation = [GridOperation gridOperation];
} else if ([op isEqualToString:SEQUENCE]) {
operation = [SequenceOperation sequenceOperation];
} else if ([op isEqualToString:TOGGLE] || [op isEqualToString:SHOW] || [op isEqualToString:HIDE]) {
operation = [VisibilityOperation visibilityOperation];
} else if ([op isEqualToString:RELAUNCH]) {
operation = [RelaunchOperation relaunchOperation];
} else if ([op isEqualToString:SHELL]) {
operation = [ShellOperation shellOperation];
} else if ([op isEqualToString:UNDO]) {
operation = [UndoOperation undoOperation];
} else {
SlateLogger(@"ERROR: Unrecognized operation '%@'", op);
@throw([NSException exceptionWithName:@"Unrecognized Operation" reason:[NSString stringWithFormat:@"Unrecognized operation '%@'", op] userInfo:nil]);
}
if (operation != nil) {
[operation setOpName:op];
[operation initOptions:options];
}
return operation;
}
+ (BOOL)isRepeatOnHoldOp:(NSString *)op {
NSArray *repeatOps = [[[SlateConfig getInstance] getConfig:REPEAT_ON_HOLD_OPS] componentsSeparatedByString:COMMA];
for (NSInteger i = 0; i < [repeatOps count]; i++) {
if ([op isEqualToString:[repeatOps objectAtIndex:i]]) {
return YES;
}
}
return NO;
}
@end