forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileTransferManager.m
375 lines (325 loc) · 14.2 KB
/
FileTransferManager.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//
// DownloadManager.m
// iTerm
//
// Created by George Nachman on 12/21/13.
//
//
#import "FileTransferManager.h"
#import "iTermApplicationDelegate.h"
#import "TransferrableFileMenuItemViewController.h"
// Finished downloads will be automatically removed from the downloads menu after this number of
// seconds.
static const NSTimeInterval kMaximumTimeToKeepFinishedDownload = 24 * 60 * 60;
@interface FileTransferManager ()
@property(nonatomic, retain) NSMutableArray *files;
@end
@implementation FileTransferManager {
NSMutableArray *_viewControllers;
NSTimer *_timer; // cleanUpMenus timer. weak reference.
}
+ (instancetype)sharedInstance {
static id instance;
static dispatch_once_t once;
dispatch_once(&once, ^{
instance = [[self alloc] init];
});
return instance;
}
- (id)init {
self = [super init];
if (self) {
_files = [[NSMutableArray alloc] init];
_viewControllers = [[NSMutableArray alloc] init];
_timer = [NSTimer scheduledTimerWithTimeInterval:60 * 10
target:self
selector:@selector(cleanUpMenus)
userInfo:nil
repeats:YES];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_files release];
[_viewControllers release];
[super dealloc];
}
- (void)cleanUpMenus {
for (NSMenu *menu in @[ [self downloadsMenu], [self uploadsMenu] ]) {
NSMutableArray *controllersToRemove = [NSMutableArray array];
for (TransferrableFileMenuItemViewController *controller in _viewControllers) {
if ([controller timeSinceLastStatusChange] > kMaximumTimeToKeepFinishedDownload &&
controller.transferrableFile.status != kTransferrableFileStatusStarting &&
controller.transferrableFile.status != kTransferrableFileStatusTransferring &&
controller.transferrableFile.status != kTransferrableFileStatusCancelling) {
[menu removeItem:controller.view.enclosingMenuItem];
[controllersToRemove addObject:controller];
}
}
for (TransferrableFileMenuItemViewController *controller in controllersToRemove) {
[_viewControllers removeObject:controller];
}
}
}
- (NSMenu *)downloadsMenu {
iTermApplicationDelegate *ad = (iTermApplicationDelegate *)[[NSApplication sharedApplication] delegate];
return [ad downloadsMenu];
}
- (NSMenu *)uploadsMenu {
iTermApplicationDelegate *ad = (iTermApplicationDelegate *)[[NSApplication sharedApplication] delegate];
return [ad uploadsMenu];
}
- (void)animateImage:(NSImage *)image
intoMenu:(AXUIElementRef)menuElement
fromPoint:(NSPoint)point
onScreen:(NSScreen *)screen {
if (menuElement) {
CFTypeRef temp;
AXUIElementCopyAttributeValue(menuElement, kAXPositionAttribute, (CFTypeRef *)&temp);
CGPoint position;
AXValueGetValue(temp, kAXValueCGPointType, &position);
CFRelease(temp);
CFRelease(menuElement);
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(point.x,
point.y,
image.size.width,
image.size.height)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
NSImageView *imageView =
[[[NSImageView alloc] initWithFrame:NSMakeRect(0,
0,
image.size.width,
image.size.height)] autorelease];
imageView.image = image;
window.contentView = imageView;
[window makeKeyAndOrderFront:nil];
[window setLevel:NSMainMenuWindowLevel];
// Todo: deal with multiple screens, mavericks
const CGFloat menuBarHeight =
[[[NSApplication sharedApplication] mainMenu] menuBarHeight];
position.y = screen.frame.size.height - position.y - menuBarHeight;
[window.animator setFrame:NSMakeRect(position.x,
position.y,
window.frame.size.width,
window.frame.size.height)
display:YES];
[self performSelector:@selector(fadeWindowOut:)
withObject:window
afterDelay:[[NSAnimationContext currentContext] duration]];
}
}
- (void)animateImage:(NSImage *)image intoDownloadsMenuFromPoint:(NSPoint)point onScreen:(NSScreen *)screen {
AXUIElementRef menuElement = [self downloadsMenuElement];
[self animateImage:image intoMenu:menuElement fromPoint:point onScreen:screen];
}
- (void)fadeWindowOut:(NSWindow *)window {
window.animator.alphaValue = 0;
[window performSelector:@selector(release)
withObject:nil
afterDelay:[[NSAnimationContext currentContext] duration]];
}
- (AXUIElementRef)downloadsMenuElement {
return [self menuElementNamed:@"Downloads"];
}
- (AXUIElementRef)uploadsMenuElement {
return [self menuElementNamed:@"Uploads"];
}
- (AXUIElementRef)menuElementNamed:(NSString *)menuName {
AXUIElementRef appElement = AXUIElementCreateApplication(getpid());
AXUIElementRef menuBar;
AXError error = AXUIElementCopyAttributeValue(appElement,
kAXMenuBarAttribute,
(CFTypeRef *)&menuBar);
if (error) {
return NULL;
}
CFIndex count = -1;
error = AXUIElementGetAttributeValueCount(menuBar, kAXChildrenAttribute, &count);
if (error) {
CFRelease(menuBar);
return NULL;
}
NSArray *children = nil;
// Despite what the name would suggest, the children array and its contents don't seen to need
// too be released by us.
error = AXUIElementCopyAttributeValues(menuBar,
kAXChildrenAttribute,
0,
count,
(CFArrayRef *)&children);
if (error) {
CFRelease(menuBar);
return NULL;
}
for (id child in children) {
AXUIElementRef element = (AXUIElementRef)child;
id title;
AXError error = AXUIElementCopyAttributeValue(element,
kAXTitleAttribute,
(CFTypeRef *)&title);
if (error) {
continue;
}
BOOL found = [title isEqualToString:menuName];
CFRelease(title);
if (found) {
return element;
}
}
return NULL;
}
- (void)openDownloadsMenu {
AXUIElementRef menuElement = [self downloadsMenuElement];
if (menuElement) {
AXUIElementPerformAction(menuElement, kAXPressAction);
CFRelease(menuElement);
}
}
- (void)openUploadsMenu {
AXUIElementRef menuElement = [self uploadsMenuElement];
if (menuElement) {
AXUIElementPerformAction(menuElement, kAXPressAction);
CFRelease(menuElement);
}
}
- (TransferrableFileMenuItemViewController *)viewControllerForTransferrableFile:(TransferrableFile *)transferrableFile {
for (TransferrableFileMenuItemViewController *controller in _viewControllers) {
if (controller.transferrableFile == transferrableFile) {
return controller;
}
}
return nil;
}
- (NSMenuItem *)menuItemForTransferrableFile:(TransferrableFile *)transferrableFile {
NSMenuItem *item = [[NSMenuItem alloc] init];
TransferrableFileMenuItemViewController *controller =
[[[TransferrableFileMenuItemViewController alloc] initWithTransferrableFile:transferrableFile] autorelease];
[_viewControllers addObject:controller];
item.view = [controller view];
[item setEnabled:YES];
[item setTarget:controller];
[item setAction:@selector(itemSelected:)];
NSMenu *submenu = [[[NSMenu alloc] init] autorelease];
NSMenuItem *subItem = [[[NSMenuItem alloc] initWithTitle:@"Stop"
action:@selector(stop:)
keyEquivalent:@""] autorelease];
[subItem setTarget:controller];
[submenu addItem:subItem];
controller.stopSubItem = subItem;
if (transferrableFile.isDownloading) {
subItem = [[[NSMenuItem alloc] initWithTitle:@"Show in Finder"
action:@selector(showInFinder:)
keyEquivalent:@""] autorelease];
[subItem setTarget:controller];
[submenu addItem:subItem];
controller.showInFinderSubItem = subItem;
}
subItem = [[[NSMenuItem alloc] initWithTitle:@"Remove from List"
action:@selector(removeFromList:)
keyEquivalent:@""] autorelease];
[subItem setTarget:controller];
[submenu addItem:subItem];
controller.removeFromListSubItem = subItem;
if (transferrableFile.isDownloading) {
subItem = [[[NSMenuItem alloc] initWithTitle:@"Open"
action:@selector(open:)
keyEquivalent:@""] autorelease];
[subItem setTarget:controller];
[submenu addItem:subItem];
controller.openSubItem = subItem;
}
subItem = [[[NSMenuItem alloc] initWithTitle:@"Get Info"
action:@selector(getInfo:)
keyEquivalent:@""] autorelease];
[subItem setTarget:controller];
[submenu addItem:subItem];
controller.openSubItem = subItem;
item.submenu = submenu;
[controller update];
return item;
}
- (NSMenu *)menuForFile:(TransferrableFile *)transferrableFile {
if (transferrableFile.isDownloading) {
return [self downloadsMenu];
} else {
return [self uploadsMenu];
}
}
- (void)transferrableFileDidStartTransfer:(TransferrableFile *)transferrableFile {
NSLog(@"Transfer started");
[[self menuForFile:transferrableFile] addItem:[self menuItemForTransferrableFile:transferrableFile]];
TransferrableFileMenuItemViewController *controller = [self viewControllerForTransferrableFile:transferrableFile];
[controller update];
}
// Number of bytes transferred has changed or total size has been discovered.
- (void)transferrableFileProgressDidChange:(TransferrableFile *)transferrableFile {
TransferrableFileMenuItemViewController *controller = [self viewControllerForTransferrableFile:transferrableFile];
[controller update];
}
// |error| is nil on success
- (void)transferrableFile:(TransferrableFile *)transferrableFile
didFinishTransmissionWithError:(NSError *)error {
transferrableFile.status = error ? kTransferrableFileStatusFinishedWithError : kTransferrableFileStatusFinishedSuccessfully;
NSLog(@"Transfer finished. error=%@", error);
TransferrableFileMenuItemViewController *controller = [self viewControllerForTransferrableFile:transferrableFile];
[controller update];
}
- (void)transferrableFileWillStop:(TransferrableFile *)transferrableFile {
NSLog(@"file transfer stop requested");
transferrableFile.status = kTransferrableFileStatusCancelling;
TransferrableFileMenuItemViewController *controller = [self viewControllerForTransferrableFile:transferrableFile];
[controller update];
}
- (void)transferrableFileDidStopTransfer:(TransferrableFile *)transferrableFile {
NSLog(@"file transfer stopped");
transferrableFile.status = kTransferrableFileStatusCancelled;
TransferrableFileMenuItemViewController *controller = [self viewControllerForTransferrableFile:transferrableFile];
[controller update];
}
// Shows a modal alert with the text in |prompt| and a freeform keyboard input. Returns the
// value entered.
- (NSString *)transferrableFile:(TransferrableFile *)transferrableFile
keyboardInteractivePrompt:(NSString *)prompt {
NSString *text = [NSString stringWithFormat:@"%@: %@", [transferrableFile displayName], prompt];
NSAlert *alert = [NSAlert alertWithMessageText:text
defaultButton:@"OK"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@""];
NSSecureTextField *input =
[[[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)] autorelease];
[input setStringValue:@""];
[alert setAccessoryView:input];
[alert layout];
[[alert window] makeFirstResponder:input];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
[input validateEditing];
return [input stringValue];
} else {
return nil;
}
}
// Shows message, returns YES if OK, NO if Cancel
- (BOOL)transferrableFile:(TransferrableFile *)transferrableFile
confirmMessage:(NSString *)message {
NSString *text = [NSString stringWithFormat:@"%@: %@", [transferrableFile displayName],
message];
NSAlert *alert = [NSAlert alertWithMessageText:text
defaultButton:@"OK"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@""];
[alert layout];
NSInteger button = [alert runModal];
return (button == NSAlertDefaultReturn);
}
- (void)removeItem:(TransferrableFileMenuItemViewController *)viewController {
NSMenuItem *item = [viewController.view enclosingMenuItem];
[[item menu] removeItem:item];
[_viewControllers removeObject:viewController];
}
@end