1 /******************************************************************************
2 * $Id: Controller.m 12556 2011-07-18 00:48:00Z livings124 $
4 * Copyright (c) 2005-2011 Transmission authors and contributors
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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 *****************************************************************************/
25 #import <IOKit/IOMessage.h>
26 #import <IOKit/pwr_mgt/IOPMLib.h>
27 #import <Carbon/Carbon.h>
29 #import "Controller.h"
31 #import "TorrentGroup.h"
32 #import "TorrentTableView.h"
33 #import "CreatorWindowController.h"
34 #import "StatsWindowController.h"
35 #import "InfoWindowController.h"
36 #import "PrefsController.h"
37 #import "GroupsController.h"
38 #import "AboutWindowController.h"
39 #import "URLSheetWindowController.h"
40 #import "AddWindowController.h"
41 #import "AddMagnetWindowController.h"
42 #import "MessageWindowController.h"
43 #import "ButtonToolbarItem.h"
44 #import "GroupToolbarItem.h"
45 #import "ToolbarSegmentedCell.h"
46 #import "BlocklistDownloader.h"
47 #import "StatusBarController.h"
48 #import "FilterBarController.h"
49 #import "BonjourController.h"
51 #import "DragOverlayWindow.h"
52 #import "NSApplicationAdditions.h"
53 #import "NSStringAdditions.h"
54 #import "ExpandedPathToPathTransformer.h"
55 #import "ExpandedPathToIconTransformer.h"
57 #import "transmission.h"
62 #import <Sparkle/Sparkle.h>
64 #define TOOLBAR_CREATE @"Toolbar Create"
65 #define TOOLBAR_OPEN_FILE @"Toolbar Open"
66 #define TOOLBAR_OPEN_WEB @"Toolbar Open Web"
67 #define TOOLBAR_REMOVE @"Toolbar Remove"
68 #define TOOLBAR_INFO @"Toolbar Info"
69 #define TOOLBAR_PAUSE_ALL @"Toolbar Pause All"
70 #define TOOLBAR_RESUME_ALL @"Toolbar Resume All"
71 #define TOOLBAR_PAUSE_RESUME_ALL @"Toolbar Pause / Resume All"
72 #define TOOLBAR_PAUSE_SELECTED @"Toolbar Pause Selected"
73 #define TOOLBAR_RESUME_SELECTED @"Toolbar Resume Selected"
74 #define TOOLBAR_PAUSE_RESUME_SELECTED @"Toolbar Pause / Resume Selected"
75 #define TOOLBAR_FILTER @"Toolbar Toggle Filter"
76 #define TOOLBAR_QUICKLOOK @"Toolbar QuickLook"
80 TOOLBAR_PAUSE_TAG = 0,
81 TOOLBAR_RESUME_TAG = 1
84 #define SORT_DATE @"Date"
85 #define SORT_NAME @"Name"
86 #define SORT_STATE @"State"
87 #define SORT_PROGRESS @"Progress"
88 #define SORT_TRACKER @"Tracker"
89 #define SORT_ORDER @"Order"
90 #define SORT_ACTIVITY @"Activity"
91 #define SORT_SIZE @"Size"
98 SORT_PROGRESS_TAG = 3,
100 SORT_TRACKER_TAG = 5,
101 SORT_ACTIVITY_TAG = 6,
111 #define GROWL_DOWNLOAD_COMPLETE @"Download Complete"
112 #define GROWL_SEEDING_COMPLETE @"Seeding Complete"
113 #define GROWL_AUTO_ADD @"Torrent Auto Added"
114 #define GROWL_AUTO_SPEED_LIMIT @"Speed Limit Auto Changed"
116 #define TORRENT_TABLE_VIEW_DATA_TYPE @"TorrentTableViewDataType"
118 #define ROW_HEIGHT_REGULAR 62.0
119 #define ROW_HEIGHT_SMALL 22.0
120 #define WINDOW_REGULAR_WIDTH 468.0
122 #define STATUS_BAR_HEIGHT 21.0
123 #define FILTER_BAR_HEIGHT 23.0
125 #define UPDATE_UI_SECONDS 1.0
127 #define DOCK_SEEDING_TAG 101
128 #define DOCK_DOWNLOADING_TAG 102
130 #define TRANSFER_PLIST @"/Library/Application Support/Transmission/Transfers.plist"
132 #define WEBSITE_URL @"http://www.transmissionbt.com/"
133 #define FORUM_URL @"http://forum.transmissionbt.com/"
134 #define TRAC_URL @"http://trac.transmissionbt.com/"
135 #define DONATE_URL @"http://www.transmissionbt.com/donate.php"
137 #define DONATE_NAG_TIME (60 * 60 * 24 * 7)
139 static void altSpeedToggledCallback(tr_session * handle UNUSED, bool active, bool byUser, void * controller)
141 NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: [[NSNumber alloc] initWithBool: active], @"Active",
142 [[NSNumber alloc] initWithBool: byUser], @"ByUser", nil];
143 [(Controller *)controller performSelectorOnMainThread: @selector(altSpeedToggledCallbackIsLimited:)
144 withObject: dict waitUntilDone: NO];
147 static tr_rpc_callback_status rpcCallback(tr_session * handle UNUSED, tr_rpc_callback_type type, struct tr_torrent * torrentStruct,
150 [(Controller *)controller rpcCallback: type forTorrentStruct: torrentStruct];
151 return TR_RPC_NOREMOVE; //we'll do the remove manually
154 static void sleepCallback(void * controller, io_service_t y, natural_t messageType, void * messageArgument)
156 [(Controller *)controller sleepCallback: messageType argument: messageArgument];
159 @implementation Controller
163 //make sure another Transmission.app isn't running already
164 BOOL othersRunning = NO;
166 if ([NSApp isOnSnowLeopardOrBetter])
168 NSArray * apps = [NSRunningApplicationSL runningApplicationsWithBundleIdentifier: [[NSBundle mainBundle] bundleIdentifier]];
169 othersRunning = [apps count] > 1;
173 NSString * bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
174 const int processIdentifier = [[NSProcessInfo processInfo] processIdentifier];
176 for (NSDictionary * dic in [[NSWorkspace sharedWorkspace] launchedApplications])
178 if ([[dic objectForKey: @"NSApplicationBundleIdentifier"] isEqualToString: bundleIdentifier]
179 && [[dic objectForKey: @"NSApplicationProcessIdentifier"] intValue] != processIdentifier)
186 NSAlert * alert = [[NSAlert alloc] init];
187 [alert addButtonWithTitle: NSLocalizedString(@"Quit", "Transmission already running alert -> button")];
188 [alert setMessageText: NSLocalizedString(@"Transmission is already running.",
189 "Transmission already running alert -> title")];
190 [alert setInformativeText: NSLocalizedString(@"There is already a copy of Transmission running. "
191 "This copy cannot be opened until that instance is quit.", "Transmission already running alert -> message")];
192 [alert setAlertStyle: NSCriticalAlertStyle];
197 //kill ourselves right away
201 [[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithContentsOfFile:
202 [[NSBundle mainBundle] pathForResource: @"Defaults" ofType: @"plist"]]];
204 //set custom value transformers
205 ExpandedPathToPathTransformer * pathTransformer = [[[ExpandedPathToPathTransformer alloc] init] autorelease];
206 [NSValueTransformer setValueTransformer: pathTransformer forName: @"ExpandedPathToPathTransformer"];
208 ExpandedPathToIconTransformer * iconTransformer = [[[ExpandedPathToIconTransformer alloc] init] autorelease];
209 [NSValueTransformer setValueTransformer: iconTransformer forName: @"ExpandedPathToIconTransformer"];
212 if ([[NSUserDefaults standardUserDefaults] boolForKey: @"WarningLegal"])
214 NSAlert * alert = [[NSAlert alloc] init];
215 [alert addButtonWithTitle: NSLocalizedString(@"I Accept", "Legal alert -> button")];
216 [alert addButtonWithTitle: NSLocalizedString(@"Quit", "Legal alert -> button")];
217 [alert setMessageText: NSLocalizedString(@"Welcome to Transmission", "Legal alert -> title")];
218 [alert setInformativeText: NSLocalizedString(@"Transmission is a file-sharing program."
219 " When you run a torrent, its data will be made available to others by means of upload."
220 " You and you alone are fully responsible for exercising proper judgement and abiding by your local laws.",
221 "Legal alert -> message")];
222 [alert setAlertStyle: NSInformationalAlertStyle];
224 if ([alert runModal] == NSAlertSecondButtonReturn)
228 [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningLegal"];
234 if ((self = [super init]))
236 fDefaults = [NSUserDefaults standardUserDefaults];
238 //checks for old version speeds of -1
239 if ([fDefaults integerForKey: @"UploadLimit"] < 0)
241 [fDefaults removeObjectForKey: @"UploadLimit"];
242 [fDefaults setBool: NO forKey: @"CheckUpload"];
244 if ([fDefaults integerForKey: @"DownloadLimit"] < 0)
246 [fDefaults removeObjectForKey: @"DownloadLimit"];
247 [fDefaults setBool: NO forKey: @"CheckDownload"];
251 tr_bencInitDict(&settings, 41);
252 tr_sessionGetDefaultSettings(&settings);
254 const BOOL usesSpeedLimitSched = [fDefaults boolForKey: @"SpeedLimitAuto"];
255 if (!usesSpeedLimitSched)
256 tr_bencDictAddBool(&settings, TR_PREFS_KEY_ALT_SPEED_ENABLED, [fDefaults boolForKey: @"SpeedLimit"]);
258 tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_UP_KBps, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
259 tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_DOWN_KBps, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
261 tr_bencDictAddBool(&settings, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, [fDefaults boolForKey: @"SpeedLimitAuto"]);
262 tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, [PrefsController dateToTimeSum:
263 [fDefaults objectForKey: @"SpeedLimitAutoOnDate"]]);
264 tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_TIME_END, [PrefsController dateToTimeSum:
265 [fDefaults objectForKey: @"SpeedLimitAutoOffDate"]]);
266 tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, [fDefaults integerForKey: @"SpeedLimitAutoDay"]);
268 tr_bencDictAddInt(&settings, TR_PREFS_KEY_DSPEED_KBps, [fDefaults integerForKey: @"DownloadLimit"]);
269 tr_bencDictAddBool(&settings, TR_PREFS_KEY_DSPEED_ENABLED, [fDefaults boolForKey: @"CheckDownload"]);
270 tr_bencDictAddInt(&settings, TR_PREFS_KEY_USPEED_KBps, [fDefaults integerForKey: @"UploadLimit"]);
271 tr_bencDictAddBool(&settings, TR_PREFS_KEY_USPEED_ENABLED, [fDefaults boolForKey: @"CheckUpload"]);
274 if ([fDefaults objectForKey: @"BindAddressIPv4"])
275 tr_bencDictAddStr(&settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4, [[fDefaults stringForKey: @"BindAddressIPv4"] UTF8String]);
276 if ([fDefaults objectForKey: @"BindAddressIPv6"])
277 tr_bencDictAddStr(&settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6, [[fDefaults stringForKey: @"BindAddressIPv6"] UTF8String]);
279 tr_bencDictAddBool(&settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, [fDefaults boolForKey: @"BlocklistNew"]);
280 if ([fDefaults objectForKey: @"BlocklistURL"])
281 tr_bencDictAddStr(&settings, TR_PREFS_KEY_BLOCKLIST_URL, [[fDefaults stringForKey: @"BlocklistURL"] UTF8String]);
282 tr_bencDictAddBool(&settings, TR_PREFS_KEY_DHT_ENABLED, [fDefaults boolForKey: @"DHTGlobal"]);
283 tr_bencDictAddStr(&settings, TR_PREFS_KEY_DOWNLOAD_DIR, [[[fDefaults stringForKey: @"DownloadFolder"]
284 stringByExpandingTildeInPath] UTF8String]);
285 tr_bencDictAddInt(&settings, TR_PREFS_KEY_IDLE_LIMIT, [fDefaults integerForKey: @"IdleLimitMinutes"]);
286 tr_bencDictAddBool(&settings, TR_PREFS_KEY_IDLE_LIMIT_ENABLED, [fDefaults boolForKey: @"IdleLimitCheck"]);
287 tr_bencDictAddStr(&settings, TR_PREFS_KEY_INCOMPLETE_DIR, [[[fDefaults stringForKey: @"IncompleteDownloadFolder"]
288 stringByExpandingTildeInPath] UTF8String]);
289 tr_bencDictAddBool(&settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, [fDefaults boolForKey: @"UseIncompleteDownloadFolder"]);
290 tr_bencDictAddBool(&settings, TR_PREFS_KEY_LPD_ENABLED, [fDefaults boolForKey: @"LocalPeerDiscoveryGlobal"]);
291 tr_bencDictAddInt(&settings, TR_PREFS_KEY_MSGLEVEL, TR_MSG_DBG);
292 tr_bencDictAddInt(&settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, [fDefaults integerForKey: @"PeersTotal"]);
293 tr_bencDictAddInt(&settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, [fDefaults integerForKey: @"PeersTorrent"]);
295 const BOOL randomPort = [fDefaults boolForKey: @"RandomPort"];
296 tr_bencDictAddBool(&settings, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, randomPort);
298 tr_bencDictAddInt(&settings, TR_PREFS_KEY_PEER_PORT, [fDefaults integerForKey: @"BindPort"]);
301 if ([fDefaults objectForKey: @"PeerSocketTOS"])
302 tr_bencDictAddStr(&settings, TR_PREFS_KEY_PEER_SOCKET_TOS, [[fDefaults stringForKey: @"PeerSocketTOS"] UTF8String]);
304 tr_bencDictAddBool(&settings, TR_PREFS_KEY_PEX_ENABLED, [fDefaults boolForKey: @"PEXGlobal"]);
305 tr_bencDictAddBool(&settings, TR_PREFS_KEY_PORT_FORWARDING, [fDefaults boolForKey: @"NatTraversal"]);
306 tr_bencDictAddReal(&settings, TR_PREFS_KEY_RATIO, [fDefaults floatForKey: @"RatioLimit"]);
307 tr_bencDictAddBool(&settings, TR_PREFS_KEY_RATIO_ENABLED, [fDefaults boolForKey: @"RatioCheck"]);
308 tr_bencDictAddBool(&settings, TR_PREFS_KEY_RENAME_PARTIAL_FILES, [fDefaults boolForKey: @"RenamePartialFiles"]);
309 tr_bencDictAddBool(&settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, [fDefaults boolForKey: @"RPCAuthorize"]);
310 tr_bencDictAddBool(&settings, TR_PREFS_KEY_RPC_ENABLED, [fDefaults boolForKey: @"RPC"]);
311 tr_bencDictAddInt(&settings, TR_PREFS_KEY_RPC_PORT, [fDefaults integerForKey: @"RPCPort"]);
312 tr_bencDictAddStr(&settings, TR_PREFS_KEY_RPC_USERNAME, [[fDefaults stringForKey: @"RPCUsername"] UTF8String]);
313 tr_bencDictAddBool(&settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, [fDefaults boolForKey: @"RPCUseWhitelist"]);
314 tr_bencDictAddBool(&settings, TR_PREFS_KEY_START, [fDefaults boolForKey: @"AutoStartDownload"]);
315 tr_bencDictAddBool(&settings, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_ENABLED, [fDefaults boolForKey: @"DoneScriptEnabled"]);
316 tr_bencDictAddStr(&settings, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_FILENAME, [[fDefaults stringForKey: @"DoneScriptPath"] UTF8String]);
317 tr_bencDictAddBool(&settings, TR_PREFS_KEY_UTP_ENABLED, [fDefaults boolForKey: @"UTPGlobal"]);
319 tr_formatter_size_init([NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024,
320 [NSLocalizedString(@"KB", "File size - kilobytes") UTF8String],
321 [NSLocalizedString(@"MB", "File size - megabytes") UTF8String],
322 [NSLocalizedString(@"GB", "File size - gigabytes") UTF8String],
323 [NSLocalizedString(@"TB", "File size - terabytes") UTF8String]);
325 tr_formatter_speed_init([NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024,
326 [NSLocalizedString(@"KB/s", "Transfer speed (kilobytes per second)") UTF8String],
327 [NSLocalizedString(@"MB/s", "Transfer speed (megabytes per second)") UTF8String],
328 [NSLocalizedString(@"GB/s", "Transfer speed (gigabytes per second)") UTF8String],
329 [NSLocalizedString(@"TB/s", "Transfer speed (terabytes per second)") UTF8String]); //why not?
331 tr_formatter_mem_init(1024, [NSLocalizedString(@"KB", "Memory size - kilobytes") UTF8String],
332 [NSLocalizedString(@"MB", "Memory size - megabytes") UTF8String],
333 [NSLocalizedString(@"GB", "Memory size - gigabytes") UTF8String],
334 [NSLocalizedString(@"TB", "Memory size - terabytes") UTF8String]);
336 const char * configDir = tr_getDefaultConfigDir("Transmission");
337 fLib = tr_sessionInit("macosx", configDir, YES, &settings);
338 tr_bencFree(&settings);
340 [NSApp setDelegate: self];
342 //register for magnet URLs (has to be in init)
343 [[NSAppleEventManager sharedAppleEventManager] setEventHandler: self andSelector: @selector(handleOpenContentsEvent:replyEvent:)
344 forEventClass: kInternetEventClass andEventID: kAEGetURL];
346 fTorrents = [[NSMutableArray alloc] init];
347 fDisplayedTorrents = [[NSMutableArray alloc] init];
349 fInfoController = [[InfoWindowController alloc] init];
351 [PrefsController setHandle: fLib];
352 fPrefsController = [[PrefsController alloc] init];
357 tr_sessionSetAltSpeedFunc(fLib, altSpeedToggledCallback, self);
358 if (usesSpeedLimitSched)
359 [fDefaults setBool: tr_sessionUsesAltSpeed(fLib) forKey: @"SpeedLimit"];
361 tr_sessionSetRPCCallback(fLib, rpcCallback, self);
363 [GrowlApplicationBridge setGrowlDelegate: self];
365 [[UKKQueue sharedFileWatcher] setDelegate: self];
367 [[SUUpdater sharedUpdater] setDelegate: self];
370 fPauseOnLaunch = (GetCurrentKeyModifiers() & (optionKey | rightOptionKey)) != 0;
375 - (void) awakeFromNib
377 NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"TRMainToolbar"];
378 [toolbar setDelegate: self];
379 [toolbar setAllowsUserCustomization: YES];
380 [toolbar setAutosavesConfiguration: YES];
381 [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
382 [fWindow setToolbar: toolbar];
385 [fWindow setDelegate: self]; //do manually to avoid placement issue
387 [fWindow makeFirstResponder: fTableView];
388 [fWindow setExcludedFromWindowsMenu: YES];
391 const BOOL small = [fDefaults boolForKey: @"SmallView"];
393 [fTableView setRowHeight: ROW_HEIGHT_SMALL];
394 [fTableView setUsesAlternatingRowBackgroundColors: !small];
396 [fWindow setContentBorderThickness: NSMinY([[fTableView enclosingScrollView] frame]) forEdge: NSMinYEdge];
397 [fWindow setMovableByWindowBackground: YES];
399 [[fTotalTorrentsField cell] setBackgroundStyle: NSBackgroundStyleRaised];
402 [self showFilterBar: [fDefaults boolForKey: @"FilterBar"] animate: NO];
405 [self showStatusBar: [fDefaults boolForKey: @"StatusBar"] animate: NO];
407 [fActionButton setToolTip: NSLocalizedString(@"Shortcuts for changing global settings.",
408 "Main window -> 1st bottom left button (action) tooltip")];
409 [fSpeedLimitButton setToolTip: NSLocalizedString(@"Speed Limit overrides the total bandwidth limits with its own limits.",
410 "Main window -> 2nd bottom left button (turtle) tooltip")];
411 [fClearCompletedButton setToolTip: NSLocalizedString(@"Remove all transfers that have completed seeding.",
412 "Main window -> 3rd bottom left button (remove all) tooltip")];
414 [fTableView registerForDraggedTypes: [NSArray arrayWithObject: TORRENT_TABLE_VIEW_DATA_TYPE]];
415 [fWindow registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, NSURLPboardType, nil]];
417 //you would think this would be called later in this method from updateUI, but it's not reached in awakeFromNib
418 //this must be called after showStatusBar:
419 [fStatusBar updateWithDownload: 0.0 upload: 0.0];
421 //this should also be after the rest of the setup
422 [self updateForAutoSize];
424 //register for sleep notifications
425 IONotificationPortRef notify;
426 io_object_t iterator;
427 if ((fRootPort = IORegisterForSystemPower(self, & notify, sleepCallback, &iterator)))
428 CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), kCFRunLoopCommonModes);
430 NSLog(@"Could not IORegisterForSystemPower");
432 //load previous transfers
433 NSArray * history = [NSArray arrayWithContentsOfFile: [NSHomeDirectory() stringByAppendingPathComponent: TRANSFER_PLIST]];
437 //old version saved transfer info in prefs file
438 if ((history = [fDefaults arrayForKey: @"History"]))
439 [fDefaults removeObjectForKey: @"History"];
444 for (NSDictionary * historyItem in history)
447 if ((torrent = [[Torrent alloc] initWithHistory: historyItem lib: fLib forcePause: fPauseOnLaunch]))
449 [fTorrents addObject: torrent];
455 fBadger = [[Badger alloc] initWithLib: fLib];
457 //observe notifications
458 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
460 [nc addObserver: self selector: @selector(updateUI)
461 name: @"UpdateUI" object: nil];
463 [nc addObserver: self selector: @selector(torrentFinishedDownloading:)
464 name: @"TorrentFinishedDownloading" object: nil];
466 [nc addObserver: self selector: @selector(torrentRestartedDownloading:)
467 name: @"TorrentRestartedDownloading" object: nil];
469 [nc addObserver: self selector: @selector(torrentFinishedSeeding:)
470 name: @"TorrentFinishedSeeding" object: nil];
472 //avoids need of setting delegate
473 [nc addObserver: self selector: @selector(torrentTableViewSelectionDidChange:)
474 name: NSOutlineViewSelectionDidChangeNotification object: fTableView];
476 [nc addObserver: self selector: @selector(changeAutoImport)
477 name: @"AutoImportSettingChange" object: nil];
479 [nc addObserver: self selector: @selector(updateForAutoSize)
480 name: @"AutoSizeSettingChange" object: nil];
482 [nc addObserver: self selector: @selector(updateForExpandCollape)
483 name: @"OutlineExpandCollapse" object: nil];
485 [nc addObserver: fWindow selector: @selector(makeKeyWindow)
486 name: @"MakeWindowKey" object: nil];
488 [nc addObserver: self selector: @selector(updateTorrentsInQueue)
489 name: @"UpdateQueue" object: nil];
491 [nc addObserver: self selector: @selector(applyFilter)
492 name: @"ApplyFilter" object: nil];
494 //open newly created torrent file
495 [nc addObserver: self selector: @selector(beginCreateFile:)
496 name: @"BeginCreateTorrentFile" object: nil];
498 //open newly created torrent file
499 [nc addObserver: self selector: @selector(openCreatedFile:)
500 name: @"OpenCreatedTorrentFile" object: nil];
502 //timer to update the interface every second
504 fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_UI_SECONDS target: self
505 selector: @selector(updateUI) userInfo: nil repeats: YES];
506 [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSModalPanelRunLoopMode];
507 [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSEventTrackingRunLoopMode];
511 [fWindow makeKeyAndOrderFront: nil];
513 if ([fDefaults boolForKey: @"InfoVisible"])
514 [self showInfo: nil];
517 - (void) applicationDidFinishLaunching: (NSNotification *) notification
519 [NSApp setServicesProvider: self];
521 //register for dock icon drags (has to be in applicationDidFinishLaunching: to work)
522 [[NSAppleEventManager sharedAppleEventManager] setEventHandler: self andSelector: @selector(handleOpenContentsEvent:replyEvent:)
523 forEventClass: kCoreEventClass andEventID: kAEOpenContents];
526 [self checkAutoImportDirectory];
528 //registering the Web UI to Bonjour
529 if ([fDefaults boolForKey: @"RPC"] && [fDefaults boolForKey: @"RPCWebDiscovery"])
530 [[BonjourController defaultController] startWithPort: [fDefaults integerForKey: @"RPCPort"]];
532 //shamelessly ask for donations
533 if ([fDefaults boolForKey: @"WarningDonate"])
535 tr_session_stats stats;
536 tr_sessionGetCumulativeStats(fLib, &stats);
537 const BOOL firstLaunch = stats.sessionCount <= 1;
539 NSDate * lastDonateDate = [fDefaults objectForKey: @"DonateAskDate"];
540 const BOOL timePassed = !lastDonateDate || (-1 * [lastDonateDate timeIntervalSinceNow]) >= DONATE_NAG_TIME;
542 if (!firstLaunch && timePassed)
544 [fDefaults setObject: [NSDate date] forKey: @"DonateAskDate"];
546 NSAlert * alert = [[NSAlert alloc] init];
547 [alert setMessageText: NSLocalizedString(@"Support open-source indie software", "Donation beg -> title")];
549 NSString * donateMessage = [NSString stringWithFormat: @"%@\n\n%@",
550 NSLocalizedString(@"Transmission is a full-featured torrent application."
551 " A lot of time and effort have gone into development, coding, and refinement."
552 " If you enjoy using it, please consider showing your love with a donation.", "Donation beg -> message"),
553 NSLocalizedString(@"Donate or not, there will be no difference to your torrenting experience.", "Donation beg -> message")];
555 [alert setInformativeText: donateMessage];
556 [alert setAlertStyle: NSInformationalAlertStyle];
558 [alert addButtonWithTitle: [NSLocalizedString(@"Donate", "Donation beg -> button") stringByAppendingEllipsis]];
559 NSButton * noDonateButton = [alert addButtonWithTitle: NSLocalizedString(@"Nope", "Donation beg -> button")];
560 [noDonateButton setKeyEquivalent: @"\e"]; //escape key
562 const BOOL allowNeverAgain = lastDonateDate != nil; //hide the "don't show again" check the first time - give them time to try the app
563 [alert setShowsSuppressionButton: allowNeverAgain];
565 [[alert suppressionButton] setTitle: NSLocalizedString(@"Don't bug me about this ever again.", "Donation beg -> button")];
567 const NSInteger donateResult = [alert runModal];
568 if (donateResult == NSAlertFirstButtonReturn)
569 [self linkDonate: self];
572 [fDefaults setBool: ([[alert suppressionButton] state] != NSOnState) forKey: @"WarningDonate"];
579 - (BOOL) applicationShouldHandleReopen: (NSApplication *) app hasVisibleWindows: (BOOL) visibleWindows
581 NSWindow * mainWindow = [NSApp mainWindow];
582 if (!mainWindow || ![mainWindow isVisible])
583 [fWindow makeKeyAndOrderFront: nil];
588 - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) sender
590 if (!fQuitRequested && [fDefaults boolForKey: @"CheckQuit"])
592 NSInteger active = 0, downloading = 0;
593 for (Torrent * torrent in fTorrents)
594 if ([torrent isActive] && ![torrent isStalled])
597 if (![torrent allDownloaded])
601 if ([fDefaults boolForKey: @"CheckQuitDownloading"] ? downloading > 0 : active > 0)
603 NSString * message = active == 1
604 ? NSLocalizedString(@"There is an active transfer that will be paused on quit."
605 " The transfer will automatically resume on the next launch.", "Confirm Quit panel -> message")
606 : [NSString stringWithFormat: NSLocalizedString(@"There are %d active transfers that will be paused on quit."
607 " The transfers will automatically resume on the next launch.", "Confirm Quit panel -> message"), active];
609 NSBeginAlertSheet(NSLocalizedString(@"Are you sure you want to quit?", "Confirm Quit panel -> title"),
610 NSLocalizedString(@"Quit", "Confirm Quit panel -> button"),
611 NSLocalizedString(@"Cancel", "Confirm Quit panel -> button"), nil, fWindow, self,
612 @selector(quitSheetDidEnd:returnCode:contextInfo:), nil, nil, message);
613 return NSTerminateLater;
617 return NSTerminateNow;
620 - (void) quitSheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
622 [NSApp replyToApplicationShouldTerminate: returnCode == NSAlertDefaultReturn];
625 - (void) applicationWillTerminate: (NSNotification *) notification
629 //stop the Bonjour service
630 [[BonjourController defaultController] stop];
632 //stop blocklist download
633 if ([BlocklistDownloader isRunning])
634 [[BlocklistDownloader downloader] cancelDownload];
636 //stop timers and notification checking
637 [[NSNotificationCenter defaultCenter] removeObserver: self];
641 if (fAutoImportTimer)
643 if ([fAutoImportTimer isValid])
644 [fAutoImportTimer invalidate];
645 [fAutoImportTimer release];
648 [fBadger setQuitting];
650 //remove all torrent downloads
651 if (fPendingTorrentDownloads)
653 for (NSDictionary * downloadDict in fPendingTorrentDownloads)
655 NSURLDownload * download = [downloadDict objectForKey: @"Download"];
659 [fPendingTorrentDownloads release];
662 //remember window states and close all windows
663 [fDefaults setBool: [[fInfoController window] isVisible] forKey: @"InfoVisible"];
665 const BOOL quickLookOpen = [NSApp isOnSnowLeopardOrBetter] && [QLPreviewPanelSL sharedPreviewPanelExists]
666 && [[QLPreviewPanelSL sharedPreviewPanel] isVisible];
668 [[QLPreviewPanelSL sharedPreviewPanel] updateController];
670 for (NSWindow * window in [NSApp windows])
673 [self showStatusBar: NO animate: NO];
674 [self showFilterBar: NO animate: NO];
677 [self updateTorrentHistory];
678 [fTableView saveCollapsedGroups];
680 //remaining calls the same as dealloc
681 [fInfoController release];
682 [fMessageController release];
683 [fPrefsController release];
685 [fStatusBar release];
686 [fFilterBar release];
689 [fDisplayedTorrents release];
691 [fOverlayWindow release];
694 [fAutoImportedNames release];
696 [fPreviewPanel release];
699 tr_sessionClose(fLib);
702 - (void) handleOpenContentsEvent: (NSAppleEventDescriptor *) event replyEvent: (NSAppleEventDescriptor *) replyEvent
704 NSString * urlString = nil;
706 NSAppleEventDescriptor * directObject = [event paramDescriptorForKeyword: keyDirectObject];
707 if ([directObject descriptorType] == typeAEList)
709 for (NSInteger i = 1; i <= [directObject numberOfItems]; i++)
710 if ((urlString = [[directObject descriptorAtIndex: i] stringValue]))
714 urlString = [directObject stringValue];
717 [self openURL: urlString];
720 - (void) download: (NSURLDownload *) download decideDestinationWithSuggestedFilename: (NSString *) suggestedName
722 if ([[suggestedName pathExtension] caseInsensitiveCompare: @"torrent"] != NSOrderedSame)
726 NSRunAlertPanel(NSLocalizedString(@"Torrent download failed", "Download not a torrent -> title"),
727 [NSString stringWithFormat: NSLocalizedString(@"It appears that the file \"%@\" from %@ is not a torrent file.",
728 "Download not a torrent -> message"), suggestedName,
729 [[[[download request] URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]],
730 NSLocalizedString(@"OK", "Download not a torrent -> button"), nil, nil);
735 [download setDestination: [NSTemporaryDirectory() stringByAppendingPathComponent: [suggestedName lastPathComponent]]
739 -(void) download: (NSURLDownload *) download didCreateDestination: (NSString *) path
741 if (!fPendingTorrentDownloads)
742 fPendingTorrentDownloads = [[NSMutableDictionary alloc] init];
744 [fPendingTorrentDownloads setObject: [NSDictionary dictionaryWithObjectsAndKeys:
745 path, @"Path", download, @"Download", nil] forKey: [[download request] URL]];
748 - (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
750 NSRunAlertPanel(NSLocalizedString(@"Torrent download failed", "Torrent download error -> title"),
751 [NSString stringWithFormat: NSLocalizedString(@"The torrent could not be downloaded from %@: %@.",
752 "Torrent download failed -> message"),
753 [[[[download request] URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding],
754 [error localizedDescription]], NSLocalizedString(@"OK", "Torrent download failed -> button"), nil, nil);
756 [fPendingTorrentDownloads removeObjectForKey: [[download request] URL]];
757 if ([fPendingTorrentDownloads count] == 0)
759 [fPendingTorrentDownloads release];
760 fPendingTorrentDownloads = nil;
766 - (void) downloadDidFinish: (NSURLDownload *) download
768 NSString * path = [[fPendingTorrentDownloads objectForKey: [[download request] URL]] objectForKey: @"Path"];
770 [self openFiles: [NSArray arrayWithObject: path] addType: ADD_URL forcePath: nil];
772 //delete the torrent file after opening
773 [[NSFileManager defaultManager] removeItemAtPath: path error: NULL];
775 [fPendingTorrentDownloads removeObjectForKey: [[download request] URL]];
776 if ([fPendingTorrentDownloads count] == 0)
778 [fPendingTorrentDownloads release];
779 fPendingTorrentDownloads = nil;
785 - (void) application: (NSApplication *) app openFiles: (NSArray *) filenames
787 [self openFiles: filenames addType: ADD_MANUAL forcePath: nil];
790 - (void) openFiles: (NSArray *) filenames addType: (addType) type forcePath: (NSString *) path
792 BOOL deleteTorrentFile, canToggleDelete = NO;
796 deleteTorrentFile = NO;
799 deleteTorrentFile = YES;
802 deleteTorrentFile = [fDefaults boolForKey: @"DeleteOriginalTorrent"];
803 canToggleDelete = YES;
806 for (NSString * torrentPath in filenames)
808 //ensure torrent doesn't already exist
809 tr_ctor * ctor = tr_ctorNew(fLib);
810 tr_ctorSetMetainfoFromFile(ctor, [torrentPath UTF8String]);
813 const tr_parse_result result = tr_torrentParse(ctor, &info);
816 if (result != TR_PARSE_OK)
818 if (result == TR_PARSE_DUPLICATE)
819 [self duplicateOpenAlert: [NSString stringWithUTF8String: info.name]];
820 else if (result == TR_PARSE_ERR)
822 if (type != ADD_AUTO)
823 [self invalidOpenAlert: [torrentPath lastPathComponent]];
826 NSAssert2(NO, @"Unknown error code (%d) when attempting to open \"%@\"", result, torrentPath);
828 tr_metainfoFree(&info);
832 //determine download location
834 BOOL lockDestination = NO; //don't override the location with a group location if it has a hardcoded path
837 location = [path stringByExpandingTildeInPath];
838 lockDestination = YES;
840 else if ([fDefaults boolForKey: @"DownloadLocationConstant"])
841 location = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
842 else if (type != ADD_URL)
843 location = [torrentPath stringByDeletingLastPathComponent];
847 //determine to show the options window
848 const BOOL showWindow = type == ADD_SHOW_OPTIONS || ([fDefaults boolForKey: @"DownloadAsk"]
849 && (info.isMultifile || ![fDefaults boolForKey: @"DownloadAskMulti"])
850 && (type != ADD_AUTO || ![fDefaults boolForKey: @"DownloadAskManual"]));
851 tr_metainfoFree(&info);
854 if (!(torrent = [[Torrent alloc] initWithPath: torrentPath location: location
855 deleteTorrentFile: showWindow ? NO : deleteTorrentFile lib: fLib]))
858 //change the location if the group calls for it (this has to wait until after the torrent is created)
859 if (!lockDestination && [[GroupsController groups] usesCustomDownloadLocationForIndex: [torrent groupValue]])
861 location = [[GroupsController groups] customDownloadLocationForIndex: [torrent groupValue]];
862 [torrent changeDownloadFolderBeforeUsing: location];
865 //verify the data right away if it was newly created
866 if (type == ADD_CREATED)
867 [torrent resetCache];
869 //add it to the "File -> Open Recent" menu
870 [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: [NSURL fileURLWithPath: torrentPath]];
872 //show the add window or add directly
873 if (showWindow || !location)
875 AddWindowController * addController = [[AddWindowController alloc] initWithTorrent: torrent destination: location
876 lockDestination: lockDestination controller: self torrentFile: torrentPath
877 deleteTorrent: deleteTorrentFile canToggleDelete: canToggleDelete];
878 [addController showWindow: self];
882 [torrent setWaitToStart: [fDefaults boolForKey: @"AutoStartDownload"]];
885 [fTorrents addObject: torrent];
890 [self updateTorrentsInQueue];
893 - (void) askOpenConfirmed: (AddWindowController *) addController add: (BOOL) add
895 Torrent * torrent = [addController torrent];
896 [addController release];
901 [fTorrents addObject: torrent];
904 [self updateTorrentsInQueue];
908 [torrent closeRemoveTorrent: NO];
913 - (void) openMagnet: (NSString *) address
915 tr_torrent * duplicateTorrent;
916 if ((duplicateTorrent = tr_torrentFindFromMagnetLink(fLib, [address UTF8String])))
918 const tr_info * info = tr_torrentInfo(duplicateTorrent);
919 NSString * name = (info != NULL && info->name != NULL) ? [NSString stringWithUTF8String: info->name] : nil;
920 [self duplicateOpenMagnetAlert: address transferName: name];
924 //determine download location
925 NSString * location = nil;
926 if ([fDefaults boolForKey: @"DownloadLocationConstant"])
927 location = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
930 if (!(torrent = [[Torrent alloc] initWithMagnetAddress: address location: location lib: fLib]))
932 [self invalidOpenMagnetAlert: address];
936 //change the location if the group calls for it (this has to wait until after the torrent is created)
937 if ([[GroupsController groups] usesCustomDownloadLocationForIndex: [torrent groupValue]])
939 location = [[GroupsController groups] customDownloadLocationForIndex: [torrent groupValue]];
940 [torrent changeDownloadFolderBeforeUsing: location];
943 if ([fDefaults boolForKey: @"MagnetOpenAsk"] || !location)
945 AddMagnetWindowController * addController = [[AddMagnetWindowController alloc] initWithTorrent: torrent destination: location
947 [addController showWindow: self];
951 [torrent setWaitToStart: [fDefaults boolForKey: @"AutoStartDownload"]];
954 [fTorrents addObject: torrent];
958 [self updateTorrentsInQueue];
961 - (void) askOpenMagnetConfirmed: (AddMagnetWindowController *) addController add: (BOOL) add
963 Torrent * torrent = [addController torrent];
964 [addController release];
969 [fTorrents addObject: torrent];
972 [self updateTorrentsInQueue];
976 [torrent closeRemoveTorrent: NO];
981 - (void) openCreatedFile: (NSNotification *) notification
983 NSDictionary * dict = [notification userInfo];
984 [self openFiles: [NSArray arrayWithObject: [dict objectForKey: @"File"]] addType: ADD_CREATED
985 forcePath: [dict objectForKey: @"Path"]];
989 - (void) openFilesWithDict: (NSDictionary *) dictionary
991 [self openFiles: [dictionary objectForKey: @"Filenames"] addType: [[dictionary objectForKey: @"AddType"] intValue] forcePath: nil];
993 [dictionary release];
996 //called on by applescript
997 - (void) open: (NSArray *) files
999 NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: files, @"Filenames",
1000 [NSNumber numberWithInt: ADD_MANUAL], @"AddType", nil];
1001 [self performSelectorOnMainThread: @selector(openFilesWithDict:) withObject: dict waitUntilDone: NO];
1004 - (void) openShowSheet: (id) sender
1006 NSOpenPanel * panel = [NSOpenPanel openPanel];
1008 [panel setAllowsMultipleSelection: YES];
1009 [panel setCanChooseFiles: YES];
1010 [panel setCanChooseDirectories: NO];
1012 [panel beginSheetForDirectory: nil file: nil types: [NSArray arrayWithObjects: @"org.bittorrent.torrent", @"torrent", nil]
1013 modalForWindow: fWindow modalDelegate: self didEndSelector: @selector(openSheetClosed:returnCode:contextInfo:)
1014 contextInfo: [NSNumber numberWithBool: sender == fOpenIgnoreDownloadFolder]];
1017 - (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (NSInteger) code contextInfo: (NSNumber *) useOptions
1019 if (code == NSOKButton)
1021 NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: [panel filenames], @"Filenames",
1022 [NSNumber numberWithInt: [useOptions boolValue] ? ADD_SHOW_OPTIONS : ADD_MANUAL], @"AddType", nil];
1023 [self performSelectorOnMainThread: @selector(openFilesWithDict:) withObject: dictionary waitUntilDone: NO];
1027 - (void) invalidOpenAlert: (NSString *) filename
1029 if (![fDefaults boolForKey: @"WarningInvalidOpen"])
1032 NSAlert * alert = [[NSAlert alloc] init];
1033 [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"\"%@\" is not a valid torrent file.",
1034 "Open invalid alert -> title"), filename]];
1035 [alert setInformativeText:
1036 NSLocalizedString(@"The torrent file cannot be opened because it contains invalid data.",
1037 "Open invalid alert -> message")];
1038 [alert setAlertStyle: NSWarningAlertStyle];
1039 [alert addButtonWithTitle: NSLocalizedString(@"OK", "Open invalid alert -> button")];
1042 if ([[alert suppressionButton] state] == NSOnState)
1043 [fDefaults setBool: NO forKey: @"WarningInvalidOpen"];
1047 - (void) invalidOpenMagnetAlert: (NSString *) address
1049 if (![fDefaults boolForKey: @"WarningInvalidOpen"])
1052 NSAlert * alert = [[NSAlert alloc] init];
1053 [alert setMessageText: NSLocalizedString(@"Adding magnetized transfer failed.", "Magnet link failed -> title")];
1054 [alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"There was an error when adding the magnet link \"%@\"."
1055 " The transfer will not occur.", "Magnet link failed -> message"), address]];
1056 [alert setAlertStyle: NSWarningAlertStyle];
1057 [alert addButtonWithTitle: NSLocalizedString(@"OK", "Magnet link failed -> button")];
1060 if ([[alert suppressionButton] state] == NSOnState)
1061 [fDefaults setBool: NO forKey: @"WarningInvalidOpen"];
1065 - (void) duplicateOpenAlert: (NSString *) name
1067 if (![fDefaults boolForKey: @"WarningDuplicate"])
1070 NSAlert * alert = [[NSAlert alloc] init];
1071 [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"A transfer of \"%@\" already exists.",
1072 "Open duplicate alert -> title"), name]];
1073 [alert setInformativeText:
1074 NSLocalizedString(@"The transfer cannot be added because it is a duplicate of an already existing transfer.",
1075 "Open duplicate alert -> message")];
1076 [alert setAlertStyle: NSWarningAlertStyle];
1077 [alert addButtonWithTitle: NSLocalizedString(@"OK", "Open duplicate alert -> button")];
1078 [alert setShowsSuppressionButton: YES];
1081 if ([[alert suppressionButton] state])
1082 [fDefaults setBool: NO forKey: @"WarningDuplicate"];
1086 - (void) duplicateOpenMagnetAlert: (NSString *) address transferName: (NSString *) name
1088 if (![fDefaults boolForKey: @"WarningDuplicate"])
1091 NSAlert * alert = [[NSAlert alloc] init];
1093 [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"A transfer of \"%@\" already exists.",
1094 "Open duplicate magnet alert -> title"), name]];
1096 [alert setMessageText: NSLocalizedString(@"Magnet link is a duplicate of an existing transfer.",
1097 "Open duplicate magnet alert -> title")];
1098 [alert setInformativeText: [NSString stringWithFormat:
1099 NSLocalizedString(@"The magnet link \"%@\" cannot be added because it is a duplicate of an already existing transfer.",
1100 "Open duplicate magnet alert -> message"), address]];
1101 [alert setAlertStyle: NSWarningAlertStyle];
1102 [alert addButtonWithTitle: NSLocalizedString(@"OK", "Open duplicate magnet alert -> button")];
1103 [alert setShowsSuppressionButton: YES];
1106 if ([[alert suppressionButton] state])
1107 [fDefaults setBool: NO forKey: @"WarningDuplicate"];
1111 - (void) openURL: (NSString *) urlString
1113 if ([urlString rangeOfString: @"magnet:" options: (NSAnchoredSearch | NSCaseInsensitiveSearch)].location != NSNotFound)
1114 [self openMagnet: urlString];
1117 if ([urlString rangeOfString: @"://"].location == NSNotFound)
1119 if ([urlString rangeOfString: @"."].location == NSNotFound)
1121 NSInteger beforeCom;
1122 if ((beforeCom = [urlString rangeOfString: @"/"].location) != NSNotFound)
1123 urlString = [NSString stringWithFormat: @"http://www.%@.com/%@",
1124 [urlString substringToIndex: beforeCom],
1125 [urlString substringFromIndex: beforeCom + 1]];
1127 urlString = [NSString stringWithFormat: @"http://www.%@.com/", urlString];
1130 urlString = [@"http://" stringByAppendingString: urlString];
1133 NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]
1134 cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 60];
1135 [[NSURLDownload alloc] initWithRequest: request delegate: self];
1139 - (void) openURLShowSheet: (id) sender
1141 [[[URLSheetWindowController alloc] initWithController: self] beginSheetForWindow: fWindow];
1144 - (void) urlSheetDidEnd: (URLSheetWindowController *) controller url: (NSString *) urlString returnCode: (NSInteger) returnCode
1146 if (returnCode == 1)
1147 [self performSelectorOnMainThread: @selector(openURL:) withObject: urlString waitUntilDone: NO];
1149 [controller release];
1152 - (void) createFile: (id) sender
1154 [CreatorWindowController createTorrentFile: fLib];
1157 - (void) resumeSelectedTorrents: (id) sender
1159 [self resumeTorrents: [fTableView selectedTorrents]];
1162 - (void) resumeAllTorrents: (id) sender
1164 NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [fTorrents count]];
1166 for (Torrent * torrent in fTorrents)
1167 if (![torrent isFinishedSeeding])
1168 [torrents addObject: torrent];
1170 [self resumeTorrents: torrents];
1173 - (void) resumeTorrents: (NSArray *) torrents
1175 for (Torrent * torrent in torrents)
1176 [torrent setWaitToStart: YES];
1178 [self updateTorrentsInQueue];
1181 - (void) resumeSelectedTorrentsNoWait: (id) sender
1183 [self resumeTorrentsNoWait: [fTableView selectedTorrents]];
1186 - (void) resumeWaitingTorrents: (id) sender
1188 NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [fTorrents count]];
1190 for (Torrent * torrent in fTorrents)
1191 if (![torrent isActive] && [torrent waitingToStart])
1192 [torrents addObject: torrent];
1194 [self resumeTorrentsNoWait: torrents];
1197 - (void) resumeTorrentsNoWait: (NSArray *) torrents
1199 //iterate through instead of all at once to ensure no conflicts
1200 for (Torrent * torrent in torrents)
1202 tr_inf( "restarting a torrent in resumeTorrentsNoWait" );
1203 [torrent startTransfer];
1208 [[fWindow toolbar] validateVisibleItems];
1209 [self updateTorrentHistory];
1212 - (void) stopSelectedTorrents: (id) sender
1214 [self stopTorrents: [fTableView selectedTorrents]];
1217 - (void) stopAllTorrents: (id) sender
1219 [self stopTorrents: fTorrents];
1222 - (void) stopTorrents: (NSArray *) torrents
1224 //don't want any of these starting then stopping
1225 for (Torrent * torrent in torrents)
1226 [torrent setWaitToStart: NO];
1228 [torrents makeObjectsPerformSelector: @selector(stopTransfer)];
1232 [[fWindow toolbar] validateVisibleItems];
1233 [self updateTorrentHistory];
1236 - (void) removeTorrents: (NSArray *) torrents deleteData: (BOOL) deleteData
1240 if ([fDefaults boolForKey: @"CheckRemove"])
1242 NSUInteger active = 0, downloading = 0;
1243 for (Torrent * torrent in torrents)
1244 if ([torrent isActive])
1247 if (![torrent isSeeding])
1251 if ([fDefaults boolForKey: @"CheckRemoveDownloading"] ? downloading > 0 : active > 0)
1253 NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
1254 torrents, @"Torrents",
1255 [NSNumber numberWithBool: deleteData], @"DeleteData", nil];
1257 NSString * title, * message;
1259 const NSInteger selected = [torrents count];
1262 NSString * torrentName = [[torrents objectAtIndex: 0] name];
1265 title = [NSString stringWithFormat:
1266 NSLocalizedString(@"Are you sure you want to remove \"%@\" from the transfer list"
1267 " and trash the data file?", "Removal confirm panel -> title"), torrentName];
1269 title = [NSString stringWithFormat:
1270 NSLocalizedString(@"Are you sure you want to remove \"%@\" from the transfer list?",
1271 "Removal confirm panel -> title"), torrentName];
1273 message = NSLocalizedString(@"This transfer is active."
1274 " Once removed, continuing the transfer will require the torrent file or magnet link.",
1275 "Removal confirm panel -> message");
1280 title = [NSString stringWithFormat:
1281 NSLocalizedString(@"Are you sure you want to remove %@ transfers from the transfer list"
1282 " and trash the data files?", "Removal confirm panel -> title"), [NSString formattedUInteger: selected]];
1284 title = [NSString stringWithFormat:
1285 NSLocalizedString(@"Are you sure you want to remove %@ transfers from the transfer list?",
1286 "Removal confirm panel -> title"), [NSString formattedUInteger: selected]];
1288 if (selected == active)
1289 message = [NSString stringWithFormat: NSLocalizedString(@"There are %@ active transfers.",
1290 "Removal confirm panel -> message part 1"), [NSString formattedUInteger: active]];
1292 message = [NSString stringWithFormat: NSLocalizedString(@"There are %@ transfers (%@ active).",
1293 "Removal confirm panel -> message part 1"), [NSString formattedUInteger: selected], [NSString formattedUInteger: active]];
1294 message = [message stringByAppendingFormat: @" %@",
1295 NSLocalizedString(@"Once removed, continuing the transfers will require the torrent files or magnet links.",
1296 "Removal confirm panel -> message part 2")];
1299 NSBeginAlertSheet(title, NSLocalizedString(@"Remove", "Removal confirm panel -> button"),
1300 NSLocalizedString(@"Cancel", "Removal confirm panel -> button"), nil, fWindow, self,
1301 nil, @selector(removeSheetDidEnd:returnCode:contextInfo:), dict, message);
1306 [self confirmRemoveTorrents: torrents deleteData: deleteData];
1309 - (void) removeSheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (NSDictionary *) dict
1311 NSArray * torrents = [dict objectForKey: @"Torrents"];
1312 if (returnCode == NSAlertDefaultReturn)
1313 [self confirmRemoveTorrents: torrents deleteData: [[dict objectForKey: @"DeleteData"] boolValue]];
1320 - (void) confirmRemoveTorrents: (NSArray *) torrents deleteData: (BOOL) deleteData
1322 NSMutableArray * selectedValues = [NSMutableArray arrayWithArray: [fTableView selectedValues]];
1323 [selectedValues removeObjectsInArray: torrents];
1325 //don't want any of these starting then stopping
1326 for (Torrent * torrent in torrents)
1327 [torrent setWaitToStart: NO];
1329 [fTorrents removeObjectsInArray: torrents];
1331 //if not removed from displayed torrents, updateTorrentsInQueue might cause a crash
1332 if ([fDisplayedTorrents count] > 0)
1334 if ([[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
1336 for (TorrentGroup * group in fDisplayedTorrents)
1337 [[group torrents] removeObjectsInArray: torrents];
1340 [fDisplayedTorrents removeObjectsInArray: torrents];
1343 for (Torrent * torrent in torrents)
1345 //let's expand all groups that have removed items - they either don't exist anymore, are already expanded, or are collapsed (rpc)
1346 [fTableView removeCollapsedGroup: [torrent groupValue]];
1348 //we can't assume the window is active - RPC removal, for example
1349 [fBadger removeTorrent: torrent];
1351 [torrent closeRemoveTorrent: deleteData];
1354 #warning why do we need them retained?
1357 [fTableView selectValues: selectedValues];
1359 [self updateTorrentsInQueue];
1362 - (void) removeNoDelete: (id) sender
1364 [self removeTorrents: [fTableView selectedTorrents] deleteData: NO];
1367 - (void) removeDeleteData: (id) sender
1369 [self removeTorrents: [fTableView selectedTorrents] deleteData: YES];
1372 - (void) clearCompleted: (id) sender
1374 NSMutableArray * torrents = [[NSMutableArray alloc] init];
1376 for (Torrent * torrent in fTorrents)
1377 if ([torrent isFinishedSeeding])
1378 [torrents addObject: torrent];
1380 if ([fDefaults boolForKey: @"WarningRemoveCompleted"])
1382 NSString * message, * info;
1383 if ([torrents count] == 1)
1385 NSString * torrentName = [[torrents objectAtIndex: 0] name];
1386 message = [NSString stringWithFormat: NSLocalizedString(@"Are you sure you want to remove \"%@\" from the transfer list?",
1387 "Remove completed confirm panel -> title"), torrentName];
1389 info = NSLocalizedString(@"Once removed, continuing the transfer will require the torrent file or magnet link.",
1390 "Remove completed confirm panel -> message");
1394 message = [NSString stringWithFormat: NSLocalizedString(@"Are you sure you want to remove %@ completed transfers from the transfer list?",
1395 "Remove completed confirm panel -> title"), [NSString formattedUInteger: [torrents count]]];
1397 info = NSLocalizedString(@"Once removed, continuing the transfers will require the torrent files or magnet links.",
1398 "Remove completed confirm panel -> message");
1401 NSAlert * alert = [[[NSAlert alloc] init] autorelease];
1402 [alert setMessageText: message];
1403 [alert setInformativeText: info];
1404 [alert setAlertStyle: NSWarningAlertStyle];
1405 [alert addButtonWithTitle: NSLocalizedString(@"Remove", "Remove completed confirm panel -> button")];
1406 [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Remove completed confirm panel -> button")];
1407 [alert setShowsSuppressionButton: YES];
1409 const NSInteger returnCode = [alert runModal];
1410 if ([[alert suppressionButton] state])
1411 [fDefaults setBool: NO forKey: @"WarningRemoveCompleted"];
1413 if (returnCode != NSAlertFirstButtonReturn)
1417 [self confirmRemoveTorrents: torrents deleteData: NO];
1420 - (void) moveDataFilesSelected: (id) sender
1422 [self moveDataFiles: [fTableView selectedTorrents]];
1425 - (void) moveDataFiles: (NSArray *) torrents
1427 NSOpenPanel * panel = [NSOpenPanel openPanel];
1428 [panel setPrompt: NSLocalizedString(@"Select", "Move torrent -> prompt")];
1429 [panel setAllowsMultipleSelection: NO];
1430 [panel setCanChooseFiles: NO];
1431 [panel setCanChooseDirectories: YES];
1432 [panel setCanCreateDirectories: YES];
1434 torrents = [torrents retain];
1435 NSInteger count = [torrents count];
1437 [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the new folder for \"%@\".",
1438 "Move torrent -> select destination folder"), [[torrents objectAtIndex: 0] name]]];
1440 [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the new folder for %d data files.",
1441 "Move torrent -> select destination folder"), count]];
1443 [panel beginSheetForDirectory: nil file: nil modalForWindow: fWindow modalDelegate: self
1444 didEndSelector: @selector(moveDataFileChoiceClosed:returnCode:contextInfo:) contextInfo: torrents];
1447 - (void) moveDataFileChoiceClosed: (NSOpenPanel *) panel returnCode: (NSInteger) code contextInfo: (NSArray *) torrents
1449 if (code == NSOKButton)
1451 for (Torrent * torrent in torrents)
1452 [torrent moveTorrentDataFileTo: [[panel filenames] objectAtIndex: 0]];
1458 - (void) copyTorrentFiles: (id) sender
1460 [self copyTorrentFileForTorrents: [[NSMutableArray alloc] initWithArray: [fTableView selectedTorrents]]];
1463 - (void) copyTorrentFileForTorrents: (NSMutableArray *) torrents
1465 if ([torrents count] == 0)
1471 Torrent * torrent = [torrents objectAtIndex: 0];
1473 if (![torrent isMagnet] && [[NSFileManager defaultManager] fileExistsAtPath: [torrent torrentLocation]])
1475 NSSavePanel * panel = [NSSavePanel savePanel];
1476 [panel setAllowedFileTypes: [NSArray arrayWithObjects: @"org.bittorrent.torrent", @"torrent", nil]];
1477 [panel setExtensionHidden: NO];
1479 [panel beginSheetForDirectory: nil file: [torrent name] modalForWindow: fWindow modalDelegate: self
1480 didEndSelector: @selector(saveTorrentCopySheetClosed:returnCode:contextInfo:) contextInfo: torrents];
1484 if (![torrent isMagnet])
1486 NSAlert * alert = [[NSAlert alloc] init];
1487 [alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent file copy alert -> button")];
1488 [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Copy of \"%@\" Cannot Be Created",
1489 "Torrent file copy alert -> title"), [torrent name]]];
1490 [alert setInformativeText: [NSString stringWithFormat:
1491 NSLocalizedString(@"The torrent file (%@) cannot be found.", "Torrent file copy alert -> message"),
1492 [torrent torrentLocation]]];
1493 [alert setAlertStyle: NSWarningAlertStyle];
1499 [torrents removeObjectAtIndex: 0];
1500 [self copyTorrentFileForTorrents: torrents];
1504 - (void) saveTorrentCopySheetClosed: (NSSavePanel *) panel returnCode: (NSInteger) code contextInfo: (NSMutableArray *) torrents
1506 //copy torrent to new location with name of data file
1507 if (code == NSOKButton)
1508 [[torrents objectAtIndex: 0] copyTorrentFileTo: [panel filename]];
1510 [torrents removeObjectAtIndex: 0];
1511 [self performSelectorOnMainThread: @selector(copyTorrentFileForTorrents:) withObject: torrents waitUntilDone: NO];
1514 - (void) copyMagnetLinks: (id) sender
1516 NSArray * torrents = [fTableView selectedTorrents];
1518 if ([torrents count] <= 0)
1521 NSMutableArray * links = [NSMutableArray arrayWithCapacity: [torrents count]];
1522 for (Torrent * torrent in torrents)
1523 [links addObject: [torrent magnetLink]];
1525 NSString * text = [links componentsJoinedByString: @"\n"];
1527 NSPasteboard * pb = [NSPasteboard generalPasteboard];
1528 if ([NSApp isOnSnowLeopardOrBetter])
1531 [pb writeObjects: [NSArray arrayWithObject: text]];
1535 [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
1536 [pb setString: text forType: NSStringPboardType];
1540 - (void) revealFile: (id) sender
1542 NSArray * selected = [fTableView selectedTorrents];
1543 if ([NSApp isOnSnowLeopardOrBetter])
1545 NSMutableArray * paths = [NSMutableArray arrayWithCapacity: [selected count]];
1546 for (Torrent * torrent in selected)
1548 NSString * location = [torrent dataLocation];
1550 [paths addObject: [NSURL fileURLWithPath: location]];
1553 if ([paths count] > 0)
1554 [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs: paths];
1558 for (Torrent * torrent in selected)
1560 NSString * location = [torrent dataLocation];
1562 [[NSWorkspace sharedWorkspace] selectFile: location inFileViewerRootedAtPath: nil];
1567 - (void) announceSelectedTorrents: (id) sender
1569 for (Torrent * torrent in [fTableView selectedTorrents])
1571 if ([torrent canManualAnnounce])
1572 [torrent manualAnnounce];
1576 - (void) verifySelectedTorrents: (id) sender
1578 [self verifyTorrents: [fTableView selectedTorrents]];
1581 - (void) verifyTorrents: (NSArray *) torrents
1583 for (Torrent * torrent in torrents)
1584 [torrent resetCache];
1589 - (void) showPreferenceWindow: (id) sender
1591 NSWindow * window = [fPrefsController window];
1592 if (![window isVisible])
1595 [window makeKeyAndOrderFront: nil];
1598 - (void) showAboutWindow: (id) sender
1600 [[AboutWindowController aboutController] showWindow: nil];
1603 - (void) showInfo: (id) sender
1605 if ([[fInfoController window] isVisible])
1606 [fInfoController close];
1609 [fInfoController updateInfoStats];
1610 [[fInfoController window] orderFront: nil];
1612 if ([fInfoController canQuickLook]
1613 && [QLPreviewPanelSL sharedPreviewPanelExists] && [[QLPreviewPanelSL sharedPreviewPanel] isVisible])
1614 [[QLPreviewPanelSL sharedPreviewPanel] reloadData];
1618 [[fWindow toolbar] validateVisibleItems];
1623 [fInfoController setInfoForTorrents: [fTableView selectedTorrents]];
1625 if ([NSApp isOnSnowLeopardOrBetter] && [QLPreviewPanelSL sharedPreviewPanelExists]
1626 && [[QLPreviewPanelSL sharedPreviewPanel] isVisible])
1627 [[QLPreviewPanelSL sharedPreviewPanel] reloadData];
1630 - (void) setInfoTab: (id) sender
1632 if (sender == fNextInfoTabItem)
1633 [fInfoController setNextTab];
1635 [fInfoController setPreviousTab];
1638 - (void) showMessageWindow: (id) sender
1640 if (!fMessageController)
1641 fMessageController = [[MessageWindowController alloc] init];
1642 [fMessageController showWindow: nil];
1645 - (void) showStatsWindow: (id) sender
1647 [[StatsWindowController statsWindow: fLib] showWindow: nil];
1652 [fTorrents makeObjectsPerformSelector: @selector(update)];
1654 //pull the upload and download speeds - most consistent by using current stats
1655 CGFloat dlRate = 0.0, ulRate = 0.0;
1656 BOOL completed = NO;
1657 for (Torrent * torrent in fTorrents)
1659 dlRate += [torrent downloadRate];
1660 ulRate += [torrent uploadRate];
1662 completed |= [torrent isFinishedSeeding];
1665 if (![NSApp isHidden])
1667 if ([fWindow isVisible])
1669 [self sortTorrents];
1671 [fStatusBar updateWithDownload: dlRate upload: ulRate];
1673 [fClearCompletedButton setHidden: !completed];
1676 //update non-constant parts of info window
1677 if ([[fInfoController window] isVisible])
1678 [fInfoController updateInfoStats];
1682 [fBadger updateBadgeWithDownload: dlRate upload: ulRate];
1685 - (void) setBottomCountText: (BOOL) filtering
1687 NSString * totalTorrentsString;
1688 NSUInteger totalCount = [fTorrents count];
1689 if (totalCount != 1)
1690 totalTorrentsString = [NSString stringWithFormat: NSLocalizedString(@"%@ transfers", "Status bar transfer count"),
1691 [NSString formattedUInteger: totalCount]];
1693 totalTorrentsString = NSLocalizedString(@"1 transfer", "Status bar transfer count");
1697 NSUInteger count = [fTableView numberOfRows]; //have to factor in collapsed rows
1698 if (count > 0 && ![[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [Torrent class]])
1699 count -= [fDisplayedTorrents count];
1701 totalTorrentsString = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Status bar transfer count"),
1702 [NSString formattedUInteger: count], totalTorrentsString];
1705 [fTotalTorrentsField setStringValue: totalTorrentsString];
1708 - (void) updateTorrentsInQueue
1710 NSUInteger desiredDownloadActive = [fDefaults boolForKey: @"Queue"] ? [self numToStartFromQueue: YES] : NSUIntegerMax,
1711 desiredSeedActive = [fDefaults boolForKey: @"QueueSeed"] ? [self numToStartFromQueue: NO] : NSUIntegerMax;
1713 for (Torrent * torrent in fTorrents)
1715 if (desiredDownloadActive == 0 && desiredSeedActive == 0)
1718 if (![torrent isActive] && ![torrent isChecking] && [torrent waitingToStart])
1720 if (![torrent allDownloaded])
1722 if (desiredDownloadActive > 0)
1724 tr_inf( "restarting download torrent in mac queue" );
1725 [torrent startTransfer];
1726 if ([torrent isActive])
1727 --desiredDownloadActive;
1733 if (desiredSeedActive > 0)
1735 tr_inf( "restarting seed torrent in mac queue" );
1736 [torrent startTransfer];
1737 if ([torrent isActive])
1738 --desiredSeedActive;
1747 [[fWindow toolbar] validateVisibleItems];
1748 [self updateTorrentHistory];
1751 - (NSUInteger) numToStartFromQueue: (BOOL) downloadQueue
1753 if (![fDefaults boolForKey: downloadQueue ? @"Queue" : @"QueueSeed"])
1756 NSUInteger desired = [fDefaults integerForKey: downloadQueue ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
1758 for (Torrent * torrent in fTorrents)
1763 if ([torrent isChecking])
1765 else if ([torrent isActive] && ![torrent isStalled] && ![torrent isError])
1767 if ([torrent allDownloaded] != downloadQueue)
1776 - (void) torrentFinishedDownloading: (NSNotification *) notification
1778 Torrent * torrent = [notification object];
1780 if ([[[notification userInfo] objectForKey: @"WasRunning"] boolValue])
1782 if (!fSoundPlaying && [fDefaults boolForKey: @"PlayDownloadSound"])
1785 if ((sound = [NSSound soundNamed: [fDefaults stringForKey: @"DownloadSound"]]))
1787 [sound setDelegate: self];
1788 fSoundPlaying = YES;
1793 NSMutableDictionary * clickContext = [NSMutableDictionary dictionaryWithObject: GROWL_DOWNLOAD_COMPLETE forKey: @"Type"];
1795 NSString * location = [torrent dataLocation];
1797 [clickContext setObject: location forKey: @"Location"];
1799 [GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Download Complete", "Growl notification title")
1800 description: [torrent name] notificationName: GROWL_DOWNLOAD_COMPLETE
1801 iconData: nil priority: 0 isSticky: NO clickContext: clickContext];
1803 if (![fWindow isMainWindow])
1804 [fBadger addCompletedTorrent: torrent];
1806 //bounce download stack
1807 [[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"com.apple.DownloadFileFinished"
1808 object: [torrent dataLocation]];
1810 if ([torrent isActive] && [fDefaults boolForKey: @"QueueSeed"] && [self numToStartFromQueue: NO] == 0)
1812 [torrent stopTransfer];
1813 [torrent setWaitToStart: YES];
1817 [self updateTorrentsInQueue];
1820 - (void) torrentRestartedDownloading: (NSNotification *) notification
1822 Torrent * torrent = [notification object];
1823 if ([torrent isActive] && [fDefaults boolForKey: @"Queue"] && [self numToStartFromQueue: YES] == 0)
1825 [torrent stopTransfer];
1826 [torrent setWaitToStart: YES];
1829 [self updateTorrentsInQueue];
1832 - (void) torrentFinishedSeeding: (NSNotification *) notification
1834 Torrent * torrent = [notification object];
1836 [self updateTorrentsInQueue];
1838 if ([[fTableView selectedTorrents] containsObject: torrent])
1840 [fInfoController updateInfoStats];
1841 [fInfoController updateOptions];
1844 if (!fSoundPlaying && [fDefaults boolForKey: @"PlaySeedingSound"])
1847 if ((sound = [NSSound soundNamed: [fDefaults stringForKey: @"SeedingSound"]]))
1849 [sound setDelegate: self];
1850 fSoundPlaying = YES;
1855 NSMutableDictionary * clickContext = [NSMutableDictionary dictionaryWithObject: GROWL_SEEDING_COMPLETE forKey: @"Type"];
1857 NSString * location = [torrent dataLocation];
1859 [clickContext setObject: location forKey: @"Location"];
1861 [GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Seeding Complete", "Growl notification title")
1862 description: [torrent name] notificationName: GROWL_SEEDING_COMPLETE
1863 iconData: nil priority: 0 isSticky: NO clickContext: clickContext];
1866 - (void) updateTorrentHistory
1868 NSMutableArray * history = [NSMutableArray arrayWithCapacity: [fTorrents count]];
1870 for (Torrent * torrent in fTorrents)
1871 [history addObject: [torrent history]];
1873 [history writeToFile: [NSHomeDirectory() stringByAppendingPathComponent: TRANSFER_PLIST] atomically: YES];
1876 - (void) setSort: (id) sender
1878 NSString * sortType;
1879 switch ([sender tag])
1881 case SORT_ORDER_TAG:
1882 sortType = SORT_ORDER;
1883 [fDefaults setBool: NO forKey: @"SortReverse"];
1886 sortType = SORT_DATE;
1889 sortType = SORT_NAME;
1891 case SORT_PROGRESS_TAG:
1892 sortType = SORT_PROGRESS;
1894 case SORT_STATE_TAG:
1895 sortType = SORT_STATE;
1897 case SORT_TRACKER_TAG:
1898 sortType = SORT_TRACKER;
1900 case SORT_ACTIVITY_TAG:
1901 sortType = SORT_ACTIVITY;
1904 sortType = SORT_SIZE;
1907 NSAssert1(NO, @"Unknown sort tag received: %d", [sender tag]);
1911 [fDefaults setObject: sortType forKey: @"Sort"];
1912 [self applyFilter]; //better than calling sortTorrents because it will even apply to queue order
1915 - (void) setSortByGroup: (id) sender
1917 BOOL sortByGroup = ![fDefaults boolForKey: @"SortByGroup"];
1918 [fDefaults setBool: sortByGroup forKey: @"SortByGroup"];
1922 [fTableView removeAllCollapsedGroups];
1927 - (void) setSortReverse: (id) sender
1929 const BOOL setReverse = [sender tag] == SORT_DESC_TAG;
1930 if (setReverse != [fDefaults boolForKey: @"SortReverse"])
1932 [fDefaults setBool: setReverse forKey: @"SortReverse"];
1933 [self sortTorrents];
1937 - (void) sortTorrents
1939 NSArray * selectedValues = [fTableView selectedValues];
1941 [self sortTorrentsIgnoreSelected]; //actually sort
1943 [fTableView selectValues: selectedValues];
1946 - (void) sortTorrentsIgnoreSelected
1948 NSString * sortType = [fDefaults stringForKey: @"Sort"];
1950 if (![sortType isEqualToString: SORT_ORDER])
1952 const BOOL asc = ![fDefaults boolForKey: @"SortReverse"];
1954 NSArray * descriptors;
1955 NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: asc
1956 selector: @selector(compareFinder:)] autorelease];
1958 if ([sortType isEqualToString: SORT_STATE])
1960 NSSortDescriptor * stateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"stateSortKey" ascending: !asc] autorelease],
1961 * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"progress" ascending: !asc] autorelease],
1962 * ratioDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"ratio" ascending: !asc] autorelease];
1964 descriptors = [[NSArray alloc] initWithObjects: stateDescriptor, progressDescriptor, ratioDescriptor,
1965 nameDescriptor, nil];
1967 else if ([sortType isEqualToString: SORT_PROGRESS])
1969 NSSortDescriptor * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"progress" ascending: asc] autorelease],
1970 * ratioProgressDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"progressStopRatio"
1971 ascending: asc] autorelease],
1972 * ratioDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"ratio" ascending: asc] autorelease];
1974 descriptors = [[NSArray alloc] initWithObjects: progressDescriptor, ratioProgressDescriptor, ratioDescriptor,
1975 nameDescriptor, nil];
1977 else if ([sortType isEqualToString: SORT_TRACKER])
1979 NSSortDescriptor * trackerDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"trackerSortKey" ascending: asc
1980 selector: @selector(localizedCaseInsensitiveCompare:)] autorelease];
1982 descriptors = [[NSArray alloc] initWithObjects: trackerDescriptor, nameDescriptor, nil];
1984 else if ([sortType isEqualToString: SORT_ACTIVITY])
1986 NSSortDescriptor * rateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"totalRate" ascending: !asc] autorelease];
1987 NSSortDescriptor * activityDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"dateActivityOrAdd" ascending: !asc]
1990 descriptors = [[NSArray alloc] initWithObjects: rateDescriptor, activityDescriptor, nameDescriptor, nil];
1992 else if ([sortType isEqualToString: SORT_DATE])
1994 NSSortDescriptor * dateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"dateAdded" ascending: asc] autorelease];
1996 descriptors = [[NSArray alloc] initWithObjects: dateDescriptor, nameDescriptor, nil];
1998 else if ([sortType isEqualToString: SORT_SIZE])
2000 NSSortDescriptor * sizeDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"size" ascending: asc] autorelease];
2002 descriptors = [[NSArray alloc] initWithObjects: sizeDescriptor, nameDescriptor, nil];
2005 descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, nil];
2008 if ([fDefaults boolForKey: @"SortByGroup"])
2010 for (TorrentGroup * group in fDisplayedTorrents)
2011 [[group torrents] sortUsingDescriptors: descriptors];
2014 [fDisplayedTorrents sortUsingDescriptors: descriptors];
2016 [descriptors release];
2019 [fTableView reloadData];
2022 - (void) applyFilter
2024 //get all the torrents in the table
2025 NSMutableArray * previousTorrents;
2026 if ([fDisplayedTorrents count] > 0 && [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
2028 previousTorrents = [NSMutableArray array];
2030 for (TorrentGroup * group in fDisplayedTorrents)
2031 [previousTorrents addObjectsFromArray: [group torrents]];
2034 previousTorrents = fDisplayedTorrents;
2036 NSArray * selectedValues = [fTableView selectedValues];
2038 NSUInteger active = 0, downloading = 0, seeding = 0, paused = 0;
2039 NSString * filterType = [fDefaults stringForKey: @"Filter"];
2040 BOOL filterActive = NO, filterDownload = NO, filterSeed = NO, filterPause = NO, filterStatus = YES;
2041 if ([filterType isEqualToString: FILTER_ACTIVE])
2043 else if ([filterType isEqualToString: FILTER_DOWNLOAD])
2044 filterDownload = YES;
2045 else if ([filterType isEqualToString: FILTER_SEED])
2047 else if ([filterType isEqualToString: FILTER_PAUSE])
2052 const NSInteger groupFilterValue = [fDefaults integerForKey: @"FilterGroup"];
2053 const BOOL filterGroup = groupFilterValue != GROUP_FILTER_ALL_TAG;
2055 NSString * searchString = [fFilterBar searchString];
2056 if (searchString && [searchString isEqualToString: @""])
2058 const BOOL filterTracker = searchString && [[fDefaults stringForKey: @"FilterSearchType"] isEqualToString: FILTER_TYPE_TRACKER];
2060 NSMutableArray * allTorrents = [NSMutableArray arrayWithCapacity: [fTorrents count]];
2062 //get count of each type
2063 for (Torrent * torrent in fTorrents)
2066 if ([torrent isActive] && ![torrent isCheckingWaiting])
2068 const BOOL isActive = ![torrent isStalled];
2072 if ([torrent isSeeding])
2075 if (filterStatus && !((filterActive && isActive) || filterSeed))
2081 if (filterStatus && !((filterActive && isActive) || filterDownload))
2088 if (filterStatus && !filterPause)
2094 if ([torrent groupValue] != groupFilterValue)
2102 BOOL removeTextField = YES;
2103 for (NSString * tracker in [torrent allTrackersFlat])
2105 if ([tracker rangeOfString: searchString options:
2106 (NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)].location != NSNotFound)
2108 removeTextField = NO;
2113 if (removeTextField)
2118 if ([[torrent name] rangeOfString: searchString options:
2119 (NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)].location == NSNotFound)
2124 [allTorrents addObject: torrent];
2127 //set button tooltips
2129 [fFilterBar setCountAll: [fTorrents count] active: active downloading: downloading seeding: seeding paused: paused];
2131 //clear display cache for not-shown torrents
2132 [previousTorrents removeObjectsInArray: allTorrents];
2133 for (Torrent * torrent in previousTorrents)
2134 [torrent setPreviousFinishedPieces: nil];
2136 //place torrents into groups
2137 const BOOL groupRows = [fDefaults boolForKey: @"SortByGroup"];
2140 NSMutableArray * oldTorrentGroups = [NSMutableArray array];
2141 if ([fDisplayedTorrents count] > 0 && [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
2142 [oldTorrentGroups addObjectsFromArray: fDisplayedTorrents];
2144 [fDisplayedTorrents removeAllObjects];
2146 NSSortDescriptor * groupDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"groupOrderValue" ascending: YES] autorelease];
2147 [allTorrents sortUsingDescriptors: [NSArray arrayWithObject: groupDescriptor]];
2149 TorrentGroup * group = nil;
2150 NSInteger lastGroupValue = -2, currentOldGroupIndex = 0;
2151 for (Torrent * torrent in allTorrents)
2153 const NSInteger groupValue = [torrent groupValue];
2154 if (groupValue != lastGroupValue)
2156 lastGroupValue = groupValue;
2160 //try to see if the group already exists
2161 for (; currentOldGroupIndex < [oldTorrentGroups count]; ++currentOldGroupIndex)
2163 TorrentGroup * currentGroup = [oldTorrentGroups objectAtIndex: currentOldGroupIndex];
2164 const NSInteger currentGroupValue = [currentGroup groupIndex];
2165 if (currentGroupValue == groupValue)
2167 group = currentGroup;
2168 [[currentGroup torrents] removeAllObjects];
2170 ++currentOldGroupIndex;
2173 if (currentGroupValue >= groupValue)
2178 group = [[[TorrentGroup alloc] initWithGroup: groupValue] autorelease];
2179 [fDisplayedTorrents addObject: group];
2182 NSAssert(group != nil, @"No group object to add torrents to");
2183 [[group torrents] addObject: torrent];
2187 [fDisplayedTorrents setArray: allTorrents];
2190 [self sortTorrentsIgnoreSelected];
2192 //reset expanded/collapsed rows
2195 for (TorrentGroup * group in fDisplayedTorrents)
2197 if ([fTableView isGroupCollapsed: [group groupIndex]])
2198 [fTableView collapseItem: group];
2200 [fTableView expandItem: group];
2204 [fTableView selectValues: selectedValues];
2205 [self resetInfo]; //if group is already selected, but the torrents in it change
2207 [self setBottomCountText: groupRows || filterStatus || filterGroup || searchString];
2209 [self setWindowSizeToFit];
2212 - (void) switchFilter: (id) sender
2214 [fFilterBar switchFilter: sender == fNextFilterItem];
2217 - (void) menuNeedsUpdate: (NSMenu *) menu
2219 if (menu == fGroupsSetMenu || menu == fGroupsSetContextMenu)
2221 for (NSInteger i = [menu numberOfItems]-1; i >= 0; i--)
2222 [menu removeItemAtIndex: i];
2224 NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(setGroup:) isSmall: NO];
2226 const NSInteger groupMenuCount = [groupMenu numberOfItems];
2227 for (NSInteger i = 0; i < groupMenuCount; i++)
2229 NSMenuItem * item = [[groupMenu itemAtIndex: 0] retain];
2230 [groupMenu removeItemAtIndex: 0];
2231 [menu addItem: item];
2235 else if (menu == fUploadMenu || menu == fDownloadMenu)
2237 if ([menu numberOfItems] > 3)
2240 const NSInteger speedLimitActionValue[] = { 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750, 1000, 1500, 2000, -1 };
2243 for (NSInteger i = 0; speedLimitActionValue[i] != -1; i++)
2245 item = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: NSLocalizedString(@"%d KB/s",
2246 "Action menu -> upload/download limit"), speedLimitActionValue[i]] action: @selector(setQuickLimitGlobal:)
2247 keyEquivalent: @""];
2248 [item setTarget: self];
2249 [item setRepresentedObject: [NSNumber numberWithInt: speedLimitActionValue[i]]];
2250 [menu addItem: item];
2254 else if (menu == fRatioStopMenu)
2256 if ([menu numberOfItems] > 3)
2259 const float ratioLimitActionValue[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, -1 };
2262 for (NSInteger i = 0; ratioLimitActionValue[i] != -1; i++)
2264 item = [[NSMenuItem alloc] initWithTitle: [NSString localizedStringWithFormat: @"%.2f", ratioLimitActionValue[i]]
2265 action: @selector(setQuickRatioGlobal:) keyEquivalent: @""];
2266 [item setTarget: self];
2267 [item setRepresentedObject: [NSNumber numberWithFloat: ratioLimitActionValue[i]]];
2268 [menu addItem: item];
2275 - (void) setGroup: (id) sender
2277 for (Torrent * torrent in [fTableView selectedTorrents])
2279 [fTableView removeCollapsedGroup: [torrent groupValue]]; //remove old collapsed group
2281 [torrent setGroupValue: [sender tag]];
2286 [self updateTorrentHistory];
2289 - (void) toggleSpeedLimit: (id) sender
2291 [fDefaults setBool: ![fDefaults boolForKey: @"SpeedLimit"] forKey: @"SpeedLimit"];
2292 [self speedLimitChanged: sender];
2295 - (void) speedLimitChanged: (id) sender
2297 tr_sessionUseAltSpeed(fLib, [fDefaults boolForKey: @"SpeedLimit"]);
2298 [fStatusBar updateSpeedFieldsToolTips];
2301 //dict has been retained
2302 - (void) altSpeedToggledCallbackIsLimited: (NSDictionary *) dict
2304 const BOOL isLimited = [[dict objectForKey: @"Active"] boolValue];
2306 [fDefaults setBool: isLimited forKey: @"SpeedLimit"];
2307 [fStatusBar updateSpeedFieldsToolTips];
2309 if (![[dict objectForKey: @"ByUser"] boolValue])
2310 [GrowlApplicationBridge notifyWithTitle: isLimited
2311 ? NSLocalizedString(@"Speed Limit Auto Enabled", "Growl notification title")
2312 : NSLocalizedString(@"Speed Limit Auto Disabled", "Growl notification title")
2313 description: NSLocalizedString(@"Bandwidth settings changed", "Growl notification description")
2314 notificationName: GROWL_AUTO_SPEED_LIMIT iconData: nil priority: 0 isSticky: NO clickContext: nil];
2319 - (void) setLimitGlobalEnabled: (id) sender
2321 BOOL upload = [sender menu] == fUploadMenu;
2322 [fDefaults setBool: sender == (upload ? fUploadLimitItem : fDownloadLimitItem) forKey: upload ? @"CheckUpload" : @"CheckDownload"];
2324 [fPrefsController applySpeedSettings: nil];
2327 - (void) setQuickLimitGlobal: (id) sender
2329 BOOL upload = [sender menu] == fUploadMenu;
2330 [fDefaults setInteger: [[sender representedObject] intValue] forKey: upload ? @"UploadLimit" : @"DownloadLimit"];
2331 [fDefaults setBool: YES forKey: upload ? @"CheckUpload" : @"CheckDownload"];
2333 [fPrefsController updateLimitFields];
2334 [fPrefsController applySpeedSettings: nil];
2337 - (void) setRatioGlobalEnabled: (id) sender
2339 [fDefaults setBool: sender == fCheckRatioItem forKey: @"RatioCheck"];
2341 [fPrefsController applyRatioSetting: nil];
2344 - (void) setQuickRatioGlobal: (id) sender
2346 [fDefaults setBool: YES forKey: @"RatioCheck"];
2347 [fDefaults setFloat: [[sender representedObject] floatValue] forKey: @"RatioLimit"];
2349 [fPrefsController updateRatioStopField];
2352 - (void) sound: (NSSound *) sound didFinishPlaying: (BOOL) finishedPlaying
2357 - (void) watcher: (id<UKFileWatcher>) watcher receivedNotification: (NSString *) notification forPath: (NSString *) path
2359 if ([notification isEqualToString: UKFileWatcherWriteNotification])
2361 if (![fDefaults boolForKey: @"AutoImport"] || ![fDefaults stringForKey: @"AutoImportDirectory"])
2364 if (fAutoImportTimer)
2366 if ([fAutoImportTimer isValid])
2367 [fAutoImportTimer invalidate];
2368 [fAutoImportTimer release];
2369 fAutoImportTimer = nil;
2372 //check again in 10 seconds in case torrent file wasn't complete
2373 fAutoImportTimer = [[NSTimer scheduledTimerWithTimeInterval: 10.0 target: self
2374 selector: @selector(checkAutoImportDirectory) userInfo: nil repeats: NO] retain];
2376 [self checkAutoImportDirectory];
2380 - (void) changeAutoImport
2382 if (fAutoImportTimer)
2384 if ([fAutoImportTimer isValid])
2385 [fAutoImportTimer invalidate];
2386 [fAutoImportTimer release];
2387 fAutoImportTimer = nil;
2390 if (fAutoImportedNames)
2392 [fAutoImportedNames release];
2393 fAutoImportedNames = nil;
2396 [self checkAutoImportDirectory];
2399 - (void) checkAutoImportDirectory
2402 if (![fDefaults boolForKey: @"AutoImport"] || !(path = [fDefaults stringForKey: @"AutoImportDirectory"]))
2405 path = [path stringByExpandingTildeInPath];
2407 NSArray * importedNames;
2408 if (!(importedNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: path error: NULL]))
2411 //only check files that have not been checked yet
2412 NSMutableArray * newNames = [importedNames mutableCopy];
2414 if (fAutoImportedNames)
2415 [newNames removeObjectsInArray: fAutoImportedNames];
2417 fAutoImportedNames = [[NSMutableArray alloc] init];
2418 [fAutoImportedNames setArray: importedNames];
2420 for (NSString * file in newNames)
2422 if ([file hasPrefix: @"."])
2425 NSString * fullFile = [path stringByAppendingPathComponent: file];
2427 if (!([[[NSWorkspace sharedWorkspace] typeOfFile: fullFile error: NULL] isEqualToString: @"org.bittorrent.torrent"]
2428 || [[fullFile pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame))
2431 tr_ctor * ctor = tr_ctorNew(fLib);
2432 tr_ctorSetMetainfoFromFile(ctor, [fullFile UTF8String]);
2434 switch (tr_torrentParse(ctor, NULL))
2437 [self openFiles: [NSArray arrayWithObject: fullFile] addType: ADD_AUTO forcePath: nil];
2439 [GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Torrent File Auto Added", "Growl notification title")
2440 description: file notificationName: GROWL_AUTO_ADD iconData: nil priority: 0 isSticky: NO
2445 [fAutoImportedNames removeObject: file];
2448 case TR_PARSE_DUPLICATE: //let's ignore this (but silence a warning)
2458 - (void) beginCreateFile: (NSNotification *) notification
2460 if (![fDefaults boolForKey: @"AutoImport"])
2463 NSString * location = [notification object],
2464 * path = [fDefaults stringForKey: @"AutoImportDirectory"];
2466 if (location && path && [[[location stringByDeletingLastPathComponent] stringByExpandingTildeInPath]
2467 isEqualToString: [path stringByExpandingTildeInPath]])
2468 [fAutoImportedNames addObject: [location lastPathComponent]];
2471 - (NSInteger) outlineView: (NSOutlineView *) outlineView numberOfChildrenOfItem: (id) item
2474 return [[item torrents] count];
2476 return [fDisplayedTorrents count];
2479 - (id) outlineView: (NSOutlineView *) outlineView child: (NSInteger) index ofItem: (id) item
2482 return [[item torrents] objectAtIndex: index];
2484 return [fDisplayedTorrents objectAtIndex: index];
2487 - (BOOL) outlineView: (NSOutlineView *) outlineView isItemExpandable: (id) item
2489 return ![item isKindOfClass: [Torrent class]];
2492 - (id) outlineView: (NSOutlineView *) outlineView objectValueForTableColumn: (NSTableColumn *) tableColumn byItem: (id) item
2494 if ([item isKindOfClass: [Torrent class]]) {
2497 return [item hashString];
2501 NSString * ident = [tableColumn identifier];
2502 if ([ident isEqualToString: @"Group"])
2504 NSInteger group = [item groupIndex];
2505 return group != -1 ? [[GroupsController groups] nameForIndex: group]
2506 : NSLocalizedString(@"No Group", "Group table row");
2508 else if ([ident isEqualToString: @"Color"])
2510 NSInteger group = [item groupIndex];
2511 return [[GroupsController groups] imageForIndex: group];
2513 else if ([ident isEqualToString: @"DL Image"])
2514 return [NSImage imageNamed: @"DownArrowGroupTemplate.png"];
2515 else if ([ident isEqualToString: @"UL Image"])
2516 return [NSImage imageNamed: [fDefaults boolForKey: @"DisplayGroupRowRatio"]
2517 ? @"YingYangGroupTemplate.png" : @"UpArrowGroupTemplate.png"];
2520 TorrentGroup * group = (TorrentGroup *)item;
2522 if ([fDefaults boolForKey: @"DisplayGroupRowRatio"])
2523 return [NSString stringForRatio: [group ratio]];
2526 CGFloat rate = [ident isEqualToString: @"UL"] ? [group uploadRate] : [group downloadRate];
2527 return [NSString stringForSpeed: rate];
2533 - (BOOL) outlineView: (NSOutlineView *) outlineView writeItems: (NSArray *) items toPasteboard: (NSPasteboard *) pasteboard
2535 //only allow reordering of rows if sorting by order
2536 if ([fDefaults boolForKey: @"SortByGroup"] || [[fDefaults stringForKey: @"Sort"] isEqualToString: SORT_ORDER])
2538 NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
2539 for (id torrent in items)
2541 if (![torrent isKindOfClass: [Torrent class]])
2544 [indexSet addIndex: [fTableView rowForItem: torrent]];
2547 [pasteboard declareTypes: [NSArray arrayWithObject: TORRENT_TABLE_VIEW_DATA_TYPE] owner: self];
2548 [pasteboard setData: [NSKeyedArchiver archivedDataWithRootObject: indexSet] forType: TORRENT_TABLE_VIEW_DATA_TYPE];
2554 - (NSDragOperation) outlineView: (NSOutlineView *) outlineView validateDrop: (id < NSDraggingInfo >) info proposedItem: (id) item
2555 proposedChildIndex: (NSInteger) index
2557 NSPasteboard * pasteboard = [info draggingPasteboard];
2558 if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE])
2560 if ([fDefaults boolForKey: @"SortByGroup"])
2563 return NSDragOperationNone;
2565 if ([[fDefaults stringForKey: @"Sort"] isEqualToString: SORT_ORDER])
2567 if ([item isKindOfClass: [Torrent class]])
2569 TorrentGroup * group = [fTableView parentForItem: item];
2570 index = [[group torrents] indexOfObject: item] + 1;
2576 if ([item isKindOfClass: [Torrent class]])
2577 item = [fTableView parentForItem: item];
2578 index = NSOutlineViewDropOnItemIndex;
2585 index = [fTableView rowForItem: item] + 1;
2590 [fTableView setDropItem: item dropChildIndex: index];
2591 return NSDragOperationGeneric;
2594 return NSDragOperationNone;
2597 - (BOOL) outlineView: (NSOutlineView *) outlineView acceptDrop: (id < NSDraggingInfo >) info item: (id) item
2598 childIndex: (NSInteger) newRow
2600 NSPasteboard * pasteboard = [info draggingPasteboard];
2601 if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE])
2603 //remember selected rows
2604 NSArray * selectedValues = [fTableView selectedValues];
2606 NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: TORRENT_TABLE_VIEW_DATA_TYPE]];
2608 //get the torrents to move
2609 NSMutableArray * movingTorrents = [NSMutableArray arrayWithCapacity: [indexes count]];
2610 for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
2611 [movingTorrents addObject: [fTableView itemAtRow: i]];
2617 NSInteger groupValue = [item groupIndex];
2618 for (Torrent * torrent in movingTorrents)
2620 //have to reset objects here to avoid weird crash
2621 [[[fTableView parentForItem: torrent] torrents] removeObject: torrent];
2622 [[item torrents] addObject: torrent];
2624 [torrent setGroupValue: groupValue];
2626 //part 2 of avoiding weird crash
2627 [fTableView reloadItem: nil reloadChildren: YES];
2630 //reorder queue order
2631 if (newRow != NSOutlineViewDropOnItemIndex)
2633 //find torrent to place under
2634 NSArray * groupTorrents = item ? [item torrents] : fDisplayedTorrents;
2635 Torrent * topTorrent = nil;
2636 for (NSInteger i = newRow-1; i >= 0; i--)
2638 Torrent * tempTorrent = [groupTorrents objectAtIndex: i];
2639 if (![movingTorrents containsObject: tempTorrent])
2641 topTorrent = tempTorrent;
2646 //remove objects to reinsert
2647 [fTorrents removeObjectsInArray: movingTorrents];
2649 //insert objects at new location
2650 NSUInteger insertIndex = topTorrent ? [fTorrents indexOfObject: topTorrent] + 1 : 0;
2651 NSIndexSet * insertIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(insertIndex, [movingTorrents count])];
2652 [fTorrents insertObjects: movingTorrents atIndexes: insertIndexes];
2656 [fTableView selectValues: selectedValues];
2662 - (void) torrentTableViewSelectionDidChange: (NSNotification *) notification
2665 [[fWindow toolbar] validateVisibleItems];
2668 - (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) info
2670 NSPasteboard * pasteboard = [info draggingPasteboard];
2671 if ([[pasteboard types] containsObject: NSFilenamesPboardType])
2673 //check if any torrent files can be added
2675 NSArray * files = [pasteboard propertyListForType: NSFilenamesPboardType];
2676 for (NSString * file in files)
2678 if ([[[NSWorkspace sharedWorkspace] typeOfFile: file error: NULL] isEqualToString: @"org.bittorrent.torrent"]
2679 || [[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
2682 tr_ctor * ctor = tr_ctorNew(fLib);
2683 tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]);
2684 if (tr_torrentParse(ctor, NULL) == TR_PARSE_OK)
2686 if (!fOverlayWindow)
2687 fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
2688 [fOverlayWindow setTorrents: files];
2690 return NSDragOperationCopy;
2696 //create a torrent file if a single file
2697 if (!torrent && [files count] == 1)
2699 if (!fOverlayWindow)
2700 fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
2701 [fOverlayWindow setFile: [[files objectAtIndex: 0] lastPathComponent]];
2703 return NSDragOperationCopy;
2706 else if ([[pasteboard types] containsObject: NSURLPboardType])
2708 if (!fOverlayWindow)
2709 fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
2710 [fOverlayWindow setURL: [[NSURL URLFromPasteboard: pasteboard] relativeString]];
2712 return NSDragOperationCopy;
2716 return NSDragOperationNone;
2719 - (void) draggingExited: (id <NSDraggingInfo>) info
2722 [fOverlayWindow fadeOut];
2725 - (BOOL) performDragOperation: (id <NSDraggingInfo>) info
2728 [fOverlayWindow fadeOut];
2730 NSPasteboard * pasteboard = [info draggingPasteboard];
2731 if ([[pasteboard types] containsObject: NSFilenamesPboardType])
2733 BOOL torrent = NO, accept = YES;
2735 //create an array of files that can be opened
2736 NSArray * files = [pasteboard propertyListForType: NSFilenamesPboardType];
2737 NSMutableArray * filesToOpen = [NSMutableArray arrayWithCapacity: [files count]];
2738 for (NSString * file in files)
2740 if ([[[NSWorkspace sharedWorkspace] typeOfFile: file error: NULL] isEqualToString: @"org.bittorrent.torrent"]
2741 || [[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
2744 tr_ctor * ctor = tr_ctorNew(fLib);
2745 tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]);
2746 if (tr_torrentParse(ctor, NULL) == TR_PARSE_OK)
2747 [filesToOpen addObject: file];
2752 if ([filesToOpen count] > 0)
2753 [self application: NSApp openFiles: filesToOpen];
2756 if (!torrent && [files count] == 1)
2757 [CreatorWindowController createTorrentFile: fLib forFile: [files objectAtIndex: 0]];
2764 else if ([[pasteboard types] containsObject: NSURLPboardType])
2767 if ((url = [NSURL URLFromPasteboard: pasteboard]))
2769 [self openURL: [url absoluteString]];
2778 - (void) toggleSmallView: (id) sender
2780 BOOL makeSmall = ![fDefaults boolForKey: @"SmallView"];
2781 [fDefaults setBool: makeSmall forKey: @"SmallView"];
2783 [fTableView setUsesAlternatingRowBackgroundColors: !makeSmall];
2785 [fTableView setRowHeight: makeSmall ? ROW_HEIGHT_SMALL : ROW_HEIGHT_REGULAR];
2787 [fTableView noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fTableView numberOfRows])]];
2789 //resize for larger min height if not set to auto size
2790 if (![fDefaults boolForKey: @"AutoSize"])
2792 const NSSize contentSize = [[fWindow contentView] frame].size;
2794 NSSize contentMinSize = [fWindow contentMinSize];
2795 contentMinSize.height = [self minWindowContentSizeAllowed];
2796 [fWindow setContentMinSize: contentMinSize];
2798 //make sure the window already isn't too small
2799 if (!makeSmall && contentSize.height < contentMinSize.height)
2801 NSRect frame = [fWindow frame];
2802 CGFloat heightChange = contentMinSize.height - contentSize.height;
2803 frame.size.height += heightChange;
2804 frame.origin.y -= heightChange;
2806 [fWindow setFrame: frame display: YES];
2810 [self setWindowSizeToFit];
2813 - (void) togglePiecesBar: (id) sender
2815 [fDefaults setBool: ![fDefaults boolForKey: @"PiecesBar"] forKey: @"PiecesBar"];
2816 [fTableView togglePiecesBar];
2819 - (void) toggleAvailabilityBar: (id) sender
2821 [fDefaults setBool: ![fDefaults boolForKey: @"DisplayProgressBarAvailable"] forKey: @"DisplayProgressBarAvailable"];
2822 [fTableView display];
2825 - (void) toggleStatusString: (id) sender
2827 if ([fDefaults boolForKey: @"SmallView"])
2828 [fDefaults setBool: ![fDefaults boolForKey: @"DisplaySmallStatusRegular"] forKey: @"DisplaySmallStatusRegular"];
2830 [fDefaults setBool: ![fDefaults boolForKey: @"DisplayStatusProgressSelected"] forKey: @"DisplayStatusProgressSelected"];
2832 [fTableView reloadData];
2835 - (NSRect) windowFrameByAddingHeight: (CGFloat) height checkLimits: (BOOL) check
2837 NSScrollView * scrollView = [fTableView enclosingScrollView];
2839 //convert pixels to points
2840 NSRect windowFrame = [fWindow frame];
2841 NSSize windowSize = [scrollView convertSize: windowFrame.size fromView: nil];
2842 windowSize.height += height;
2846 //we can't call minSize, since it might be set to the current size (auto size)
2847 const CGFloat minHeight = [self minWindowContentSizeAllowed]
2848 + (NSHeight([fWindow frame]) - NSHeight([[fWindow contentView] frame])); //contentView to window
2850 if (windowSize.height < minHeight)
2851 windowSize.height =minHeight;
2854 NSSize maxSize = [scrollView convertSize: [[fWindow screen] visibleFrame].size fromView: nil];
2856 maxSize.height -= STATUS_BAR_HEIGHT;
2858 maxSize.height -= FILTER_BAR_HEIGHT;
2859 if (windowSize.height > maxSize.height)
2860 windowSize.height = maxSize.height;
2864 //convert points to pixels
2865 windowSize = [scrollView convertSize: windowSize toView: nil];
2867 windowFrame.origin.y -= (windowSize.height - windowFrame.size.height);
2868 windowFrame.size.height = windowSize.height;
2872 - (void) toggleStatusBar: (id) sender
2874 const BOOL show = fStatusBar == nil;
2875 [self showStatusBar: show animate: YES];
2876 [fDefaults setBool: show forKey: @"StatusBar"];
2879 //doesn't save shown state
2880 - (void) showStatusBar: (BOOL) show animate: (BOOL) animate
2882 const BOOL prevShown = fStatusBar != nil;
2883 if (show == prevShown)
2888 fStatusBar = [[StatusBarController alloc] initWithLib: fLib];
2890 NSView * contentView = [fWindow contentView];
2891 const NSSize windowSize = [contentView convertSize: [fWindow frame].size fromView: nil];
2893 NSRect statusBarFrame = [[fStatusBar view] frame];
2894 statusBarFrame.size.width = windowSize.width;
2895 [[fStatusBar view] setFrame: statusBarFrame];
2897 [contentView addSubview: [fStatusBar view]];
2898 [[fStatusBar view] setFrameOrigin: NSMakePoint(0.0, NSMaxY([contentView frame]))];
2902 CGFloat heightChange = [[fStatusBar view] frame].size.height;
2906 //allow bar to show even if not enough room
2907 if (show && ![fDefaults boolForKey: @"AutoSize"])
2909 frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
2910 CGFloat change = [[fWindow screen] visibleFrame].size.height - frame.size.height;
2913 frame = [fWindow frame];
2914 frame.size.height += change;
2915 frame.origin.y -= change;
2916 [fWindow setFrame: frame display: NO animate: NO];
2922 NSScrollView * scrollView = [fTableView enclosingScrollView];
2924 //set views to not autoresize
2925 const NSUInteger statsMask = [[fStatusBar view] autoresizingMask];
2926 [[fStatusBar view] setAutoresizingMask: NSViewNotSizable];
2927 NSUInteger filterMask;
2930 filterMask = [[fFilterBar view] autoresizingMask];
2931 [[fFilterBar view] setAutoresizingMask: NSViewNotSizable];
2933 const NSUInteger scrollMask = [scrollView autoresizingMask];
2934 [scrollView setAutoresizingMask: NSViewNotSizable];
2936 frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
2937 [fWindow setFrame: frame display: YES animate: animate];
2939 //re-enable autoresize
2940 [[fStatusBar view] setAutoresizingMask: statsMask];
2942 [[fFilterBar view] setAutoresizingMask: filterMask];
2943 [scrollView setAutoresizingMask: scrollMask];
2947 [[fStatusBar view] removeFromSuperviewWithoutNeedingDisplay];
2948 [fStatusBar release];
2952 if ([fDefaults boolForKey: @"AutoSize"])
2953 [self setWindowMinMaxToCurrent];
2957 NSSize minSize = [fWindow contentMinSize];
2958 minSize.height += heightChange;
2959 [fWindow setContentMinSize: minSize];
2963 - (void) toggleFilterBar: (id) sender
2965 const BOOL show = fFilterBar == nil;
2967 [self showFilterBar: show animate: YES];
2968 [fDefaults setBool: show forKey: @"FilterBar"];
2969 [[fWindow toolbar] validateVisibleItems];
2971 //disable filtering when hiding
2974 [[NSUserDefaults standardUserDefaults] setObject: FILTER_NONE forKey: @"Filter"];
2975 [[NSUserDefaults standardUserDefaults] setInteger: GROUP_FILTER_ALL_TAG forKey: @"FilterGroup"];
2976 [[NSUserDefaults standardUserDefaults] removeObjectForKey: @"FilterSearchString"];
2979 [self applyFilter]; //do even if showing to ensure tooltips are updated
2982 //doesn't save shown state
2983 - (void) showFilterBar: (BOOL) show animate: (BOOL) animate
2985 const BOOL prevShown = fFilterBar != nil;
2986 if (show == prevShown)
2991 fFilterBar = [[FilterBarController alloc] init];
2993 NSView * contentView = [fWindow contentView];
2994 const NSSize windowSize = [contentView convertSize: [fWindow frame].size fromView: nil];
2996 NSRect filterBarFrame = [[fFilterBar view] frame];
2997 filterBarFrame.size.width = windowSize.width;
2998 [[fFilterBar view] setFrame: filterBarFrame];
3001 [contentView addSubview: [fFilterBar view] positioned: NSWindowBelow relativeTo: [fStatusBar view]];
3003 [contentView addSubview: [fFilterBar view]];
3004 const CGFloat originY = fStatusBar ? NSMinY([[fStatusBar view] frame]) : NSMaxY([contentView frame]);
3005 [[fFilterBar view] setFrameOrigin: NSMakePoint(0.0, originY)];
3008 [fWindow makeFirstResponder: fTableView];
3010 CGFloat heightChange = NSHeight([[fFilterBar view] frame]);
3014 //allow bar to show even if not enough room
3015 if (show && ![fDefaults boolForKey: @"AutoSize"])
3017 NSRect frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
3018 const CGFloat change = NSHeight([[fWindow screen] visibleFrame]) - NSHeight(frame);
3021 frame = [fWindow frame];
3022 frame.size.height += change;
3023 frame.origin.y -= change;
3024 [fWindow setFrame: frame display: NO animate: NO];
3028 NSScrollView * scrollView = [fTableView enclosingScrollView];
3030 //set views to not autoresize
3031 const NSUInteger filterMask = [[fFilterBar view] autoresizingMask];
3032 const NSUInteger scrollMask = [scrollView autoresizingMask];
3033 [[fFilterBar view] setAutoresizingMask: NSViewNotSizable];
3034 [scrollView setAutoresizingMask: NSViewNotSizable];
3036 const NSRect frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
3037 [fWindow setFrame: frame display: YES animate: animate];
3039 //re-enable autoresize
3040 [[fFilterBar view] setAutoresizingMask: filterMask];
3041 [scrollView setAutoresizingMask: scrollMask];
3045 [[fFilterBar view] removeFromSuperviewWithoutNeedingDisplay];
3046 [fFilterBar release];
3050 if ([fDefaults boolForKey: @"AutoSize"])
3051 [self setWindowMinMaxToCurrent];
3055 NSSize minSize = [fWindow contentMinSize];
3056 minSize.height += heightChange;
3057 [fWindow setContentMinSize: minSize];
3061 - (void) focusFilterField
3064 [self toggleFilterBar: self];
3065 [fFilterBar focusSearchField];
3068 #warning change from id to QLPreviewPanel
3069 - (BOOL) acceptsPreviewPanelControl: (id) panel
3074 - (void) beginPreviewPanelControl: (id) panel
3076 fPreviewPanel = [panel retain];
3077 [fPreviewPanel setDelegate: self];
3078 [fPreviewPanel setDataSource: self];
3081 - (void) endPreviewPanelControl: (id) panel
3083 [fPreviewPanel release];
3084 fPreviewPanel = nil;
3087 - (NSArray *) quickLookableTorrents
3089 NSArray * selectedTorrents = [fTableView selectedTorrents];
3090 NSMutableArray * qlArray = [NSMutableArray arrayWithCapacity: [selectedTorrents count]];
3092 for (Torrent * torrent in selectedTorrents)
3093 if (([torrent isFolder] || [torrent isComplete]) && [torrent dataLocation])
3094 [qlArray addObject: torrent];
3099 - (NSInteger) numberOfPreviewItemsInPreviewPanel: (id) panel
3101 if ([fInfoController canQuickLook])
3102 return [[fInfoController quickLookURLs] count];
3104 return [[self quickLookableTorrents] count];
3107 - (id /*<QLPreviewItem>*/) previewPanel: (id) panel previewItemAtIndex: (NSInteger) index
3109 if ([fInfoController canQuickLook])
3110 return [[fInfoController quickLookURLs] objectAtIndex: index];
3112 return [[self quickLookableTorrents] objectAtIndex: index];
3115 - (BOOL) previewPanel: (id) panel handleEvent: (NSEvent *) event
3117 /*if ([event type] == NSKeyDown)
3119 [super keyDown: event];
3126 - (NSRect) previewPanel: (id) panel sourceFrameOnScreenForPreviewItem: (id /*<QLPreviewItem>*/) item
3128 if ([fInfoController canQuickLook])
3129 return [fInfoController quickLookSourceFrameForPreviewItem: item];
3132 if (![fWindow isVisible])
3135 const NSInteger row = [fTableView rowForItem: item];
3139 NSRect frame = [fTableView iconRectForRow: row];
3141 if (!NSIntersectsRect([fTableView visibleRect], frame))
3144 frame.origin = [fTableView convertPoint: frame.origin toView: nil];
3145 frame.origin = [fWindow convertBaseToScreen: frame.origin];
3146 frame.origin.y -= frame.size.height;
3151 - (ButtonToolbarItem *) standardToolbarButtonWithIdentifier: (NSString *) ident
3153 ButtonToolbarItem * item = [[ButtonToolbarItem alloc] initWithItemIdentifier: ident];
3155 NSButton * button = [[NSButton alloc] init];
3156 [button setBezelStyle: NSTexturedRoundedBezelStyle];
3157 [button setStringValue: @""];
3159 [item setView: button];
3162 const NSSize buttonSize = NSMakeSize(36.0, 25.0);
3163 [item setMinSize: buttonSize];
3164 [item setMaxSize: buttonSize];
3166 return [item autorelease];
3169 - (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
3171 if ([ident isEqualToString: TOOLBAR_CREATE])
3173 ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
3175 [item setLabel: NSLocalizedString(@"Create", "Create toolbar item -> label")];
3176 [item setPaletteLabel: NSLocalizedString(@"Create Torrent File", "Create toolbar item -> palette label")];
3177 [item setToolTip: NSLocalizedString(@"Create torrent file", "Create toolbar item -> tooltip")];
3178 [item setImage: [NSImage imageNamed: @"ToolbarCreateTemplate.png"]];
3179 [item setTarget: self];
3180 [item setAction: @selector(createFile:)];
3181 [item setAutovalidates: NO];
3185 else if ([ident isEqualToString: TOOLBAR_OPEN_FILE])
3187 ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
3189 [item setLabel: NSLocalizedString(@"Open", "Open toolbar item -> label")];
3190 [item setPaletteLabel: NSLocalizedString(@"Open Torrent Files", "Open toolbar item -> palette label")];
3191 [item setToolTip: NSLocalizedString(@"Open torrent files", "Open toolbar item -> tooltip")];
3192 [item setImage: [NSImage imageNamed: @"ToolbarOpenTemplate.png"]];
3193 [item setTarget: self];
3194 [item setAction: @selector(openShowSheet:)];
3195 [item setAutovalidates: NO];
3199 else if ([ident isEqualToString: TOOLBAR_OPEN_WEB])
3201 ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
3203 [item setLabel: NSLocalizedString(@"Open Address", "Open address toolbar item -> label")];
3204 [item setPaletteLabel: NSLocalizedString(@"Open Torrent Address", "Open address toolbar item -> palette label")];
3205 [item setToolTip: NSLocalizedString(@"Open torrent web address", "Open address toolbar item -> tooltip")];
3206 [item setImage: [NSImage imageNamed: @"ToolbarOpenWebTemplate.png"]];
3207 [item setTarget: self];
3208 [item setAction: @selector(openURLShowSheet:)];
3209 [item setAutovalidates: NO];
3213 else if ([ident isEqualToString: TOOLBAR_REMOVE])
3215 ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
3217 [item setLabel: NSLocalizedString(@"Remove", "Remove toolbar item -> label")];
3218 [item setPaletteLabel: NSLocalizedString(@"Remove Selected", "Remove toolbar item -> palette label")];
3219 [item setToolTip: NSLocalizedString(@"Remove selected transfers", "Remove toolbar item -> tooltip")];
3220 [item setImage: [NSImage imageNamed: @"ToolbarRemoveTemplate.png"]];
3221 [item setTarget: self];
3222 [item setAction: @selector(removeNoDelete:)];
3223 [item setVisibilityPriority: NSToolbarItemVisibilityPriorityHigh];
3227 else if ([ident isEqualToString: TOOLBAR_INFO])
3229 ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
3230 [[(NSButton *)[item view] cell] setShowsStateBy: NSContentsCellMask]; //blue when enabled
3232 [item setLabel: NSLocalizedString(@"Inspector", "Inspector toolbar item -> label")];
3233 [item setPaletteLabel: NSLocalizedString(@"Toggle Inspector", "Inspector toolbar item -> palette label")];
3234 [item setToolTip: NSLocalizedString(@"Toggle the torrent inspector", "Inspector toolbar item -> tooltip")];
3235 [item setImage: [NSImage imageNamed: @"ToolbarInfoTemplate.png"]];
3236 [item setTarget: self];
3237 [item setAction: @selector(showInfo:)];
3241 else if ([ident isEqualToString: TOOLBAR_PAUSE_RESUME_ALL])
3243 GroupToolbarItem * groupItem = [[GroupToolbarItem alloc] initWithItemIdentifier: ident];
3245 NSSegmentedControl * segmentedControl = [[NSSegmentedControl alloc] initWithFrame: NSZeroRect];
3246 [segmentedControl setCell: [[[ToolbarSegmentedCell alloc] init] autorelease]];
3247 [groupItem setView: segmentedControl];
3248 NSSegmentedCell * segmentedCell = (NSSegmentedCell *)[segmentedControl cell];
3250 [segmentedControl setSegmentCount: 2];
3251 [segmentedCell setTrackingMode: NSSegmentSwitchTrackingMomentary];
3253 const NSSize groupSize = NSMakeSize(72.0, 25.0);
3254 [groupItem setMinSize: groupSize];
3255 [groupItem setMaxSize: groupSize];
3257 [groupItem setLabel: NSLocalizedString(@"Apply All", "All toolbar item -> label")];
3258 [groupItem setPaletteLabel: NSLocalizedString(@"Pause / Resume All", "All toolbar item -> palette label")];
3259 [groupItem setTarget: self];
3260 [groupItem setAction: @selector(allToolbarClicked:)];
3262 [groupItem setIdentifiers: [NSArray arrayWithObjects: TOOLBAR_PAUSE_ALL, TOOLBAR_RESUME_ALL, nil]];
3264 [segmentedCell setTag: TOOLBAR_PAUSE_TAG forSegment: TOOLBAR_PAUSE_TAG];
3265 [segmentedControl setImage: [NSImage imageNamed: @"ToolbarPauseAllTemplate.png"] forSegment: TOOLBAR_PAUSE_TAG];
3266 [segmentedCell setToolTip: NSLocalizedString(@"Pause all transfers",
3267 "All toolbar item -> tooltip") forSegment: TOOLBAR_PAUSE_TAG];
3269 [segmentedCell setTag: TOOLBAR_RESUME_TAG forSegment: TOOLBAR_RESUME_TAG];
3270 [segmentedControl setImage: [NSImage imageNamed: @"ToolbarResumeAllTemplate.png"] forSegment: TOOLBAR_RESUME_TAG];
3271 [segmentedCell setToolTip: NSLocalizedString(@"Resume all transfers",
3272 "All toolbar item -> tooltip") forSegment: TOOLBAR_RESUME_TAG];
3274 [groupItem createMenu: [NSArray arrayWithObjects: NSLocalizedString(@"Pause All", "All toolbar item -> label"),
3275 NSLocalizedString(@"Resume All", "All toolbar item -> label"), nil]];
3277 [segmentedControl release];
3279 [groupItem setVisibilityPriority: NSToolbarItemVisibilityPriorityHigh];
3281 return [groupItem autorelease];
3283 else if ([ident isEqualToString: TOOLBAR_PAUSE_RESUME_SELECTED])
3285 GroupToolbarItem * groupItem = [[GroupToolbarItem alloc] initWithItemIdentifier: ident];
3287 NSSegmentedControl * segmentedControl = [[NSSegmentedControl alloc] initWithFrame: NSZeroRect];
3288 [segmentedControl setCell: [[[ToolbarSegmentedCell alloc] init] autorelease]];
3289 [groupItem setView: segmentedControl];
3290 NSSegmentedCell * segmentedCell = (NSSegmentedCell *)[segmentedControl cell];
3292 [segmentedControl setSegmentCount: 2];
3293 [segmentedCell setTrackingMode: NSSegmentSwitchTrackingMomentary];
3295 const NSSize groupSize = NSMakeSize(72.0, 25.0);
3296 [groupItem setMinSize: groupSize];
3297 [groupItem setMaxSize: groupSize];
3299 [groupItem setLabel: NSLocalizedString(@"Apply Selected", "Selected toolbar item -> label")];
3300 [groupItem setPaletteLabel: NSLocalizedString(@"Pause / Resume Selected", "Selected toolbar item -> palette label")];
3301 [groupItem setTarget: self];
3302 [groupItem setAction: @selector(selectedToolbarClicked:)];
3304 [groupItem setIdentifiers: [NSArray arrayWithObjects: TOOLBAR_PAUSE_SELECTED, TOOLBAR_RESUME_SELECTED, nil]];
3306 [segmentedCell setTag: TOOLBAR_PAUSE_TAG forSegment: TOOLBAR_PAUSE_TAG];
3307 [segmentedControl setImage: [NSImage imageNamed: @"ToolbarPauseSelectedTemplate.png"] forSegment: TOOLBAR_PAUSE_TAG];
3308 [segmentedCell setToolTip: NSLocalizedString(@"Pause selected transfers",
3309 "Selected toolbar item -> tooltip") forSegment: TOOLBAR_PAUSE_TAG];
3311 [segmentedCell setTag: TOOLBAR_RESUME_TAG forSegment: TOOLBAR_RESUME_TAG];
3312 [segmentedControl setImage: [NSImage imageNamed: @"ToolbarResumeSelectedTemplate.png"] forSegment: TOOLBAR_RESUME_TAG];
3313 [segmentedCell setToolTip: NSLocalizedString(@"Resume selected transfers",
3314 "Selected toolbar item -> tooltip") forSegment: TOOLBAR_RESUME_TAG];
3316 [groupItem createMenu: [NSArray arrayWithObjects: NSLocalizedString(@"Pause Selected", "Selected toolbar item -> label"),
3317 NSLocalizedString(@"Resume Selected", "Selected toolbar item -> label"), nil]];
3319 [segmentedControl release];
3321 [groupItem setVisibilityPriority: NSToolbarItemVisibilityPriorityHigh];
3323 return [groupItem autorelease];
3325 else if ([ident isEqualToString: TOOLBAR_FILTER])
3327 ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
3328 [[(NSButton *)[item view] cell] setShowsStateBy: NSContentsCellMask]; //blue when enabled
3330 [item setLabel: NSLocalizedString(@"Filter", "Filter toolbar item -> label")];
3331 [item setPaletteLabel: NSLocalizedString(@"Toggle Filter", "Filter toolbar item -> palette label")];
3332 [item setToolTip: NSLocalizedString(@"Toggle the filter bar", "Filter toolbar item -> tooltip")];
3333 [item setImage: [NSImage imageNamed: @"ToolbarFilterTemplate.png"]];
3334 [item setTarget: self];
3335 [item setAction: @selector(toggleFilterBar:)];
3339 else if ([ident isEqualToString: TOOLBAR_QUICKLOOK])
3341 ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
3342 [[(NSButton *)[item view] cell] setShowsStateBy: NSContentsCellMask]; //blue when enabled
3344 [item setLabel: NSLocalizedString(@"Quick Look", "QuickLook toolbar item -> label")];
3345 [item setPaletteLabel: NSLocalizedString(@"Quick Look", "QuickLook toolbar item -> palette label")];
3346 [item setToolTip: NSLocalizedString(@"Quick Look", "QuickLook toolbar item -> tooltip")];
3347 [item setImage: [NSImage imageNamed: NSImageNameQuickLookTemplate]];
3348 [item setTarget: self];
3349 [item setAction: @selector(toggleQuickLook:)];
3350 [item setVisibilityPriority: NSToolbarItemVisibilityPriorityLow];
3358 - (void) allToolbarClicked: (id) sender
3360 NSInteger tagValue = [sender isKindOfClass: [NSSegmentedControl class]]
3361 ? [(NSSegmentedCell *)[sender cell] tagForSegment: [sender selectedSegment]] : [sender tag];
3364 case TOOLBAR_PAUSE_TAG:
3365 [self stopAllTorrents: sender];
3367 case TOOLBAR_RESUME_TAG:
3368 [self resumeAllTorrents: sender];
3373 - (void) selectedToolbarClicked: (id) sender
3375 NSInteger tagValue = [sender isKindOfClass: [NSSegmentedControl class]]
3376 ? [(NSSegmentedCell *)[sender cell] tagForSegment: [sender selectedSegment]] : [sender tag];
3379 case TOOLBAR_PAUSE_TAG:
3380 [self stopSelectedTorrents: sender];
3382 case TOOLBAR_RESUME_TAG:
3383 [self resumeSelectedTorrents: sender];
3388 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
3390 return [NSArray arrayWithObjects:
3391 TOOLBAR_CREATE, TOOLBAR_OPEN_FILE, TOOLBAR_OPEN_WEB, TOOLBAR_REMOVE,
3392 TOOLBAR_PAUSE_RESUME_SELECTED, TOOLBAR_PAUSE_RESUME_ALL,
3393 TOOLBAR_QUICKLOOK, TOOLBAR_FILTER, TOOLBAR_INFO,
3394 NSToolbarSeparatorItemIdentifier,
3395 NSToolbarSpaceItemIdentifier,
3396 NSToolbarFlexibleSpaceItemIdentifier,
3397 NSToolbarCustomizeToolbarItemIdentifier, nil];
3400 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
3402 return [NSArray arrayWithObjects:
3403 TOOLBAR_CREATE, TOOLBAR_OPEN_FILE, TOOLBAR_REMOVE, NSToolbarSpaceItemIdentifier,
3404 TOOLBAR_PAUSE_RESUME_ALL, NSToolbarFlexibleSpaceItemIdentifier,
3405 TOOLBAR_QUICKLOOK, TOOLBAR_FILTER, TOOLBAR_INFO, nil];
3408 - (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
3410 NSString * ident = [toolbarItem itemIdentifier];
3412 //enable remove item
3413 if ([ident isEqualToString: TOOLBAR_REMOVE])
3414 return [fTableView numberOfSelectedRows] > 0;
3416 //enable pause all item
3417 if ([ident isEqualToString: TOOLBAR_PAUSE_ALL])
3419 for (Torrent * torrent in fTorrents)
3420 if ([torrent isActive] || [torrent waitingToStart])
3425 //enable resume all item
3426 if ([ident isEqualToString: TOOLBAR_RESUME_ALL])
3428 for (Torrent * torrent in fTorrents)
3429 if (![torrent isActive] && ![torrent waitingToStart])
3435 if ([ident isEqualToString: TOOLBAR_PAUSE_SELECTED])
3437 for (Torrent * torrent in [fTableView selectedTorrents])
3438 if ([torrent isActive] || [torrent waitingToStart])
3443 //enable resume item
3444 if ([ident isEqualToString: TOOLBAR_RESUME_SELECTED])
3446 for (Torrent * torrent in [fTableView selectedTorrents])
3447 if (![torrent isActive] && ![torrent waitingToStart])
3453 if ([ident isEqualToString: TOOLBAR_INFO])
3455 [(NSButton *)[toolbarItem view] setState: [[fInfoController window] isVisible]];
3460 if ([ident isEqualToString: TOOLBAR_FILTER])
3462 [(NSButton *)[toolbarItem view] setState: fFilterBar != nil];
3466 //set quick look item
3467 if ([ident isEqualToString: TOOLBAR_QUICKLOOK])
3469 [(NSButton *)[toolbarItem view] setState: [NSApp isOnSnowLeopardOrBetter] && [QLPreviewPanelSL sharedPreviewPanelExists]
3470 && [[QLPreviewPanelSL sharedPreviewPanel] isVisible]];
3471 return [NSApp isOnSnowLeopardOrBetter];
3477 - (BOOL) validateMenuItem: (NSMenuItem *) menuItem
3479 SEL action = [menuItem action];
3481 if (action == @selector(toggleSpeedLimit:))
3483 [menuItem setState: [fDefaults boolForKey: @"SpeedLimit"] ? NSOnState : NSOffState];
3487 //only enable some items if it is in a context menu or the window is useable
3488 BOOL canUseTable = [fWindow isKeyWindow] || [[menuItem menu] supermenu] != [NSApp mainMenu];
3491 if (action == @selector(openShowSheet:) || action == @selector(openURLShowSheet:))
3492 return [fWindow attachedSheet] == nil;
3494 //enable sort options
3495 if (action == @selector(setSort:))
3497 NSString * sortType;
3498 switch ([menuItem tag])
3500 case SORT_ORDER_TAG:
3501 sortType = SORT_ORDER;
3504 sortType = SORT_DATE;
3507 sortType = SORT_NAME;
3509 case SORT_PROGRESS_TAG:
3510 sortType = SORT_PROGRESS;
3512 case SORT_STATE_TAG:
3513 sortType = SORT_STATE;
3515 case SORT_TRACKER_TAG:
3516 sortType = SORT_TRACKER;
3518 case SORT_ACTIVITY_TAG:
3519 sortType = SORT_ACTIVITY;
3522 sortType = SORT_SIZE;
3525 NSAssert1(NO, @"Unknown sort tag received: %d", [menuItem tag]);
3528 [menuItem setState: [sortType isEqualToString: [fDefaults stringForKey: @"Sort"]] ? NSOnState : NSOffState];
3529 return [fWindow isVisible];
3532 if (action == @selector(setGroup:))
3536 NSInteger index = [menuItem tag];
3537 for (Torrent * torrent in [fTableView selectedTorrents])
3538 if (index == [torrent groupValue])
3544 [menuItem setState: checked ? NSOnState : NSOffState];
3545 return canUseTable && [fTableView numberOfSelectedRows] > 0;
3548 if (action == @selector(toggleSmallView:))
3550 [menuItem setState: [fDefaults boolForKey: @"SmallView"] ? NSOnState : NSOffState];
3551 return [fWindow isVisible];
3554 if (action == @selector(togglePiecesBar:))
3556 [menuItem setState: [fDefaults boolForKey: @"PiecesBar"] ? NSOnState : NSOffState];
3557 return [fWindow isVisible];
3560 if (action == @selector(toggleStatusString:))
3562 if ([fDefaults boolForKey: @"SmallView"])
3564 [menuItem setTitle: NSLocalizedString(@"Remaining Time", "Action menu -> status string toggle")];
3565 [menuItem setState: ![fDefaults boolForKey: @"DisplaySmallStatusRegular"] ? NSOnState : NSOffState];
3569 [menuItem setTitle: NSLocalizedString(@"Status of Selected Files", "Action menu -> status string toggle")];
3570 [menuItem setState: [fDefaults boolForKey: @"DisplayStatusProgressSelected"] ? NSOnState : NSOffState];
3573 return [fWindow isVisible];
3576 if (action == @selector(toggleAvailabilityBar:))
3578 [menuItem setState: [fDefaults boolForKey: @"DisplayProgressBarAvailable"] ? NSOnState : NSOffState];
3579 return [fWindow isVisible];
3582 if (action == @selector(setLimitGlobalEnabled:))
3584 BOOL upload = [menuItem menu] == fUploadMenu;
3585 BOOL limit = menuItem == (upload ? fUploadLimitItem : fDownloadLimitItem);
3587 [menuItem setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
3588 "Action menu -> upload/download limit"),
3589 [fDefaults integerForKey: upload ? @"UploadLimit" : @"DownloadLimit"]]];
3591 [menuItem setState: [fDefaults boolForKey: upload ? @"CheckUpload" : @"CheckDownload"] ? limit : !limit];
3595 if (action == @selector(setRatioGlobalEnabled:))
3597 BOOL check = menuItem == fCheckRatioItem;
3599 [menuItem setTitle: [NSString localizedStringWithFormat: NSLocalizedString(@"Stop at Ratio (%.2f)",
3600 "Action menu -> ratio stop"), [fDefaults floatForKey: @"RatioLimit"]]];
3602 [menuItem setState: [fDefaults boolForKey: @"RatioCheck"] ? check : !check];
3607 if (action == @selector(showInfo:))
3609 NSString * title = [[fInfoController window] isVisible] ? NSLocalizedString(@"Hide Inspector", "View menu -> Inspector")
3610 : NSLocalizedString(@"Show Inspector", "View menu -> Inspector");
3611 [menuItem setTitle: title];
3616 //enable prev/next inspector tab
3617 if (action == @selector(setInfoTab:))
3618 return [[fInfoController window] isVisible];
3620 //enable toggle status bar
3621 if (action == @selector(toggleStatusBar:))
3623 NSString * title = !fStatusBar ? NSLocalizedString(@"Show Status Bar", "View menu -> Status Bar")
3624 : NSLocalizedString(@"Hide Status Bar", "View menu -> Status Bar");
3625 [menuItem setTitle: title];
3627 return [fWindow isVisible];
3630 //enable toggle filter bar
3631 if (action == @selector(toggleFilterBar:))
3633 NSString * title = !fFilterBar ? NSLocalizedString(@"Show Filter Bar", "View menu -> Filter Bar")
3634 : NSLocalizedString(@"Hide Filter Bar", "View menu -> Filter Bar");
3635 [menuItem setTitle: title];
3637 return [fWindow isVisible];
3640 //enable prev/next filter button
3641 if (action == @selector(switchFilter:))
3642 return [fWindow isVisible] && fFilterBar;
3644 //enable reveal in finder
3645 if (action == @selector(revealFile:))
3646 return canUseTable && [fTableView numberOfSelectedRows] > 0;
3648 //enable remove items
3649 if (action == @selector(removeNoDelete:) || action == @selector(removeDeleteData:))
3653 for (Torrent * torrent in [fTableView selectedTorrents])
3655 if ([torrent isActive])
3657 if ([fDefaults boolForKey: @"CheckRemoveDownloading"] ? ![torrent isSeeding] : YES)
3665 //append or remove ellipsis when needed
3666 NSString * title = [menuItem title], * ellipsis = [NSString ellipsis];
3667 if (warning && [fDefaults boolForKey: @"CheckRemove"])
3669 if (![title hasSuffix: ellipsis])
3670 [menuItem setTitle: [title stringByAppendingEllipsis]];
3674 if ([title hasSuffix: ellipsis])
3675 [menuItem setTitle: [title substringToIndex: [title rangeOfString: ellipsis].location]];
3678 return canUseTable && [fTableView numberOfSelectedRows] > 0;
3681 //remove all completed transfers item
3682 if (action == @selector(clearCompleted:))
3684 //append or remove ellipsis when needed
3685 NSString * title = [menuItem title], * ellipsis = [NSString ellipsis];
3686 if ([fDefaults boolForKey: @"WarningRemoveCompleted"])
3688 if (![title hasSuffix: ellipsis])
3689 [menuItem setTitle: [title stringByAppendingEllipsis]];
3693 if ([title hasSuffix: ellipsis])
3694 [menuItem setTitle: [title substringToIndex: [title rangeOfString: ellipsis].location]];
3697 for (Torrent * torrent in fTorrents)
3698 if ([torrent isFinishedSeeding])
3703 //enable pause all item
3704 if (action == @selector(stopAllTorrents:))
3706 for (Torrent * torrent in fTorrents)
3707 if ([torrent isActive] || [torrent waitingToStart])
3712 //enable resume all item
3713 if (action == @selector(resumeAllTorrents:))
3715 for (Torrent * torrent in fTorrents)
3716 if (![torrent isActive] && ![torrent waitingToStart] && ![torrent isFinishedSeeding])
3721 //enable resume all waiting item
3722 if (action == @selector(resumeWaitingTorrents:))
3724 if (![fDefaults boolForKey: @"Queue"] && ![fDefaults boolForKey: @"QueueSeed"])
3727 for (Torrent * torrent in fTorrents)
3728 if (![torrent isActive] && [torrent waitingToStart])
3733 //enable resume selected waiting item
3734 if (action == @selector(resumeSelectedTorrentsNoWait:))
3739 for (Torrent * torrent in [fTableView selectedTorrents])
3740 if (![torrent isActive])
3746 if (action == @selector(stopSelectedTorrents:))
3751 for (Torrent * torrent in [fTableView selectedTorrents])
3752 if ([torrent isActive] || [torrent waitingToStart])
3757 //enable resume item
3758 if (action == @selector(resumeSelectedTorrents:))
3763 for (Torrent * torrent in [fTableView selectedTorrents])
3764 if (![torrent isActive] && ![torrent waitingToStart])
3769 //enable manual announce item
3770 if (action == @selector(announceSelectedTorrents:))
3775 for (Torrent * torrent in [fTableView selectedTorrents])
3776 if ([torrent canManualAnnounce])
3781 //enable reset cache item
3782 if (action == @selector(verifySelectedTorrents:))
3787 for (Torrent * torrent in [fTableView selectedTorrents])
3788 if (![torrent isMagnet])
3793 //enable move torrent file item
3794 if (action == @selector(moveDataFilesSelected:))
3795 return canUseTable && [fTableView numberOfSelectedRows] > 0;
3797 //enable copy torrent file item
3798 if (action == @selector(copyTorrentFiles:))
3803 for (Torrent * torrent in [fTableView selectedTorrents])
3804 if (![torrent isMagnet])
3809 //enable copy torrent file item
3810 if (action == @selector(copyMagnetLinks:))
3811 return canUseTable && [fTableView numberOfSelectedRows] > 0;
3813 //enable reverse sort item
3814 if (action == @selector(setSortReverse:))
3816 const BOOL isReverse = [menuItem tag] == SORT_DESC_TAG;
3817 [menuItem setState: (isReverse == [fDefaults boolForKey: @"SortReverse"]) ? NSOnState : NSOffState];
3818 return ![[fDefaults stringForKey: @"Sort"] isEqualToString: SORT_ORDER];
3821 //enable group sort item
3822 if (action == @selector(setSortByGroup:))
3824 [menuItem setState: [fDefaults boolForKey: @"SortByGroup"] ? NSOnState : NSOffState];
3828 if (action == @selector(toggleQuickLook:))
3830 const BOOL visible = [NSApp isOnSnowLeopardOrBetter] && [QLPreviewPanelSL sharedPreviewPanelExists]
3831 && [[QLPreviewPanelSL sharedPreviewPanel] isVisible];
3832 //text consistent with Finder
3833 NSString * title = !visible ? NSLocalizedString(@"Quick Look", "View menu -> Quick Look")
3834 : NSLocalizedString(@"Close Quick Look", "View menu -> Quick Look");
3835 [menuItem setTitle: title];
3837 return [NSApp isOnSnowLeopardOrBetter];
3843 - (void) sleepCallback: (natural_t) messageType argument: (void *) messageArgument
3845 switch (messageType)
3847 case kIOMessageSystemWillSleep:
3848 //if there are any running transfers, wait 15 seconds for them to stop
3849 for (Torrent * torrent in fTorrents)
3850 if ([torrent isActive])
3852 //stop all transfers (since some are active) before going to sleep and remember to resume when we wake up
3853 for (Torrent * torrent in fTorrents)
3859 IOAllowPowerChange(fRootPort, (long) messageArgument);
3862 case kIOMessageCanSystemSleep:
3863 if ([fDefaults boolForKey: @"SleepPrevent"])
3865 //prevent idle sleep unless no torrents are active
3866 for (Torrent * torrent in fTorrents)
3867 if ([torrent isActive] && ![torrent isStalled] && ![torrent isError])
3869 IOCancelPowerChange(fRootPort, (long) messageArgument);
3874 IOAllowPowerChange(fRootPort, (long) messageArgument);
3877 case kIOMessageSystemHasPoweredOn:
3878 //resume sleeping transfers after we wake up
3879 for (Torrent * torrent in fTorrents)
3885 - (NSMenu *) applicationDockMenu: (NSApplication *) sender
3887 NSInteger seeding = 0, downloading = 0;
3888 for (Torrent * torrent in fTorrents)
3890 if ([torrent isSeeding])
3892 else if ([torrent isActive])
3897 NSMenuItem * seedingItem = [fDockMenu itemWithTag: DOCK_SEEDING_TAG],
3898 * downloadingItem = [fDockMenu itemWithTag: DOCK_DOWNLOADING_TAG];
3900 BOOL hasSeparator = seedingItem || downloadingItem;
3904 NSString * title = [NSString stringWithFormat: NSLocalizedString(@"%d Seeding", "Dock item - Seeding"), seeding];
3907 seedingItem = [[[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""] autorelease];
3908 [seedingItem setTag: DOCK_SEEDING_TAG];
3909 [fDockMenu insertItem: seedingItem atIndex: 0];
3912 [seedingItem setTitle: title];
3917 [fDockMenu removeItem: seedingItem];
3920 if (downloading > 0)
3922 NSString * title = [NSString stringWithFormat: NSLocalizedString(@"%d Downloading", "Dock item - Downloading"), downloading];
3923 if (!downloadingItem)
3925 downloadingItem = [[[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""] autorelease];
3926 [downloadingItem setTag: DOCK_DOWNLOADING_TAG];
3927 [fDockMenu insertItem: downloadingItem atIndex: seeding > 0 ? 1 : 0];
3930 [downloadingItem setTitle: title];
3934 if (downloadingItem)
3935 [fDockMenu removeItem: downloadingItem];
3938 if (seeding > 0 || downloading > 0)
3941 [fDockMenu insertItem: [NSMenuItem separatorItem] atIndex: (seeding > 0 && downloading > 0) ? 2 : 1];
3946 [fDockMenu removeItemAtIndex: 0];
3952 - (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
3954 //if auto size is enabled, the current frame shouldn't need to change
3955 NSRect frame = [fDefaults boolForKey: @"AutoSize"] ? [window frame] : [self sizedWindowFrame];
3957 frame.size.width = [fDefaults boolForKey: @"SmallView"] ? [fWindow minSize].width : WINDOW_REGULAR_WIDTH;
3961 - (void) setWindowSizeToFit
3963 if ([fDefaults boolForKey: @"AutoSize"])
3965 NSScrollView * scrollView = [fTableView enclosingScrollView];
3967 [scrollView setHasVerticalScroller: NO];
3968 [fWindow setFrame: [self sizedWindowFrame] display: YES animate: YES];
3969 [scrollView setHasVerticalScroller: YES];
3971 //hack to ensure scrollbars don't disappear after resizing
3972 if (![NSApp isOnSnowLeopardOrBetter])
3974 [scrollView setAutohidesScrollers: NO];
3975 [scrollView setAutohidesScrollers: YES];
3978 [self setWindowMinMaxToCurrent];
3982 - (NSRect) sizedWindowFrame
3984 NSInteger groups = ([fDisplayedTorrents count] > 0 && ![[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [Torrent class]])
3985 ? [fDisplayedTorrents count] : 0;
3987 CGFloat heightChange = (GROUP_SEPARATOR_HEIGHT + [fTableView intercellSpacing].height) * groups
3988 + ([fTableView rowHeight] + [fTableView intercellSpacing].height) * ([fTableView numberOfRows] - groups)
3989 - NSHeight([[fTableView enclosingScrollView] frame]);
3991 return [self windowFrameByAddingHeight: heightChange checkLimits: YES];
3994 - (void) updateForAutoSize
3996 if ([fDefaults boolForKey: @"AutoSize"])
3997 [self setWindowSizeToFit];
4000 NSSize contentMinSize = [fWindow contentMinSize];
4001 contentMinSize.height = [self minWindowContentSizeAllowed];
4003 [fWindow setContentMinSize: contentMinSize];
4005 NSSize contentMaxSize = [fWindow contentMaxSize];
4006 contentMaxSize.height = FLT_MAX;
4007 [fWindow setContentMaxSize: contentMaxSize];
4011 - (void) setWindowMinMaxToCurrent
4013 const CGFloat height = NSHeight([[fWindow contentView] frame]);
4015 NSSize minSize = [fWindow contentMinSize],
4016 maxSize = [fWindow contentMaxSize];
4017 minSize.height = height;
4018 maxSize.height = height;
4020 [fWindow setContentMinSize: minSize];
4021 [fWindow setContentMaxSize: maxSize];
4024 - (CGFloat) minWindowContentSizeAllowed
4026 CGFloat contentMinHeight = NSHeight([[fWindow contentView] frame]) - NSHeight([[fTableView enclosingScrollView] frame])
4027 + [fTableView rowHeight] + [fTableView intercellSpacing].height;
4028 return contentMinHeight;
4031 - (void) updateForExpandCollape
4033 [self setWindowSizeToFit];
4034 [self setBottomCountText: YES];
4037 - (void) showMainWindow: (id) sender
4039 [fWindow makeKeyAndOrderFront: nil];
4042 - (void) windowDidBecomeMain: (NSNotification *) notification
4044 [fBadger clearCompleted];
4048 - (void) applicationWillUnhide: (NSNotification *) notification
4053 - (void) toggleQuickLook: (id) sender
4055 if (![NSApp isOnSnowLeopardOrBetter])
4058 if ([[QLPreviewPanelSL sharedPreviewPanel] isVisible])
4059 [[QLPreviewPanelSL sharedPreviewPanel] orderOut: nil];
4061 [[QLPreviewPanelSL sharedPreviewPanel] makeKeyAndOrderFront: nil];
4064 - (void) linkHomepage: (id) sender
4066 [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: WEBSITE_URL]];
4069 - (void) linkForums: (id) sender
4071 [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FORUM_URL]];
4074 - (void) linkTrac: (id) sender
4076 [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: TRAC_URL]];
4079 - (void) linkDonate: (id) sender
4081 [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: DONATE_URL]];
4084 - (void) updaterWillRelaunchApplication: (SUUpdater *) updater
4086 fQuitRequested = YES;
4089 - (NSDictionary *) registrationDictionaryForGrowl
4091 NSArray * notifications = [NSArray arrayWithObjects: GROWL_DOWNLOAD_COMPLETE, GROWL_SEEDING_COMPLETE,
4092 GROWL_AUTO_ADD, GROWL_AUTO_SPEED_LIMIT, nil];
4093 return [NSDictionary dictionaryWithObjectsAndKeys: notifications, GROWL_NOTIFICATIONS_ALL,
4094 notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
4097 - (void) growlNotificationWasClicked: (id) clickContext
4099 if (!clickContext || ![clickContext isKindOfClass: [NSDictionary class]])
4102 NSString * type = [clickContext objectForKey: @"Type"], * location;
4103 if (([type isEqualToString: GROWL_DOWNLOAD_COMPLETE] || [type isEqualToString: GROWL_SEEDING_COMPLETE])
4104 && (location = [clickContext objectForKey: @"Location"]))
4106 if ([NSApp isOnSnowLeopardOrBetter])
4108 NSURL * file = [NSURL fileURLWithPath: location];
4109 [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs: [NSArray arrayWithObject: file]];
4112 [[NSWorkspace sharedWorkspace] selectFile: location inFileViewerRootedAtPath: nil];
4116 - (void) rpcCallback: (tr_rpc_callback_type) type forTorrentStruct: (struct tr_torrent *) torrentStruct
4118 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
4121 Torrent * torrent = nil;
4122 if (torrentStruct != NULL && (type != TR_RPC_TORRENT_ADDED && type != TR_RPC_SESSION_CHANGED && type != TR_RPC_SESSION_CLOSE))
4124 for (torrent in fTorrents)
4125 if (torrentStruct == [torrent torrentStruct])
4135 NSLog(@"No torrent found matching the given torrent struct from the RPC callback!");
4142 case TR_RPC_TORRENT_ADDED:
4143 [self performSelectorOnMainThread: @selector(rpcAddTorrentStruct:) withObject:
4144 [[NSValue valueWithPointer: torrentStruct] retain] waitUntilDone: NO];
4147 case TR_RPC_TORRENT_STARTED:
4148 case TR_RPC_TORRENT_STOPPED:
4149 [self performSelectorOnMainThread: @selector(rpcStartedStoppedTorrent:) withObject: torrent waitUntilDone: NO];
4152 case TR_RPC_TORRENT_REMOVING:
4153 [self performSelectorOnMainThread: @selector(rpcRemoveTorrent:) withObject: torrent waitUntilDone: NO];
4156 case TR_RPC_TORRENT_TRASHING:
4157 [self performSelectorOnMainThread: @selector(rpcRemoveTorrentDeleteData:) withObject: torrent waitUntilDone: NO];
4160 case TR_RPC_TORRENT_CHANGED:
4161 [self performSelectorOnMainThread: @selector(rpcChangedTorrent:) withObject: torrent waitUntilDone: NO];
4164 case TR_RPC_TORRENT_MOVED:
4165 [self performSelectorOnMainThread: @selector(rpcMovedTorrent:) withObject: torrent waitUntilDone: NO];
4168 case TR_RPC_SESSION_CHANGED:
4169 [fPrefsController performSelectorOnMainThread: @selector(rpcUpdatePrefs) withObject: nil waitUntilDone: NO];
4172 case TR_RPC_SESSION_CLOSE:
4173 fQuitRequested = YES;
4174 [NSApp performSelectorOnMainThread: @selector(terminate:) withObject: self waitUntilDone: NO];
4178 NSAssert1(NO, @"Unknown RPC command received: %d", type);
4185 - (void) rpcAddTorrentStruct: (NSValue *) torrentStructPtr
4187 tr_torrent * torrentStruct = (tr_torrent *)[torrentStructPtr pointerValue];
4188 [torrentStructPtr release];
4190 NSString * location = nil;
4191 if (tr_torrentGetDownloadDir(torrentStruct) != NULL)
4192 location = [NSString stringWithUTF8String: tr_torrentGetDownloadDir(torrentStruct)];
4194 Torrent * torrent = [[Torrent alloc] initWithTorrentStruct: torrentStruct location: location lib: fLib];
4197 [fTorrents addObject: torrent];
4200 [self updateTorrentsInQueue];
4203 - (void) rpcRemoveTorrent: (Torrent *) torrent
4205 [self confirmRemoveTorrents: [[NSArray arrayWithObject: torrent] retain] deleteData: NO];
4209 - (void) rpcRemoveTorrentDeleteData: (Torrent *) torrent
4211 [self confirmRemoveTorrents: [[NSArray arrayWithObject: torrent] retain] deleteData: YES];
4215 - (void) rpcStartedStoppedTorrent: (Torrent *) torrent
4222 [self updateTorrentHistory];
4225 - (void) rpcChangedTorrent: (Torrent *) torrent
4229 if ([[fTableView selectedTorrents] containsObject: torrent])
4231 [fInfoController updateInfoStats]; //this will reload the file table
4232 [fInfoController updateOptions];
4238 - (void) rpcMovedTorrent: (Torrent *) torrent
4241 [torrent updateTimeMachineExclude];
4243 if ([[fTableView selectedTorrents] containsObject: torrent])
4244 [fInfoController updateInfoStats];