1 | /****************************************************************************** |
---|
2 | * $Id: Controller.m 733 2006-08-07 00:46:47Z 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 | #define TOOLBAR_FILTER @"Toolbar Toggle Filter" |
---|
43 | |
---|
44 | #define GROWL_DOWNLOAD_COMPLETE @"Download Complete" |
---|
45 | #define GROWL_SEEDING_COMPLETE @"Seeding Complete" |
---|
46 | #define GROWL_AUTO_ADD @"Torrent Auto Added" |
---|
47 | |
---|
48 | #define TORRENT_TABLE_VIEW_DATA_TYPE @"TorrentTableViewDataType" |
---|
49 | |
---|
50 | #define ROW_HEIGHT_REGULAR 65.0 |
---|
51 | #define ROW_HEIGHT_SMALL 40.0 |
---|
52 | #define WINDOW_REGULAR_WIDTH 468.0 |
---|
53 | |
---|
54 | #define WEBSITE_URL @"http://transmission.m0k.org/" |
---|
55 | #define FORUM_URL @"http://transmission.m0k.org/forum/" |
---|
56 | |
---|
57 | static void sleepCallBack(void * controller, io_service_t y, natural_t messageType, void * messageArgument) |
---|
58 | { |
---|
59 | Controller * c = controller; |
---|
60 | [c sleepCallBack: messageType argument: messageArgument]; |
---|
61 | } |
---|
62 | |
---|
63 | @implementation Controller |
---|
64 | |
---|
65 | - (id) init |
---|
66 | { |
---|
67 | if ((self = [super init])) |
---|
68 | { |
---|
69 | fLib = tr_init(); |
---|
70 | |
---|
71 | fTorrents = [[NSMutableArray alloc] initWithCapacity: 10]; |
---|
72 | fDisplayedTorrents = [[NSMutableArray alloc] initWithCapacity: 10]; |
---|
73 | |
---|
74 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
75 | fInfoController = [[InfoWindowController alloc] initWithWindowNibName: @"InfoWindow"]; |
---|
76 | fPrefsController = [[PrefsController alloc] initWithWindowNibName: @"PrefsWindow" handle: fLib]; |
---|
77 | fBadger = [[Badger alloc] init]; |
---|
78 | |
---|
79 | fAutoImportedNames = [[NSMutableArray alloc] init]; |
---|
80 | |
---|
81 | [GrowlApplicationBridge setGrowlDelegate: self]; |
---|
82 | } |
---|
83 | return self; |
---|
84 | } |
---|
85 | |
---|
86 | - (void) dealloc |
---|
87 | { |
---|
88 | [[NSNotificationCenter defaultCenter] removeObserver: self]; |
---|
89 | |
---|
90 | [fTorrents release]; |
---|
91 | [fDisplayedTorrents release]; |
---|
92 | |
---|
93 | [fToolbar release]; |
---|
94 | [fInfoController release]; |
---|
95 | [fPrefsController release]; |
---|
96 | [fBadger release]; |
---|
97 | |
---|
98 | [fSortType release]; |
---|
99 | [fFilterType release]; |
---|
100 | [fAutoImportedNames release]; |
---|
101 | |
---|
102 | tr_close(fLib); |
---|
103 | [super dealloc]; |
---|
104 | } |
---|
105 | |
---|
106 | - (void) awakeFromNib |
---|
107 | { |
---|
108 | [fStatusBar setBackgroundImage: [NSImage imageNamed: @"StatusBarBackground.png"]]; |
---|
109 | [fFilterBar setBackgroundImage: [NSImage imageNamed: @"FilterBarBackground.png"]]; |
---|
110 | |
---|
111 | [fWindow setAcceptsMouseMovedEvents: YES]; //ensure filter buttons display correctly |
---|
112 | |
---|
113 | [fAdvancedBarItem setState: [fDefaults boolForKey: @"UseAdvancedBar"]]; |
---|
114 | |
---|
115 | fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Transmission Toolbar"]; |
---|
116 | [fToolbar setDelegate: self]; |
---|
117 | [fToolbar setAllowsUserCustomization: YES]; |
---|
118 | [fToolbar setAutosavesConfiguration: YES]; |
---|
119 | [fWindow setToolbar: fToolbar]; |
---|
120 | [fWindow setDelegate: self]; |
---|
121 | |
---|
122 | [fWindow makeFirstResponder: fTableView]; |
---|
123 | |
---|
124 | //set table size |
---|
125 | if ([fDefaults boolForKey: @"SmallView"]) |
---|
126 | { |
---|
127 | [fTableView setRowHeight: ROW_HEIGHT_SMALL]; |
---|
128 | [fSmallViewItem setState: NSOnState]; |
---|
129 | } |
---|
130 | |
---|
131 | //window min height |
---|
132 | NSSize contentMinSize = [fWindow contentMinSize]; |
---|
133 | contentMinSize.height = [[fWindow contentView] frame].size.height - [fScrollView frame].size.height |
---|
134 | + [fTableView rowHeight] + [fTableView intercellSpacing].height; |
---|
135 | [fWindow setContentMinSize: contentMinSize]; |
---|
136 | |
---|
137 | //set info keyboard shortcuts |
---|
138 | unichar ch = NSRightArrowFunctionKey; |
---|
139 | [fNextInfoTabItem setKeyEquivalent: [NSString stringWithCharacters: & ch length: 1]]; |
---|
140 | ch = NSLeftArrowFunctionKey; |
---|
141 | [fPrevInfoTabItem setKeyEquivalent: [NSString stringWithCharacters: & ch length: 1]]; |
---|
142 | |
---|
143 | //set up filter bar |
---|
144 | NSView * contentView = [fWindow contentView]; |
---|
145 | [fFilterBar setHidden: YES]; |
---|
146 | |
---|
147 | NSRect filterBarFrame = [fFilterBar frame]; |
---|
148 | filterBarFrame.size.width = [fWindow frame].size.width; |
---|
149 | [fFilterBar setFrame: filterBarFrame]; |
---|
150 | |
---|
151 | [contentView addSubview: fFilterBar]; |
---|
152 | [fFilterBar setFrameOrigin: NSMakePoint(0, NSMaxY([contentView frame]))]; |
---|
153 | |
---|
154 | [self showFilterBar: [fDefaults boolForKey: @"FilterBar"] animate: NO]; |
---|
155 | |
---|
156 | //set up status bar |
---|
157 | [fStatusBar setHidden: YES]; |
---|
158 | |
---|
159 | NSRect statusBarFrame = [fStatusBar frame]; |
---|
160 | statusBarFrame.size.width = [fWindow frame].size.width; |
---|
161 | [fStatusBar setFrame: statusBarFrame]; |
---|
162 | |
---|
163 | [contentView addSubview: fStatusBar]; |
---|
164 | [fStatusBar setFrameOrigin: NSMakePoint(0, NSMaxY([contentView frame]))]; |
---|
165 | [self showStatusBar: [fDefaults boolForKey: @"StatusBar"] animate: NO]; |
---|
166 | |
---|
167 | //set speed limit |
---|
168 | fSpeedLimitNormalImage = [fSpeedLimitButton image]; |
---|
169 | fSpeedLimitBlueImage = [NSImage imageNamed: @"SpeedLimitButtonBlue.png"]; |
---|
170 | fSpeedLimitGraphiteImage = [NSImage imageNamed: @"SpeedLimitButtonGraphite.png"]; |
---|
171 | |
---|
172 | [self updateControlTint: nil]; |
---|
173 | |
---|
174 | if ((fSpeedLimitEnabled = [fDefaults boolForKey: @"SpeedLimit"])) |
---|
175 | { |
---|
176 | [fSpeedLimitItem setState: NSOnState]; |
---|
177 | [fSpeedLimitDockItem setState: NSOnState]; |
---|
178 | |
---|
179 | [fSpeedLimitButton setImage: [NSColor currentControlTint] == NSBlueControlTint |
---|
180 | ? fSpeedLimitBlueImage : fSpeedLimitGraphiteImage]; |
---|
181 | } |
---|
182 | |
---|
183 | [fActionButton setToolTip: @"Shortcuts for changing global settings."]; |
---|
184 | [fSpeedLimitButton setToolTip: @"Speed Limit overrides the total bandwidth limits with its own limits."]; |
---|
185 | |
---|
186 | [fTableView setTorrents: fDisplayedTorrents]; |
---|
187 | [[fTableView tableColumnWithIdentifier: @"Torrent"] setDataCell: [[TorrentCell alloc] init]]; |
---|
188 | |
---|
189 | [fTableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, |
---|
190 | TORRENT_TABLE_VIEW_DATA_TYPE, nil]]; |
---|
191 | |
---|
192 | //register for sleep notifications |
---|
193 | IONotificationPortRef notify; |
---|
194 | io_object_t iterator; |
---|
195 | if (fRootPort = IORegisterForSystemPower(self, & notify, sleepCallBack, & iterator)) |
---|
196 | CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), |
---|
197 | kCFRunLoopCommonModes); |
---|
198 | else |
---|
199 | NSLog(@"Could not IORegisterForSystemPower"); |
---|
200 | |
---|
201 | //load torrents from history |
---|
202 | Torrent * torrent; |
---|
203 | NSDictionary * historyItem; |
---|
204 | NSEnumerator * enumerator = [[fDefaults arrayForKey: @"History"] objectEnumerator]; |
---|
205 | while ((historyItem = [enumerator nextObject])) |
---|
206 | if ((torrent = [[Torrent alloc] initWithHistory: historyItem lib: fLib])) |
---|
207 | { |
---|
208 | [fTorrents addObject: torrent]; |
---|
209 | [torrent release]; |
---|
210 | } |
---|
211 | |
---|
212 | //set sort |
---|
213 | fSortType = [[fDefaults stringForKey: @"Sort"] retain]; |
---|
214 | |
---|
215 | NSMenuItem * currentSortItem, * currentSortActionItem; |
---|
216 | if ([fSortType isEqualToString: @"Name"]) |
---|
217 | { |
---|
218 | currentSortItem = fNameSortItem; |
---|
219 | currentSortActionItem = fNameSortActionItem; |
---|
220 | } |
---|
221 | else if ([fSortType isEqualToString: @"State"]) |
---|
222 | { |
---|
223 | currentSortItem = fStateSortItem; |
---|
224 | currentSortActionItem = fStateSortActionItem; |
---|
225 | } |
---|
226 | else if ([fSortType isEqualToString: @"Progress"]) |
---|
227 | { |
---|
228 | currentSortItem = fProgressSortItem; |
---|
229 | currentSortActionItem = fProgressSortActionItem; |
---|
230 | } |
---|
231 | else if ([fSortType isEqualToString: @"Date"]) |
---|
232 | { |
---|
233 | currentSortItem = fDateSortItem; |
---|
234 | currentSortActionItem = fDateSortActionItem; |
---|
235 | } |
---|
236 | else |
---|
237 | { |
---|
238 | currentSortItem = fOrderSortItem; |
---|
239 | currentSortActionItem = fOrderSortActionItem; |
---|
240 | } |
---|
241 | [currentSortItem setState: NSOnState]; |
---|
242 | [currentSortActionItem setState: NSOnState]; |
---|
243 | |
---|
244 | //set filter |
---|
245 | fFilterType = [[fDefaults stringForKey: @"Filter"] retain]; |
---|
246 | |
---|
247 | BarButton * currentFilterButton; |
---|
248 | if ([fFilterType isEqualToString: @"Pause"]) |
---|
249 | currentFilterButton = fPauseFilterButton; |
---|
250 | else if ([fFilterType isEqualToString: @"Seed"]) |
---|
251 | currentFilterButton = fSeedFilterButton; |
---|
252 | else if ([fFilterType isEqualToString: @"Download"]) |
---|
253 | currentFilterButton = fDownloadFilterButton; |
---|
254 | else |
---|
255 | currentFilterButton = fNoFilterButton; |
---|
256 | |
---|
257 | [currentFilterButton setEnabled: YES]; |
---|
258 | |
---|
259 | //set upload limit action button |
---|
260 | [fUploadLimitItem setTitle: [NSString stringWithFormat: @"Limit (%d KB/s)", |
---|
261 | [fDefaults integerForKey: @"UploadLimit"]]]; |
---|
262 | if ([fDefaults boolForKey: @"CheckUpload"]) |
---|
263 | [fUploadLimitItem setState: NSOnState]; |
---|
264 | else |
---|
265 | [fUploadNoLimitItem setState: NSOnState]; |
---|
266 | |
---|
267 | //set download limit action menu |
---|
268 | [fDownloadLimitItem setTitle: [NSString stringWithFormat: @"Limit (%d KB/s)", |
---|
269 | [fDefaults integerForKey: @"DownloadLimit"]]]; |
---|
270 | if ([fDefaults boolForKey: @"CheckDownload"]) |
---|
271 | [fDownloadLimitItem setState: NSOnState]; |
---|
272 | else |
---|
273 | [fDownloadNoLimitItem setState: NSOnState]; |
---|
274 | |
---|
275 | //set ratio action menu |
---|
276 | [fRatioSetItem setTitle: [NSString stringWithFormat: @"Stop at Ratio (%.2f)", |
---|
277 | [fDefaults floatForKey: @"RatioLimit"]]]; |
---|
278 | if ([fDefaults boolForKey: @"RatioCheck"]) |
---|
279 | [fRatioSetItem setState: NSOnState]; |
---|
280 | else |
---|
281 | [fRatioNotSetItem setState: NSOnState]; |
---|
282 | |
---|
283 | //observe notifications |
---|
284 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
---|
285 | |
---|
286 | [nc addObserver: self selector: @selector(torrentFinishedDownloading:) |
---|
287 | name: @"TorrentFinishedDownloading" object: nil]; |
---|
288 | |
---|
289 | [nc addObserver: self selector: @selector(updateControlTint:) |
---|
290 | name: NSControlTintDidChangeNotification object: nil]; |
---|
291 | |
---|
292 | [nc addObserver: self selector: @selector(prepareForUpdate:) |
---|
293 | name: SUUpdaterWillRestartNotification object: nil]; |
---|
294 | fUpdateInProgress = NO; |
---|
295 | |
---|
296 | [nc addObserver: self selector: @selector(limitGlobalChange:) |
---|
297 | name: @"LimitGlobalChange" object: nil]; |
---|
298 | |
---|
299 | [nc addObserver: self selector: @selector(ratioGlobalChange:) |
---|
300 | name: @"RatioGlobalChange" object: nil]; |
---|
301 | |
---|
302 | [nc addObserver: self selector: @selector(autoImportChange:) |
---|
303 | name: @"AutoImportSettingChange" object: nil]; |
---|
304 | |
---|
305 | [nc addObserver: self selector: @selector(setWindowSizeToFit) |
---|
306 | name: @"AutoSizeSettingChange" object: nil]; |
---|
307 | |
---|
308 | //check to start another because of stopped torrent |
---|
309 | [nc addObserver: self selector: @selector(checkWaitingForStopped:) |
---|
310 | name: @"StoppedDownloading" object: nil]; |
---|
311 | |
---|
312 | //check all torrents for starting |
---|
313 | [nc addObserver: self selector: @selector(globalStartSettingChange:) |
---|
314 | name: @"GlobalStartSettingChange" object: nil]; |
---|
315 | |
---|
316 | //check if torrent should now start |
---|
317 | [nc addObserver: self selector: @selector(torrentStartSettingChange:) |
---|
318 | name: @"TorrentStartSettingChange" object: nil]; |
---|
319 | |
---|
320 | //check if torrent should now start |
---|
321 | [nc addObserver: self selector: @selector(torrentStoppedForRatio:) |
---|
322 | name: @"TorrentStoppedForRatio" object: nil]; |
---|
323 | |
---|
324 | //change that just impacts the inspector |
---|
325 | [nc addObserver: self selector: @selector(reloadInspectorSettings:) |
---|
326 | name: @"TorrentSettingChange" object: nil]; |
---|
327 | |
---|
328 | //change that just impacts the dock badge |
---|
329 | [nc addObserver: self selector: @selector(resetDockBadge:) |
---|
330 | name: @"DockBadgeChange" object: nil]; |
---|
331 | |
---|
332 | //timer to update the interface every second |
---|
333 | fCompleted = 0; |
---|
334 | [self updateUI: nil]; |
---|
335 | fTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self |
---|
336 | selector: @selector(updateUI:) userInfo: nil repeats: YES]; |
---|
337 | [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSModalPanelRunLoopMode]; |
---|
338 | [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSEventTrackingRunLoopMode]; |
---|
339 | |
---|
340 | [self applyFilter: nil]; |
---|
341 | |
---|
342 | [fWindow makeKeyAndOrderFront: nil]; |
---|
343 | |
---|
344 | if ([fDefaults boolForKey: @"InfoVisible"]) |
---|
345 | [self showInfo: nil]; |
---|
346 | |
---|
347 | //timer to check for auto import every 15 seconds, must do after everything else is set up |
---|
348 | fAutoImportTimer = [NSTimer scheduledTimerWithTimeInterval: 15.0 target: self |
---|
349 | selector: @selector(checkAutoImportDirectory:) userInfo: nil repeats: YES]; |
---|
350 | [[NSRunLoop currentRunLoop] addTimer: fAutoImportTimer forMode: NSDefaultRunLoopMode]; |
---|
351 | } |
---|
352 | |
---|
353 | - (BOOL) applicationShouldHandleReopen: (NSApplication *) app hasVisibleWindows: (BOOL) visibleWindows |
---|
354 | { |
---|
355 | if (![fWindow isVisible] && ![[fPrefsController window] isVisible]) |
---|
356 | [fWindow makeKeyAndOrderFront: nil]; |
---|
357 | return NO; |
---|
358 | } |
---|
359 | |
---|
360 | - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) sender |
---|
361 | { |
---|
362 | if (!fUpdateInProgress && [fDefaults boolForKey: @"CheckQuit"]) |
---|
363 | { |
---|
364 | int active = 0, downloading = 0; |
---|
365 | Torrent * torrent; |
---|
366 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
367 | while ((torrent = [enumerator nextObject])) |
---|
368 | if ([torrent isActive]) |
---|
369 | { |
---|
370 | active++; |
---|
371 | if (![torrent isSeeding]) |
---|
372 | downloading++; |
---|
373 | } |
---|
374 | |
---|
375 | if ([fDefaults boolForKey: @"CheckQuitDownloading"] ? downloading > 0 : active > 0) |
---|
376 | { |
---|
377 | NSString * message = active == 1 |
---|
378 | ? @"There is an active transfer. Do you really want to quit?" |
---|
379 | : [NSString stringWithFormat: |
---|
380 | @"There are %d active transfers. Do you really want to quit?", active]; |
---|
381 | |
---|
382 | NSBeginAlertSheet(@"Confirm Quit", @"Quit", @"Cancel", nil, fWindow, self, |
---|
383 | @selector(quitSheetDidEnd:returnCode:contextInfo:), nil, nil, message); |
---|
384 | return NSTerminateLater; |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | return NSTerminateNow; |
---|
389 | } |
---|
390 | |
---|
391 | - (void) quitSheetDidEnd: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo |
---|
392 | { |
---|
393 | [NSApp replyToApplicationShouldTerminate: returnCode == NSAlertDefaultReturn]; |
---|
394 | } |
---|
395 | |
---|
396 | - (void) applicationWillTerminate: (NSNotification *) notification |
---|
397 | { |
---|
398 | //stop timers |
---|
399 | [fAutoImportTimer invalidate]; |
---|
400 | [fTimer invalidate]; |
---|
401 | |
---|
402 | //save history and stop running torrents |
---|
403 | [self updateTorrentHistory]; |
---|
404 | [fTorrents makeObjectsPerformSelector: @selector(stopTransferForQuit)]; |
---|
405 | |
---|
406 | //remember window states and close all windows |
---|
407 | [fDefaults setBool: [[fInfoController window] isVisible] forKey: @"InfoVisible"]; |
---|
408 | [[NSApp windows] makeObjectsPerformSelector: @selector(close)]; |
---|
409 | [self showStatusBar: NO animate: NO]; |
---|
410 | [self showFilterBar: NO animate: NO]; |
---|
411 | |
---|
412 | //clear badge |
---|
413 | [fBadger clearBadge]; |
---|
414 | |
---|
415 | //end quickly if the app is updating |
---|
416 | if (fUpdateInProgress) |
---|
417 | return; |
---|
418 | |
---|
419 | //wait for running transfers to stop (5 second timeout) |
---|
420 | NSDate * start = [NSDate date]; |
---|
421 | BOOL timeUp = NO; |
---|
422 | |
---|
423 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
424 | Torrent * torrent; |
---|
425 | while (!timeUp && (torrent = [enumerator nextObject])) |
---|
426 | while (![torrent isPaused] && !(timeUp = [start timeIntervalSinceNow] < -5.0)) |
---|
427 | { |
---|
428 | usleep(100000); |
---|
429 | [torrent update]; |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | - (NSArray *) torrentsAtIndexes: (NSIndexSet *) indexSet |
---|
434 | { |
---|
435 | if ([fDisplayedTorrents respondsToSelector: @selector(objectsAtIndexes:)]) |
---|
436 | return [fDisplayedTorrents objectsAtIndexes: indexSet]; |
---|
437 | else |
---|
438 | { |
---|
439 | NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [indexSet count]]; |
---|
440 | unsigned int i; |
---|
441 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
442 | [torrents addObject: [fDisplayedTorrents objectAtIndex: i]]; |
---|
443 | |
---|
444 | return torrents; |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | - (void) application: (NSApplication *) sender openFiles: (NSArray *) filenames |
---|
449 | { |
---|
450 | [self openFiles: filenames ignoreDownloadFolder: NO]; |
---|
451 | } |
---|
452 | |
---|
453 | - (void) openFiles: (NSArray *) filenames ignoreDownloadFolder: (BOOL) ignore |
---|
454 | { |
---|
455 | NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"], * torrentPath; |
---|
456 | Torrent * torrent; |
---|
457 | NSEnumerator * enumerator = [filenames objectEnumerator]; |
---|
458 | while ((torrentPath = [enumerator nextObject])) |
---|
459 | { |
---|
460 | if (!(torrent = [[Torrent alloc] initWithPath: torrentPath lib: fLib])) |
---|
461 | continue; |
---|
462 | |
---|
463 | //add it to the "File > Open Recent" menu |
---|
464 | [[NSDocumentController sharedDocumentController] |
---|
465 | noteNewRecentDocumentURL: [NSURL fileURLWithPath: torrentPath]]; |
---|
466 | |
---|
467 | if (ignore || [downloadChoice isEqualToString: @"Ask"]) |
---|
468 | { |
---|
469 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
470 | |
---|
471 | [panel setPrompt: @"Select"]; |
---|
472 | [panel setAllowsMultipleSelection: NO]; |
---|
473 | [panel setCanChooseFiles: NO]; |
---|
474 | [panel setCanChooseDirectories: YES]; |
---|
475 | |
---|
476 | [panel setMessage: [NSString stringWithFormat: @"Select the download folder for \"%@\"", [torrent name]]]; |
---|
477 | |
---|
478 | [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: fWindow modalDelegate: self |
---|
479 | didEndSelector: @selector(folderChoiceClosed:returnCode:contextInfo:) contextInfo: torrent]; |
---|
480 | } |
---|
481 | else |
---|
482 | { |
---|
483 | NSString * folder = [downloadChoice isEqualToString: @"Constant"] |
---|
484 | ? [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath] |
---|
485 | : [torrentPath stringByDeletingLastPathComponent]; |
---|
486 | |
---|
487 | [torrent setDownloadFolder: folder]; |
---|
488 | [torrent update]; |
---|
489 | [self attemptToStartAuto: torrent]; |
---|
490 | |
---|
491 | [fTorrents addObject: torrent]; |
---|
492 | } |
---|
493 | |
---|
494 | [torrent release]; |
---|
495 | } |
---|
496 | |
---|
497 | [self updateUI: nil]; |
---|
498 | [self applyFilter: nil]; |
---|
499 | |
---|
500 | [self updateTorrentHistory]; |
---|
501 | } |
---|
502 | |
---|
503 | - (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code |
---|
504 | contextInfo: (Torrent *) torrent |
---|
505 | { |
---|
506 | if (code == NSOKButton) |
---|
507 | { |
---|
508 | [torrent setDownloadFolder: [[openPanel filenames] objectAtIndex: 0]]; |
---|
509 | [torrent update]; |
---|
510 | [self attemptToStartAuto: torrent]; |
---|
511 | |
---|
512 | [fTorrents addObject: torrent]; |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | //called on by applescript |
---|
517 | - (void) open: (NSArray *) files |
---|
518 | { |
---|
519 | [self performSelectorOnMainThread: @selector(openFiles:) withObject: files waitUntilDone: NO]; |
---|
520 | } |
---|
521 | |
---|
522 | - (void) openFiles: (NSArray *) filenames |
---|
523 | { |
---|
524 | [self openFiles: filenames ignoreDownloadFolder: NO]; |
---|
525 | } |
---|
526 | |
---|
527 | - (void) openShowSheet: (id) sender |
---|
528 | { |
---|
529 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
530 | |
---|
531 | [panel setAllowsMultipleSelection: YES]; |
---|
532 | [panel setCanChooseFiles: YES]; |
---|
533 | [panel setCanChooseDirectories: NO]; |
---|
534 | |
---|
535 | [panel beginSheetForDirectory: nil file: nil types: [NSArray arrayWithObject: @"torrent"] |
---|
536 | modalForWindow: fWindow modalDelegate: self didEndSelector: @selector(openSheetClosed:returnCode:contextInfo:) |
---|
537 | contextInfo: [NSNumber numberWithBool: sender == fOpenIgnoreDownloadFolder]]; |
---|
538 | } |
---|
539 | |
---|
540 | - (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (int) code contextInfo: (NSNumber *) ignore |
---|
541 | { |
---|
542 | if (code == NSOKButton) |
---|
543 | { |
---|
544 | NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
545 | [panel filenames], @"Files", ignore, @"Ignore", nil]; |
---|
546 | [self performSelectorOnMainThread: @selector(openFromSheet:) withObject: dictionary waitUntilDone: NO]; |
---|
547 | } |
---|
548 | } |
---|
549 | |
---|
550 | - (void) openFromSheet: (NSDictionary *) dictionary |
---|
551 | { |
---|
552 | [self openFiles: [dictionary objectForKey: @"Files"] |
---|
553 | ignoreDownloadFolder: [[dictionary objectForKey: @"Ignore"] boolValue]]; |
---|
554 | |
---|
555 | [dictionary release]; |
---|
556 | } |
---|
557 | |
---|
558 | - (void) resumeSelectedTorrents: (id) sender |
---|
559 | { |
---|
560 | [self resumeTorrents: [self torrentsAtIndexes: [fTableView selectedRowIndexes]]]; |
---|
561 | } |
---|
562 | |
---|
563 | - (void) resumeAllTorrents: (id) sender |
---|
564 | { |
---|
565 | [self resumeTorrents: fTorrents]; |
---|
566 | } |
---|
567 | |
---|
568 | - (void) resumeWaitingTorrents: (id) sender |
---|
569 | { |
---|
570 | NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [fTorrents count]]; |
---|
571 | |
---|
572 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
573 | Torrent * torrent; |
---|
574 | while ((torrent = [enumerator nextObject])) |
---|
575 | if ([torrent waitingToStart]) |
---|
576 | [torrents addObject: torrent]; |
---|
577 | |
---|
578 | [self resumeTorrents: torrents]; |
---|
579 | } |
---|
580 | |
---|
581 | - (void) resumeTorrents: (NSArray *) torrents |
---|
582 | { |
---|
583 | [torrents makeObjectsPerformSelector: @selector(startTransfer)]; |
---|
584 | |
---|
585 | [self updateUI: nil]; |
---|
586 | [self applyFilter: nil]; |
---|
587 | [fInfoController updateInfoStatsAndSettings]; |
---|
588 | [self updateTorrentHistory]; |
---|
589 | } |
---|
590 | |
---|
591 | - (void) stopSelectedTorrents: (id) sender |
---|
592 | { |
---|
593 | [self stopTorrents: [self torrentsAtIndexes: [fTableView selectedRowIndexes]]]; |
---|
594 | } |
---|
595 | |
---|
596 | - (void) stopAllTorrents: (id) sender |
---|
597 | { |
---|
598 | [self stopTorrents: fTorrents]; |
---|
599 | } |
---|
600 | |
---|
601 | - (void) stopTorrents: (NSArray *) torrents |
---|
602 | { |
---|
603 | //don't want any of these starting then stopping |
---|
604 | NSEnumerator * enumerator = [torrents objectEnumerator]; |
---|
605 | Torrent * torrent; |
---|
606 | while ((torrent = [enumerator nextObject])) |
---|
607 | [torrent setWaitToStart: NO]; |
---|
608 | |
---|
609 | [torrents makeObjectsPerformSelector: @selector(stopTransfer)]; |
---|
610 | |
---|
611 | [self updateUI: nil]; |
---|
612 | [self applyFilter: nil]; |
---|
613 | [fInfoController updateInfoStatsAndSettings]; |
---|
614 | [self updateTorrentHistory]; |
---|
615 | } |
---|
616 | |
---|
617 | - (void) removeTorrents: (NSArray *) torrents |
---|
618 | deleteData: (BOOL) deleteData deleteTorrent: (BOOL) deleteTorrent |
---|
619 | { |
---|
620 | [torrents retain]; |
---|
621 | int active = 0, downloading = 0; |
---|
622 | |
---|
623 | if ([fDefaults boolForKey: @"CheckRemove"]) |
---|
624 | { |
---|
625 | Torrent * torrent; |
---|
626 | NSEnumerator * enumerator = [torrents objectEnumerator]; |
---|
627 | while ((torrent = [enumerator nextObject])) |
---|
628 | if ([torrent isActive]) |
---|
629 | { |
---|
630 | active++; |
---|
631 | if (![torrent isSeeding]) |
---|
632 | downloading++; |
---|
633 | } |
---|
634 | |
---|
635 | if ([fDefaults boolForKey: @"CheckRemoveDownloading"] ? downloading > 0 : active > 0) |
---|
636 | { |
---|
637 | NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
638 | torrents, @"Torrents", |
---|
639 | [NSNumber numberWithBool: deleteData], @"DeleteData", |
---|
640 | [NSNumber numberWithBool: deleteTorrent], @"DeleteTorrent", nil]; |
---|
641 | |
---|
642 | NSString * title, * message; |
---|
643 | |
---|
644 | int selected = [fTableView numberOfSelectedRows]; |
---|
645 | if (selected == 1) |
---|
646 | { |
---|
647 | title = [NSString stringWithFormat: @"Comfirm Removal of \"%@\"", |
---|
648 | [[fDisplayedTorrents objectAtIndex: [fTableView selectedRow]] name]]; |
---|
649 | message = @"This transfer is active." |
---|
650 | " Once removed, continuing the transfer will require the torrent file." |
---|
651 | " Do you really want to remove it?"; |
---|
652 | } |
---|
653 | else |
---|
654 | { |
---|
655 | title = [NSString stringWithFormat: @"Comfirm Removal of %d Transfers", selected]; |
---|
656 | if (selected == active) |
---|
657 | message = [NSString stringWithFormat: @"There are %d active transfers.", active]; |
---|
658 | else |
---|
659 | message = [NSString stringWithFormat: @"There are %d transfers (%d active).", selected, active]; |
---|
660 | message = [message stringByAppendingString: |
---|
661 | @" Once removed, continuing the transfers will require the torrent files." |
---|
662 | " Do you really want to remove them?"]; |
---|
663 | } |
---|
664 | |
---|
665 | NSBeginAlertSheet(title, @"Remove", @"Cancel", nil, fWindow, self, |
---|
666 | nil, @selector(removeSheetDidEnd:returnCode:contextInfo:), dict, message); |
---|
667 | return; |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | [self confirmRemoveTorrents: torrents deleteData: deleteData deleteTorrent: deleteTorrent]; |
---|
672 | } |
---|
673 | |
---|
674 | - (void) removeSheetDidEnd: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (NSDictionary *) dict |
---|
675 | { |
---|
676 | NSArray * torrents = [dict objectForKey: @"Torrents"]; |
---|
677 | BOOL deleteData = [[dict objectForKey: @"DeleteData"] boolValue], |
---|
678 | deleteTorrent = [[dict objectForKey: @"DeleteTorrent"] boolValue]; |
---|
679 | [dict release]; |
---|
680 | |
---|
681 | if (returnCode == NSAlertDefaultReturn) |
---|
682 | [self confirmRemoveTorrents: torrents deleteData: deleteData deleteTorrent: deleteTorrent]; |
---|
683 | else |
---|
684 | [torrents release]; |
---|
685 | } |
---|
686 | |
---|
687 | - (void) confirmRemoveTorrents: (NSArray *) torrents deleteData: (BOOL) deleteData deleteTorrent: (BOOL) deleteTorrent |
---|
688 | { |
---|
689 | //don't want any of these starting then stopping |
---|
690 | NSEnumerator * enumerator = [torrents objectEnumerator]; |
---|
691 | Torrent * torrent; |
---|
692 | while ((torrent = [enumerator nextObject])) |
---|
693 | [torrent setWaitToStart: NO]; |
---|
694 | |
---|
695 | NSNumber * lowestOrderValue = [NSNumber numberWithInt: [torrents count]], * currentOrderValue; |
---|
696 | |
---|
697 | enumerator = [torrents objectEnumerator]; |
---|
698 | while ((torrent = [enumerator nextObject])) |
---|
699 | { |
---|
700 | [torrent stopTransfer]; |
---|
701 | |
---|
702 | if (deleteData) |
---|
703 | [torrent trashData]; |
---|
704 | if (deleteTorrent) |
---|
705 | [torrent trashTorrent]; |
---|
706 | |
---|
707 | //determine lowest order value |
---|
708 | currentOrderValue = [torrent orderValue]; |
---|
709 | if ([lowestOrderValue compare: currentOrderValue] == NSOrderedDescending) |
---|
710 | lowestOrderValue = currentOrderValue; |
---|
711 | |
---|
712 | [torrent removeForever]; |
---|
713 | |
---|
714 | [fTorrents removeObject: torrent]; |
---|
715 | [fDisplayedTorrents removeObject: torrent]; |
---|
716 | } |
---|
717 | [torrents release]; |
---|
718 | |
---|
719 | //reset the order values if necessary |
---|
720 | if ([lowestOrderValue intValue] < [fTorrents count]) |
---|
721 | { |
---|
722 | NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
723 | @"orderValue" ascending: YES] autorelease]; |
---|
724 | NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil]; |
---|
725 | |
---|
726 | NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors]; |
---|
727 | [descriptors release]; |
---|
728 | |
---|
729 | int i; |
---|
730 | for (i = [lowestOrderValue intValue]; i < [tempTorrents count]; i++) |
---|
731 | [[tempTorrents objectAtIndex: i] setOrderValue: i]; |
---|
732 | } |
---|
733 | |
---|
734 | [fTableView deselectAll: nil]; |
---|
735 | |
---|
736 | [self updateUI: nil]; |
---|
737 | [self applyFilter: nil]; |
---|
738 | |
---|
739 | [self updateTorrentHistory]; |
---|
740 | } |
---|
741 | |
---|
742 | - (void) removeNoDelete: (id) sender |
---|
743 | { |
---|
744 | [self removeTorrents: [self torrentsAtIndexes: [fTableView selectedRowIndexes]] |
---|
745 | deleteData: NO deleteTorrent: NO]; |
---|
746 | } |
---|
747 | |
---|
748 | - (void) removeDeleteData: (id) sender |
---|
749 | { |
---|
750 | [self removeTorrents: [self torrentsAtIndexes: [fTableView selectedRowIndexes]] |
---|
751 | deleteData: YES deleteTorrent: NO]; |
---|
752 | } |
---|
753 | |
---|
754 | - (void) removeDeleteTorrent: (id) sender |
---|
755 | { |
---|
756 | [self removeTorrents: [self torrentsAtIndexes: [fTableView selectedRowIndexes]] |
---|
757 | deleteData: NO deleteTorrent: YES]; |
---|
758 | } |
---|
759 | |
---|
760 | - (void) removeDeleteDataAndTorrent: (id) sender |
---|
761 | { |
---|
762 | [self removeTorrents: [self torrentsAtIndexes: [fTableView selectedRowIndexes]] |
---|
763 | deleteData: YES deleteTorrent: YES]; |
---|
764 | } |
---|
765 | |
---|
766 | - (void) copyTorrentFile: (id) sender |
---|
767 | { |
---|
768 | [self copyTorrentFileForTorrents: [[NSMutableArray alloc] initWithArray: |
---|
769 | [self torrentsAtIndexes: [fTableView selectedRowIndexes]]]]; |
---|
770 | } |
---|
771 | |
---|
772 | - (void) copyTorrentFileForTorrents: (NSMutableArray *) torrents |
---|
773 | { |
---|
774 | if ([torrents count] == 0) |
---|
775 | { |
---|
776 | [torrents release]; |
---|
777 | return; |
---|
778 | } |
---|
779 | |
---|
780 | Torrent * torrent = [torrents objectAtIndex: 0]; |
---|
781 | |
---|
782 | //warn user if torrent file can't be found |
---|
783 | if (![[NSFileManager defaultManager] fileExistsAtPath: [torrent torrentLocation]]) |
---|
784 | { |
---|
785 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
786 | [alert addButtonWithTitle: @"OK"]; |
---|
787 | [alert setMessageText: [NSString stringWithFormat: |
---|
788 | @"Copy of \"%@\" Cannot Be Created", [torrent name]]]; |
---|
789 | [alert setInformativeText: [NSString stringWithFormat: |
---|
790 | @"The torrent file (%@) cannot be found.", [torrent torrentLocation]]]; |
---|
791 | [alert setAlertStyle: NSWarningAlertStyle]; |
---|
792 | |
---|
793 | [alert runModal]; |
---|
794 | |
---|
795 | [torrents removeObjectAtIndex: 0]; |
---|
796 | [self copyTorrentFileForTorrents: torrents]; |
---|
797 | } |
---|
798 | else |
---|
799 | { |
---|
800 | NSSavePanel * panel = [NSSavePanel savePanel]; |
---|
801 | [panel setRequiredFileType: @"torrent"]; |
---|
802 | [panel setCanSelectHiddenExtension: YES]; |
---|
803 | |
---|
804 | [panel beginSheetForDirectory: nil file: [torrent name] |
---|
805 | modalForWindow: fWindow modalDelegate: self didEndSelector: |
---|
806 | @selector(saveTorrentCopySheetClosed:returnCode:contextInfo:) contextInfo: torrents]; |
---|
807 | } |
---|
808 | } |
---|
809 | |
---|
810 | - (void) saveTorrentCopySheetClosed: (NSSavePanel *) panel returnCode: (int) code |
---|
811 | contextInfo: (NSMutableArray *) torrents |
---|
812 | { |
---|
813 | //if save successful, copy torrent to new location with name of data file |
---|
814 | if (code == NSOKButton) |
---|
815 | [[NSFileManager defaultManager] copyPath: [[torrents objectAtIndex: 0] torrentLocation] |
---|
816 | toPath: [panel filename] handler: nil]; |
---|
817 | |
---|
818 | [torrents removeObjectAtIndex: 0]; |
---|
819 | [self performSelectorOnMainThread: @selector(copyTorrentFileForTorrents:) |
---|
820 | withObject: torrents waitUntilDone: NO]; |
---|
821 | } |
---|
822 | |
---|
823 | - (void) revealFile: (id) sender |
---|
824 | { |
---|
825 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
826 | unsigned int i; |
---|
827 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
828 | [[fDisplayedTorrents objectAtIndex: i] revealData]; |
---|
829 | } |
---|
830 | |
---|
831 | - (void) showPreferenceWindow: (id) sender |
---|
832 | { |
---|
833 | NSWindow * window = [fPrefsController window]; |
---|
834 | if (![window isVisible]) |
---|
835 | [window center]; |
---|
836 | |
---|
837 | [window makeKeyAndOrderFront: nil]; |
---|
838 | } |
---|
839 | |
---|
840 | - (void) showInfo: (id) sender |
---|
841 | { |
---|
842 | if ([[fInfoController window] isVisible]) |
---|
843 | [fInfoController close]; |
---|
844 | else |
---|
845 | { |
---|
846 | [fInfoController updateInfoStats]; |
---|
847 | [[fInfoController window] orderFront: nil]; |
---|
848 | } |
---|
849 | } |
---|
850 | |
---|
851 | - (void) setInfoTab: (id) sender |
---|
852 | { |
---|
853 | if (sender == fNextInfoTabItem) |
---|
854 | [fInfoController setNextTab]; |
---|
855 | else |
---|
856 | [fInfoController setPreviousTab]; |
---|
857 | } |
---|
858 | |
---|
859 | - (void) updateControlTint: (NSNotification *) notification |
---|
860 | { |
---|
861 | if (fSpeedLimitEnabled) |
---|
862 | [fSpeedLimitButton setImage: [NSColor currentControlTint] == NSBlueControlTint |
---|
863 | ? fSpeedLimitBlueImage : fSpeedLimitGraphiteImage]; |
---|
864 | } |
---|
865 | |
---|
866 | - (void) updateUI: (NSTimer *) t |
---|
867 | { |
---|
868 | [fTorrents makeObjectsPerformSelector: @selector(update)]; |
---|
869 | |
---|
870 | //resort if necessary or just update the table |
---|
871 | if ([fSortType isEqualToString: @"Progress"] || [fSortType isEqualToString: @"State"]) |
---|
872 | [self sortTorrents]; |
---|
873 | else |
---|
874 | [fTableView reloadData]; |
---|
875 | |
---|
876 | //update the global DL/UL rates |
---|
877 | float downloadRate, uploadRate; |
---|
878 | tr_torrentRates(fLib, & downloadRate, & uploadRate); |
---|
879 | if (![fStatusBar isHidden]) |
---|
880 | { |
---|
881 | [fTotalDLField setStringValue: [@"Total DL: " stringByAppendingString: [NSString stringForSpeed: downloadRate]]]; |
---|
882 | [fTotalULField setStringValue: [@"Total UL: " stringByAppendingString: [NSString stringForSpeed: uploadRate]]]; |
---|
883 | } |
---|
884 | |
---|
885 | //update non-constant parts of info window |
---|
886 | if ([[fInfoController window] isVisible]) |
---|
887 | [fInfoController updateInfoStats]; |
---|
888 | |
---|
889 | //badge dock |
---|
890 | [fBadger updateBadgeWithCompleted: fCompleted uploadRate: uploadRate downloadRate: downloadRate]; |
---|
891 | } |
---|
892 | |
---|
893 | - (void) torrentFinishedDownloading: (NSNotification *) notification |
---|
894 | { |
---|
895 | Torrent * torrent = [notification object]; |
---|
896 | |
---|
897 | [self applyFilter: nil]; |
---|
898 | [self checkToStartWaiting: torrent]; |
---|
899 | |
---|
900 | if ([fDefaults boolForKey: @"PlayDownloadSound"]) |
---|
901 | { |
---|
902 | NSSound * sound; |
---|
903 | if ((sound = [NSSound soundNamed: @"Glass"])) |
---|
904 | [sound play]; |
---|
905 | } |
---|
906 | |
---|
907 | [GrowlApplicationBridge notifyWithTitle: @"Download Complete" description: [torrent name] |
---|
908 | notificationName: GROWL_DOWNLOAD_COMPLETE iconData: nil priority: 0 isSticky: NO clickContext: nil]; |
---|
909 | |
---|
910 | if (![fWindow isKeyWindow]) |
---|
911 | fCompleted++; |
---|
912 | } |
---|
913 | |
---|
914 | - (void) updateTorrentHistory |
---|
915 | { |
---|
916 | NSMutableArray * history = [NSMutableArray arrayWithCapacity: [fTorrents count]]; |
---|
917 | |
---|
918 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
919 | Torrent * torrent; |
---|
920 | while ((torrent = [enumerator nextObject])) |
---|
921 | [history addObject: [torrent history]]; |
---|
922 | |
---|
923 | [fDefaults setObject: history forKey: @"History"]; |
---|
924 | [fDefaults synchronize]; |
---|
925 | } |
---|
926 | |
---|
927 | - (void) sortTorrents |
---|
928 | { |
---|
929 | //remember selected rows if needed |
---|
930 | NSArray * selectedTorrents = nil; |
---|
931 | int numSelected = [fTableView numberOfSelectedRows]; |
---|
932 | if (numSelected > 0 && numSelected < [fDisplayedTorrents count]) |
---|
933 | selectedTorrents = [self torrentsAtIndexes: [fTableView selectedRowIndexes]]; |
---|
934 | |
---|
935 | [self sortTorrentsIgnoreSelected]; //actually sort |
---|
936 | |
---|
937 | //set selected rows if needed |
---|
938 | if (selectedTorrents) |
---|
939 | { |
---|
940 | Torrent * torrent; |
---|
941 | NSEnumerator * enumerator = [selectedTorrents objectEnumerator]; |
---|
942 | NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init]; |
---|
943 | while ((torrent = [enumerator nextObject])) |
---|
944 | [indexSet addIndex: [fDisplayedTorrents indexOfObject: torrent]]; |
---|
945 | |
---|
946 | [fTableView selectRowIndexes: indexSet byExtendingSelection: NO]; |
---|
947 | [indexSet release]; |
---|
948 | } |
---|
949 | } |
---|
950 | |
---|
951 | //doesn't remember selected rows |
---|
952 | - (void) sortTorrentsIgnoreSelected |
---|
953 | { |
---|
954 | NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" |
---|
955 | ascending: YES selector: @selector(caseInsensitiveCompare:)] autorelease], |
---|
956 | * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"orderValue" |
---|
957 | ascending: YES] autorelease]; |
---|
958 | |
---|
959 | NSArray * descriptors; |
---|
960 | if ([fSortType isEqualToString: @"Name"]) |
---|
961 | descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, orderDescriptor, nil]; |
---|
962 | else if ([fSortType isEqualToString: @"State"]) |
---|
963 | { |
---|
964 | NSSortDescriptor * stateDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
965 | @"stateSortKey" ascending: NO] autorelease], |
---|
966 | * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
967 | @"progressSortKey" ascending: NO] autorelease]; |
---|
968 | |
---|
969 | descriptors = [[NSArray alloc] initWithObjects: stateDescriptor, progressDescriptor, |
---|
970 | nameDescriptor, orderDescriptor, nil]; |
---|
971 | } |
---|
972 | else if ([fSortType isEqualToString: @"Progress"]) |
---|
973 | { |
---|
974 | NSSortDescriptor * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
975 | @"progressSortKey" ascending: YES] autorelease]; |
---|
976 | |
---|
977 | descriptors = [[NSArray alloc] initWithObjects: progressDescriptor, nameDescriptor, orderDescriptor, nil]; |
---|
978 | } |
---|
979 | else if ([fSortType isEqualToString: @"Date"]) |
---|
980 | { |
---|
981 | NSSortDescriptor * dateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"date" ascending: YES] autorelease]; |
---|
982 | |
---|
983 | descriptors = [[NSArray alloc] initWithObjects: dateDescriptor, orderDescriptor, nil]; |
---|
984 | } |
---|
985 | else |
---|
986 | descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil]; |
---|
987 | |
---|
988 | [fDisplayedTorrents sortUsingDescriptors: descriptors]; |
---|
989 | [descriptors release]; |
---|
990 | |
---|
991 | [fTableView reloadData]; |
---|
992 | } |
---|
993 | |
---|
994 | - (void) setSort: (id) sender |
---|
995 | { |
---|
996 | //get checked items |
---|
997 | NSMenuItem * prevSortItem, * prevSortActionItem; |
---|
998 | if ([fSortType isEqualToString: @"Name"]) |
---|
999 | { |
---|
1000 | prevSortItem = fNameSortItem; |
---|
1001 | prevSortActionItem = fNameSortActionItem; |
---|
1002 | } |
---|
1003 | else if ([fSortType isEqualToString: @"State"]) |
---|
1004 | { |
---|
1005 | prevSortItem = fStateSortItem; |
---|
1006 | prevSortActionItem = fStateSortActionItem; |
---|
1007 | } |
---|
1008 | else if ([fSortType isEqualToString: @"Progress"]) |
---|
1009 | { |
---|
1010 | prevSortItem = fProgressSortItem; |
---|
1011 | prevSortActionItem = fProgressSortActionItem; |
---|
1012 | } |
---|
1013 | else if ([fSortType isEqualToString: @"Date"]) |
---|
1014 | { |
---|
1015 | prevSortItem = fDateSortItem; |
---|
1016 | prevSortActionItem = fDateSortActionItem; |
---|
1017 | } |
---|
1018 | else |
---|
1019 | { |
---|
1020 | prevSortItem = fOrderSortItem; |
---|
1021 | prevSortActionItem = fOrderSortActionItem; |
---|
1022 | } |
---|
1023 | |
---|
1024 | if (sender != prevSortItem && sender != prevSortActionItem) |
---|
1025 | { |
---|
1026 | [fSortType release]; |
---|
1027 | |
---|
1028 | //get new items to check |
---|
1029 | NSMenuItem * currentSortItem, * currentSortActionItem; |
---|
1030 | if (sender == fNameSortItem || sender == fNameSortActionItem) |
---|
1031 | { |
---|
1032 | currentSortItem = fNameSortItem; |
---|
1033 | currentSortActionItem = fNameSortActionItem; |
---|
1034 | fSortType = [[NSString alloc] initWithString: @"Name"]; |
---|
1035 | } |
---|
1036 | else if (sender == fStateSortItem || sender == fStateSortActionItem) |
---|
1037 | { |
---|
1038 | currentSortItem = fStateSortItem; |
---|
1039 | currentSortActionItem = fStateSortActionItem; |
---|
1040 | fSortType = [[NSString alloc] initWithString: @"State"]; |
---|
1041 | } |
---|
1042 | else if (sender == fProgressSortItem || sender == fProgressSortActionItem) |
---|
1043 | { |
---|
1044 | currentSortItem = fProgressSortItem; |
---|
1045 | currentSortActionItem = fProgressSortActionItem; |
---|
1046 | fSortType = [[NSString alloc] initWithString: @"Progress"]; |
---|
1047 | } |
---|
1048 | else if (sender == fDateSortItem || sender == fDateSortActionItem) |
---|
1049 | { |
---|
1050 | currentSortItem = fDateSortItem; |
---|
1051 | currentSortActionItem = fDateSortActionItem; |
---|
1052 | fSortType = [[NSString alloc] initWithString: @"Date"]; |
---|
1053 | } |
---|
1054 | else |
---|
1055 | { |
---|
1056 | currentSortItem = fOrderSortItem; |
---|
1057 | currentSortActionItem = fOrderSortActionItem; |
---|
1058 | fSortType = [[NSString alloc] initWithString: @"Order"]; |
---|
1059 | } |
---|
1060 | |
---|
1061 | [prevSortItem setState: NSOffState]; |
---|
1062 | [prevSortActionItem setState: NSOffState]; |
---|
1063 | [currentSortItem setState: NSOnState]; |
---|
1064 | [currentSortActionItem setState: NSOnState]; |
---|
1065 | |
---|
1066 | [fDefaults setObject: fSortType forKey: @"Sort"]; |
---|
1067 | } |
---|
1068 | |
---|
1069 | [self sortTorrents]; |
---|
1070 | } |
---|
1071 | |
---|
1072 | - (void) applyFilter: (id) sender |
---|
1073 | { |
---|
1074 | //remember selected rows if needed |
---|
1075 | NSArray * selectedTorrents = [fTableView numberOfSelectedRows] > 0 |
---|
1076 | ? [self torrentsAtIndexes: [fTableView selectedRowIndexes]] : nil; |
---|
1077 | |
---|
1078 | NSMutableArray * tempTorrents = [[NSMutableArray alloc] initWithCapacity: [fTorrents count]]; |
---|
1079 | |
---|
1080 | BOOL filtering = YES; |
---|
1081 | if ([fFilterType isEqualToString: @"Pause"]) |
---|
1082 | { |
---|
1083 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1084 | Torrent * torrent; |
---|
1085 | while ((torrent = [enumerator nextObject])) |
---|
1086 | if (![torrent isActive]) |
---|
1087 | [tempTorrents addObject: torrent]; |
---|
1088 | } |
---|
1089 | else if ([fFilterType isEqualToString: @"Seed"]) |
---|
1090 | { |
---|
1091 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1092 | Torrent * torrent; |
---|
1093 | while ((torrent = [enumerator nextObject])) |
---|
1094 | if ([torrent isActive] && [torrent progress] >= 1.0) |
---|
1095 | [tempTorrents addObject: torrent]; |
---|
1096 | } |
---|
1097 | else if ([fFilterType isEqualToString: @"Download"]) |
---|
1098 | { |
---|
1099 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1100 | Torrent * torrent; |
---|
1101 | while ((torrent = [enumerator nextObject])) |
---|
1102 | if ([torrent isActive] && [torrent progress] < 1.0) |
---|
1103 | [tempTorrents addObject: torrent]; |
---|
1104 | } |
---|
1105 | else |
---|
1106 | { |
---|
1107 | filtering = NO; |
---|
1108 | [tempTorrents setArray: fTorrents]; |
---|
1109 | } |
---|
1110 | |
---|
1111 | NSString * searchString = [fSearchFilterField stringValue]; |
---|
1112 | if ([searchString length] > 0) |
---|
1113 | { |
---|
1114 | filtering = YES; |
---|
1115 | |
---|
1116 | int i; |
---|
1117 | for (i = [tempTorrents count] - 1; i >= 0; i--) |
---|
1118 | if ([[[tempTorrents objectAtIndex: i] name] rangeOfString: searchString |
---|
1119 | options: NSCaseInsensitiveSearch].location == NSNotFound) |
---|
1120 | [tempTorrents removeObjectAtIndex: i]; |
---|
1121 | } |
---|
1122 | |
---|
1123 | [fDisplayedTorrents setArray: tempTorrents]; |
---|
1124 | [tempTorrents release]; |
---|
1125 | |
---|
1126 | [self sortTorrentsIgnoreSelected]; |
---|
1127 | |
---|
1128 | //set selected rows if needed |
---|
1129 | if (selectedTorrents) |
---|
1130 | { |
---|
1131 | Torrent * torrent; |
---|
1132 | NSEnumerator * enumerator = [selectedTorrents objectEnumerator]; |
---|
1133 | NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init]; |
---|
1134 | unsigned index; |
---|
1135 | while ((torrent = [enumerator nextObject])) |
---|
1136 | if ((index = [fDisplayedTorrents indexOfObject: torrent]) != NSNotFound) |
---|
1137 | [indexSet addIndex: index]; |
---|
1138 | |
---|
1139 | [fTableView selectRowIndexes: indexSet byExtendingSelection: NO]; |
---|
1140 | [indexSet release]; |
---|
1141 | } |
---|
1142 | |
---|
1143 | //set status bar torrent count text |
---|
1144 | NSMutableString * totalTorrentsString = [NSMutableString stringWithString: @""]; |
---|
1145 | if (filtering) |
---|
1146 | [totalTorrentsString appendFormat: @"%d/", [fDisplayedTorrents count]]; |
---|
1147 | |
---|
1148 | int totalCount = [fTorrents count]; |
---|
1149 | [totalTorrentsString appendFormat: @"%d Transfer%s", totalCount, totalCount == 1 ? "" : "s"]; |
---|
1150 | |
---|
1151 | [fTotalTorrentsField setStringValue: totalTorrentsString]; |
---|
1152 | |
---|
1153 | [self setWindowSizeToFit]; |
---|
1154 | } |
---|
1155 | |
---|
1156 | //resets filter and sorts torrents |
---|
1157 | - (void) setFilter: (id) sender |
---|
1158 | { |
---|
1159 | BarButton * prevFilterButton; |
---|
1160 | if ([fFilterType isEqualToString: @"Pause"]) |
---|
1161 | prevFilterButton = fPauseFilterButton; |
---|
1162 | else if ([fFilterType isEqualToString: @"Seed"]) |
---|
1163 | prevFilterButton = fSeedFilterButton; |
---|
1164 | else if ([fFilterType isEqualToString: @"Download"]) |
---|
1165 | prevFilterButton = fDownloadFilterButton; |
---|
1166 | else |
---|
1167 | prevFilterButton = fNoFilterButton; |
---|
1168 | |
---|
1169 | if (sender != prevFilterButton) |
---|
1170 | { |
---|
1171 | [prevFilterButton setEnabled: NO]; |
---|
1172 | [sender setEnabled: YES]; |
---|
1173 | |
---|
1174 | [fFilterType release]; |
---|
1175 | if (sender == fDownloadFilterButton) |
---|
1176 | fFilterType = [[NSString alloc] initWithString: @"Download"]; |
---|
1177 | else if (sender == fPauseFilterButton) |
---|
1178 | fFilterType = [[NSString alloc] initWithString: @"Pause"]; |
---|
1179 | else if (sender == fSeedFilterButton) |
---|
1180 | fFilterType = [[NSString alloc] initWithString: @"Seed"]; |
---|
1181 | else |
---|
1182 | fFilterType = [[NSString alloc] initWithString: @"None"]; |
---|
1183 | |
---|
1184 | [fDefaults setObject: fFilterType forKey: @"Filter"]; |
---|
1185 | } |
---|
1186 | |
---|
1187 | [self applyFilter: nil]; |
---|
1188 | } |
---|
1189 | |
---|
1190 | - (void) toggleSpeedLimit: (id) sender |
---|
1191 | { |
---|
1192 | fSpeedLimitEnabled = !fSpeedLimitEnabled; |
---|
1193 | int state = fSpeedLimitEnabled ? NSOnState : NSOffState; |
---|
1194 | |
---|
1195 | [fSpeedLimitItem setState: state]; |
---|
1196 | [fSpeedLimitDockItem setState: state]; |
---|
1197 | |
---|
1198 | [fSpeedLimitButton setImage: !fSpeedLimitEnabled ? fSpeedLimitNormalImage |
---|
1199 | : ([NSColor currentControlTint] == NSBlueControlTint ? fSpeedLimitBlueImage : fSpeedLimitGraphiteImage)]; |
---|
1200 | |
---|
1201 | [fPrefsController enableSpeedLimit: fSpeedLimitEnabled]; |
---|
1202 | } |
---|
1203 | |
---|
1204 | - (void) setLimitGlobalEnabled: (id) sender |
---|
1205 | { |
---|
1206 | [fPrefsController setLimitEnabled: (sender == fUploadLimitItem || sender == fDownloadLimitItem) |
---|
1207 | type: (sender == fUploadLimitItem || sender == fUploadNoLimitItem) ? @"Upload" : @"Download"]; |
---|
1208 | } |
---|
1209 | |
---|
1210 | - (void) setQuickLimitGlobal: (id) sender |
---|
1211 | { |
---|
1212 | [fPrefsController setQuickLimit: [[sender title] intValue] |
---|
1213 | type: [sender menu] == fUploadMenu ? @"Upload" : @"Download"]; |
---|
1214 | } |
---|
1215 | |
---|
1216 | - (void) limitGlobalChange: (NSNotification *) notification |
---|
1217 | { |
---|
1218 | NSDictionary * dict = [notification object]; |
---|
1219 | |
---|
1220 | NSMenuItem * limitItem, * noLimitItem; |
---|
1221 | if ([[dict objectForKey: @"Type"] isEqualToString: @"Upload"]) |
---|
1222 | { |
---|
1223 | limitItem = fUploadLimitItem; |
---|
1224 | noLimitItem = fUploadNoLimitItem; |
---|
1225 | } |
---|
1226 | else |
---|
1227 | { |
---|
1228 | limitItem = fDownloadLimitItem; |
---|
1229 | noLimitItem = fDownloadNoLimitItem; |
---|
1230 | } |
---|
1231 | |
---|
1232 | BOOL enable = [[dict objectForKey: @"Enable"] boolValue]; |
---|
1233 | [limitItem setState: enable ? NSOnState : NSOffState]; |
---|
1234 | [noLimitItem setState: !enable ? NSOnState : NSOffState]; |
---|
1235 | |
---|
1236 | [limitItem setTitle: [NSString stringWithFormat: @"Limit (%d KB/s)", |
---|
1237 | [[dict objectForKey: @"Limit"] intValue]]]; |
---|
1238 | |
---|
1239 | [dict release]; |
---|
1240 | } |
---|
1241 | |
---|
1242 | - (void) setRatioGlobalEnabled: (id) sender |
---|
1243 | { |
---|
1244 | [fPrefsController setRatioEnabled: sender == fRatioSetItem]; |
---|
1245 | } |
---|
1246 | |
---|
1247 | - (void) setQuickRatioGlobal: (id) sender |
---|
1248 | { |
---|
1249 | [fPrefsController setQuickRatio: [[sender title] floatValue]]; |
---|
1250 | } |
---|
1251 | |
---|
1252 | - (void) ratioGlobalChange: (NSNotification *) notification |
---|
1253 | { |
---|
1254 | NSDictionary * dict = [notification object]; |
---|
1255 | |
---|
1256 | BOOL enable = [[dict objectForKey: @"Enable"] boolValue]; |
---|
1257 | [fRatioSetItem setState: enable ? NSOnState : NSOffState]; |
---|
1258 | [fRatioNotSetItem setState: !enable ? NSOnState : NSOffState]; |
---|
1259 | |
---|
1260 | [fRatioSetItem setTitle: [NSString stringWithFormat: @"Stop at Ratio (%.2f)", |
---|
1261 | [[dict objectForKey: @"Ratio"] floatValue]]]; |
---|
1262 | |
---|
1263 | [dict release]; |
---|
1264 | } |
---|
1265 | |
---|
1266 | - (void) checkWaitingForStopped: (NSNotification *) notification |
---|
1267 | { |
---|
1268 | [self checkToStartWaiting: [notification object]]; |
---|
1269 | |
---|
1270 | [self updateUI: nil]; |
---|
1271 | [self applyFilter: nil]; |
---|
1272 | [fInfoController updateInfoStatsAndSettings]; |
---|
1273 | [self updateTorrentHistory]; |
---|
1274 | } |
---|
1275 | |
---|
1276 | - (void) checkToStartWaiting: (Torrent *) finishedTorrent |
---|
1277 | { |
---|
1278 | //don't try to start a transfer if there should be none waiting |
---|
1279 | if (![[fDefaults stringForKey: @"StartSetting"] isEqualToString: @"Wait"]) |
---|
1280 | return; |
---|
1281 | |
---|
1282 | int desiredActive = [fDefaults integerForKey: @"WaitToStartNumber"]; |
---|
1283 | |
---|
1284 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1285 | Torrent * torrent, * torrentToStart = nil; |
---|
1286 | while ((torrent = [enumerator nextObject])) |
---|
1287 | { |
---|
1288 | //ignore the torrent just stopped |
---|
1289 | if (torrent == finishedTorrent) |
---|
1290 | continue; |
---|
1291 | |
---|
1292 | if ([torrent isActive]) |
---|
1293 | { |
---|
1294 | if (![torrent isSeeding]) |
---|
1295 | { |
---|
1296 | desiredActive--; |
---|
1297 | if (desiredActive <= 0) |
---|
1298 | return; |
---|
1299 | } |
---|
1300 | } |
---|
1301 | else |
---|
1302 | { |
---|
1303 | //use as next if it is waiting to start and either no previous or order value is lower |
---|
1304 | if ([torrent waitingToStart] && (!torrentToStart |
---|
1305 | || [[torrentToStart orderValue] compare: [torrent orderValue]] == NSOrderedDescending)) |
---|
1306 | torrentToStart = torrent; |
---|
1307 | } |
---|
1308 | } |
---|
1309 | |
---|
1310 | //since it hasn't returned, the queue amount has not been met |
---|
1311 | if (torrentToStart) |
---|
1312 | { |
---|
1313 | [torrentToStart startTransfer]; |
---|
1314 | |
---|
1315 | [self updateUI: nil]; |
---|
1316 | [self applyFilter: nil]; |
---|
1317 | [fInfoController updateInfoStatsAndSettings]; |
---|
1318 | [self updateTorrentHistory]; |
---|
1319 | } |
---|
1320 | } |
---|
1321 | |
---|
1322 | - (void) torrentStartSettingChange: (NSNotification *) notification |
---|
1323 | { |
---|
1324 | [self attemptToStartMultipleAuto: [notification object]]; |
---|
1325 | |
---|
1326 | [self updateUI: nil]; |
---|
1327 | [self applyFilter: nil]; |
---|
1328 | [fInfoController updateInfoStatsAndSettings]; |
---|
1329 | [self updateTorrentHistory]; |
---|
1330 | } |
---|
1331 | |
---|
1332 | - (void) globalStartSettingChange: (NSNotification *) notification |
---|
1333 | { |
---|
1334 | [self attemptToStartMultipleAuto: fTorrents]; |
---|
1335 | |
---|
1336 | [self updateUI: nil]; |
---|
1337 | [self applyFilter: nil]; |
---|
1338 | [fInfoController updateInfoStatsAndSettings]; |
---|
1339 | [self updateTorrentHistory]; |
---|
1340 | } |
---|
1341 | |
---|
1342 | - (void) torrentStoppedForRatio: (NSNotification *) notification |
---|
1343 | { |
---|
1344 | [self applyFilter: nil]; |
---|
1345 | [fInfoController updateInfoStatsAndSettings]; |
---|
1346 | |
---|
1347 | [GrowlApplicationBridge notifyWithTitle: @"Seeding Complete" description: [[notification object] name] |
---|
1348 | notificationName: GROWL_SEEDING_COMPLETE iconData: nil priority: 0 isSticky: NO clickContext: nil]; |
---|
1349 | } |
---|
1350 | |
---|
1351 | - (void) attemptToStartAuto: (Torrent *) torrent |
---|
1352 | { |
---|
1353 | [self attemptToStartMultipleAuto: [NSArray arrayWithObject: torrent]]; |
---|
1354 | } |
---|
1355 | |
---|
1356 | //will try to start, taking into consideration the start preference |
---|
1357 | - (void) attemptToStartMultipleAuto: (NSArray *) torrents |
---|
1358 | { |
---|
1359 | NSString * startSetting = [fDefaults stringForKey: @"StartSetting"]; |
---|
1360 | if ([startSetting isEqualToString: @"Start"]) |
---|
1361 | { |
---|
1362 | NSEnumerator * enumerator = [torrents objectEnumerator]; |
---|
1363 | Torrent * torrent; |
---|
1364 | while ((torrent = [enumerator nextObject])) |
---|
1365 | if ([torrent waitingToStart]) |
---|
1366 | [torrent startTransfer]; |
---|
1367 | |
---|
1368 | return; |
---|
1369 | } |
---|
1370 | else if (![startSetting isEqualToString: @"Wait"]) |
---|
1371 | return; |
---|
1372 | else; |
---|
1373 | |
---|
1374 | //determine the number of downloads needed to start |
---|
1375 | int desiredActive = [fDefaults integerForKey: @"WaitToStartNumber"]; |
---|
1376 | |
---|
1377 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1378 | Torrent * torrent; |
---|
1379 | while ((torrent = [enumerator nextObject])) |
---|
1380 | if ([torrent isActive] && ![torrent isSeeding]) |
---|
1381 | { |
---|
1382 | desiredActive--; |
---|
1383 | if (desiredActive <= 0) |
---|
1384 | break; |
---|
1385 | } |
---|
1386 | |
---|
1387 | //sort torrents by order value |
---|
1388 | NSArray * sortedTorrents; |
---|
1389 | if ([torrents count] > 1 && desiredActive > 0) |
---|
1390 | { |
---|
1391 | NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: |
---|
1392 | @"orderValue" ascending: YES] autorelease]; |
---|
1393 | NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil]; |
---|
1394 | |
---|
1395 | sortedTorrents = [torrents sortedArrayUsingDescriptors: descriptors]; |
---|
1396 | [descriptors release]; |
---|
1397 | } |
---|
1398 | else |
---|
1399 | sortedTorrents = torrents; |
---|
1400 | |
---|
1401 | enumerator = [sortedTorrents objectEnumerator]; |
---|
1402 | while ((torrent = [enumerator nextObject])) |
---|
1403 | { |
---|
1404 | if ([torrent waitingToStart]) |
---|
1405 | { |
---|
1406 | if ([torrent progress] >= 1.0) |
---|
1407 | [torrent startTransfer]; |
---|
1408 | else if (desiredActive > 0) |
---|
1409 | { |
---|
1410 | [torrent startTransfer]; |
---|
1411 | desiredActive--; |
---|
1412 | } |
---|
1413 | else |
---|
1414 | continue; |
---|
1415 | |
---|
1416 | [torrent update]; |
---|
1417 | } |
---|
1418 | } |
---|
1419 | } |
---|
1420 | |
---|
1421 | - (void) reloadInspectorSettings: (NSNotification *) notification |
---|
1422 | { |
---|
1423 | [fInfoController updateInfoStatsAndSettings]; |
---|
1424 | } |
---|
1425 | |
---|
1426 | - (void) checkAutoImportDirectory: (NSTimer *) timer |
---|
1427 | { |
---|
1428 | if (![fDefaults boolForKey: @"AutoImport"]) |
---|
1429 | return; |
---|
1430 | |
---|
1431 | NSString * path = [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath]; |
---|
1432 | |
---|
1433 | //if folder cannot be found or the contents hasn't changed simply give up |
---|
1434 | NSArray * allFileNames; |
---|
1435 | if (!(allFileNames = [[NSFileManager defaultManager] directoryContentsAtPath: path]) |
---|
1436 | || [allFileNames isEqualToArray: fAutoImportedNames]) |
---|
1437 | return; |
---|
1438 | |
---|
1439 | //try to add files that haven't already been added |
---|
1440 | NSMutableArray * newFileNames = [NSMutableArray arrayWithArray: allFileNames]; |
---|
1441 | [newFileNames removeObjectsInArray: fAutoImportedNames]; |
---|
1442 | |
---|
1443 | //save the current list of files |
---|
1444 | [fAutoImportedNames setArray: allFileNames]; |
---|
1445 | |
---|
1446 | NSEnumerator * enumerator = [newFileNames objectEnumerator]; |
---|
1447 | NSString * file; |
---|
1448 | unsigned oldCount; |
---|
1449 | while ((file = [enumerator nextObject])) |
---|
1450 | if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame) |
---|
1451 | { |
---|
1452 | oldCount = [fTorrents count]; |
---|
1453 | [self openFiles: [NSArray arrayWithObject: [path stringByAppendingPathComponent: file]]]; |
---|
1454 | |
---|
1455 | //import only actually happened if the torrent array is larger |
---|
1456 | if (oldCount < [fTorrents count]) |
---|
1457 | [GrowlApplicationBridge notifyWithTitle: @"Torrent File Auto Added" description: file |
---|
1458 | notificationName: GROWL_AUTO_ADD iconData: nil priority: 0 isSticky: NO clickContext: nil]; |
---|
1459 | } |
---|
1460 | } |
---|
1461 | |
---|
1462 | - (void) autoImportChange: (NSNotification *) notification |
---|
1463 | { |
---|
1464 | [fAutoImportedNames removeAllObjects]; |
---|
1465 | [self checkAutoImportDirectory: nil]; |
---|
1466 | } |
---|
1467 | |
---|
1468 | - (int) numberOfRowsInTableView: (NSTableView *) t |
---|
1469 | { |
---|
1470 | return [fDisplayedTorrents count]; |
---|
1471 | } |
---|
1472 | |
---|
1473 | - (void) tableView: (NSTableView *) t willDisplayCell: (id) cell |
---|
1474 | forTableColumn: (NSTableColumn *) tableColumn row: (int) row |
---|
1475 | { |
---|
1476 | [cell setTorrent: [fDisplayedTorrents objectAtIndex: row]]; |
---|
1477 | } |
---|
1478 | |
---|
1479 | - (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) indexes |
---|
1480 | toPasteboard: (NSPasteboard *) pasteboard |
---|
1481 | { |
---|
1482 | //only allow reordering of rows if sorting by order with no filter |
---|
1483 | if ([fSortType isEqualToString: @"Order"] && [fFilterType isEqualToString: @"None"] |
---|
1484 | && [[fSearchFilterField stringValue] length] == 0) |
---|
1485 | { |
---|
1486 | [pasteboard declareTypes: [NSArray arrayWithObject: TORRENT_TABLE_VIEW_DATA_TYPE] owner: self]; |
---|
1487 | [pasteboard setData: [NSKeyedArchiver archivedDataWithRootObject: indexes] |
---|
1488 | forType: TORRENT_TABLE_VIEW_DATA_TYPE]; |
---|
1489 | return YES; |
---|
1490 | } |
---|
1491 | return NO; |
---|
1492 | } |
---|
1493 | |
---|
1494 | - (NSDragOperation) tableView: (NSTableView *) t validateDrop: (id <NSDraggingInfo>) info |
---|
1495 | proposedRow: (int) row proposedDropOperation: (NSTableViewDropOperation) operation |
---|
1496 | { |
---|
1497 | NSPasteboard * pasteboard = [info draggingPasteboard]; |
---|
1498 | if ([[pasteboard types] containsObject: NSFilenamesPboardType]) |
---|
1499 | { |
---|
1500 | //check if any files to add have "torrent" as an extension |
---|
1501 | NSEnumerator * enumerator = [[pasteboard propertyListForType: NSFilenamesPboardType] objectEnumerator]; |
---|
1502 | NSString * file; |
---|
1503 | while ((file = [enumerator nextObject])) |
---|
1504 | if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame) |
---|
1505 | { |
---|
1506 | [fTableView setDropRow: -1 dropOperation: NSTableViewDropOn]; |
---|
1507 | return NSDragOperationGeneric; |
---|
1508 | } |
---|
1509 | } |
---|
1510 | else if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE]) |
---|
1511 | { |
---|
1512 | [fTableView setDropRow: row dropOperation: NSTableViewDropAbove]; |
---|
1513 | return NSDragOperationGeneric; |
---|
1514 | } |
---|
1515 | else |
---|
1516 | return NSDragOperationNone; |
---|
1517 | } |
---|
1518 | |
---|
1519 | - (BOOL) tableView: (NSTableView *) t acceptDrop: (id <NSDraggingInfo>) info |
---|
1520 | row: (int) newRow dropOperation: (NSTableViewDropOperation) operation |
---|
1521 | { |
---|
1522 | NSPasteboard * pasteboard = [info draggingPasteboard]; |
---|
1523 | if ([[pasteboard types] containsObject: NSFilenamesPboardType]) |
---|
1524 | { |
---|
1525 | //create an array of files with the "torrent" extension |
---|
1526 | NSMutableArray * filesToOpen = [[NSMutableArray alloc] init]; |
---|
1527 | NSEnumerator * enumerator = [[pasteboard propertyListForType: NSFilenamesPboardType] objectEnumerator]; |
---|
1528 | NSString * file; |
---|
1529 | while ((file = [enumerator nextObject])) |
---|
1530 | if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame) |
---|
1531 | [filesToOpen addObject: file]; |
---|
1532 | |
---|
1533 | [self application: NSApp openFiles: filesToOpen]; |
---|
1534 | [filesToOpen release]; |
---|
1535 | } |
---|
1536 | else |
---|
1537 | { |
---|
1538 | //remember selected rows if needed |
---|
1539 | NSArray * selectedTorrents = nil; |
---|
1540 | int numSelected = [fTableView numberOfSelectedRows]; |
---|
1541 | if (numSelected > 0 && numSelected < [fDisplayedTorrents count]) |
---|
1542 | selectedTorrents = [self torrentsAtIndexes: [fTableView selectedRowIndexes]]; |
---|
1543 | |
---|
1544 | NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: |
---|
1545 | [pasteboard dataForType: TORRENT_TABLE_VIEW_DATA_TYPE]]; |
---|
1546 | |
---|
1547 | //move torrent in array |
---|
1548 | NSArray * movingTorrents = [[self torrentsAtIndexes: indexes] retain]; |
---|
1549 | [fDisplayedTorrents removeObjectsInArray: movingTorrents]; |
---|
1550 | |
---|
1551 | //determine the insertion index now that transfers to move have been removed |
---|
1552 | int i, decrease = 0; |
---|
1553 | for (i = [indexes firstIndex]; i < newRow && i != NSNotFound; i = [indexes indexGreaterThanIndex: i]) |
---|
1554 | decrease++; |
---|
1555 | |
---|
1556 | //insert objects at new location |
---|
1557 | for (i = 0; i < [movingTorrents count]; i++) |
---|
1558 | [fDisplayedTorrents insertObject: [movingTorrents objectAtIndex: i] atIndex: newRow - decrease + i]; |
---|
1559 | |
---|
1560 | [movingTorrents release]; |
---|
1561 | |
---|
1562 | //redo order values |
---|
1563 | int low = [indexes firstIndex], high = [indexes lastIndex]; |
---|
1564 | if (newRow < low) |
---|
1565 | low = newRow; |
---|
1566 | else if (newRow > high + 1) |
---|
1567 | high = newRow - 1; |
---|
1568 | else; |
---|
1569 | |
---|
1570 | for (i = low; i <= high; i++) |
---|
1571 | [[fDisplayedTorrents objectAtIndex: i] setOrderValue: i]; |
---|
1572 | |
---|
1573 | [fTableView reloadData]; |
---|
1574 | |
---|
1575 | //set selected rows if needed |
---|
1576 | if (selectedTorrents) |
---|
1577 | { |
---|
1578 | Torrent * torrent; |
---|
1579 | NSEnumerator * enumerator = [selectedTorrents objectEnumerator]; |
---|
1580 | NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init]; |
---|
1581 | while ((torrent = [enumerator nextObject])) |
---|
1582 | [indexSet addIndex: [fDisplayedTorrents indexOfObject: torrent]]; |
---|
1583 | |
---|
1584 | [fTableView selectRowIndexes: indexSet byExtendingSelection: NO]; |
---|
1585 | [indexSet release]; |
---|
1586 | } |
---|
1587 | } |
---|
1588 | |
---|
1589 | return YES; |
---|
1590 | } |
---|
1591 | |
---|
1592 | - (void) tableViewSelectionDidChange: (NSNotification *) notification |
---|
1593 | { |
---|
1594 | [fInfoController updateInfoForTorrents: [self torrentsAtIndexes: [fTableView selectedRowIndexes]]]; |
---|
1595 | } |
---|
1596 | |
---|
1597 | - (void) toggleSmallView: (id) sender |
---|
1598 | { |
---|
1599 | BOOL makeSmall = ![fDefaults boolForKey: @"SmallView"]; |
---|
1600 | |
---|
1601 | [fTableView setRowHeight: makeSmall ? ROW_HEIGHT_SMALL : ROW_HEIGHT_REGULAR]; |
---|
1602 | [fSmallViewItem setState: makeSmall]; |
---|
1603 | |
---|
1604 | [fDefaults setBool: makeSmall forKey: @"SmallView"]; |
---|
1605 | |
---|
1606 | //window min height |
---|
1607 | NSSize contentMinSize = [fWindow contentMinSize], |
---|
1608 | contentSize = [[fWindow contentView] frame].size; |
---|
1609 | contentMinSize.height = contentSize.height - [fScrollView frame].size.height |
---|
1610 | + [fTableView rowHeight] + [fTableView intercellSpacing].height; |
---|
1611 | [fWindow setContentMinSize: contentMinSize]; |
---|
1612 | |
---|
1613 | //resize for larger min height if not set to auto size |
---|
1614 | if (![fDefaults boolForKey: @"AutoSize"]) |
---|
1615 | { |
---|
1616 | if (!makeSmall && contentSize.height < contentMinSize.height) |
---|
1617 | { |
---|
1618 | NSRect frame = [fWindow frame]; |
---|
1619 | float heightChange = contentMinSize.height - contentSize.height; |
---|
1620 | frame.size.height += heightChange; |
---|
1621 | frame.origin.y -= heightChange; |
---|
1622 | |
---|
1623 | [fWindow setFrame: frame display: YES]; |
---|
1624 | [fTableView reloadData]; |
---|
1625 | } |
---|
1626 | } |
---|
1627 | else |
---|
1628 | [self setWindowSizeToFit]; |
---|
1629 | } |
---|
1630 | |
---|
1631 | - (void) toggleStatusBar: (id) sender |
---|
1632 | { |
---|
1633 | [self showStatusBar: [fStatusBar isHidden] animate: YES]; |
---|
1634 | [fDefaults setBool: ![fStatusBar isHidden] forKey: @"StatusBar"]; |
---|
1635 | } |
---|
1636 | |
---|
1637 | - (void) showStatusBar: (BOOL) show animate: (BOOL) animate |
---|
1638 | { |
---|
1639 | if (show != [fStatusBar isHidden]) |
---|
1640 | return; |
---|
1641 | |
---|
1642 | if (show) |
---|
1643 | [fStatusBar setHidden: NO]; |
---|
1644 | |
---|
1645 | NSRect frame = [fWindow frame]; |
---|
1646 | float heightChange = [fStatusBar frame].size.height; |
---|
1647 | if (!show) |
---|
1648 | heightChange *= -1; |
---|
1649 | |
---|
1650 | frame.size.height += heightChange; |
---|
1651 | frame.origin.y -= heightChange; |
---|
1652 | |
---|
1653 | [self updateUI: nil]; |
---|
1654 | |
---|
1655 | //set views to not autoresize |
---|
1656 | unsigned int statsMask = [fStatusBar autoresizingMask]; |
---|
1657 | unsigned int filterMask = [fFilterBar autoresizingMask]; |
---|
1658 | unsigned int scrollMask = [fScrollView autoresizingMask]; |
---|
1659 | [fStatusBar setAutoresizingMask: 0]; |
---|
1660 | [fFilterBar setAutoresizingMask: 0]; |
---|
1661 | [fScrollView setAutoresizingMask: 0]; |
---|
1662 | |
---|
1663 | [fWindow setFrame: frame display: YES animate: animate]; |
---|
1664 | |
---|
1665 | //re-enable autoresize |
---|
1666 | [fStatusBar setAutoresizingMask: statsMask]; |
---|
1667 | [fFilterBar setAutoresizingMask: filterMask]; |
---|
1668 | [fScrollView setAutoresizingMask: scrollMask]; |
---|
1669 | |
---|
1670 | //change min size |
---|
1671 | NSSize minSize = [fWindow contentMinSize]; |
---|
1672 | minSize.height += heightChange; |
---|
1673 | [fWindow setContentMinSize: minSize]; |
---|
1674 | |
---|
1675 | if (!show) |
---|
1676 | [fStatusBar setHidden: YES]; |
---|
1677 | } |
---|
1678 | |
---|
1679 | - (void) toggleFilterBar: (id) sender |
---|
1680 | { |
---|
1681 | //disable filtering when hiding |
---|
1682 | if (![fFilterBar isHidden]) |
---|
1683 | { |
---|
1684 | [fSearchFilterField setStringValue: @""]; |
---|
1685 | [self setFilter: fNoFilterButton]; |
---|
1686 | } |
---|
1687 | |
---|
1688 | [self showFilterBar: [fFilterBar isHidden] animate: YES]; |
---|
1689 | [fDefaults setBool: ![fFilterBar isHidden] forKey: @"FilterBar"]; |
---|
1690 | } |
---|
1691 | |
---|
1692 | - (void) showFilterBar: (BOOL) show animate: (BOOL) animate |
---|
1693 | { |
---|
1694 | if (show != [fFilterBar isHidden]) |
---|
1695 | return; |
---|
1696 | |
---|
1697 | if (show) |
---|
1698 | [fFilterBar setHidden: NO]; |
---|
1699 | |
---|
1700 | NSRect frame = [fWindow frame]; |
---|
1701 | float heightChange = [fFilterBar frame].size.height; |
---|
1702 | if (!show) |
---|
1703 | heightChange *= -1; |
---|
1704 | |
---|
1705 | frame.size.height += heightChange; |
---|
1706 | frame.origin.y -= heightChange; |
---|
1707 | |
---|
1708 | //set views to not autoresize |
---|
1709 | unsigned int filterMask = [fFilterBar autoresizingMask]; |
---|
1710 | unsigned int scrollMask = [fScrollView autoresizingMask]; |
---|
1711 | [fFilterBar setAutoresizingMask: 0]; |
---|
1712 | [fScrollView setAutoresizingMask: 0]; |
---|
1713 | |
---|
1714 | [fWindow setFrame: frame display: YES animate: animate]; |
---|
1715 | |
---|
1716 | //re-enable autoresize |
---|
1717 | [fFilterBar setAutoresizingMask: filterMask]; |
---|
1718 | [fScrollView setAutoresizingMask: scrollMask]; |
---|
1719 | |
---|
1720 | //change min size |
---|
1721 | NSSize minSize = [fWindow contentMinSize]; |
---|
1722 | minSize.height += heightChange; |
---|
1723 | [fWindow setContentMinSize: minSize]; |
---|
1724 | |
---|
1725 | if (!show) |
---|
1726 | { |
---|
1727 | [fFilterBar setHidden: YES]; |
---|
1728 | [fWindow makeFirstResponder: fTableView]; |
---|
1729 | } |
---|
1730 | } |
---|
1731 | |
---|
1732 | - (void) toggleAdvancedBar: (id) sender |
---|
1733 | { |
---|
1734 | int state = ![fAdvancedBarItem state]; |
---|
1735 | [fAdvancedBarItem setState: state]; |
---|
1736 | [fDefaults setBool: state forKey: @"UseAdvancedBar"]; |
---|
1737 | |
---|
1738 | [fTableView display]; |
---|
1739 | } |
---|
1740 | |
---|
1741 | - (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier: |
---|
1742 | (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag |
---|
1743 | { |
---|
1744 | NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier: ident]; |
---|
1745 | |
---|
1746 | if ([ident isEqualToString: TOOLBAR_OPEN]) |
---|
1747 | { |
---|
1748 | [item setLabel: @"Open"]; |
---|
1749 | [item setPaletteLabel: @"Open Torrent Files"]; |
---|
1750 | [item setToolTip: @"Open torrent files"]; |
---|
1751 | [item setImage: [NSImage imageNamed: @"Open.png"]]; |
---|
1752 | [item setTarget: self]; |
---|
1753 | [item setAction: @selector(openShowSheet:)]; |
---|
1754 | } |
---|
1755 | else if ([ident isEqualToString: TOOLBAR_REMOVE]) |
---|
1756 | { |
---|
1757 | [item setLabel: @"Remove"]; |
---|
1758 | [item setPaletteLabel: @"Remove Selected"]; |
---|
1759 | [item setToolTip: @"Remove selected transfers"]; |
---|
1760 | [item setImage: [NSImage imageNamed: @"Remove.png"]]; |
---|
1761 | [item setTarget: self]; |
---|
1762 | [item setAction: @selector(removeNoDelete:)]; |
---|
1763 | } |
---|
1764 | else if ([ident isEqualToString: TOOLBAR_INFO]) |
---|
1765 | { |
---|
1766 | [item setLabel: @"Inspector"]; |
---|
1767 | [item setPaletteLabel: @"Toggle Inspector"]; |
---|
1768 | [item setToolTip: @"Toggle the torrent inspector"]; |
---|
1769 | [item setImage: [NSImage imageNamed: @"Info.png"]]; |
---|
1770 | [item setTarget: self]; |
---|
1771 | [item setAction: @selector(showInfo:)]; |
---|
1772 | } |
---|
1773 | else if ([ident isEqualToString: TOOLBAR_PAUSE_ALL]) |
---|
1774 | { |
---|
1775 | [item setLabel: @"Pause All"]; |
---|
1776 | [item setPaletteLabel: [item label]]; |
---|
1777 | [item setToolTip: @"Pause all transfers"]; |
---|
1778 | [item setImage: [NSImage imageNamed: @"PauseAll.png"]]; |
---|
1779 | [item setTarget: self]; |
---|
1780 | [item setAction: @selector(stopAllTorrents:)]; |
---|
1781 | } |
---|
1782 | else if ([ident isEqualToString: TOOLBAR_RESUME_ALL]) |
---|
1783 | { |
---|
1784 | [item setLabel: @"Resume All"]; |
---|
1785 | [item setPaletteLabel: [item label]]; |
---|
1786 | [item setToolTip: @"Resume all transfers"]; |
---|
1787 | [item setImage: [NSImage imageNamed: @"ResumeAll.png"]]; |
---|
1788 | [item setTarget: self]; |
---|
1789 | [item setAction: @selector(resumeAllTorrents:)]; |
---|
1790 | } |
---|
1791 | else if ([ident isEqualToString: TOOLBAR_PAUSE_SELECTED]) |
---|
1792 | { |
---|
1793 | [item setLabel: @"Pause"]; |
---|
1794 | [item setPaletteLabel: @"Pause Selected"]; |
---|
1795 | [item setToolTip: @"Pause selected transfers"]; |
---|
1796 | [item setImage: [NSImage imageNamed: @"PauseSelected.png"]]; |
---|
1797 | [item setTarget: self]; |
---|
1798 | [item setAction: @selector(stopSelectedTorrents:)]; |
---|
1799 | } |
---|
1800 | else if ([ident isEqualToString: TOOLBAR_RESUME_SELECTED]) |
---|
1801 | { |
---|
1802 | [item setLabel: @"Resume"]; |
---|
1803 | [item setPaletteLabel: @"Resume Selected"]; |
---|
1804 | [item setToolTip: @"Resume selected transfers"]; |
---|
1805 | [item setImage: [NSImage imageNamed: @"ResumeSelected.png"]]; |
---|
1806 | [item setTarget: self]; |
---|
1807 | [item setAction: @selector(resumeSelectedTorrents:)]; |
---|
1808 | } |
---|
1809 | else if ([ident isEqualToString: TOOLBAR_FILTER]) |
---|
1810 | { |
---|
1811 | [item setLabel: @"Filter"]; |
---|
1812 | [item setPaletteLabel: @"Toggle Filter"]; |
---|
1813 | [item setToolTip: @"Toggle the filter bar"]; |
---|
1814 | [item setImage: [NSImage imageNamed: @"Filter.png"]]; |
---|
1815 | [item setTarget: self]; |
---|
1816 | [item setAction: @selector(toggleFilterBar:)]; |
---|
1817 | } |
---|
1818 | else |
---|
1819 | { |
---|
1820 | [item release]; |
---|
1821 | return nil; |
---|
1822 | } |
---|
1823 | |
---|
1824 | return item; |
---|
1825 | } |
---|
1826 | |
---|
1827 | - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) t |
---|
1828 | { |
---|
1829 | return [NSArray arrayWithObjects: |
---|
1830 | TOOLBAR_OPEN, TOOLBAR_REMOVE, |
---|
1831 | TOOLBAR_PAUSE_SELECTED, TOOLBAR_RESUME_SELECTED, |
---|
1832 | TOOLBAR_PAUSE_ALL, TOOLBAR_RESUME_ALL, TOOLBAR_FILTER, TOOLBAR_INFO, |
---|
1833 | NSToolbarSeparatorItemIdentifier, |
---|
1834 | NSToolbarSpaceItemIdentifier, |
---|
1835 | NSToolbarFlexibleSpaceItemIdentifier, |
---|
1836 | NSToolbarCustomizeToolbarItemIdentifier, nil]; |
---|
1837 | } |
---|
1838 | |
---|
1839 | - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) t |
---|
1840 | { |
---|
1841 | return [NSArray arrayWithObjects: |
---|
1842 | TOOLBAR_OPEN, TOOLBAR_REMOVE, |
---|
1843 | NSToolbarSeparatorItemIdentifier, |
---|
1844 | TOOLBAR_PAUSE_ALL, TOOLBAR_RESUME_ALL, |
---|
1845 | NSToolbarFlexibleSpaceItemIdentifier, |
---|
1846 | TOOLBAR_FILTER, TOOLBAR_INFO, nil]; |
---|
1847 | } |
---|
1848 | |
---|
1849 | - (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem |
---|
1850 | { |
---|
1851 | NSString * ident = [toolbarItem itemIdentifier]; |
---|
1852 | |
---|
1853 | //enable remove item |
---|
1854 | if ([ident isEqualToString: TOOLBAR_REMOVE]) |
---|
1855 | return [fTableView numberOfSelectedRows] > 0; |
---|
1856 | |
---|
1857 | //enable pause all item |
---|
1858 | if ([ident isEqualToString: TOOLBAR_PAUSE_ALL]) |
---|
1859 | { |
---|
1860 | Torrent * torrent; |
---|
1861 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1862 | while ((torrent = [enumerator nextObject])) |
---|
1863 | if ([torrent isActive]) |
---|
1864 | return YES; |
---|
1865 | return NO; |
---|
1866 | } |
---|
1867 | |
---|
1868 | //enable resume all item |
---|
1869 | if ([ident isEqualToString: TOOLBAR_RESUME_ALL]) |
---|
1870 | { |
---|
1871 | Torrent * torrent; |
---|
1872 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
1873 | while ((torrent = [enumerator nextObject])) |
---|
1874 | if ([torrent isPaused]) |
---|
1875 | return YES; |
---|
1876 | return NO; |
---|
1877 | } |
---|
1878 | |
---|
1879 | //enable pause item |
---|
1880 | if ([ident isEqualToString: TOOLBAR_PAUSE_SELECTED]) |
---|
1881 | { |
---|
1882 | Torrent * torrent; |
---|
1883 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
1884 | unsigned int i; |
---|
1885 | |
---|
1886 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
1887 | if ([[fDisplayedTorrents objectAtIndex: i] isActive]) |
---|
1888 | return YES; |
---|
1889 | return NO; |
---|
1890 | } |
---|
1891 | |
---|
1892 | //enable resume item |
---|
1893 | if ([ident isEqualToString: TOOLBAR_RESUME_SELECTED]) |
---|
1894 | { |
---|
1895 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
1896 | unsigned int i; |
---|
1897 | |
---|
1898 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
1899 | if ([[fDisplayedTorrents objectAtIndex: i] isPaused]) |
---|
1900 | return YES; |
---|
1901 | return NO; |
---|
1902 | } |
---|
1903 | |
---|
1904 | return YES; |
---|
1905 | } |
---|
1906 | |
---|
1907 | - (BOOL) validateMenuItem: (NSMenuItem *) menuItem |
---|
1908 | { |
---|
1909 | SEL action = [menuItem action]; |
---|
1910 | |
---|
1911 | //only enable some items if it is in a context menu or the window is useable |
---|
1912 | BOOL canUseMenu = [fWindow isKeyWindow] || [[[menuItem menu] title] isEqualToString: @"Context"]; |
---|
1913 | |
---|
1914 | //enable open items |
---|
1915 | if (action == @selector(openShowSheet:)) |
---|
1916 | return [fWindow attachedSheet] == nil; |
---|
1917 | |
---|
1918 | //enable show info |
---|
1919 | if (action == @selector(showInfo:)) |
---|
1920 | { |
---|
1921 | NSString * title = [[fInfoController window] isVisible] ? @"Hide Inspector" : @"Show Inspector"; |
---|
1922 | if (![[menuItem title] isEqualToString: title]) |
---|
1923 | [menuItem setTitle: title]; |
---|
1924 | |
---|
1925 | return YES; |
---|
1926 | } |
---|
1927 | |
---|
1928 | //enable prev/next inspector tab |
---|
1929 | if (action == @selector(setInfoTab:)) |
---|
1930 | return [[fInfoController window] isVisible]; |
---|
1931 | |
---|
1932 | //enable toggle status bar |
---|
1933 | if (action == @selector(toggleStatusBar:)) |
---|
1934 | { |
---|
1935 | NSString * title = [fStatusBar isHidden] ? @"Show Status Bar" : @"Hide Status Bar"; |
---|
1936 | if (![[menuItem title] isEqualToString: title]) |
---|
1937 | [menuItem setTitle: title]; |
---|
1938 | |
---|
1939 | return canUseMenu; |
---|
1940 | } |
---|
1941 | |
---|
1942 | //enable toggle filter bar |
---|
1943 | if (action == @selector(toggleFilterBar:)) |
---|
1944 | { |
---|
1945 | NSString * title = [fFilterBar isHidden] ? @"Show Filter Bar" : @"Hide Filter Bar"; |
---|
1946 | if (![[menuItem title] isEqualToString: title]) |
---|
1947 | [menuItem setTitle: title]; |
---|
1948 | |
---|
1949 | return canUseMenu; |
---|
1950 | } |
---|
1951 | |
---|
1952 | //enable reveal in finder |
---|
1953 | if (action == @selector(revealFile:)) |
---|
1954 | return canUseMenu && [fTableView numberOfSelectedRows] > 0; |
---|
1955 | |
---|
1956 | //enable remove items |
---|
1957 | if (action == @selector(removeNoDelete:) || action == @selector(removeDeleteData:) |
---|
1958 | || action == @selector(removeDeleteTorrent:) || action == @selector(removeDeleteDataAndTorrent:)) |
---|
1959 | { |
---|
1960 | BOOL warning = NO, |
---|
1961 | onlyDownloading = [fDefaults boolForKey: @"CheckRemoveDownloading"], |
---|
1962 | canDelete = action != @selector(removeDeleteTorrent:) && action != @selector(removeDeleteDataAndTorrent:); |
---|
1963 | Torrent * torrent; |
---|
1964 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
1965 | unsigned int i; |
---|
1966 | |
---|
1967 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
1968 | { |
---|
1969 | torrent = [fDisplayedTorrents objectAtIndex: i]; |
---|
1970 | if (!warning && [torrent isActive]) |
---|
1971 | { |
---|
1972 | warning = onlyDownloading ? ![torrent isSeeding] : YES; |
---|
1973 | if (warning && canDelete) |
---|
1974 | break; |
---|
1975 | } |
---|
1976 | if (!canDelete && [torrent publicTorrent]) |
---|
1977 | { |
---|
1978 | canDelete = YES; |
---|
1979 | if (warning) |
---|
1980 | break; |
---|
1981 | } |
---|
1982 | } |
---|
1983 | |
---|
1984 | //append or remove ellipsis when needed |
---|
1985 | NSString * title = [menuItem title], * ellipsis = [NSString ellipsis]; |
---|
1986 | if (warning && [fDefaults boolForKey: @"CheckRemove"]) |
---|
1987 | { |
---|
1988 | if (![title hasSuffix: ellipsis]) |
---|
1989 | [menuItem setTitle: [title stringByAppendingEllipsis]]; |
---|
1990 | } |
---|
1991 | else |
---|
1992 | { |
---|
1993 | if ([title hasSuffix: ellipsis]) |
---|
1994 | [menuItem setTitle: [title substringToIndex: [title rangeOfString: ellipsis].location]]; |
---|
1995 | } |
---|
1996 | |
---|
1997 | return canUseMenu && canDelete && [fTableView numberOfSelectedRows] > 0; |
---|
1998 | } |
---|
1999 | |
---|
2000 | //enable pause all item |
---|
2001 | if (action == @selector(stopAllTorrents:)) |
---|
2002 | { |
---|
2003 | Torrent * torrent; |
---|
2004 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
2005 | while ((torrent = [enumerator nextObject])) |
---|
2006 | if ([torrent isActive]) |
---|
2007 | return YES; |
---|
2008 | return NO; |
---|
2009 | } |
---|
2010 | |
---|
2011 | //enable resume all item |
---|
2012 | if (action == @selector(resumeAllTorrents:)) |
---|
2013 | { |
---|
2014 | Torrent * torrent; |
---|
2015 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
2016 | while ((torrent = [enumerator nextObject])) |
---|
2017 | if ([torrent isPaused]) |
---|
2018 | return YES; |
---|
2019 | return NO; |
---|
2020 | } |
---|
2021 | |
---|
2022 | //enable resume waiting item |
---|
2023 | if (action == @selector(resumeWaitingTorrents:)) |
---|
2024 | { |
---|
2025 | if (![[fDefaults stringForKey: @"StartSetting"] isEqualToString: @"Wait"]) |
---|
2026 | return NO; |
---|
2027 | |
---|
2028 | Torrent * torrent; |
---|
2029 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
2030 | while ((torrent = [enumerator nextObject])) |
---|
2031 | if ([torrent waitingToStart]) |
---|
2032 | return YES; |
---|
2033 | return NO; |
---|
2034 | } |
---|
2035 | |
---|
2036 | //enable pause item |
---|
2037 | if (action == @selector(stopSelectedTorrents:)) |
---|
2038 | { |
---|
2039 | if (!canUseMenu) |
---|
2040 | return NO; |
---|
2041 | |
---|
2042 | Torrent * torrent; |
---|
2043 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
2044 | unsigned int i; |
---|
2045 | |
---|
2046 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
2047 | { |
---|
2048 | torrent = [fDisplayedTorrents objectAtIndex: i]; |
---|
2049 | if ([torrent isActive]) |
---|
2050 | return YES; |
---|
2051 | } |
---|
2052 | return NO; |
---|
2053 | } |
---|
2054 | |
---|
2055 | //enable resume item |
---|
2056 | if (action == @selector(resumeSelectedTorrents:)) |
---|
2057 | { |
---|
2058 | if (!canUseMenu) |
---|
2059 | return NO; |
---|
2060 | |
---|
2061 | Torrent * torrent; |
---|
2062 | NSIndexSet * indexSet = [fTableView selectedRowIndexes]; |
---|
2063 | unsigned int i; |
---|
2064 | |
---|
2065 | for (i = [indexSet firstIndex]; i != NSNotFound; i = [indexSet indexGreaterThanIndex: i]) |
---|
2066 | { |
---|
2067 | torrent = [fDisplayedTorrents objectAtIndex: i]; |
---|
2068 | if ([torrent isPaused]) |
---|
2069 | return YES; |
---|
2070 | } |
---|
2071 | return NO; |
---|
2072 | } |
---|
2073 | |
---|
2074 | //enable sort and advanced bar items |
---|
2075 | if (action == @selector(setSort:) || action == @selector(toggleAdvancedBar:) |
---|
2076 | || action == @selector(toggleSmallView:)) |
---|
2077 | return canUseMenu; |
---|
2078 | |
---|
2079 | //enable copy torrent file item |
---|
2080 | if (action == @selector(copyTorrentFile:)) |
---|
2081 | return canUseMenu && [fTableView numberOfSelectedRows] > 0; |
---|
2082 | |
---|
2083 | return YES; |
---|
2084 | } |
---|
2085 | |
---|
2086 | - (void) sleepCallBack: (natural_t) messageType argument: (void *) messageArgument |
---|
2087 | { |
---|
2088 | NSEnumerator * enumerator; |
---|
2089 | Torrent * torrent; |
---|
2090 | BOOL active; |
---|
2091 | |
---|
2092 | switch (messageType) |
---|
2093 | { |
---|
2094 | case kIOMessageSystemWillSleep: |
---|
2095 | //close all connections before going to sleep and remember we should resume when we wake up |
---|
2096 | [fTorrents makeObjectsPerformSelector: @selector(sleep)]; |
---|
2097 | |
---|
2098 | //wait for running transfers to stop (5 second timeout) |
---|
2099 | NSDate * start = [NSDate date]; |
---|
2100 | BOOL timeUp = NO; |
---|
2101 | |
---|
2102 | NSEnumerator * enumerator = [fTorrents objectEnumerator]; |
---|
2103 | Torrent * torrent; |
---|
2104 | while (!timeUp && (torrent = [enumerator nextObject])) |
---|
2105 | while (![torrent isPaused] && !(timeUp = [start timeIntervalSinceNow] < -5.0)) |
---|
2106 | { |
---|
2107 | usleep(100000); |
---|
2108 | [torrent update]; |
---|
2109 | } |
---|
2110 | |
---|
2111 | IOAllowPowerChange(fRootPort, (long) messageArgument); |
---|
2112 | break; |
---|
2113 | |
---|
2114 | case kIOMessageCanSystemSleep: |
---|
2115 | //pevent idle sleep unless all paused |
---|
2116 | active = NO; |
---|
2117 | enumerator = [fTorrents objectEnumerator]; |
---|
2118 | while ((torrent = [enumerator nextObject])) |
---|
2119 | if ([torrent isActive]) |
---|
2120 | { |
---|
2121 | active = YES; |
---|
2122 | break; |
---|
2123 | } |
---|
2124 | |
---|
2125 | if (active) |
---|
2126 | IOCancelPowerChange(fRootPort, (long) messageArgument); |
---|
2127 | else |
---|
2128 | IOAllowPowerChange(fRootPort, (long) messageArgument); |
---|
2129 | break; |
---|
2130 | |
---|
2131 | case kIOMessageSystemHasPoweredOn: |
---|
2132 | //resume sleeping transfers after we wake up |
---|
2133 | [fTorrents makeObjectsPerformSelector: @selector(wakeUp)]; |
---|
2134 | break; |
---|
2135 | } |
---|
2136 | } |
---|
2137 | |
---|
2138 | - (void) resetDockBadge: (NSNotification *) notification |
---|
2139 | { |
---|
2140 | float downloadRate, uploadRate; |
---|
2141 | tr_torrentRates(fLib, & downloadRate, & uploadRate); |
---|
2142 | |
---|
2143 | [fBadger updateBadgeWithCompleted: fCompleted uploadRate: uploadRate downloadRate: downloadRate]; |
---|
2144 | } |
---|
2145 | |
---|
2146 | - (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame |
---|
2147 | { |
---|
2148 | //if auto size is enabled, the current frame shouldn't need to change |
---|
2149 | NSRect frame = [fDefaults boolForKey: @"AutoSize"] ? [window frame] : [self sizedWindowFrame]; |
---|
2150 | |
---|
2151 | frame.size.width = [fDefaults boolForKey: @"SmallView"] ? [fWindow minSize].width : WINDOW_REGULAR_WIDTH; |
---|
2152 | return frame; |
---|
2153 | } |
---|
2154 | |
---|
2155 | - (void) setWindowSizeToFit |
---|
2156 | { |
---|
2157 | if ([fDefaults boolForKey: @"AutoSize"]) |
---|
2158 | [fWindow setFrame: [self sizedWindowFrame] display: YES animate: YES]; |
---|
2159 | } |
---|
2160 | |
---|
2161 | - (NSRect) sizedWindowFrame |
---|
2162 | { |
---|
2163 | NSRect frame = [fWindow frame]; |
---|
2164 | float newHeight = frame.size.height - [fScrollView frame].size.height |
---|
2165 | + [fDisplayedTorrents count] * ([fTableView rowHeight] + [fTableView intercellSpacing].height); |
---|
2166 | |
---|
2167 | float minHeight = [fWindow minSize].height; |
---|
2168 | if (newHeight < minHeight) |
---|
2169 | newHeight = minHeight; |
---|
2170 | else |
---|
2171 | { |
---|
2172 | float maxHeight = [[fWindow screen] visibleFrame].size.height; |
---|
2173 | if ([fStatusBar isHidden]) |
---|
2174 | maxHeight -= [fStatusBar frame].size.height; |
---|
2175 | if ([fFilterBar isHidden]) |
---|
2176 | maxHeight -= [fFilterBar frame].size.height; |
---|
2177 | |
---|
2178 | if (newHeight > maxHeight) |
---|
2179 | newHeight = maxHeight; |
---|
2180 | } |
---|
2181 | |
---|
2182 | frame.origin.y -= (newHeight - frame.size.height); |
---|
2183 | frame.size.height = newHeight; |
---|
2184 | return frame; |
---|
2185 | } |
---|
2186 | |
---|
2187 | - (void) showMainWindow: (id) sender |
---|
2188 | { |
---|
2189 | [fWindow makeKeyAndOrderFront: nil]; |
---|
2190 | } |
---|
2191 | |
---|
2192 | - (void) windowDidBecomeKey: (NSNotification *) notification |
---|
2193 | { |
---|
2194 | //reset dock badge for completed |
---|
2195 | if (fCompleted > 0) |
---|
2196 | { |
---|
2197 | fCompleted = 0; |
---|
2198 | [self resetDockBadge: nil]; |
---|
2199 | } |
---|
2200 | } |
---|
2201 | |
---|
2202 | - (NSSize) windowWillResize: (NSWindow *) sender toSize: (NSSize) proposedFrameSize |
---|
2203 | { |
---|
2204 | //only resize horizontally if autosize is enabled |
---|
2205 | if ([fDefaults boolForKey: @"AutoSize"]) |
---|
2206 | proposedFrameSize.height = [fWindow frame].size.height; |
---|
2207 | return proposedFrameSize; |
---|
2208 | } |
---|
2209 | |
---|
2210 | - (void) windowDidResize: (NSNotification *) notification |
---|
2211 | { |
---|
2212 | //hide search filter if it overlaps filter buttons |
---|
2213 | [fSearchFilterField setHidden: NSMaxX([fPauseFilterButton frame]) + 2.0 > [fSearchFilterField frame].origin.x]; |
---|
2214 | } |
---|
2215 | |
---|
2216 | - (void) linkHomepage: (id) sender |
---|
2217 | { |
---|
2218 | [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: WEBSITE_URL]]; |
---|
2219 | } |
---|
2220 | |
---|
2221 | - (void) linkForums: (id) sender |
---|
2222 | { |
---|
2223 | [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FORUM_URL]]; |
---|
2224 | } |
---|
2225 | |
---|
2226 | - (void) checkUpdate: (id) sender |
---|
2227 | { |
---|
2228 | [fPrefsController checkUpdate]; |
---|
2229 | } |
---|
2230 | |
---|
2231 | - (void) prepareForUpdate: (NSNotification *) notification |
---|
2232 | { |
---|
2233 | fUpdateInProgress = YES; |
---|
2234 | } |
---|
2235 | |
---|
2236 | - (NSDictionary *) registrationDictionaryForGrowl |
---|
2237 | { |
---|
2238 | NSArray * notifications = [NSArray arrayWithObjects: GROWL_DOWNLOAD_COMPLETE, |
---|
2239 | GROWL_SEEDING_COMPLETE, GROWL_AUTO_ADD, nil]; |
---|
2240 | return [NSDictionary dictionaryWithObjectsAndKeys: notifications, GROWL_NOTIFICATIONS_ALL, |
---|
2241 | notifications, GROWL_NOTIFICATIONS_DEFAULT, nil]; |
---|
2242 | } |
---|
2243 | |
---|
2244 | @end |
---|