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