1 | /****************************************************************************** |
---|
2 | * $Id: Controller.m 463 2006-06-25 18:33:52Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import <IOKit/IOMessage.h> |
---|
26 | |
---|
27 | #import "Controller.h" |
---|
28 | #import "Torrent.h" |
---|
29 | #import "TorrentCell.h" |
---|
30 | #import "TorrentTableView.h" |
---|
31 | #import "StringAdditions.h" |
---|
32 | |
---|
33 | #import <Sparkle/Sparkle.h> |
---|
34 | |
---|
35 | #define TOOLBAR_OPEN @"Toolbar Open" |
---|
36 | #define TOOLBAR_REMOVE @"Toolbar Remove" |
---|
37 | #define TOOLBAR_INFO @"Toolbar Info" |
---|
38 | #define TOOLBAR_PAUSE_ALL @"Toolbar Pause All" |
---|
39 | #define TOOLBAR_RESUME_ALL @"Toolbar Resume All" |
---|
40 | #define TOOLBAR_PAUSE_SELECTED @"Toolbar Pause Selected" |
---|
41 | #define TOOLBAR_RESUME_SELECTED @"Toolbar Resume Selected" |
---|
42 | |
---|
43 | #define WEBSITE_URL @"http://transmission.m0k.org/" |
---|
44 | #define FORUM_URL @"http://transmission.m0k.org/forum/" |
---|
45 | |
---|
46 | #define GROWL_PATH @"/Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app" |
---|
47 | |
---|
48 | static void sleepCallBack(void * controller, io_service_t y, |
---|
49 | natural_t messageType, void * messageArgument) |
---|
50 | { |
---|
51 | Controller * c = controller; |
---|
52 | [c sleepCallBack: messageType argument: messageArgument]; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | @implementation Controller |
---|
57 | |
---|
58 | - (id) init |
---|
59 | { |
---|
60 | if ((self = [super init])) |
---|
61 | { |
---|
62 | fLib = tr_init(); |
---|
63 | fTorrents = [[NSMutableArray alloc] initWithCapacity: 10]; |
---|
64 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
65 | fInfoController = [[InfoWindowController alloc] initWithWindowNibName: @"InfoWindow"]; |
---|
66 | fPrefsController = [[PrefsController alloc] initWithWindowNibName: @"PrefsWindow"]; |
---|
67 | fBadger = [[Badger alloc] init]; |
---|
68 | |
---|
69 | //check and register Growl if it is installed for this user or all users |
---|
70 | NSFileManager * manager = [NSFileManager defaultManager]; |
---|
71 | fHasGrowl = [manager fileExistsAtPath: GROWL_PATH] |
---|
72 | || [manager fileExistsAtPath: [NSHomeDirectory() stringByAppendingPathComponent: GROWL_PATH]]; |
---|
73 | [self growlRegister]; |
---|
74 | } |
---|
75 | return self; |
---|
76 | } |
---|
77 | |
---|
78 | - (void) dealloc |
---|
79 | { |
---|
80 | [[NSNotificationCenter defaultCenter] removeObserver: self]; |
---|
81 | |
---|
82 | [fTorrents release]; |
---|
83 | [fToolbar release]; |
---|
84 | [fInfoController release]; |
---|
85 | [fBadger release]; |
---|
86 | [fSortType release]; |
---|
87 | |
---|
88 | tr_close( fLib ); |
---|
89 | [super dealloc]; |
---|
90 | } |
---|
91 | |
---|
92 | - (void) awakeFromNib |
---|
93 | { |
---|
94 | [fPrefsController setPrefs: fLib]; |
---|
95 | |
---|
96 | [fAdvancedBarItem setState: [fDefaults boolForKey: @"UseAdvancedBar"]]; |
---|
97 | |
---|
98 | fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Transmission Toolbar"]; |
---|
99 | [fToolbar setDelegate: self]; |
---|
100 | [fToolbar setAllowsUserCustomization: YES]; |
---|
101 | [fToolbar setAutosavesConfiguration: YES]; |
---|
102 | [fWindow setToolbar: fToolbar]; |
---|
103 | [fWindow setDelegate: self]; |
---|
104 | |
---|
105 | //window min height |
---|
106 | NSSize contentMinSize = [fWindow contentMinSize]; |
---|
107 | contentMinSize.height = [[fWindow contentView] frame].size.height - [fScrollView frame].size.height |
---|
108 | + [fTableView rowHeight] + [fTableView intercellSpacing].height; |
---|
109 | [fWindow setContentMinSize: contentMinSize]; |
---|
110 | |
---|
111 | //set info keyboard shortcuts |
---|
112 | unichar ch = NSRightArrowFunctionKey; |
---|
113 | [fNextInfoTabItem setKeyEquivalent: [NSString stringWithCharacters: & ch length: 1]]; |
---|
114 | ch = NSLeftArrowFunctionKey; |
---|
115 | [fPrevInfoTabItem setKeyEquivalent: [NSString stringWithCharacters: & ch length: 1]]; |
---|
116 | |
---|
117 | //set up status bar |
---|
118 | NSRect statusBarFrame = [fStatusBar frame]; |
---|
119 | statusBarFrame.size.width = [fWindow frame].size.width; |
---|
120 | [fStatusBar setFrame: statusBarFrame]; |
---|
121 | |
---|
122 | NSView * contentView = [fWindow contentView]; |
---|
123 | [contentView addSubview: fStatusBar]; |
---|
124 | [fStatusBar setFrameOrigin: NSMakePoint(0, [fScrollView frame].origin.y |
---|
125 | + [fScrollView frame].size.height)]; |
---|
126 | [self showStatusBar: [fDefaults boolForKey: @"StatusBar"] animate: NO]; |
---|
127 | |
---|
128 | [fActionButton setToolTip: @"Shortcuts for changing global settings."]; |
---|
129 | |
---|
130 | [fTableView setTorrents: fTorrents]; |
---|
131 | [[fTableView tableColumnWithIdentifier: @"Torrent"] setDataCell: |
---|
132 | [[TorrentCell alloc] init]]; |
---|
133 | |
---|
134 | [fTableView registerForDraggedTypes: |
---|
135 | [NSArray arrayWithObject: NSFilenamesPboardType]]; |
---|
136 | |
---|
137 | //register for sleep notifications |
---|
138 | IONotificationPortRef notify; |
---|
139 | io_object_t iterator; |
---|
140 | if (fRootPort = IORegisterForSystemPower(self, & notify, sleepCallBack, & iterator)) |
---|
141 | { |
---|
142 | CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), |
---|
143 | kCFRunLoopCommonModes); |
---|
144 | } |
---|
145 | else |
---|
146 | NSLog(@"Could not IORegisterForSystemPower"); |
---|
147 | |
---|
148 | //load torrents from history |
---|
149 | Torrent * torrent; |
---|
150 | NSDictionary * historyItem; |
---|
151 | NSEnumerator * enumerator = [[fDefaults arrayForKey: @"History"] objectEnumerator]; |
---|
152 | while ((historyItem = [enumerator nextObject])) |
---|
153 | if ((torrent = [[Torrent alloc] initWithHistory: historyItem lib: fLib])) |
---|
154 | { |
---|
155 | [fTorrents addObject: torrent]; |
---|
156 | [torrent release]; |
---|
157 | } |
---|
158 | |
---|
159 | [self torrentNumberChanged]; |
---|
160 | |
---|
161 | //set sort |
---|
162 | fSortType = [[fDefaults stringForKey: @"Sort"] retain]; |
---|
163 | |
---|
164 | NSMenuItem * currentSortItem; |
---|
165 | if ([fSortType isEqualToString: @"Name"]) |
---|
166 | currentSortItem = fNameSortItem; |
---|
167 | else if ([fSortType isEqualToString: @"State"]) |
---|
168 | currentSortItem = fStateSortItem; |
---|
169 | else if ([fSortType isEqualToString: @"Progress"]) |
---|
170 | currentSortItem = fProgressSortItem; |
---|
171 | else if ([fSortType isEqualToString: @"Date"]) |
---|
172 | currentSortItem = fDateSortItem; |
---|
173 | else |
---|
174 | currentSortItem = fOrderSortItem; |
---|
175 | [currentSortItem setState: NSOnState]; |
---|
176 | |
---|
177 | //set upload limit action button |
---|
178 | [fUploadLimitItem setTitle: [NSString stringWithFormat: @"Limit (%d KB/s)", |
---|
179 | [fDefaults integerForKey: @"UploadLimit"]]]; |
---|
180 | if ([fDefaults boolForKey: @"CheckUpload"]) |
---|
181 | [fUploadLimitItem setState: NSOnState]; |
---|
182 | else |
---|
183 | [fUploadNoLimitItem setState: NSOnState]; |
---|
184 | |
---|
185 | //set download limit action menu |
---|
186 | [fDownloadLimitItem setTitle: [NSString stringWithFormat: @"Limit (%d KB/s)", |
---|
187 | [fDefaults integerForKey: @"DownloadLimit"]]]; |
---|
188 | if ([fDefaults boolForKey: @"CheckDownload"]) |
---|
189 | [fDownloadLimitItem setState: NSOnState]; |
---|
190 | else |
---|
191 | [fDownloadNoLimitItem setState: NSOnState]; |
---|
192 | |
---|
193 | //set ratio action menu |
---|
194 | [fRatioSetItem setTitle: [NSString stringWithFormat: @"Stop at Ratio (%.2f)", |
---|
195 | [fDefaults floatForKey: @"RatioLimit"]]]; |
---|
196 | if ([fDefaults boolForKey: @"RatioCheck"]) |
---|
197 | [fRatioSetItem setState: NSOnState]; |
---|
198 | else |
---|
199 | [fRatioNotSetItem setState: NSOnState]; |
---|
200 | |
---|
201 | //observe notifications |
---|
202 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
---|
203 | |
---|
204 | [nc addObserver: self selector: @selector(prepareForUpdate:) |
---|
205 | name: SUUpdaterWillRestartNotification object: nil]; |
---|
206 | fUpdateInProgress = NO; |
---|
207 | |
---|
208 | [nc addObserver: self selector: @selector(limitGlobalChange:) |
---|
209 | name: @"LimitGlobalChange" object: nil]; |
---|
210 | |
---|
211 | [nc addObserver: self selector: @selector(ratioGlobalChange:) |
---|
212 | name: @"RatioGlobalChange" object: nil]; |
---|
213 | |
---|
214 | //check to start another because of stopped torrent |
---|
215 | [nc addObserver: self selector: @selector(checkWaitingForStopped:) |
---|
216 | name: @"StoppedDownloading" object: nil]; |
---|
217 | |
---|
218 | //check all torrents for starting |
---|
219 | [nc addObserver: self selector: @selector(globalStartSettingChange:) |
---|
220 | name: @"GlobalStartSettingChange" object: nil]; |
---|
221 | |
---|
222 | //check if torrent should now start |
---|
223 | [nc addObserver: self selector: @selector(torrentStartSettingChange:) |
---|
224 | name: @"TorrentStartSettingChange" object: nil]; |
---|
225 | |
---|
226 | //change that just impacts the inspector |
---|
227 | [nc addObserver: self selector: @selector(reloadInspector:) |
---|
228 | name: @"TorrentSettingChange" object: nil]; |
---|
229 | |
---|
230 | //timer to update the interface |
---|
231 | fCompleted = 0; |
---|
232 | [self updateUI: nil]; |
---|
233 | fTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self |
---|
234 | selector: @selector( updateUI: ) userInfo: nil repeats: YES]; |
---|
235 | [[NSRunLoop currentRunLoop] addTimer: fTimer |
---|
236 | forMode: NSModalPanelRunLoopMode]; |
---|
237 | [[NSRunLoop currentRunLoop] addTimer: fTimer |
---|
238 | forMode: NSEventTrackingRunLoopMode]; |
---|
239 | |
---|
240 | [self sortTorrents]; |
---|
241 | |
---|
242 | //show windows |
---|
243 | [fWindow makeKeyAndOrderFront: nil]; |
---|
244 | |
---|
245 | [self reloadInspector: nil]; |
---|
246 | if ([fDefaults boolForKey: @"InfoVisible"]) |
---|
247 | [self showInfo: nil]; |
---|
248 | } |
---|
249 | |
---|
250 | - (BOOL) applicationShouldHandleReopen: (NSApplication *) app hasVisibleWindows: (BOOL) visibleWindows |
---|
251 | { |
---|
252 | if (![fWindow isVisible] && ![[fPrefsController window] isVisible]) |
---|
253 | [fWindow makeKeyAndOrderFront: nil]; |
---|
254 | return NO; |
---|
255 | } |
---|
256 | |
---|
257 | - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) sender |
---|
258 | { |
---|
259 | if (!fUpdateInProgress && [fDefaults boolForKey: @"CheckQuit"]) |
---|
260 | { |
---|
261 | int active = 0; |
---|
262 | Torrent * torrent; |
---|
263 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
264 | while ((torrent = [enumerator nextObject])) |
---|
265 | if ([torrent isActive]) |
---|
266 | active++; |
---|
267 | |
---|
268 | if (active > 0) |
---|
269 | { |
---|
270 | NSString * message = active == 1 |
---|
271 | ? @"There is an active transfer. Do you really want to quit?" |
---|
272 | : [NSString stringWithFormat: |
---|
273 | @"There are %d active transfers. Do you really want to quit?", active]; |
---|
274 | |
---|
275 | NSBeginAlertSheet(@"Confirm Quit", @"Quit", @"Cancel", nil, fWindow, self, |
---|
276 | @selector(quitSheetDidEnd:returnCode:contextInfo:), |
---|
277 | nil, nil, message); |
---|
278 | return NSTerminateLater; |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | return NSTerminateNow; |
---|
283 | } |
---|
284 | |
---|
285 | - (void) quitSheetDidEnd: (NSWindow *) sheet returnCode: (int) returnCode |
---|
286 | contextInfo: (void *) contextInfo |
---|
287 | { |
---|
288 | [NSApp stopModal]; |
---|
289 | [NSApp replyToApplicationShouldTerminate: returnCode == NSAlertDefaultReturn]; |
---|
290 | } |
---|
291 | |
---|
292 | - (void) applicationWillTerminate: (NSNotification *) notification |
---|
293 | { |
---|
294 | // Stop updating the interface |
---|
295 | [fTimer invalidate]; |
---|
296 | |
---|
297 | //save history |
---|
298 | [self updateTorrentHistory]; |
---|
299 | |
---|
300 | //remember window states |
---|
301 | [fDefaults setBool: [[fInfoController window] isVisible] forKey: @"InfoVisible"]; |
---|
302 | [fWindow close]; |
---|
303 | [self showStatusBar: NO animate: NO]; |
---|
304 | |
---|
305 | //clear badge |
---|
306 | [fBadger clearBadge]; |
---|
307 | |
---|
308 | //end quickly if updated version will open |
---|
309 | if (fUpdateInProgress) |
---|
310 | return; |
---|
311 | |
---|
312 | //stop running torrents and wait for them to stop (5 seconds timeout) |
---|
313 | [fTorrents makeObjectsPerformSelector: @selector(stopTransfer)]; |
---|
314 | |
---|
315 | NSDate * start = [NSDate date]; |
---|
316 | Torrent * torrent; |
---|
317 | while ([fTorrents count] > 0) |
---|
318 | { |
---|
319 | torrent = [fTorrents objectAtIndex: 0]; |
---|
320 | while( [[NSDate date] timeIntervalSinceDate: start] < 5 && |
---|
321 | ![torrent isPaused] ) |
---|
322 | { |
---|
323 | usleep( 100000 ); |
---|
324 | [torrent update]; |
---|
325 | } |
---|
326 | [fTorrents removeObject: torrent]; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | - (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code |
---|
331 | contextInfo: (Torrent *) torrent |
---|
332 | { |
---|
333 | if (code == NSOKButton) |
---|
334 | { |
---|
335 | [torrent setDownloadFolder: [[openPanel filenames] objectAtIndex: 0]]; |
---|
336 | [self attemptToStartAuto: torrent]; |
---|
337 | [fTorrents addObject: torrent]; |
---|
338 | [torrent update]; |
---|
339 | |
---|
340 | [self torrentNumberChanged]; |
---|
341 | } |
---|
342 | |
---|
343 | [NSApp stopModal]; |
---|
344 | } |
---|
345 | |
---|
346 | - (void) application: (NSApplication *) sender openFiles: (NSArray *) filenames |
---|
347 | { |
---|
348 | NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"], * torrentPath; |
---|
349 | Torrent * torrent; |
---|
350 | NSEnumerator * enumerator = [filenames objectEnumerator]; |
---|
351 | while ((torrentPath = [enumerator nextObject])) |
---|
352 | { |
---|
353 | if (!(torrent = [[Torrent alloc] initWithPath: torrentPath lib: fLib])) |
---|
354 | continue; |
---|
355 | |
---|
356 | //add it to the "File > Open Recent" menu |
---|
357 | [[NSDocumentController sharedDocumentController] |
---|
358 | noteNewRecentDocumentURL: [NSURL fileURLWithPath: torrentPath]]; |
---|
359 | |
---|
360 | if ([downloadChoice isEqualToString: @"Ask"]) |
---|
361 | { |
---|
362 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
363 | |
---|
364 | [panel setPrompt: @"Select Download Folder"]; |
---|
365 | [panel setAllowsMultipleSelection: NO]; |
---|
366 | [panel setCanChooseFiles: NO]; |
---|
367 | [panel setCanChooseDirectories: YES]; |
---|
368 | |
---|
369 | [panel setMessage: [@"Select the download folder for " |
---|
370 | stringByAppendingString: [torrentPath lastPathComponent]]]; |
---|
371 | |
---|
372 | [panel beginSheetForDirectory: nil file: nil types: nil |
---|
373 | modalForWindow: fWindow modalDelegate: self didEndSelector: |
---|
374 | @selector( folderChoiceClosed:returnCode:contextInfo: ) |
---|
375 | contextInfo: torrent]; |
---|
376 | [NSApp runModalForWindow: panel]; |
---|
377 | } |
---|
378 | else |
---|
379 | { |
---|
380 | NSString * folder = [downloadChoice isEqualToString: @"Constant"] |
---|
381 | ? [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath] |
---|
382 | : [torrentPath stringByDeletingLastPathComponent]; |
---|
383 | |
---|
384 | [torrent setDownloadFolder: folder]; |
---|
385 | [self attemptToStartAuto: torrent]; |
---|
386 | [fTorrents addObject: torrent]; |
---|
387 | [torrent update]; |
---|
388 | } |
---|
389 | |
---|
390 | [torrent release]; |
---|
391 | } |
---|
392 | |
---|
393 | [self torrentNumberChanged]; |
---|
394 | |
---|
395 | [self updateUI: nil]; |
---|
396 | [self sortTorrents]; |
---|
397 | [self updateTorrentHistory]; |
---|
398 | } |
---|
399 | |
---|
400 | - (NSArray *) torrentsAtIndexes: (NSIndexSet *) indexSet |
---|
401 | { |
---|
402 | if ([fTorrents respondsToSelector: @selector(objectsAtIndexes:)]) |
---|
403 | return [fTorrents objectsAtIndexes: indexSet]; |
---|
404 | else |
---|
405 | { |
---|
406 | NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [indexSet count]]; |
---|
407 | unsigned int i; |
---|
408 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
409 | [torrents addObject: [fTorrents objectAtIndex: i]]; |
---|
410 | |
---|
411 | return torrents; |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | - (void) torrentNumberChanged |
---|
416 | { |
---|
417 | int count = [fTorrents count]; |
---|
418 | [fTotalTorrentsField setStringValue: [NSString stringWithFormat: |
---|
419 | @"%d Transfer%s", count, count == 1 ? "" : "s"]]; |
---|
420 | } |
---|
421 | |
---|
422 | - (void) advancedChanged: (id) sender |
---|
423 | { |
---|
424 | [fAdvancedBarItem setState: ![fAdvancedBarItem state]]; |
---|
425 | [fDefaults setBool: [fAdvancedBarItem state] forKey: @"UseAdvancedBar"]; |
---|
426 | |
---|
427 | [fTableView display]; |
---|
428 | } |
---|
429 | |
---|
430 | //called on by applescript |
---|
431 | - (void) open: (NSArray *) files |
---|
432 | { |
---|
433 | [self performSelectorOnMainThread: @selector(openFromSheet:) withObject: files waitUntilDone: NO]; |
---|
434 | } |
---|
435 | |
---|
436 | - (void) openShowSheet: (id) sender |
---|
437 | { |
---|
438 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
439 | NSArray * fileTypes = [NSArray arrayWithObject: @"torrent"]; |
---|
440 | |
---|
441 | [panel setAllowsMultipleSelection: YES]; |
---|
442 | [panel setCanChooseFiles: YES]; |
---|
443 | [panel setCanChooseDirectories: NO]; |
---|
444 | |
---|
445 | [panel beginSheetForDirectory: nil file: nil types: fileTypes |
---|
446 | modalForWindow: fWindow modalDelegate: self didEndSelector: |
---|
447 | @selector(openSheetClosed:returnCode:contextInfo:) contextInfo: nil]; |
---|
448 | } |
---|
449 | |
---|
450 | - (void) openFromSheet: (NSArray *) filenames |
---|
451 | { |
---|
452 | [self application: NSApp openFiles: filenames]; |
---|
453 | } |
---|
454 | |
---|
455 | - (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (int) code |
---|
456 | contextInfo: (void *) info |
---|
457 | { |
---|
458 | if (code == NSOKButton) |
---|
459 | [self performSelectorOnMainThread: @selector(openFromSheet:) |
---|
460 | withObject: [panel filenames] waitUntilDone: NO]; |
---|
461 | } |
---|
462 | |
---|
463 | - (void) resumeTorrent: (id) sender |
---|
464 | { |
---|
465 | [self resumeTorrentWithIndex: [fTableView selectedRowIndexes]]; |
---|
466 | } |
---|
467 | |
---|
468 | - (void) resumeAllTorrents: (id) sender |
---|
469 | { |
---|
470 | [self resumeTorrentWithIndex: [NSIndexSet indexSetWithIndexesInRange: |
---|
471 | NSMakeRange(0, [fTorrents count])]]; |
---|
472 | } |
---|
473 | |
---|
474 | - (void) resumeTorrentWithIndex: (NSIndexSet *) indexSet |
---|
475 | { |
---|
476 | unsigned int i; |
---|
477 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
478 | [[fTorrents objectAtIndex: i] startTransfer]; |
---|
479 | |
---|
480 | [self updateUI: nil]; |
---|
481 | [self reloadInspector: nil]; |
---|
482 | [self updateTorrentHistory]; |
---|
483 | } |
---|
484 | |
---|
485 | - (void) stopTorrent: (id) sender |
---|
486 | { |
---|
487 | [self stopTorrentWithIndex: [fTableView selectedRowIndexes]]; |
---|
488 | } |
---|
489 | |
---|
490 | - (void) stopAllTorrents: (id) sender |
---|
491 | { |
---|
492 | [self stopTorrentWithIndex: [NSIndexSet indexSetWithIndexesInRange: |
---|
493 | NSMakeRange(0, [fTorrents count])]]; |
---|
494 | } |
---|
495 | |
---|
496 | - (void) stopTorrentWithIndex: (NSIndexSet *) indexSet |
---|
497 | { |
---|
498 | //don't want any of these starting then stopping |
---|
499 | unsigned int i; |
---|
500 | Torrent * torrent; |
---|
501 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
502 | { |
---|
503 | torrent = [fTorrents objectAtIndex: i]; |
---|
504 | [torrent setWaitToStart: NO]; |
---|
505 | } |
---|
506 | |
---|
507 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
508 | [[fTorrents objectAtIndex: i] stopTransfer]; |
---|
509 | |
---|
510 | [self updateUI: nil]; |
---|
511 | [self reloadInspector: nil]; |
---|
512 | [self updateTorrentHistory]; |
---|
513 | } |
---|
514 | |
---|
515 | - (void) removeWithIndex: (NSIndexSet *) indexSet |
---|
516 | deleteData: (BOOL) deleteData deleteTorrent: (BOOL) deleteTorrent |
---|
517 | { |
---|
518 | NSArray * torrents = [[self torrentsAtIndexes: indexSet] retain]; |
---|
519 | int active = 0; |
---|
520 | |
---|
521 | Torrent * torrent; |
---|
522 | NSEnumerator * enumerator = [torrents objectEnumerator]; |
---|
523 | while ((torrent = [enumerator nextObject])) |
---|
524 | if ([torrent isActive]) |
---|
525 | active++; |
---|
526 | |
---|
527 | if (active > 0 && [fDefaults boolForKey: @"CheckRemove"]) |
---|
528 | { |
---|
529 | NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
530 | torrents, @"Torrents", |
---|
531 | [NSNumber numberWithBool: deleteData], @"DeleteData", |
---|
532 | [NSNumber numberWithBool: deleteTorrent], @"DeleteTorrent", nil]; |
---|
533 | |
---|
534 | NSString * title, * message; |
---|
535 | |
---|
536 | int selected = [fTableView numberOfSelectedRows]; |
---|
537 | if (selected == 1) |
---|
538 | { |
---|
539 | title = [NSString stringWithFormat: @"Comfirm Removal of \"%@\"", |
---|
540 | [[fTorrents objectAtIndex: [fTableView selectedRow]] name]]; |
---|
541 | message = @"This transfer is active." |
---|
542 | " Once removed, continuing the transfer will require the torrent file." |
---|
543 | " Do you really want to remove it?"; |
---|
544 | } |
---|
545 | else |
---|
546 | { |
---|
547 | title = [NSString stringWithFormat: @"Comfirm Removal of %d Transfers", selected]; |
---|
548 | if (selected == active) |
---|
549 | message = [NSString stringWithFormat: |
---|
550 | @"There are %d active transfers.", active]; |
---|
551 | else |
---|
552 | message = [NSString stringWithFormat: |
---|
553 | @"There are %d transfers (%d active).", selected, active]; |
---|
554 | message = [message stringByAppendingString: |
---|
555 | @" Once removed, continuing the transfers will require the torrent files." |
---|
556 | " Do you really want to remove them?"]; |
---|
557 | } |
---|
558 | |
---|
559 | NSBeginAlertSheet(title, @"Remove", @"Cancel", nil, fWindow, self, |
---|
560 | @selector(removeSheetDidEnd:returnCode:contextInfo:), nil, dict, message); |
---|
561 | } |
---|
562 | else |
---|
563 | [self confirmRemove: torrents deleteData: deleteData deleteTorrent: deleteTorrent]; |
---|
564 | } |
---|
565 | |
---|
566 | - (void) removeSheetDidEnd: (NSWindow *) sheet returnCode: (int) returnCode |
---|
567 | contextInfo: (NSDictionary *) dict |
---|
568 | { |
---|
569 | [NSApp stopModal]; |
---|
570 | |
---|
571 | NSArray * torrents = [dict objectForKey: @"Torrents"]; |
---|
572 | BOOL deleteData = [[dict objectForKey: @"DeleteData"] boolValue], |
---|
573 | deleteTorrent = [[dict objectForKey: @"DeleteTorrent"] boolValue]; |
---|
574 | [dict release]; |
---|
575 | |
---|
576 | if (returnCode == NSAlertDefaultReturn) |
---|
577 | [self confirmRemove: torrents deleteData: deleteData deleteTorrent: deleteTorrent]; |
---|
578 | else |
---|
579 | [torrents release]; |
---|
580 | } |
---|
581 | |
---|
582 | - (void) confirmRemove: (NSArray *) torrents |
---|
583 | deleteData: (BOOL) deleteData deleteTorrent: (BOOL) deleteTorrent |
---|
584 | { |
---|
585 | //don't want any of these starting then stopping |
---|
586 | NSEnumerator * enumerator = [torrents objectEnumerator]; |
---|
587 | Torrent * torrent; |
---|
588 | while ((torrent = [enumerator nextObject])) |
---|
589 | [torrent setWaitToStart: NO]; |
---|
590 | |
---|
591 | NSNumber * lowestOrderValue = [NSNumber numberWithInt: [torrents count]], * currentOrederValue; |
---|
592 | |
---|
593 | enumerator = [torrents objectEnumerator]; |
---|
594 | while ((torrent = [enumerator nextObject])) |
---|
595 | { |
---|
596 | [torrent stopTransfer]; |
---|
597 | |
---|
598 | if (deleteData) |
---|
599 | [torrent trashData]; |
---|
600 | if (deleteTorrent) |
---|
601 | [torrent trashTorrent]; |
---|
602 | |
---|
603 | //determine lowest order value |
---|
604 | currentOrederValue = [torrent orderValue]; |
---|
605 | if ([lowestOrderValue compare: currentOrederValue] == NSOrderedDescending) |
---|
606 | lowestOrderValue = currentOrederValue; |
---|
607 | |
---|
608 | [torrent removeForever]; |
---|
609 | [fTorrents removeObject: torrent]; |
---|
610 | } |
---|
611 | [torrents release]; |
---|
612 | |
---|
613 | //reset the order values if necessary |
---|
614 | if ([lowestOrderValue intValue] < [fTorrents count]) |
---|
615 | { |
---|
616 | NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
617 | @"orderValue" ascending: YES] autorelease]; |
---|
618 | NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil]; |
---|
619 | |
---|
620 | NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors]; |
---|
621 | [descriptors release]; |
---|
622 | |
---|
623 | int i; |
---|
624 | for (i = [lowestOrderValue intValue]; i < [tempTorrents count]; i++) |
---|
625 | [[tempTorrents objectAtIndex: i] setOrderValue: i]; |
---|
626 | } |
---|
627 | |
---|
628 | [self torrentNumberChanged]; |
---|
629 | [fTableView deselectAll: nil]; |
---|
630 | [self updateUI: nil]; |
---|
631 | [self updateTorrentHistory]; |
---|
632 | } |
---|
633 | |
---|
634 | - (void) removeNoDelete: (id) sender |
---|
635 | { |
---|
636 | [self removeWithIndex: [fTableView selectedRowIndexes] deleteData: NO deleteTorrent: NO]; |
---|
637 | } |
---|
638 | |
---|
639 | - (void) removeDeleteData: (id) sender |
---|
640 | { |
---|
641 | [self removeWithIndex: [fTableView selectedRowIndexes] deleteData: YES deleteTorrent: NO]; |
---|
642 | } |
---|
643 | |
---|
644 | - (void) removeDeleteTorrent: (id) sender |
---|
645 | { |
---|
646 | [self removeWithIndex: [fTableView selectedRowIndexes] deleteData: NO deleteTorrent: YES]; |
---|
647 | } |
---|
648 | |
---|
649 | - (void) removeDeleteBoth: (id) sender |
---|
650 | { |
---|
651 | [self removeWithIndex: [fTableView selectedRowIndexes] deleteData: YES deleteTorrent: YES]; |
---|
652 | } |
---|
653 | |
---|
654 | - (void) copyTorrentFile: (id) sender |
---|
655 | { |
---|
656 | [self copyTorrentFileForTorrents: [[NSMutableArray alloc] initWithArray: |
---|
657 | [self torrentsAtIndexes: [fTableView selectedRowIndexes]]]]; |
---|
658 | } |
---|
659 | |
---|
660 | - (void) copyTorrentFileForTorrents: (NSMutableArray *) torrents |
---|
661 | { |
---|
662 | if ([torrents count] == 0) |
---|
663 | { |
---|
664 | [torrents release]; |
---|
665 | return; |
---|
666 | } |
---|
667 | |
---|
668 | Torrent * torrent = [torrents objectAtIndex: 0]; |
---|
669 | |
---|
670 | //warn user if torrent file can't be found |
---|
671 | if (![[NSFileManager defaultManager] fileExistsAtPath: [torrent torrentLocation]]) |
---|
672 | { |
---|
673 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
674 | [alert addButtonWithTitle: @"OK"]; |
---|
675 | [alert setMessageText: [NSString stringWithFormat: |
---|
676 | @"Copy of \"%@\" Cannot Be Created", [torrent name]]]; |
---|
677 | [alert setInformativeText: [NSString stringWithFormat: |
---|
678 | @"The torrent file (%@) cannot be found.", [torrent torrentLocation]]]; |
---|
679 | [alert setAlertStyle: NSWarningAlertStyle]; |
---|
680 | |
---|
681 | [alert runModal]; |
---|
682 | |
---|
683 | [torrents removeObjectAtIndex: 0]; |
---|
684 | [self copyTorrentFileForTorrents: torrents]; |
---|
685 | } |
---|
686 | else |
---|
687 | { |
---|
688 | NSSavePanel * panel = [NSSavePanel savePanel]; |
---|
689 | [panel setRequiredFileType: @"torrent"]; |
---|
690 | [panel setCanSelectHiddenExtension: NO]; |
---|
691 | [panel setExtensionHidden: NO]; |
---|
692 | |
---|
693 | [panel beginSheetForDirectory: nil file: [torrent name] |
---|
694 | modalForWindow: fWindow modalDelegate: self didEndSelector: |
---|
695 | @selector( saveTorrentCopySheetClosed:returnCode:contextInfo: ) |
---|
696 | contextInfo: torrents]; |
---|
697 | } |
---|
698 | } |
---|
699 | |
---|
700 | - (void) saveTorrentCopySheetClosed: (NSSavePanel *) panel returnCode: (int) code |
---|
701 | contextInfo: (NSMutableArray *) torrents |
---|
702 | { |
---|
703 | //if save successful, copy torrent to new location with name of data file |
---|
704 | if (code == NSOKButton) |
---|
705 | [[NSFileManager defaultManager] copyPath: [[torrents objectAtIndex: 0] torrentLocation] |
---|
706 | toPath: [panel filename] handler: nil]; |
---|
707 | |
---|
708 | [torrents removeObjectAtIndex: 0]; |
---|
709 | [self performSelectorOnMainThread: @selector(copyTorrentFileForTorrents:) |
---|
710 | withObject: torrents waitUntilDone: NO]; |
---|
711 | } |
---|
712 | |
---|
713 | - (void) revealFile: (id) sender |
---|
714 | { |
---|
715 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
716 | unsigned int i; |
---|
717 | |
---|
718 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
719 | [[fTorrents objectAtIndex: i] revealData]; |
---|
720 | } |
---|
721 | |
---|
722 | - (void) showPreferenceWindow: (id) sender |
---|
723 | { |
---|
724 | NSWindow * window = [fPrefsController window]; |
---|
725 | if (![window isVisible]) |
---|
726 | [window center]; |
---|
727 | |
---|
728 | [window makeKeyAndOrderFront: nil]; |
---|
729 | } |
---|
730 | |
---|
731 | - (void) showInfo: (id) sender |
---|
732 | { |
---|
733 | if ([[fInfoController window] isVisible]) |
---|
734 | [[fInfoController window] performClose: nil]; |
---|
735 | else |
---|
736 | { |
---|
737 | [fInfoController updateInfoStats]; |
---|
738 | [[fInfoController window] orderFront: nil]; |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | - (void) setInfoTab: (id) sender |
---|
743 | { |
---|
744 | if (sender == fNextInfoTabItem) |
---|
745 | [fInfoController setNextTab]; |
---|
746 | else |
---|
747 | [fInfoController setPreviousTab]; |
---|
748 | } |
---|
749 | |
---|
750 | - (void) updateUI: (NSTimer *) t |
---|
751 | { |
---|
752 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
753 | Torrent * torrent; |
---|
754 | while ((torrent = [enumerator nextObject])) |
---|
755 | { |
---|
756 | [torrent update]; |
---|
757 | |
---|
758 | if ([torrent justFinished]) |
---|
759 | { |
---|
760 | [self checkWaitingForFinished: torrent]; |
---|
761 | |
---|
762 | //notifications |
---|
763 | [self notifyGrowl: [torrent name]]; |
---|
764 | if (![fWindow isKeyWindow]) |
---|
765 | fCompleted++; |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | if ([fSortType isEqualToString: @"Progress"] || [fSortType isEqualToString: @"State"]) |
---|
770 | [self sortTorrents]; |
---|
771 | else |
---|
772 | [fTableView reloadData]; |
---|
773 | |
---|
774 | //update the global DL/UL rates |
---|
775 | float downloadRate, uploadRate; |
---|
776 | tr_torrentRates(fLib, & downloadRate, & uploadRate); |
---|
777 | if (fStatusBarVisible) |
---|
778 | { |
---|
779 | [fTotalDLField setStringValue: [NSString stringForSpeed: downloadRate]]; |
---|
780 | [fTotalULField setStringValue: [NSString stringForSpeed: uploadRate]]; |
---|
781 | } |
---|
782 | |
---|
783 | if ([[fInfoController window] isVisible]) |
---|
784 | [fInfoController updateInfoStats]; |
---|
785 | |
---|
786 | //badge dock |
---|
787 | [fBadger updateBadgeWithCompleted: fCompleted |
---|
788 | uploadRate: uploadRate downloadRate: downloadRate]; |
---|
789 | } |
---|
790 | |
---|
791 | - (void) updateTorrentHistory |
---|
792 | { |
---|
793 | NSMutableArray * history = [NSMutableArray |
---|
794 | arrayWithCapacity: [fTorrents count]]; |
---|
795 | |
---|
796 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
797 | Torrent * torrent; |
---|
798 | while( ( torrent = [enumerator nextObject] ) ) |
---|
799 | [history addObject: [torrent history]]; |
---|
800 | |
---|
801 | [fDefaults setObject: history forKey: @"History"]; |
---|
802 | [fDefaults synchronize]; |
---|
803 | } |
---|
804 | |
---|
805 | - (void) sortTorrents |
---|
806 | { |
---|
807 | //remember selected rows if needed |
---|
808 | NSArray * selectedTorrents = nil; |
---|
809 | int numSelected = [fTableView numberOfSelectedRows]; |
---|
810 | if (numSelected > 0 && numSelected < [fTorrents count]) |
---|
811 | selectedTorrents = [self torrentsAtIndexes: [fTableView selectedRowIndexes]]; |
---|
812 | |
---|
813 | NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
814 | @"name" ascending: YES] autorelease], |
---|
815 | * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
816 | @"orderValue" ascending: YES] autorelease]; |
---|
817 | |
---|
818 | NSArray * descriptors; |
---|
819 | if ([fSortType isEqualToString: @"Name"]) |
---|
820 | descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, orderDescriptor, nil]; |
---|
821 | else if ([fSortType isEqualToString: @"State"]) |
---|
822 | { |
---|
823 | NSSortDescriptor * stateDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
824 | @"stateSortKey" ascending: NO] autorelease], |
---|
825 | * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
826 | @"progressSortKey" ascending: NO] autorelease]; |
---|
827 | |
---|
828 | descriptors = [[NSArray alloc] initWithObjects: stateDescriptor, progressDescriptor, |
---|
829 | nameDescriptor, orderDescriptor, nil]; |
---|
830 | } |
---|
831 | else if ([fSortType isEqualToString: @"Progress"]) |
---|
832 | { |
---|
833 | NSSortDescriptor * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
834 | @"progressSortKey" ascending: YES] autorelease]; |
---|
835 | |
---|
836 | descriptors = [[NSArray alloc] initWithObjects: progressDescriptor, nameDescriptor, orderDescriptor, nil]; |
---|
837 | } |
---|
838 | else if ([fSortType isEqualToString: @"Date"]) |
---|
839 | { |
---|
840 | NSSortDescriptor * dateDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
841 | @"date" ascending: YES] autorelease]; |
---|
842 | |
---|
843 | descriptors = [[NSArray alloc] initWithObjects: dateDescriptor, orderDescriptor, nil]; |
---|
844 | } |
---|
845 | else |
---|
846 | descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil]; |
---|
847 | |
---|
848 | [fTorrents sortUsingDescriptors: descriptors]; |
---|
849 | |
---|
850 | [descriptors release]; |
---|
851 | |
---|
852 | [fTableView reloadData]; |
---|
853 | |
---|
854 | //set selected rows if needed |
---|
855 | if (selectedTorrents) |
---|
856 | { |
---|
857 | Torrent * torrent; |
---|
858 | NSEnumerator * enumerator = [selectedTorrents objectEnumerator]; |
---|
859 | NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init]; |
---|
860 | while ((torrent = [enumerator nextObject])) |
---|
861 | [indexSet addIndex: [fTorrents indexOfObject: torrent]]; |
---|
862 | |
---|
863 | [fTableView selectRowIndexes: indexSet byExtendingSelection: NO]; |
---|
864 | [indexSet release]; |
---|
865 | } |
---|
866 | } |
---|
867 | |
---|
868 | - (void) setSort: (id) sender |
---|
869 | { |
---|
870 | NSMenuItem * prevSortItem; |
---|
871 | if ([fSortType isEqualToString: @"Name"]) |
---|
872 | prevSortItem = fNameSortItem; |
---|
873 | else if ([fSortType isEqualToString: @"State"]) |
---|
874 | prevSortItem = fStateSortItem; |
---|
875 | else if ([fSortType isEqualToString: @"Progress"]) |
---|
876 | prevSortItem = fProgressSortItem; |
---|
877 | else if ([fSortType isEqualToString: @"Date"]) |
---|
878 | prevSortItem = fDateSortItem; |
---|
879 | else |
---|
880 | prevSortItem = fOrderSortItem; |
---|
881 | |
---|
882 | if (sender != prevSortItem) |
---|
883 | { |
---|
884 | [prevSortItem setState: NSOffState]; |
---|
885 | [sender setState: NSOnState]; |
---|
886 | |
---|
887 | [fSortType release]; |
---|
888 | if (sender == fNameSortItem) |
---|
889 | fSortType = [[NSString alloc] initWithString: @"Name"]; |
---|
890 | else if (sender == fStateSortItem) |
---|
891 | fSortType = [[NSString alloc] initWithString: @"State"]; |
---|
892 | else if (sender == fProgressSortItem) |
---|
893 | fSortType = [[NSString alloc] initWithString: @"Progress"]; |
---|
894 | else if (sender == fDateSortItem) |
---|
895 | fSortType = [[NSString alloc] initWithString: @"Date"]; |
---|
896 | else |
---|
897 | fSortType = [[NSString alloc] initWithString: @"Order"]; |
---|
898 | |
---|
899 | [fDefaults setObject: fSortType forKey: @"Sort"]; |
---|
900 | } |
---|
901 | |
---|
902 | [self sortTorrents]; |
---|
903 | } |
---|
904 | |
---|
905 | - (void) setLimitGlobalEnabled: (id) sender |
---|
906 | { |
---|
907 | [fPrefsController setLimitEnabled: (sender == fUploadLimitItem || sender == fDownloadLimitItem) |
---|
908 | type: (sender == fUploadLimitItem || sender == fUploadNoLimitItem) ? @"Upload" : @"Download"]; |
---|
909 | } |
---|
910 | |
---|
911 | - (void) setQuickLimitGlobal: (id) sender |
---|
912 | { |
---|
913 | [fPrefsController setQuickLimit: [[sender title] intValue] |
---|
914 | type: [sender menu] == fUploadMenu ? @"Upload" : @"Download"]; |
---|
915 | } |
---|
916 | |
---|
917 | - (void) limitGlobalChange: (NSNotification *) notification |
---|
918 | { |
---|
919 | NSDictionary * dict = [notification object]; |
---|
920 | |
---|
921 | NSMenuItem * limitItem, * noLimitItem; |
---|
922 | if ([[dict objectForKey: @"Type"] isEqualToString: @"Upload"]) |
---|
923 | { |
---|
924 | limitItem = fUploadLimitItem; |
---|
925 | noLimitItem = fUploadNoLimitItem; |
---|
926 | } |
---|
927 | else |
---|
928 | { |
---|
929 | limitItem = fDownloadLimitItem; |
---|
930 | noLimitItem = fDownloadNoLimitItem; |
---|
931 | } |
---|
932 | |
---|
933 | BOOL enable = [[dict objectForKey: @"Enable"] boolValue]; |
---|
934 | [limitItem setState: enable ? NSOnState : NSOffState]; |
---|
935 | [noLimitItem setState: !enable ? NSOnState : NSOffState]; |
---|
936 | |
---|
937 | [limitItem setTitle: [NSString stringWithFormat: @"Limit (%d KB/s)", |
---|
938 | [[dict objectForKey: @"Limit"] intValue]]]; |
---|
939 | |
---|
940 | [dict release]; |
---|
941 | } |
---|
942 | |
---|
943 | - (void) setRatioGlobalEnabled: (id) sender |
---|
944 | { |
---|
945 | [fPrefsController setRatioEnabled: sender == fRatioSetItem]; |
---|
946 | } |
---|
947 | |
---|
948 | - (void) setQuickRatioGlobal: (id) sender |
---|
949 | { |
---|
950 | [fPrefsController setQuickRatio: [[sender title] floatValue]]; |
---|
951 | } |
---|
952 | |
---|
953 | - (void) ratioGlobalChange: (NSNotification *) notification |
---|
954 | { |
---|
955 | NSDictionary * dict = [notification object]; |
---|
956 | |
---|
957 | BOOL enable = [[dict objectForKey: @"Enable"] boolValue]; |
---|
958 | [fRatioSetItem setState: enable ? NSOnState : NSOffState]; |
---|
959 | [fRatioNotSetItem setState: !enable ? NSOnState : NSOffState]; |
---|
960 | |
---|
961 | [fRatioSetItem setTitle: [NSString stringWithFormat: @"Stop at Ratio (%.2f)", |
---|
962 | [[dict objectForKey: @"Ratio"] floatValue]]]; |
---|
963 | |
---|
964 | [dict release]; |
---|
965 | } |
---|
966 | |
---|
967 | - (void) checkWaitingForStopped: (NSNotification *) notification |
---|
968 | { |
---|
969 | [self checkWaitingForFinished: [notification object]]; |
---|
970 | } |
---|
971 | |
---|
972 | - (void) checkWaitingForFinished: (Torrent *) finishedTorrent |
---|
973 | { |
---|
974 | //don't try to start a transfer if there should be none waiting |
---|
975 | if (![[fDefaults stringForKey: @"StartSetting"] isEqualToString: @"Wait"]) |
---|
976 | return; |
---|
977 | |
---|
978 | int desiredActive = [fDefaults integerForKey: @"WaitToStartNumber"], active = 0; |
---|
979 | |
---|
980 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
981 | Torrent * torrent, * torrentToStart = nil; |
---|
982 | while ((torrent = [enumerator nextObject])) |
---|
983 | { |
---|
984 | //ignore the torrent just stopped; for some reason it is not marked instantly as not active |
---|
985 | if (torrent == finishedTorrent) |
---|
986 | continue; |
---|
987 | |
---|
988 | if ([torrent isActive]) |
---|
989 | { |
---|
990 | if (![torrent isSeeding]) |
---|
991 | { |
---|
992 | active++; |
---|
993 | if (active >= desiredActive) |
---|
994 | return; |
---|
995 | } |
---|
996 | } |
---|
997 | else |
---|
998 | { |
---|
999 | //use as next if it is waiting to start and either no previous or order value is lower |
---|
1000 | if ([torrent waitingToStart] && (!torrentToStart |
---|
1001 | || [[torrentToStart orderValue] compare: [torrent orderValue]] == NSOrderedDescending)) |
---|
1002 | torrentToStart = torrent; |
---|
1003 | } |
---|
1004 | } |
---|
1005 | |
---|
1006 | //since it hasn't returned, the queue amount has not been met |
---|
1007 | if (torrentToStart) |
---|
1008 | { |
---|
1009 | [torrentToStart startTransfer]; |
---|
1010 | [self updateUI: nil]; |
---|
1011 | } |
---|
1012 | } |
---|
1013 | |
---|
1014 | - (void) globalStartSettingChange: (NSNotification *) notification |
---|
1015 | { |
---|
1016 | NSString * startSetting = [fDefaults stringForKey: @"StartSetting"]; |
---|
1017 | |
---|
1018 | if ([startSetting isEqualToString: @"Start"]) |
---|
1019 | { |
---|
1020 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1021 | Torrent * torrent; |
---|
1022 | while ((torrent = [enumerator nextObject])) |
---|
1023 | if ([torrent waitingToStart]) |
---|
1024 | [torrent startTransfer]; |
---|
1025 | } |
---|
1026 | else if ([startSetting isEqualToString: @"Wait"]) |
---|
1027 | { |
---|
1028 | NSMutableArray * waitingTorrents = [[NSMutableArray alloc] initWithCapacity: [fTorrents count]]; |
---|
1029 | |
---|
1030 | int amountToStart = [fDefaults integerForKey: @"WaitToStartNumber"]; |
---|
1031 | |
---|
1032 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1033 | Torrent * torrent; |
---|
1034 | while ((torrent = [enumerator nextObject])) |
---|
1035 | { |
---|
1036 | if ([torrent isActive]) |
---|
1037 | { |
---|
1038 | if (![torrent isSeeding]) |
---|
1039 | { |
---|
1040 | amountToStart--; |
---|
1041 | if (amountToStart <= 0) |
---|
1042 | break; |
---|
1043 | } |
---|
1044 | } |
---|
1045 | else if ([torrent waitingToStart]) |
---|
1046 | [waitingTorrents addObject: torrent]; |
---|
1047 | else; |
---|
1048 | } |
---|
1049 | |
---|
1050 | int waitingCount = [waitingTorrents count]; |
---|
1051 | if (amountToStart > 0 && waitingCount > 0) |
---|
1052 | { |
---|
1053 | if (amountToStart > waitingCount) |
---|
1054 | amountToStart = waitingCount; |
---|
1055 | |
---|
1056 | //sort torrents by date to start earliest added |
---|
1057 | if (amountToStart < waitingCount) |
---|
1058 | { |
---|
1059 | NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
1060 | @"orderValue" ascending: YES] autorelease]; |
---|
1061 | NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil]; |
---|
1062 | |
---|
1063 | [waitingTorrents sortUsingDescriptors: descriptors]; |
---|
1064 | [descriptors release]; |
---|
1065 | } |
---|
1066 | |
---|
1067 | int i; |
---|
1068 | for (i = 0; i < amountToStart; i++) |
---|
1069 | [[waitingTorrents objectAtIndex: i] startTransfer]; |
---|
1070 | } |
---|
1071 | |
---|
1072 | [waitingTorrents release]; |
---|
1073 | } |
---|
1074 | else; |
---|
1075 | |
---|
1076 | [self updateUI: nil]; |
---|
1077 | |
---|
1078 | //update info for changed start setting |
---|
1079 | [self reloadInspector: nil]; |
---|
1080 | } |
---|
1081 | |
---|
1082 | - (void) torrentStartSettingChange: (NSNotification *) notification |
---|
1083 | { |
---|
1084 | [self attemptToStartAuto: [notification object]]; |
---|
1085 | |
---|
1086 | [self updateUI: nil]; |
---|
1087 | [self reloadInspector: nil]; |
---|
1088 | [self updateTorrentHistory]; |
---|
1089 | } |
---|
1090 | |
---|
1091 | //will try to start, taking into consideration the start preference |
---|
1092 | - (void) attemptToStartAuto: (Torrent *) torrent |
---|
1093 | { |
---|
1094 | #warning should check if transfer was already done |
---|
1095 | if (![torrent waitingToStart]) |
---|
1096 | return; |
---|
1097 | |
---|
1098 | NSString * startSetting = [fDefaults stringForKey: @"StartSetting"]; |
---|
1099 | if ([startSetting isEqualToString: @"Wait"]) |
---|
1100 | { |
---|
1101 | int desiredActive = [fDefaults integerForKey: @"WaitToStartNumber"]; |
---|
1102 | |
---|
1103 | Torrent * tempTorrent; |
---|
1104 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1105 | while ((tempTorrent = [enumerator nextObject])) |
---|
1106 | if ([tempTorrent isActive] && ![tempTorrent isSeeding]) |
---|
1107 | { |
---|
1108 | desiredActive--; |
---|
1109 | if (desiredActive <= 0) |
---|
1110 | return; |
---|
1111 | } |
---|
1112 | |
---|
1113 | [torrent startTransfer]; |
---|
1114 | } |
---|
1115 | else if ([startSetting isEqualToString: @"Start"]) |
---|
1116 | [torrent startTransfer]; |
---|
1117 | else; |
---|
1118 | } |
---|
1119 | |
---|
1120 | - (void) reloadInspector: (NSNotification *) notification |
---|
1121 | { |
---|
1122 | [fInfoController updateInfoForTorrents: [self torrentsAtIndexes: [fTableView selectedRowIndexes]]]; |
---|
1123 | } |
---|
1124 | |
---|
1125 | - (int) numberOfRowsInTableView: (NSTableView *) t |
---|
1126 | { |
---|
1127 | return [fTorrents count]; |
---|
1128 | } |
---|
1129 | |
---|
1130 | - (void) tableView: (NSTableView *) t willDisplayCell: (id) cell |
---|
1131 | forTableColumn: (NSTableColumn *) tableColumn row: (int) row |
---|
1132 | { |
---|
1133 | [cell setTorrent: [fTorrents objectAtIndex: row]]; |
---|
1134 | } |
---|
1135 | |
---|
1136 | - (BOOL) tableView: (NSTableView *) t acceptDrop: |
---|
1137 | (id <NSDraggingInfo>) info row: (int) row dropOperation: |
---|
1138 | (NSTableViewDropOperation) operation |
---|
1139 | { |
---|
1140 | [self application: NSApp openFiles: [[[info draggingPasteboard] |
---|
1141 | propertyListForType: NSFilenamesPboardType] |
---|
1142 | pathsMatchingExtensions: [NSArray arrayWithObject: @"torrent"]]]; |
---|
1143 | return YES; |
---|
1144 | } |
---|
1145 | |
---|
1146 | - (NSDragOperation) tableView: (NSTableView *) t validateDrop: |
---|
1147 | (id <NSDraggingInfo>) info proposedRow: (int) row |
---|
1148 | proposedDropOperation: (NSTableViewDropOperation) operation |
---|
1149 | { |
---|
1150 | NSPasteboard * pasteboard = [info draggingPasteboard]; |
---|
1151 | if (![[pasteboard types] containsObject: NSFilenamesPboardType] |
---|
1152 | || [[[pasteboard propertyListForType: NSFilenamesPboardType] |
---|
1153 | pathsMatchingExtensions: [NSArray arrayWithObject: @"torrent"]] count] == 0) |
---|
1154 | return NSDragOperationNone; |
---|
1155 | |
---|
1156 | [fTableView setDropRow: [fTableView numberOfRows] dropOperation: NSTableViewDropAbove]; |
---|
1157 | return NSDragOperationGeneric; |
---|
1158 | } |
---|
1159 | |
---|
1160 | - (void) tableViewSelectionDidChange: (NSNotification *) notification |
---|
1161 | { |
---|
1162 | [self reloadInspector: nil]; |
---|
1163 | } |
---|
1164 | |
---|
1165 | - (void) toggleStatusBar: (id) sender |
---|
1166 | { |
---|
1167 | [self showStatusBar: !fStatusBarVisible animate: YES]; |
---|
1168 | [fDefaults setBool: fStatusBarVisible forKey: @"StatusBar"]; |
---|
1169 | } |
---|
1170 | |
---|
1171 | - (void) showStatusBar: (BOOL) show animate: (BOOL) animate |
---|
1172 | { |
---|
1173 | if (show == fStatusBarVisible) |
---|
1174 | return; |
---|
1175 | |
---|
1176 | NSRect frame = [fWindow frame]; |
---|
1177 | float heightChange = [fStatusBar frame].size.height; |
---|
1178 | if (!show) |
---|
1179 | heightChange *= -1; |
---|
1180 | |
---|
1181 | frame.size.height += heightChange; |
---|
1182 | frame.origin.y -= heightChange; |
---|
1183 | |
---|
1184 | fStatusBarVisible = !fStatusBarVisible; |
---|
1185 | |
---|
1186 | //reloads stats |
---|
1187 | [self updateUI: nil]; |
---|
1188 | |
---|
1189 | //set views to not autoresize |
---|
1190 | unsigned int statsMask = [fStatusBar autoresizingMask]; |
---|
1191 | unsigned int scrollMask = [fScrollView autoresizingMask]; |
---|
1192 | [fStatusBar setAutoresizingMask: 0]; |
---|
1193 | [fScrollView setAutoresizingMask: 0]; |
---|
1194 | |
---|
1195 | [fWindow setFrame: frame display: YES animate: animate]; |
---|
1196 | |
---|
1197 | //re-enable autoresize |
---|
1198 | [fStatusBar setAutoresizingMask: statsMask]; |
---|
1199 | [fScrollView setAutoresizingMask: scrollMask]; |
---|
1200 | |
---|
1201 | //change min size |
---|
1202 | NSSize minSize = [fWindow contentMinSize]; |
---|
1203 | minSize.height += heightChange; |
---|
1204 | [fWindow setContentMinSize: minSize]; |
---|
1205 | } |
---|
1206 | |
---|
1207 | - (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier: |
---|
1208 | (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag |
---|
1209 | { |
---|
1210 | NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier: ident]; |
---|
1211 | |
---|
1212 | if( [ident isEqualToString: TOOLBAR_OPEN] ) |
---|
1213 | { |
---|
1214 | [item setLabel: @"Open"]; |
---|
1215 | [item setPaletteLabel: @"Open Torrent Files"]; |
---|
1216 | [item setToolTip: @"Open torrent files"]; |
---|
1217 | [item setImage: [NSImage imageNamed: @"Open.png"]]; |
---|
1218 | [item setTarget: self]; |
---|
1219 | [item setAction: @selector( openShowSheet: )]; |
---|
1220 | } |
---|
1221 | else if( [ident isEqualToString: TOOLBAR_REMOVE] ) |
---|
1222 | { |
---|
1223 | [item setLabel: @"Remove"]; |
---|
1224 | [item setPaletteLabel: @"Remove Selected"]; |
---|
1225 | [item setToolTip: @"Remove selected transfers"]; |
---|
1226 | [item setImage: [NSImage imageNamed: @"Remove.png"]]; |
---|
1227 | [item setTarget: self]; |
---|
1228 | [item setAction: @selector( removeNoDelete: )]; |
---|
1229 | } |
---|
1230 | else if( [ident isEqualToString: TOOLBAR_INFO] ) |
---|
1231 | { |
---|
1232 | [item setLabel: @"Inspector"]; |
---|
1233 | [item setPaletteLabel: @"Show/Hide Inspector"]; |
---|
1234 | [item setToolTip: @"Display torrent inspector"]; |
---|
1235 | [item setImage: [NSImage imageNamed: @"Info.png"]]; |
---|
1236 | [item setTarget: self]; |
---|
1237 | [item setAction: @selector( showInfo: )]; |
---|
1238 | } |
---|
1239 | else if( [ident isEqualToString: TOOLBAR_PAUSE_ALL] ) |
---|
1240 | { |
---|
1241 | [item setLabel: @"Pause All"]; |
---|
1242 | [item setPaletteLabel: [item label]]; |
---|
1243 | [item setToolTip: @"Pause all transfers"]; |
---|
1244 | [item setImage: [NSImage imageNamed: @"PauseAll.png"]]; |
---|
1245 | [item setTarget: self]; |
---|
1246 | [item setAction: @selector( stopAllTorrents: )]; |
---|
1247 | } |
---|
1248 | else if( [ident isEqualToString: TOOLBAR_RESUME_ALL] ) |
---|
1249 | { |
---|
1250 | [item setLabel: @"Resume All"]; |
---|
1251 | [item setPaletteLabel: [item label]]; |
---|
1252 | [item setToolTip: @"Resume all transfers"]; |
---|
1253 | [item setImage: [NSImage imageNamed: @"ResumeAll.png"]]; |
---|
1254 | [item setTarget: self]; |
---|
1255 | [item setAction: @selector( resumeAllTorrents: )]; |
---|
1256 | } |
---|
1257 | else if( [ident isEqualToString: TOOLBAR_PAUSE_SELECTED] ) |
---|
1258 | { |
---|
1259 | [item setLabel: @"Pause"]; |
---|
1260 | [item setPaletteLabel: @"Pause Selected"]; |
---|
1261 | [item setToolTip: @"Pause selected transfers"]; |
---|
1262 | [item setImage: [NSImage imageNamed: @"PauseSelected.png"]]; |
---|
1263 | [item setTarget: self]; |
---|
1264 | [item setAction: @selector( stopTorrent: )]; |
---|
1265 | } |
---|
1266 | else if( [ident isEqualToString: TOOLBAR_RESUME_SELECTED] ) |
---|
1267 | { |
---|
1268 | [item setLabel: @"Resume"]; |
---|
1269 | [item setPaletteLabel: @"Resume Selected"]; |
---|
1270 | [item setToolTip: @"Resume selected transfers"]; |
---|
1271 | [item setImage: [NSImage imageNamed: @"ResumeSelected.png"]]; |
---|
1272 | [item setTarget: self]; |
---|
1273 | [item setAction: @selector( resumeTorrent: )]; |
---|
1274 | } |
---|
1275 | else |
---|
1276 | { |
---|
1277 | [item release]; |
---|
1278 | return nil; |
---|
1279 | } |
---|
1280 | |
---|
1281 | return item; |
---|
1282 | } |
---|
1283 | |
---|
1284 | - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) t |
---|
1285 | { |
---|
1286 | return [NSArray arrayWithObjects: |
---|
1287 | TOOLBAR_OPEN, TOOLBAR_REMOVE, |
---|
1288 | TOOLBAR_PAUSE_SELECTED, TOOLBAR_RESUME_SELECTED, |
---|
1289 | TOOLBAR_PAUSE_ALL, TOOLBAR_RESUME_ALL, |
---|
1290 | TOOLBAR_INFO, |
---|
1291 | NSToolbarSeparatorItemIdentifier, |
---|
1292 | NSToolbarSpaceItemIdentifier, |
---|
1293 | NSToolbarFlexibleSpaceItemIdentifier, |
---|
1294 | NSToolbarCustomizeToolbarItemIdentifier, nil]; |
---|
1295 | } |
---|
1296 | |
---|
1297 | - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) t |
---|
1298 | { |
---|
1299 | return [NSArray arrayWithObjects: |
---|
1300 | TOOLBAR_OPEN, TOOLBAR_REMOVE, |
---|
1301 | NSToolbarSeparatorItemIdentifier, |
---|
1302 | TOOLBAR_PAUSE_ALL, TOOLBAR_RESUME_ALL, |
---|
1303 | NSToolbarFlexibleSpaceItemIdentifier, |
---|
1304 | TOOLBAR_INFO, nil]; |
---|
1305 | } |
---|
1306 | |
---|
1307 | - (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem |
---|
1308 | { |
---|
1309 | NSString * ident = [toolbarItem itemIdentifier]; |
---|
1310 | |
---|
1311 | //enable remove item |
---|
1312 | if ([ident isEqualToString: TOOLBAR_REMOVE]) |
---|
1313 | return [fTableView numberOfSelectedRows] > 0; |
---|
1314 | |
---|
1315 | //enable pause all item |
---|
1316 | if ([ident isEqualToString: TOOLBAR_PAUSE_ALL]) |
---|
1317 | { |
---|
1318 | Torrent * torrent; |
---|
1319 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1320 | while ((torrent = [enumerator nextObject])) |
---|
1321 | if ([torrent isActive]) |
---|
1322 | return YES; |
---|
1323 | return NO; |
---|
1324 | } |
---|
1325 | |
---|
1326 | //enable resume all item |
---|
1327 | if ([ident isEqualToString: TOOLBAR_RESUME_ALL]) |
---|
1328 | { |
---|
1329 | Torrent * torrent; |
---|
1330 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1331 | while ((torrent = [enumerator nextObject])) |
---|
1332 | if ([torrent isPaused]) |
---|
1333 | return YES; |
---|
1334 | return NO; |
---|
1335 | } |
---|
1336 | |
---|
1337 | //enable pause item |
---|
1338 | if ([ident isEqualToString: TOOLBAR_PAUSE_SELECTED]) |
---|
1339 | { |
---|
1340 | Torrent * torrent; |
---|
1341 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
1342 | unsigned int i; |
---|
1343 | |
---|
1344 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
1345 | if ([[fTorrents objectAtIndex: i] isActive]) |
---|
1346 | return YES; |
---|
1347 | return NO; |
---|
1348 | } |
---|
1349 | |
---|
1350 | //enable resume item |
---|
1351 | if ([ident isEqualToString: TOOLBAR_RESUME_SELECTED]) |
---|
1352 | { |
---|
1353 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
1354 | unsigned int i; |
---|
1355 | |
---|
1356 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
1357 | if ([[fTorrents objectAtIndex: i] isPaused]) |
---|
1358 | return YES; |
---|
1359 | return NO; |
---|
1360 | } |
---|
1361 | |
---|
1362 | return YES; |
---|
1363 | } |
---|
1364 | |
---|
1365 | - (BOOL) validateMenuItem: (NSMenuItem *) menuItem |
---|
1366 | { |
---|
1367 | SEL action = [menuItem action]; |
---|
1368 | |
---|
1369 | //only enable some items if it is in a context menu or the window is useable |
---|
1370 | BOOL canUseMenu = [[[menuItem menu] title] isEqualToString: @"Context"] |
---|
1371 | || [fWindow isKeyWindow]; |
---|
1372 | |
---|
1373 | //enable show info |
---|
1374 | if (action == @selector(showInfo:)) |
---|
1375 | { |
---|
1376 | NSString * title = [[fInfoController window] isVisible] ? @"Hide Inspector" : @"Show Inspector"; |
---|
1377 | if (![[menuItem title] isEqualToString: title]) |
---|
1378 | [menuItem setTitle: title]; |
---|
1379 | |
---|
1380 | return YES; |
---|
1381 | } |
---|
1382 | |
---|
1383 | if (action == @selector(setInfoTab:)) |
---|
1384 | return [[fInfoController window] isVisible]; |
---|
1385 | |
---|
1386 | //enable toggle status bar |
---|
1387 | if (action == @selector(toggleStatusBar:)) |
---|
1388 | { |
---|
1389 | NSString * title = fStatusBarVisible ? @"Hide Status Bar" : @"Show Status Bar"; |
---|
1390 | if (![[menuItem title] isEqualToString: title]) |
---|
1391 | [menuItem setTitle: title]; |
---|
1392 | |
---|
1393 | return canUseMenu; |
---|
1394 | } |
---|
1395 | |
---|
1396 | //enable resume all item |
---|
1397 | if (action == @selector(resumeAllTorrents:)) |
---|
1398 | { |
---|
1399 | Torrent * torrent; |
---|
1400 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1401 | while ((torrent = [enumerator nextObject])) |
---|
1402 | if ([torrent isPaused]) |
---|
1403 | return YES; |
---|
1404 | return NO; |
---|
1405 | } |
---|
1406 | |
---|
1407 | //enable pause all item |
---|
1408 | if (action == @selector(stopAllTorrents:)) |
---|
1409 | { |
---|
1410 | Torrent * torrent; |
---|
1411 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1412 | while ((torrent = [enumerator nextObject])) |
---|
1413 | if ([torrent isActive]) |
---|
1414 | return YES; |
---|
1415 | return NO; |
---|
1416 | } |
---|
1417 | |
---|
1418 | if (action == @selector(revealFile:)) |
---|
1419 | { |
---|
1420 | return canUseMenu && [fTableView numberOfSelectedRows] > 0; |
---|
1421 | } |
---|
1422 | |
---|
1423 | //enable remove items |
---|
1424 | if (action == @selector(removeNoDelete:) || action == @selector(removeDeleteData:) |
---|
1425 | || action == @selector(removeDeleteTorrent:) || action == @selector(removeDeleteBoth:)) |
---|
1426 | { |
---|
1427 | BOOL active = NO, |
---|
1428 | canDelete = action != @selector(removeDeleteTorrent:) && action != @selector(removeDeleteBoth:); |
---|
1429 | Torrent * torrent; |
---|
1430 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
1431 | unsigned int i; |
---|
1432 | |
---|
1433 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
1434 | { |
---|
1435 | torrent = [fTorrents objectAtIndex: i]; |
---|
1436 | if (!active && [torrent isActive]) |
---|
1437 | { |
---|
1438 | active = YES; |
---|
1439 | if (canDelete) |
---|
1440 | break; |
---|
1441 | } |
---|
1442 | if (!canDelete && [torrent publicTorrent]) |
---|
1443 | { |
---|
1444 | canDelete = YES; |
---|
1445 | if (active) |
---|
1446 | break; |
---|
1447 | } |
---|
1448 | } |
---|
1449 | |
---|
1450 | //append or remove ellipsis when needed |
---|
1451 | NSString * title = [menuItem title], * ellipsis = [NSString ellipsis]; |
---|
1452 | if (active && [fDefaults boolForKey: @"CheckRemove"]) |
---|
1453 | { |
---|
1454 | if (![title hasSuffix: ellipsis]) |
---|
1455 | [menuItem setTitle: [title stringByAppendingEllipsis]]; |
---|
1456 | } |
---|
1457 | else |
---|
1458 | { |
---|
1459 | if ([title hasSuffix: ellipsis]) |
---|
1460 | [menuItem setTitle: [title substringToIndex: [title rangeOfString: ellipsis].location]]; |
---|
1461 | } |
---|
1462 | |
---|
1463 | return canUseMenu && canDelete && [fTableView numberOfSelectedRows] > 0; |
---|
1464 | } |
---|
1465 | |
---|
1466 | //enable pause item |
---|
1467 | if( action == @selector(stopTorrent:) ) |
---|
1468 | { |
---|
1469 | if (!canUseMenu) |
---|
1470 | return NO; |
---|
1471 | |
---|
1472 | Torrent * torrent; |
---|
1473 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
1474 | unsigned int i; |
---|
1475 | |
---|
1476 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
1477 | { |
---|
1478 | torrent = [fTorrents objectAtIndex: i]; |
---|
1479 | if ([torrent isActive]) |
---|
1480 | return YES; |
---|
1481 | } |
---|
1482 | return NO; |
---|
1483 | } |
---|
1484 | |
---|
1485 | //enable resume item |
---|
1486 | if( action == @selector(resumeTorrent:) ) |
---|
1487 | { |
---|
1488 | if (!canUseMenu) |
---|
1489 | return NO; |
---|
1490 | |
---|
1491 | Torrent * torrent; |
---|
1492 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
1493 | unsigned int i; |
---|
1494 | |
---|
1495 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
1496 | { |
---|
1497 | torrent = [fTorrents objectAtIndex: i]; |
---|
1498 | if ([torrent isPaused]) |
---|
1499 | return YES; |
---|
1500 | } |
---|
1501 | return NO; |
---|
1502 | } |
---|
1503 | |
---|
1504 | //enable resume item |
---|
1505 | if (action == @selector(setSort:) || (action == @selector(advancedChanged:))) |
---|
1506 | return canUseMenu; |
---|
1507 | |
---|
1508 | //enable copy torrent file item |
---|
1509 | if( action == @selector(copyTorrentFile:) ) |
---|
1510 | { |
---|
1511 | return canUseMenu && [fTableView numberOfSelectedRows] > 0; |
---|
1512 | } |
---|
1513 | |
---|
1514 | return YES; |
---|
1515 | } |
---|
1516 | |
---|
1517 | - (void) sleepCallBack: (natural_t) messageType argument: (void *) messageArgument |
---|
1518 | { |
---|
1519 | NSEnumerator * enumerator; |
---|
1520 | Torrent * torrent; |
---|
1521 | BOOL active; |
---|
1522 | |
---|
1523 | switch( messageType ) |
---|
1524 | { |
---|
1525 | case kIOMessageSystemWillSleep: |
---|
1526 | /* Close all connections before going to sleep and remember |
---|
1527 | we should resume when we wake up */ |
---|
1528 | [fTorrents makeObjectsPerformSelector: @selector(sleep)]; |
---|
1529 | |
---|
1530 | /* Wait for torrents to stop (5 seconds timeout) */ |
---|
1531 | NSDate * start = [NSDate date]; |
---|
1532 | enumerator = [fTorrents objectEnumerator]; |
---|
1533 | while( ( torrent = [enumerator nextObject] ) ) |
---|
1534 | { |
---|
1535 | while( [[NSDate date] timeIntervalSinceDate: start] < 5 && |
---|
1536 | ![torrent isPaused] ) |
---|
1537 | { |
---|
1538 | usleep( 100000 ); |
---|
1539 | [torrent update]; |
---|
1540 | } |
---|
1541 | } |
---|
1542 | |
---|
1543 | IOAllowPowerChange( fRootPort, (long) messageArgument ); |
---|
1544 | break; |
---|
1545 | |
---|
1546 | case kIOMessageCanSystemSleep: |
---|
1547 | /* Prevent idle sleep unless all paused */ |
---|
1548 | active = NO; |
---|
1549 | enumerator = [fTorrents objectEnumerator]; |
---|
1550 | while ((torrent = [enumerator nextObject])) |
---|
1551 | if ([torrent isActive]) |
---|
1552 | { |
---|
1553 | active = YES; |
---|
1554 | break; |
---|
1555 | } |
---|
1556 | |
---|
1557 | if (active) |
---|
1558 | IOCancelPowerChange( fRootPort, (long) messageArgument ); |
---|
1559 | else |
---|
1560 | IOAllowPowerChange( fRootPort, (long) messageArgument ); |
---|
1561 | break; |
---|
1562 | |
---|
1563 | case kIOMessageSystemHasPoweredOn: |
---|
1564 | /* Resume download after we wake up */ |
---|
1565 | [fTorrents makeObjectsPerformSelector: @selector(wakeUp)]; |
---|
1566 | break; |
---|
1567 | } |
---|
1568 | } |
---|
1569 | |
---|
1570 | - (NSRect) windowWillUseStandardFrame: (NSWindow *) w defaultFrame: (NSRect) defaultFrame |
---|
1571 | { |
---|
1572 | NSRect windowRect = [fWindow frame]; |
---|
1573 | float newHeight = windowRect.size.height - [fScrollView frame].size.height |
---|
1574 | + [fTorrents count] * ([fTableView rowHeight] + [fTableView intercellSpacing].height) + 30.0; |
---|
1575 | |
---|
1576 | float minHeight = [fWindow minSize].height; |
---|
1577 | if (newHeight < minHeight) |
---|
1578 | newHeight = minHeight; |
---|
1579 | |
---|
1580 | windowRect.origin.y -= (newHeight - windowRect.size.height); |
---|
1581 | windowRect.size.height = newHeight; |
---|
1582 | |
---|
1583 | return windowRect; |
---|
1584 | } |
---|
1585 | |
---|
1586 | - (void) showMainWindow: (id) sender |
---|
1587 | { |
---|
1588 | [fWindow makeKeyAndOrderFront: nil]; |
---|
1589 | } |
---|
1590 | |
---|
1591 | - (void) windowDidBecomeKey: (NSNotification *) notification |
---|
1592 | { |
---|
1593 | fCompleted = 0; |
---|
1594 | } |
---|
1595 | |
---|
1596 | - (void) linkHomepage: (id) sender |
---|
1597 | { |
---|
1598 | [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: WEBSITE_URL]]; |
---|
1599 | } |
---|
1600 | |
---|
1601 | - (void) linkForums: (id) sender |
---|
1602 | { |
---|
1603 | [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FORUM_URL]]; |
---|
1604 | } |
---|
1605 | |
---|
1606 | - (void) notifyGrowl: (NSString * ) file |
---|
1607 | { |
---|
1608 | if (!fHasGrowl) |
---|
1609 | return; |
---|
1610 | |
---|
1611 | NSString * growlScript = [NSString stringWithFormat: |
---|
1612 | @"tell application \"System Events\"\n" |
---|
1613 | " if exists application process \"GrowlHelperApp\" then\n" |
---|
1614 | " tell application \"GrowlHelperApp\"\n " |
---|
1615 | " notify with name \"Download Complete\"" |
---|
1616 | " title \"Download Complete\"" |
---|
1617 | " description \"%@\"" |
---|
1618 | " application name \"Transmission\"\n" |
---|
1619 | " end tell\n" |
---|
1620 | " end if\n" |
---|
1621 | "end tell", file]; |
---|
1622 | |
---|
1623 | NSAppleScript * appleScript = [[NSAppleScript alloc] initWithSource: growlScript]; |
---|
1624 | NSDictionary * error; |
---|
1625 | if (![appleScript executeAndReturnError: & error]) |
---|
1626 | NSLog(@"Growl notify failed"); |
---|
1627 | [appleScript release]; |
---|
1628 | } |
---|
1629 | |
---|
1630 | - (void) growlRegister |
---|
1631 | { |
---|
1632 | if (!fHasGrowl) |
---|
1633 | return; |
---|
1634 | |
---|
1635 | NSString * growlScript = [NSString stringWithFormat: |
---|
1636 | @"tell application \"System Events\"\n" |
---|
1637 | " if exists application process \"GrowlHelperApp\" then\n" |
---|
1638 | " tell application \"GrowlHelperApp\"\n" |
---|
1639 | " register as application \"Transmission\" " |
---|
1640 | " all notifications {\"Download Complete\"}" |
---|
1641 | " default notifications {\"Download Complete\"}" |
---|
1642 | " icon of application \"Transmission\"\n" |
---|
1643 | " end tell\n" |
---|
1644 | " end if\n" |
---|
1645 | "end tell"]; |
---|
1646 | |
---|
1647 | NSAppleScript * appleScript = [[NSAppleScript alloc] initWithSource: growlScript]; |
---|
1648 | NSDictionary * error; |
---|
1649 | if (![appleScript executeAndReturnError: & error]) |
---|
1650 | NSLog(@"Growl registration failed"); |
---|
1651 | [appleScript release]; |
---|
1652 | } |
---|
1653 | |
---|
1654 | - (void) checkUpdate: (id) sender |
---|
1655 | { |
---|
1656 | [fPrefsController checkUpdate]; |
---|
1657 | } |
---|
1658 | |
---|
1659 | - (void) prepareForUpdate: (NSNotification *) notification |
---|
1660 | { |
---|
1661 | fUpdateInProgress = YES; |
---|
1662 | } |
---|
1663 | |
---|
1664 | @end |
---|