transmission/macosx/Controller.m

4245 lines
158 KiB
Mathematica
Raw Normal View History

2007-09-16 01:02:06 +00:00
/******************************************************************************
* $Id$
*
* Copyright (c) 2005-2011 Transmission authors and contributors
2007-09-16 01:02:06 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#import <IOKit/IOMessage.h>
#import <IOKit/pwr_mgt/IOPMLib.h>
#import <Carbon/Carbon.h>
2007-09-16 01:02:06 +00:00
#import "Controller.h"
#import "Torrent.h"
#import "TorrentGroup.h"
2007-09-16 01:02:06 +00:00
#import "TorrentTableView.h"
#import "CreatorWindowController.h"
#import "StatsWindowController.h"
2009-10-11 02:22:38 +00:00
#import "InfoWindowController.h"
#import "PrefsController.h"
#import "GroupsController.h"
2007-09-16 01:02:06 +00:00
#import "AboutWindowController.h"
#import "URLSheetWindowController.h"
2009-10-11 02:22:38 +00:00
#import "AddWindowController.h"
#import "AddMagnetWindowController.h"
2009-10-11 02:22:38 +00:00
#import "MessageWindowController.h"
#import "GlobalOptionsPopoverViewController.h"
#import "ButtonToolbarItem.h"
#import "GroupToolbarItem.h"
2008-04-22 13:25:53 +00:00
#import "ToolbarSegmentedCell.h"
#import "BlocklistDownloader.h"
#import "StatusBarController.h"
2011-02-20 03:58:50 +00:00
#import "FilterBarController.h"
#import "BonjourController.h"
2009-10-11 02:22:38 +00:00
#import "Badger.h"
#import "DragOverlayWindow.h"
#import "NSApplicationAdditions.h"
2007-09-16 01:02:06 +00:00
#import "NSStringAdditions.h"
#import "ExpandedPathToPathTransformer.h"
#import "ExpandedPathToIconTransformer.h"
#import "transmission.h"
#import "bencode.h"
#import "utils.h"
2007-09-16 01:02:06 +00:00
#import "UKKQueue.h"
2007-09-16 01:02:06 +00:00
#import <Sparkle/Sparkle.h>
#define TOOLBAR_CREATE @"Toolbar Create"
#define TOOLBAR_OPEN_FILE @"Toolbar Open"
#define TOOLBAR_OPEN_WEB @"Toolbar Open Web"
#define TOOLBAR_REMOVE @"Toolbar Remove"
#define TOOLBAR_INFO @"Toolbar Info"
#define TOOLBAR_PAUSE_ALL @"Toolbar Pause All"
#define TOOLBAR_RESUME_ALL @"Toolbar Resume All"
#define TOOLBAR_PAUSE_RESUME_ALL @"Toolbar Pause / Resume All"
#define TOOLBAR_PAUSE_SELECTED @"Toolbar Pause Selected"
#define TOOLBAR_RESUME_SELECTED @"Toolbar Resume Selected"
#define TOOLBAR_PAUSE_RESUME_SELECTED @"Toolbar Pause / Resume Selected"
#define TOOLBAR_FILTER @"Toolbar Toggle Filter"
2008-05-21 16:02:38 +00:00
#define TOOLBAR_QUICKLOOK @"Toolbar QuickLook"
2007-09-16 01:02:06 +00:00
typedef enum
{
TOOLBAR_PAUSE_TAG = 0,
TOOLBAR_RESUME_TAG = 1
} toolbarGroupTag;
2007-09-16 01:02:06 +00:00
#define SORT_DATE @"Date"
#define SORT_NAME @"Name"
#define SORT_STATE @"State"
#define SORT_PROGRESS @"Progress"
#define SORT_TRACKER @"Tracker"
#define SORT_ORDER @"Order"
2007-11-21 18:32:41 +00:00
#define SORT_ACTIVITY @"Activity"
2011-07-04 19:47:15 +00:00
#define SORT_SIZE @"Size"
2007-09-16 01:02:06 +00:00
typedef enum
{
SORT_ORDER_TAG = 0,
SORT_DATE_TAG = 1,
SORT_NAME_TAG = 2,
SORT_PROGRESS_TAG = 3,
SORT_STATE_TAG = 4,
2007-11-21 18:32:41 +00:00
SORT_TRACKER_TAG = 5,
2011-07-04 19:47:15 +00:00
SORT_ACTIVITY_TAG = 6,
SORT_SIZE_TAG = 7
} sortTag;
typedef enum
{
SORT_ASC_TAG = 0,
SORT_DESC_TAG = 1
} sortOrderTag;
2007-09-16 01:02:06 +00:00
#define GROWL_DOWNLOAD_COMPLETE @"Download Complete"
#define GROWL_SEEDING_COMPLETE @"Seeding Complete"
#define GROWL_AUTO_ADD @"Torrent Auto Added"
#define GROWL_AUTO_SPEED_LIMIT @"Speed Limit Auto Changed"
#define TORRENT_TABLE_VIEW_DATA_TYPE @"TorrentTableViewDataType"
2008-01-17 01:57:14 +00:00
#define ROW_HEIGHT_REGULAR 62.0
#define ROW_HEIGHT_SMALL 22.0
2007-09-16 01:02:06 +00:00
#define WINDOW_REGULAR_WIDTH 468.0
#define STATUS_BAR_HEIGHT 21.0
#define FILTER_BAR_HEIGHT 23.0
#define UPDATE_UI_SECONDS 1.0
2007-09-16 01:02:06 +00:00
#define DOCK_SEEDING_TAG 101
#define DOCK_DOWNLOADING_TAG 102
#define TRANSFER_PLIST @"/Library/Application Support/Transmission/Transfers.plist"
2007-12-30 18:53:44 +00:00
#define WEBSITE_URL @"http://www.transmissionbt.com/"
2008-01-06 17:00:38 +00:00
#define FORUM_URL @"http://forum.transmissionbt.com/"
2010-12-20 14:25:20 +00:00
#define TRAC_URL @"http://trac.transmissionbt.com/"
2007-12-30 18:53:44 +00:00
#define DONATE_URL @"http://www.transmissionbt.com/donate.php"
2007-09-16 01:02:06 +00:00
#define DONATE_NAG_TIME (60 * 60 * 24 * 7)
static void altSpeedToggledCallback(tr_session * handle UNUSED, bool active, bool byUser, void * controller)
{
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: [[NSNumber alloc] initWithBool: active], @"Active",
[[NSNumber alloc] initWithBool: byUser], @"ByUser", nil];
[(Controller *)controller performSelectorOnMainThread: @selector(altSpeedToggledCallbackIsLimited:)
withObject: dict waitUntilDone: NO];
}
2010-09-18 19:40:12 +00:00
static tr_rpc_callback_status rpcCallback(tr_session * handle UNUSED, tr_rpc_callback_type type, struct tr_torrent * torrentStruct,
void * controller)
2007-09-16 01:02:06 +00:00
{
[(Controller *)controller rpcCallback: type forTorrentStruct: torrentStruct];
return TR_RPC_NOREMOVE; //we'll do the remove manually
}
static void sleepCallback(void * controller, io_service_t y, natural_t messageType, void * messageArgument)
{
[(Controller *)controller sleepCallback: messageType argument: messageArgument];
2007-09-16 01:02:06 +00:00
}
@implementation Controller
+ (void) initialize
{
//make sure another Transmission.app isn't running already
BOOL othersRunning = NO;
2009-10-10 13:24:13 +00:00
if ([NSApp isOnSnowLeopardOrBetter])
{
2009-10-10 13:24:13 +00:00
NSArray * apps = [NSRunningApplicationSL runningApplicationsWithBundleIdentifier: [[NSBundle mainBundle] bundleIdentifier]];
othersRunning = [apps count] > 1;
}
2009-10-10 13:24:13 +00:00
else
2007-09-16 01:02:06 +00:00
{
NSString * bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
const int processIdentifier = [[NSProcessInfo processInfo] processIdentifier];
for (NSDictionary * dic in [[NSWorkspace sharedWorkspace] launchedApplications])
2007-09-16 01:02:06 +00:00
{
if ([[dic objectForKey: @"NSApplicationBundleIdentifier"] isEqualToString: bundleIdentifier]
&& [[dic objectForKey: @"NSApplicationProcessIdentifier"] intValue] != processIdentifier)
othersRunning = YES;
2007-09-16 01:02:06 +00:00
}
}
if (othersRunning)
{
NSAlert * alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: NSLocalizedString(@"Quit", "Transmission already running alert -> button")];
[alert setMessageText: NSLocalizedString(@"Transmission is already running.",
"Transmission already running alert -> title")];
[alert setInformativeText: NSLocalizedString(@"There is already a copy of Transmission running. "
"This copy cannot be opened until that instance is quit.", "Transmission already running alert -> message")];
[alert setAlertStyle: NSCriticalAlertStyle];
[alert runModal];
[alert release];
//kill ourselves right away
exit(0);
}
2007-09-16 01:02:06 +00:00
[[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource: @"Defaults" ofType: @"plist"]]];
//set custom value transformers
2008-03-29 23:38:38 +00:00
ExpandedPathToPathTransformer * pathTransformer = [[[ExpandedPathToPathTransformer alloc] init] autorelease];
2007-09-16 01:02:06 +00:00
[NSValueTransformer setValueTransformer: pathTransformer forName: @"ExpandedPathToPathTransformer"];
2008-03-29 23:38:38 +00:00
ExpandedPathToIconTransformer * iconTransformer = [[[ExpandedPathToIconTransformer alloc] init] autorelease];
2007-09-16 01:02:06 +00:00
[NSValueTransformer setValueTransformer: iconTransformer forName: @"ExpandedPathToIconTransformer"];
//cover our asses
2009-10-02 21:51:56 +00:00
if ([[NSUserDefaults standardUserDefaults] boolForKey: @"WarningLegal"])
{
NSAlert * alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: NSLocalizedString(@"I Accept", "Legal alert -> button")];
[alert addButtonWithTitle: NSLocalizedString(@"Quit", "Legal alert -> button")];
2010-05-01 02:29:01 +00:00
[alert setMessageText: NSLocalizedString(@"Welcome to Transmission", "Legal alert -> title")];
[alert setInformativeText: NSLocalizedString(@"Transmission is a file-sharing program."
" When you run a torrent, its data will be made available to others by means of upload."
" You and you alone are fully responsible for exercising proper judgement and abiding by your local laws.",
"Legal alert -> message")];
[alert setAlertStyle: NSInformationalAlertStyle];
if ([alert runModal] == NSAlertSecondButtonReturn)
exit(0);
[alert release];
[[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningLegal"];
}
2007-09-16 01:02:06 +00:00
}
- (id) init
{
if ((self = [super init]))
{
2007-12-24 15:29:29 +00:00
fDefaults = [NSUserDefaults standardUserDefaults];
//checks for old version speeds of -1
if ([fDefaults integerForKey: @"UploadLimit"] < 0)
{
[fDefaults removeObjectForKey: @"UploadLimit"];
[fDefaults setBool: NO forKey: @"CheckUpload"];
}
if ([fDefaults integerForKey: @"DownloadLimit"] < 0)
{
[fDefaults removeObjectForKey: @"DownloadLimit"];
[fDefaults setBool: NO forKey: @"CheckDownload"];
}
//upgrading from versions < 2.40: clear recent items
[[NSDocumentController sharedDocumentController] clearRecentDocuments: nil];
tr_benc settings;
tr_bencInitDict(&settings, 41);
2011-03-21 16:28:44 +00:00
tr_sessionGetDefaultSettings(&settings);
const BOOL usesSpeedLimitSched = [fDefaults boolForKey: @"SpeedLimitAuto"];
if (!usesSpeedLimitSched)
tr_bencDictAddBool(&settings, TR_PREFS_KEY_ALT_SPEED_ENABLED, [fDefaults boolForKey: @"SpeedLimit"]);
2010-07-06 03:31:17 +00:00
tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_UP_KBps, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_DOWN_KBps, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, [fDefaults boolForKey: @"SpeedLimitAuto"]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, [PrefsController dateToTimeSum:
[fDefaults objectForKey: @"SpeedLimitAutoOnDate"]]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_TIME_END, [PrefsController dateToTimeSum:
[fDefaults objectForKey: @"SpeedLimitAutoOffDate"]]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, [fDefaults integerForKey: @"SpeedLimitAutoDay"]);
2010-07-06 03:31:17 +00:00
tr_bencDictAddInt(&settings, TR_PREFS_KEY_DSPEED_KBps, [fDefaults integerForKey: @"DownloadLimit"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_DSPEED_ENABLED, [fDefaults boolForKey: @"CheckDownload"]);
2010-07-06 03:31:17 +00:00
tr_bencDictAddInt(&settings, TR_PREFS_KEY_USPEED_KBps, [fDefaults integerForKey: @"UploadLimit"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_USPEED_ENABLED, [fDefaults boolForKey: @"CheckUpload"]);
//hidden prefs
if ([fDefaults objectForKey: @"BindAddressIPv4"])
2010-04-20 23:47:28 +00:00
tr_bencDictAddStr(&settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4, [[fDefaults stringForKey: @"BindAddressIPv4"] UTF8String]);
if ([fDefaults objectForKey: @"BindAddressIPv6"])
2010-04-20 23:47:28 +00:00
tr_bencDictAddStr(&settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6, [[fDefaults stringForKey: @"BindAddressIPv6"] UTF8String]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, [fDefaults boolForKey: @"BlocklistNew"]);
if ([fDefaults objectForKey: @"BlocklistURL"])
tr_bencDictAddStr(&settings, TR_PREFS_KEY_BLOCKLIST_URL, [[fDefaults stringForKey: @"BlocklistURL"] UTF8String]);
2009-05-21 04:35:40 +00:00
tr_bencDictAddBool(&settings, TR_PREFS_KEY_DHT_ENABLED, [fDefaults boolForKey: @"DHTGlobal"]);
tr_bencDictAddStr(&settings, TR_PREFS_KEY_DOWNLOAD_DIR, [[[fDefaults stringForKey: @"DownloadFolder"]
stringByExpandingTildeInPath] UTF8String]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_DOWNLOAD_QUEUE_ENABLED, [fDefaults boolForKey: @"Queue"]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_DOWNLOAD_QUEUE_SIZE, [fDefaults integerForKey: @"QueueDownloadNumber"]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_IDLE_LIMIT, [fDefaults integerForKey: @"IdleLimitMinutes"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_IDLE_LIMIT_ENABLED, [fDefaults boolForKey: @"IdleLimitCheck"]);
tr_bencDictAddStr(&settings, TR_PREFS_KEY_INCOMPLETE_DIR, [[[fDefaults stringForKey: @"IncompleteDownloadFolder"]
stringByExpandingTildeInPath] UTF8String]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, [fDefaults boolForKey: @"UseIncompleteDownloadFolder"]);
2010-08-03 23:54:02 +00:00
tr_bencDictAddBool(&settings, TR_PREFS_KEY_LPD_ENABLED, [fDefaults boolForKey: @"LocalPeerDiscoveryGlobal"]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_MSGLEVEL, TR_MSG_DBG);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, [fDefaults integerForKey: @"PeersTotal"]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, [fDefaults integerForKey: @"PeersTorrent"]);
const BOOL randomPort = [fDefaults boolForKey: @"RandomPort"];
tr_bencDictAddBool(&settings, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, randomPort);
if (!randomPort)
tr_bencDictAddInt(&settings, TR_PREFS_KEY_PEER_PORT, [fDefaults integerForKey: @"BindPort"]);
//hidden pref
if ([fDefaults objectForKey: @"PeerSocketTOS"])
tr_bencDictAddStr(&settings, TR_PREFS_KEY_PEER_SOCKET_TOS, [[fDefaults stringForKey: @"PeerSocketTOS"] UTF8String]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_PEX_ENABLED, [fDefaults boolForKey: @"PEXGlobal"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_PORT_FORWARDING, [fDefaults boolForKey: @"NatTraversal"]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_QUEUE_STALLED_MINUTES, [fDefaults integerForKey: @"StalledMinutes"]);
tr_bencDictAddReal(&settings, TR_PREFS_KEY_RATIO, [fDefaults floatForKey: @"RatioLimit"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_RATIO_ENABLED, [fDefaults boolForKey: @"RatioCheck"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_RENAME_PARTIAL_FILES, [fDefaults boolForKey: @"RenamePartialFiles"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, [fDefaults boolForKey: @"RPCAuthorize"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_RPC_ENABLED, [fDefaults boolForKey: @"RPC"]);
2009-10-22 00:10:41 +00:00
tr_bencDictAddInt(&settings, TR_PREFS_KEY_RPC_PORT, [fDefaults integerForKey: @"RPCPort"]);
tr_bencDictAddStr(&settings, TR_PREFS_KEY_RPC_USERNAME, [[fDefaults stringForKey: @"RPCUsername"] UTF8String]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, [fDefaults boolForKey: @"RPCUseWhitelist"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_SEED_QUEUE_ENABLED, [fDefaults boolForKey: @"QueueSeed"]);
tr_bencDictAddInt(&settings, TR_PREFS_KEY_SEED_QUEUE_SIZE, [fDefaults integerForKey: @"QueueSeedNumber"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_START, [fDefaults boolForKey: @"AutoStartDownload"]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_ENABLED, [fDefaults boolForKey: @"DoneScriptEnabled"]);
tr_bencDictAddStr(&settings, TR_PREFS_KEY_SCRIPT_TORRENT_DONE_FILENAME, [[fDefaults stringForKey: @"DoneScriptPath"] UTF8String]);
tr_bencDictAddBool(&settings, TR_PREFS_KEY_UTP_ENABLED, [fDefaults boolForKey: @"UTPGlobal"]);
tr_formatter_size_init([NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024,
[NSLocalizedString(@"KB", "File size - kilobytes") UTF8String],
[NSLocalizedString(@"MB", "File size - megabytes") UTF8String],
[NSLocalizedString(@"GB", "File size - gigabytes") UTF8String],
[NSLocalizedString(@"TB", "File size - terabytes") UTF8String]);
tr_formatter_speed_init([NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024,
[NSLocalizedString(@"KB/s", "Transfer speed (kilobytes per second)") UTF8String],
[NSLocalizedString(@"MB/s", "Transfer speed (megabytes per second)") UTF8String],
[NSLocalizedString(@"GB/s", "Transfer speed (gigabytes per second)") UTF8String],
[NSLocalizedString(@"TB/s", "Transfer speed (terabytes per second)") UTF8String]); //why not?
tr_formatter_mem_init(1024, [NSLocalizedString(@"KB", "Memory size - kilobytes") UTF8String],
[NSLocalizedString(@"MB", "Memory size - megabytes") UTF8String],
[NSLocalizedString(@"GB", "Memory size - gigabytes") UTF8String],
[NSLocalizedString(@"TB", "Memory size - terabytes") UTF8String]);
2010-07-06 03:31:17 +00:00
2011-03-21 16:30:58 +00:00
const char * configDir = tr_getDefaultConfigDir("Transmission");
fLib = tr_sessionInit("macosx", configDir, YES, &settings);
tr_bencFree(&settings);
[NSApp setDelegate: self];
//register for magnet URLs (has to be in init)
[[NSAppleEventManager sharedAppleEventManager] setEventHandler: self andSelector: @selector(handleOpenContentsEvent:replyEvent:)
forEventClass: kInternetEventClass andEventID: kAEGetURL];
2007-09-16 01:02:06 +00:00
fTorrents = [[NSMutableArray alloc] init];
fDisplayedTorrents = [[NSMutableArray alloc] init];
fInfoController = [[InfoWindowController alloc] init];
[PrefsController setHandle: fLib];
fPrefsController = [[PrefsController alloc] init];
2007-09-16 01:02:06 +00:00
fQuitting = NO;
fGlobalPopoverShown = NO;
fSoundPlaying = NO;
tr_sessionSetAltSpeedFunc(fLib, altSpeedToggledCallback, self);
if (usesSpeedLimitSched)
[fDefaults setBool: tr_sessionUsesAltSpeed(fLib) forKey: @"SpeedLimit"];
tr_sessionSetRPCCallback(fLib, rpcCallback, self);
2007-09-16 01:02:06 +00:00
[GrowlApplicationBridge setGrowlDelegate: self];
2008-08-15 01:44:46 +00:00
2007-09-16 01:02:06 +00:00
[[UKKQueue sharedFileWatcher] setDelegate: self];
2008-08-15 01:44:46 +00:00
2009-03-01 17:02:58 +00:00
[[SUUpdater sharedUpdater] setDelegate: self];
fQuitRequested = NO;
fPauseOnLaunch = (GetCurrentKeyModifiers() & (optionKey | rightOptionKey)) != 0;
2007-09-16 01:02:06 +00:00
}
return self;
}
- (void) awakeFromNib
{
NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"TRMainToolbar"];
2007-09-16 01:02:06 +00:00
[toolbar setDelegate: self];
[toolbar setAllowsUserCustomization: YES];
[toolbar setAutosavesConfiguration: YES];
2007-12-08 18:51:02 +00:00
[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
2007-09-16 01:02:06 +00:00
[fWindow setToolbar: toolbar];
[toolbar release];
[fWindow setDelegate: self]; //do manually to avoid placement issue
[fWindow makeFirstResponder: fTableView];
[fWindow setExcludedFromWindowsMenu: YES];
//set table size
const BOOL small = [fDefaults boolForKey: @"SmallView"];
if (small)
2007-09-16 01:02:06 +00:00
[fTableView setRowHeight: ROW_HEIGHT_SMALL];
[fTableView setUsesAlternatingRowBackgroundColors: !small];
2007-09-16 01:02:06 +00:00
[fWindow setContentBorderThickness: NSMinY([[fTableView enclosingScrollView] frame]) forEdge: NSMinYEdge];
[fWindow setMovableByWindowBackground: YES];
2008-12-26 05:57:51 +00:00
[[fTotalTorrentsField cell] setBackgroundStyle: NSBackgroundStyleRaised];
//set up filter bar
2007-09-16 01:02:06 +00:00
[self showFilterBar: [fDefaults boolForKey: @"FilterBar"] animate: NO];
//set up status bar
[self showStatusBar: [fDefaults boolForKey: @"StatusBar"] animate: NO];
2007-09-16 01:02:06 +00:00
[fActionButton setToolTip: NSLocalizedString(@"Shortcuts for changing global settings.",
"Main window -> 1st bottom left button (action) tooltip")];
[fSpeedLimitButton setToolTip: NSLocalizedString(@"Speed Limit overrides the total bandwidth limits with its own limits.",
"Main window -> 2nd bottom left button (turtle) tooltip")];
[fClearCompletedButton setToolTip: NSLocalizedString(@"Remove all transfers that have completed seeding.",
"Main window -> 3rd bottom left button (remove all) tooltip")];
2007-09-16 01:02:06 +00:00
2008-02-07 11:57:24 +00:00
[fTableView registerForDraggedTypes: [NSArray arrayWithObject: TORRENT_TABLE_VIEW_DATA_TYPE]];
2007-09-16 01:02:06 +00:00
[fWindow registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, NSURLPboardType, nil]];
//you would think this would be called later in this method from updateUI, but it's not reached in awakeFromNib
//this must be called after showStatusBar:
[fStatusBar updateWithDownload: 0.0 upload: 0.0];
2007-09-16 01:02:06 +00:00
//this should also be after the rest of the setup
[self updateForAutoSize];
2007-09-16 01:02:06 +00:00
//register for sleep notifications
IONotificationPortRef notify;
io_object_t iterator;
if ((fRootPort = IORegisterForSystemPower(self, & notify, sleepCallback, &iterator)))
2007-09-16 01:02:06 +00:00
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), kCFRunLoopCommonModes);
else
NSLog(@"Could not IORegisterForSystemPower");
//load previous transfers
NSArray * history = [NSArray arrayWithContentsOfFile: [NSHomeDirectory() stringByAppendingPathComponent: TRANSFER_PLIST]];
2007-09-16 01:02:06 +00:00
if (!history)
{
//old version saved transfer info in prefs file
2007-09-16 01:02:06 +00:00
if ((history = [fDefaults arrayForKey: @"History"]))
[fDefaults removeObjectForKey: @"History"];
2007-09-16 01:02:06 +00:00
}
if (history)
{
NSMutableArray * waitToStartTorrents = [NSMutableArray arrayWithCapacity: (([history count] > 0 && !fPauseOnLaunch) ? [history count]-1 : 0)]; //theoretical max without doing a lot of work
for (NSDictionary * historyItem in history)
{
Torrent * torrent;
if ((torrent = [[Torrent alloc] initWithHistory: historyItem lib: fLib forcePause: fPauseOnLaunch]))
2007-09-16 01:02:06 +00:00
{
[fTorrents addObject: torrent];
NSNumber * waitToStart;
if (!fPauseOnLaunch && (waitToStart = [historyItem objectForKey: @"WaitToStart"]) && [waitToStart boolValue])
[waitToStartTorrents addObject: torrent];
2007-09-16 01:02:06 +00:00
[torrent release];
}
}
//now that all are loaded, let's set those in the queue to waiting
for (Torrent * torrent in waitToStartTorrents)
[torrent startTransfer];
2007-09-16 01:02:06 +00:00
}
fBadger = [[Badger alloc] initWithLib: fLib];
2007-09-16 01:02:06 +00:00
//observe notifications
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc addObserver: self selector: @selector(updateUI)
name: @"UpdateUI" object: nil];
[nc addObserver: self selector: @selector(torrentFinishedDownloading:)
name: @"TorrentFinishedDownloading" object: nil];
[nc addObserver: self selector: @selector(torrentRestartedDownloading:)
name: @"TorrentRestartedDownloading" object: nil];
[nc addObserver: self selector: @selector(torrentFinishedSeeding:)
name: @"TorrentFinishedSeeding" object: nil];
2007-09-16 01:02:06 +00:00
//avoids need of setting delegate
[nc addObserver: self selector: @selector(torrentTableViewSelectionDidChange:)
name: NSOutlineViewSelectionDidChangeNotification object: fTableView];
2007-09-16 01:02:06 +00:00
[nc addObserver: self selector: @selector(changeAutoImport)
name: @"AutoImportSettingChange" object: nil];
[nc addObserver: self selector: @selector(updateForAutoSize)
2007-09-16 01:02:06 +00:00
name: @"AutoSizeSettingChange" object: nil];
[nc addObserver: self selector: @selector(updateForExpandCollape)
name: @"OutlineExpandCollapse" object: nil];
2007-09-16 01:02:06 +00:00
[nc addObserver: fWindow selector: @selector(makeKeyWindow)
name: @"MakeWindowKey" object: nil];
#warning look at this
2007-09-16 01:02:06 +00:00
[nc addObserver: self selector: @selector(updateTorrentsInQueue)
name: @"UpdateQueue" object: nil];
2011-02-20 03:58:50 +00:00
[nc addObserver: self selector: @selector(applyFilter)
name: @"ApplyFilter" object: nil];
2007-09-16 01:02:06 +00:00
//open newly created torrent file
[nc addObserver: self selector: @selector(beginCreateFile:)
name: @"BeginCreateTorrentFile" object: nil];
//open newly created torrent file
[nc addObserver: self selector: @selector(openCreatedFile:)
name: @"OpenCreatedTorrentFile" object: nil];
//timer to update the interface every second
[self updateUI];
fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_UI_SECONDS target: self
selector: @selector(updateUI) userInfo: nil repeats: YES];
2008-09-01 23:41:46 +00:00
[[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSModalPanelRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSEventTrackingRunLoopMode];
2007-09-16 01:02:06 +00:00
2011-02-20 03:58:50 +00:00
[self applyFilter];
2007-09-16 01:02:06 +00:00
[fWindow makeKeyAndOrderFront: nil];
2007-09-16 01:02:06 +00:00
if ([fDefaults boolForKey: @"InfoVisible"])
[self showInfo: nil];
}
- (void) applicationDidFinishLaunching: (NSNotification *) notification
{
[NSApp setServicesProvider: self];
//register for dock icon drags (has to be in applicationDidFinishLaunching: to work)
[[NSAppleEventManager sharedAppleEventManager] setEventHandler: self andSelector: @selector(handleOpenContentsEvent:replyEvent:)
forEventClass: kCoreEventClass andEventID: kAEOpenContents];
//auto importing
[self checkAutoImportDirectory];
//registering the Web UI to Bonjour
if ([fDefaults boolForKey: @"RPC"] && [fDefaults boolForKey: @"RPCWebDiscovery"])
[[BonjourController defaultController] startWithPort: [fDefaults integerForKey: @"RPCPort"]];
//shamelessly ask for donations
2009-08-19 00:38:53 +00:00
if ([fDefaults boolForKey: @"WarningDonate"])
{
2009-08-19 00:38:53 +00:00
tr_session_stats stats;
tr_sessionGetCumulativeStats(fLib, &stats);
const BOOL firstLaunch = stats.sessionCount <= 1;
2009-08-19 00:38:53 +00:00
NSDate * lastDonateDate = [fDefaults objectForKey: @"DonateAskDate"];
const BOOL timePassed = !lastDonateDate || (-1 * [lastDonateDate timeIntervalSinceNow]) >= DONATE_NAG_TIME;
2009-08-19 00:38:53 +00:00
if (!firstLaunch && timePassed)
{
[fDefaults setObject: [NSDate date] forKey: @"DonateAskDate"];
2009-08-19 00:38:53 +00:00
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: NSLocalizedString(@"Support open-source indie software", "Donation beg -> title")];
NSString * donateMessage = [NSString stringWithFormat: @"%@\n\n%@",
NSLocalizedString(@"Transmission is a full-featured torrent application."
" A lot of time and effort have gone into development, coding, and refinement."
" If you enjoy using it, please consider showing your love with a donation.", "Donation beg -> message"),
NSLocalizedString(@"Donate or not, there will be no difference to your torrenting experience.", "Donation beg -> message")];
[alert setInformativeText: donateMessage];
[alert setAlertStyle: NSInformationalAlertStyle];
[alert addButtonWithTitle: [NSLocalizedString(@"Donate", "Donation beg -> button") stringByAppendingEllipsis]];
NSButton * noDonateButton = [alert addButtonWithTitle: NSLocalizedString(@"Nope", "Donation beg -> button")];
[noDonateButton setKeyEquivalent: @"\e"]; //escape key
const BOOL allowNeverAgain = lastDonateDate != nil; //hide the "don't show again" check the first time - give them time to try the app
[alert setShowsSuppressionButton: allowNeverAgain];
if (allowNeverAgain)
[[alert suppressionButton] setTitle: NSLocalizedString(@"Don't bug me about this ever again.", "Donation beg -> button")];
const NSInteger donateResult = [alert runModal];
if (donateResult == NSAlertFirstButtonReturn)
[self linkDonate: self];
if (allowNeverAgain)
[fDefaults setBool: ([[alert suppressionButton] state] != NSOnState) forKey: @"WarningDonate"];
[alert release];
}
}
2007-09-16 01:02:06 +00:00
}
- (BOOL) applicationShouldHandleReopen: (NSApplication *) app hasVisibleWindows: (BOOL) visibleWindows
{
2009-12-12 02:39:31 +00:00
NSWindow * mainWindow = [NSApp mainWindow];
if (!mainWindow || ![mainWindow isVisible])
[fWindow makeKeyAndOrderFront: nil];
2007-09-16 01:02:06 +00:00
return NO;
}
- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) sender
{
if (!fQuitRequested && [fDefaults boolForKey: @"CheckQuit"])
2007-09-16 01:02:06 +00:00
{
2008-11-02 13:52:45 +00:00
NSInteger active = 0, downloading = 0;
for (Torrent * torrent in fTorrents)
if ([torrent isActive] && ![torrent isStalled])
2007-09-16 01:02:06 +00:00
{
active++;
if (![torrent allDownloaded])
2007-09-16 01:02:06 +00:00
downloading++;
}
2007-09-16 01:02:06 +00:00
if ([fDefaults boolForKey: @"CheckQuitDownloading"] ? downloading > 0 : active > 0)
{
NSString * message = active == 1
2008-04-23 03:33:04 +00:00
? NSLocalizedString(@"There is an active transfer that will be paused on quit."
" The transfer will automatically resume on the next launch.", "Confirm Quit panel -> message")
: [NSString stringWithFormat: NSLocalizedString(@"There are %d active transfers that will be paused on quit."
" The transfers will automatically resume on the next launch.", "Confirm Quit panel -> message"), active];
2007-09-16 01:02:06 +00:00
2008-04-23 03:33:04 +00:00
NSBeginAlertSheet(NSLocalizedString(@"Are you sure you want to quit?", "Confirm Quit panel -> title"),
2007-09-16 01:02:06 +00:00
NSLocalizedString(@"Quit", "Confirm Quit panel -> button"),
NSLocalizedString(@"Cancel", "Confirm Quit panel -> button"), nil, fWindow, self,
@selector(quitSheetDidEnd:returnCode:contextInfo:), nil, nil, message);
return NSTerminateLater;
}
}
2007-09-16 01:02:06 +00:00
return NSTerminateNow;
}
2008-11-02 13:52:45 +00:00
- (void) quitSheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
2007-09-16 01:02:06 +00:00
{
[NSApp replyToApplicationShouldTerminate: returnCode == NSAlertDefaultReturn];
}
- (void) applicationWillTerminate: (NSNotification *) notification
{
fQuitting = YES;
//stop the Bonjour service
[[BonjourController defaultController] stop];
//stop blocklist download
if ([BlocklistDownloader isRunning])
[[BlocklistDownloader downloader] cancelDownload];
//stop timers and notification checking
[[NSNotificationCenter defaultCenter] removeObserver: self];
[fTimer invalidate];
if (fAutoImportTimer)
{
if ([fAutoImportTimer isValid])
[fAutoImportTimer invalidate];
[fAutoImportTimer release];
}
[fBadger setQuitting];
2007-09-16 01:02:06 +00:00
//remove all torrent downloads
if (fPendingTorrentDownloads)
{
for (NSDictionary * downloadDict in fPendingTorrentDownloads)
2007-09-16 01:02:06 +00:00
{
NSURLDownload * download = [downloadDict objectForKey: @"Download"];
2007-09-16 01:02:06 +00:00
[download cancel];
[download release];
}
[fPendingTorrentDownloads release];
2007-09-16 01:02:06 +00:00
}
//remember window states and close all windows
[fDefaults setBool: [[fInfoController window] isVisible] forKey: @"InfoVisible"];
const BOOL quickLookOpen = [NSApp isOnSnowLeopardOrBetter] && [QLPreviewPanelSL sharedPreviewPanelExists]
&& [[QLPreviewPanelSL sharedPreviewPanel] isVisible];
if (quickLookOpen)
[[QLPreviewPanelSL sharedPreviewPanel] updateController];
for (NSWindow * window in [NSApp windows])
[window close];
2007-09-16 01:02:06 +00:00
[self showStatusBar: NO animate: NO];
[self showFilterBar: NO animate: NO];
//save history
[self updateTorrentHistory];
2008-02-07 21:26:04 +00:00
[fTableView saveCollapsedGroups];
2007-09-16 01:02:06 +00:00
2007-11-02 15:05:20 +00:00
//remaining calls the same as dealloc
[fInfoController release];
[fMessageController release];
[fPrefsController release];
[fStatusBar release];
[fFilterBar release];
[fTorrents release];
[fDisplayedTorrents release];
[fOverlayWindow release];
2008-12-26 05:57:51 +00:00
[fBadger release];
[fAutoImportedNames release];
2007-11-02 15:05:20 +00:00
[fPreviewPanel release];
//complete cleanup
tr_sessionClose(fLib);
2007-09-16 01:02:06 +00:00
}
- (void) handleOpenContentsEvent: (NSAppleEventDescriptor *) event replyEvent: (NSAppleEventDescriptor *) replyEvent
{
NSString * urlString = nil;
NSAppleEventDescriptor * directObject = [event paramDescriptorForKeyword: keyDirectObject];
if ([directObject descriptorType] == typeAEList)
{
for (NSInteger i = 1; i <= [directObject numberOfItems]; i++)
2007-09-16 01:02:06 +00:00
if ((urlString = [[directObject descriptorAtIndex: i] stringValue]))
break;
}
else
urlString = [directObject stringValue];
if (urlString)
[self openURL: urlString];
2007-09-16 01:02:06 +00:00
}
- (void) download: (NSURLDownload *) download decideDestinationWithSuggestedFilename: (NSString *) suggestedName
{
if ([[suggestedName pathExtension] caseInsensitiveCompare: @"torrent"] != NSOrderedSame)
{
[download cancel];
NSRunAlertPanel(NSLocalizedString(@"Torrent download failed", "Download not a torrent -> title"),
[NSString stringWithFormat: NSLocalizedString(@"It appears that the file \"%@\" from %@ is not a torrent file.",
"Download not a torrent -> message"), suggestedName,
2007-09-16 01:02:06 +00:00
[[[[download request] URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]],
NSLocalizedString(@"OK", "Download not a torrent -> button"), nil, nil);
2007-09-16 01:02:06 +00:00
[download release];
}
else
[download setDestination: [NSTemporaryDirectory() stringByAppendingPathComponent: [suggestedName lastPathComponent]]
allowOverwrite: NO];
}
-(void) download: (NSURLDownload *) download didCreateDestination: (NSString *) path
{
if (!fPendingTorrentDownloads)
fPendingTorrentDownloads = [[NSMutableDictionary alloc] init];
[fPendingTorrentDownloads setObject: [NSDictionary dictionaryWithObjectsAndKeys:
path, @"Path", download, @"Download", nil] forKey: [[download request] URL]];
}
- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
{
NSRunAlertPanel(NSLocalizedString(@"Torrent download failed", "Torrent download error -> title"),
[NSString stringWithFormat: NSLocalizedString(@"The torrent could not be downloaded from %@: %@.",
"Torrent download failed -> message"),
2007-09-16 01:02:06 +00:00
[[[[download request] URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding],
[error localizedDescription]], NSLocalizedString(@"OK", "Torrent download failed -> button"), nil, nil);
2007-09-16 01:02:06 +00:00
[fPendingTorrentDownloads removeObjectForKey: [[download request] URL]];
if ([fPendingTorrentDownloads count] == 0)
{
[fPendingTorrentDownloads release];
fPendingTorrentDownloads = nil;
}
2007-09-16 01:02:06 +00:00
[download release];
}
- (void) downloadDidFinish: (NSURLDownload *) download
{
NSString * path = [[fPendingTorrentDownloads objectForKey: [[download request] URL]] objectForKey: @"Path"];
[self openFiles: [NSArray arrayWithObject: path] addType: ADD_URL forcePath: nil];
2007-09-16 01:02:06 +00:00
//delete the torrent file after opening
2008-12-26 05:57:51 +00:00
[[NSFileManager defaultManager] removeItemAtPath: path error: NULL];
2007-09-16 01:02:06 +00:00
[fPendingTorrentDownloads removeObjectForKey: [[download request] URL]];
if ([fPendingTorrentDownloads count] == 0)
{
[fPendingTorrentDownloads release];
fPendingTorrentDownloads = nil;
}
2007-09-16 01:02:06 +00:00
[download release];
2007-09-16 01:02:06 +00:00
}
- (void) application: (NSApplication *) app openFiles: (NSArray *) filenames
{
[self openFiles: filenames addType: ADD_MANUAL forcePath: nil];
2007-09-16 01:02:06 +00:00
}
- (void) openFiles: (NSArray *) filenames addType: (addType) type forcePath: (NSString *) path
2007-09-16 01:02:06 +00:00
{
BOOL deleteTorrentFile, canToggleDelete = NO;
switch (type)
{
case ADD_CREATED:
deleteTorrentFile = NO;
break;
case ADD_URL:
deleteTorrentFile = YES;
break;
default:
deleteTorrentFile = [fDefaults boolForKey: @"DeleteOriginalTorrent"];
canToggleDelete = YES;
}
for (NSString * torrentPath in filenames)
2007-09-16 01:02:06 +00:00
{
2008-01-08 05:31:20 +00:00
//ensure torrent doesn't already exist
tr_ctor * ctor = tr_ctorNew(fLib);
tr_ctorSetMetainfoFromFile(ctor, [torrentPath UTF8String]);
tr_info info;
const tr_parse_result result = tr_torrentParse(ctor, &info);
tr_ctorFree(ctor);
2009-08-05 02:13:45 +00:00
if (result != TR_PARSE_OK)
2007-09-16 01:02:06 +00:00
{
if (result == TR_PARSE_DUPLICATE)
[self duplicateOpenAlert: [NSString stringWithUTF8String: info.name]];
else if (result == TR_PARSE_ERR)
{
if (type != ADD_AUTO)
[self invalidOpenAlert: [torrentPath lastPathComponent]];
}
else
NSAssert2(NO, @"Unknown error code (%d) when attempting to open \"%@\"", result, torrentPath);
2007-09-16 01:02:06 +00:00
tr_metainfoFree(&info);
continue;
}
2008-01-08 05:31:20 +00:00
//determine download location
NSString * location;
BOOL lockDestination = NO; //don't override the location with a group location if it has a hardcoded path
2008-01-08 05:31:20 +00:00
if (path)
{
2008-01-08 05:31:20 +00:00
location = [path stringByExpandingTildeInPath];
lockDestination = YES;
}
2008-01-08 05:31:20 +00:00
else if ([fDefaults boolForKey: @"DownloadLocationConstant"])
location = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
else if (type != ADD_URL)
location = [torrentPath stringByDeletingLastPathComponent];
else
location = nil;
//determine to show the options window
const BOOL showWindow = type == ADD_SHOW_OPTIONS || ([fDefaults boolForKey: @"DownloadAsk"]
&& (info.isMultifile || ![fDefaults boolForKey: @"DownloadAskMulti"])
&& (type != ADD_AUTO || ![fDefaults boolForKey: @"DownloadAskManual"]));
tr_metainfoFree(&info);
Torrent * torrent;
if (!(torrent = [[Torrent alloc] initWithPath: torrentPath location: location
deleteTorrentFile: showWindow ? NO : deleteTorrentFile lib: fLib]))
2007-09-16 01:02:06 +00:00
continue;
//change the location if the group calls for it (this has to wait until after the torrent is created)
if (!lockDestination && [[GroupsController groups] usesCustomDownloadLocationForIndex: [torrent groupValue]])
{
location = [[GroupsController groups] customDownloadLocationForIndex: [torrent groupValue]];
[torrent changeDownloadFolderBeforeUsing: location];
}
//verify the data right away if it was newly created
if (type == ADD_CREATED)
[torrent resetCache];
2008-01-08 05:31:20 +00:00
//show the add window or add directly
if (showWindow || !location)
{
AddWindowController * addController = [[AddWindowController alloc] initWithTorrent: torrent destination: location
lockDestination: lockDestination controller: self torrentFile: torrentPath
deleteTorrent: deleteTorrentFile canToggleDelete: canToggleDelete];
[addController showWindow: self];
}
else
{
if ([fDefaults boolForKey: @"AutoStartDownload"])
[torrent startTransfer];
2008-01-04 04:54:26 +00:00
[torrent update];
[fTorrents addObject: torrent];
[torrent release];
}
2007-09-16 01:02:06 +00:00
}
[self updateTorrentsInQueue];
}
- (void) askOpenConfirmed: (AddWindowController *) addController add: (BOOL) add
{
Torrent * torrent = [addController torrent];
[addController release];
if (add)
{
[torrent update];
[fTorrents addObject: torrent];
[torrent release];
[self updateTorrentsInQueue];
}
else
{
[torrent closeRemoveTorrent: NO];
[torrent release];
}
}
- (void) openMagnet: (NSString *) address
{
tr_torrent * duplicateTorrent;
if ((duplicateTorrent = tr_torrentFindFromMagnetLink(fLib, [address UTF8String])))
{
const tr_info * info = tr_torrentInfo(duplicateTorrent);
NSString * name = (info != NULL && info->name != NULL) ? [NSString stringWithUTF8String: info->name] : nil;
[self duplicateOpenMagnetAlert: address transferName: name];
return;
}
2010-04-24 16:50:18 +00:00
//determine download location
NSString * location = nil;
if ([fDefaults boolForKey: @"DownloadLocationConstant"])
location = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
Torrent * torrent;
2010-04-24 16:50:18 +00:00
if (!(torrent = [[Torrent alloc] initWithMagnetAddress: address location: location lib: fLib]))
{
[self invalidOpenMagnetAlert: address];
return;
}
//change the location if the group calls for it (this has to wait until after the torrent is created)
if ([[GroupsController groups] usesCustomDownloadLocationForIndex: [torrent groupValue]])
{
location = [[GroupsController groups] customDownloadLocationForIndex: [torrent groupValue]];
[torrent changeDownloadFolderBeforeUsing: location];
}
if ([fDefaults boolForKey: @"MagnetOpenAsk"] || !location)
{
AddMagnetWindowController * addController = [[AddMagnetWindowController alloc] initWithTorrent: torrent destination: location
controller: self];
[addController showWindow: self];
}
else
{
if ([fDefaults boolForKey: @"AutoStartDownload"])
[torrent startTransfer];
[torrent update];
[fTorrents addObject: torrent];
[torrent release];
}
[self updateTorrentsInQueue];
}
- (void) askOpenMagnetConfirmed: (AddMagnetWindowController *) addController add: (BOOL) add
{
Torrent * torrent = [addController torrent];
2008-01-08 05:31:20 +00:00
[addController release];
if (add)
{
[torrent update];
[fTorrents addObject: torrent];
[torrent release];
[self updateTorrentsInQueue];
}
else
{
[torrent closeRemoveTorrent: NO];
[torrent release];
}
}
2007-09-16 01:02:06 +00:00
- (void) openCreatedFile: (NSNotification *) notification
{
NSDictionary * dict = [notification userInfo];
[self openFiles: [NSArray arrayWithObject: [dict objectForKey: @"File"]] addType: ADD_CREATED
forcePath: [dict objectForKey: @"Path"]];
2007-09-16 01:02:06 +00:00
[dict release];
}
- (void) openFilesWithDict: (NSDictionary *) dictionary
{
[self openFiles: [dictionary objectForKey: @"Filenames"] addType: [[dictionary objectForKey: @"AddType"] intValue] forcePath: nil];
2007-09-16 01:02:06 +00:00
[dictionary release];
}
//called on by applescript
- (void) open: (NSArray *) files
{
2008-01-10 16:46:15 +00:00
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: files, @"Filenames",
[NSNumber numberWithInt: ADD_MANUAL], @"AddType", nil];
2008-01-10 16:46:15 +00:00
[self performSelectorOnMainThread: @selector(openFilesWithDict:) withObject: dict waitUntilDone: NO];
2007-09-16 01:02:06 +00:00
}
- (void) openShowSheet: (id) sender
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection: YES];
[panel setCanChooseFiles: YES];
[panel setCanChooseDirectories: NO];
[panel beginSheetForDirectory: nil file: nil types: [NSArray arrayWithObjects: @"org.bittorrent.torrent", @"torrent", nil]
2007-09-16 01:02:06 +00:00
modalForWindow: fWindow modalDelegate: self didEndSelector: @selector(openSheetClosed:returnCode:contextInfo:)
contextInfo: [NSNumber numberWithBool: sender == fOpenIgnoreDownloadFolder]];
}
2008-11-02 13:52:45 +00:00
- (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (NSInteger) code contextInfo: (NSNumber *) useOptions
2007-09-16 01:02:06 +00:00
{
if (code == NSOKButton)
{
NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: [panel filenames], @"Filenames",
[NSNumber numberWithInt: [useOptions boolValue] ? ADD_SHOW_OPTIONS : ADD_MANUAL], @"AddType", nil];
2008-01-10 16:46:15 +00:00
[self performSelectorOnMainThread: @selector(openFilesWithDict:) withObject: dictionary waitUntilDone: NO];
2007-09-16 01:02:06 +00:00
}
}
- (void) invalidOpenAlert: (NSString *) filename
{
if (![fDefaults boolForKey: @"WarningInvalidOpen"])
return;
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"\"%@\" is not a valid torrent file.",
"Open invalid alert -> title"), filename]];
[alert setInformativeText:
NSLocalizedString(@"The torrent file cannot be opened because it contains invalid data.",
"Open invalid alert -> message")];
[alert setAlertStyle: NSWarningAlertStyle];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Open invalid alert -> button")];
2008-12-26 05:57:51 +00:00
[alert runModal];
if ([[alert suppressionButton] state] == NSOnState)
[fDefaults setBool: NO forKey: @"WarningInvalidOpen"];
[alert release];
}
- (void) invalidOpenMagnetAlert: (NSString *) address
{
if (![fDefaults boolForKey: @"WarningInvalidOpen"])
return;
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: NSLocalizedString(@"Adding magnetized transfer failed.", "Magnet link failed -> title")];
[alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"There was an error when adding the magnet link \"%@\"."
" The transfer will not occur.", "Magnet link failed -> message"), address]];
[alert setAlertStyle: NSWarningAlertStyle];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Magnet link failed -> button")];
[alert runModal];
if ([[alert suppressionButton] state] == NSOnState)
[fDefaults setBool: NO forKey: @"WarningInvalidOpen"];
[alert release];
}
2007-09-16 01:02:06 +00:00
- (void) duplicateOpenAlert: (NSString *) name
{
if (![fDefaults boolForKey: @"WarningDuplicate"])
return;
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"A transfer of \"%@\" already exists.",
2007-09-16 01:02:06 +00:00
"Open duplicate alert -> title"), name]];
[alert setInformativeText:
NSLocalizedString(@"The transfer cannot be added because it is a duplicate of an already existing transfer.",
2007-09-16 01:02:06 +00:00
"Open duplicate alert -> message")];
[alert setAlertStyle: NSWarningAlertStyle];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Open duplicate alert -> button")];
2008-12-26 05:57:51 +00:00
[alert setShowsSuppressionButton: YES];
2008-12-26 05:57:51 +00:00
[alert runModal];
if ([[alert suppressionButton] state])
2007-09-16 01:02:06 +00:00
[fDefaults setBool: NO forKey: @"WarningDuplicate"];
[alert release];
}
- (void) duplicateOpenMagnetAlert: (NSString *) address transferName: (NSString *) name
{
if (![fDefaults boolForKey: @"WarningDuplicate"])
return;
NSAlert * alert = [[NSAlert alloc] init];
if (name)
[alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"A transfer of \"%@\" already exists.",
"Open duplicate magnet alert -> title"), name]];
else
[alert setMessageText: NSLocalizedString(@"Magnet link is a duplicate of an existing transfer.",
"Open duplicate magnet alert -> title")];
[alert setInformativeText: [NSString stringWithFormat:
NSLocalizedString(@"The magnet link \"%@\" cannot be added because it is a duplicate of an already existing transfer.",
"Open duplicate magnet alert -> message"), address]];
[alert setAlertStyle: NSWarningAlertStyle];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Open duplicate magnet alert -> button")];
[alert setShowsSuppressionButton: YES];
[alert runModal];
if ([[alert suppressionButton] state])
[fDefaults setBool: NO forKey: @"WarningDuplicate"];
[alert release];
}
- (void) openURL: (NSString *) urlString
2007-09-16 01:02:06 +00:00
{
if ([urlString rangeOfString: @"magnet:" options: (NSAnchoredSearch | NSCaseInsensitiveSearch)].location != NSNotFound)
[self openMagnet: urlString];
else
{
if ([urlString rangeOfString: @"://"].location == NSNotFound)
{
if ([urlString rangeOfString: @"."].location == NSNotFound)
{
NSInteger beforeCom;
if ((beforeCom = [urlString rangeOfString: @"/"].location) != NSNotFound)
urlString = [NSString stringWithFormat: @"http://www.%@.com/%@",
[urlString substringToIndex: beforeCom],
[urlString substringFromIndex: beforeCom + 1]];
else
urlString = [NSString stringWithFormat: @"http://www.%@.com/", urlString];
}
else
urlString = [@"http://" stringByAppendingString: urlString];
}
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]
cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 60];
[[NSURLDownload alloc] initWithRequest: request delegate: self];
}
2007-09-16 01:02:06 +00:00
}
- (void) openURLShowSheet: (id) sender
{
[[[URLSheetWindowController alloc] initWithController: self] beginSheetForWindow: fWindow];
2007-09-16 01:02:06 +00:00
}
- (void) urlSheetDidEnd: (URLSheetWindowController *) controller url: (NSString *) urlString returnCode: (NSInteger) returnCode
2007-09-16 01:02:06 +00:00
{
if (returnCode == 1)
[self performSelectorOnMainThread: @selector(openURL:) withObject: urlString waitUntilDone: NO];
2007-09-16 01:02:06 +00:00
[controller release];
2007-09-16 01:02:06 +00:00
}
- (void) createFile: (id) sender
{
[CreatorWindowController createTorrentFile: fLib];
}
- (void) resumeSelectedTorrents: (id) sender
{
[self resumeTorrents: [fTableView selectedTorrents]];
2007-09-16 01:02:06 +00:00
}
- (void) resumeAllTorrents: (id) sender
{
NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [fTorrents count]];
for (Torrent * torrent in fTorrents)
if (![torrent isFinishedSeeding])
[torrents addObject: torrent];
[self resumeTorrents: torrents];
2007-09-16 01:02:06 +00:00
}
- (void) resumeTorrents: (NSArray *) torrents
{
for (Torrent * torrent in torrents)
[torrent startTransfer];
2007-09-16 01:02:06 +00:00
[self updateTorrentsInQueue];
}
- (void) resumeSelectedTorrentsNoWait: (id) sender
{
[self resumeTorrentsNoWait: [fTableView selectedTorrents]];
2007-09-16 01:02:06 +00:00
}
- (void) resumeWaitingTorrents: (id) sender
{
NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [fTorrents count]];
for (Torrent * torrent in fTorrents)
if ([torrent waitingToStart])
2007-09-16 01:02:06 +00:00
[torrents addObject: torrent];
[self resumeTorrentsNoWait: torrents];
}
- (void) resumeTorrentsNoWait: (NSArray *) torrents
{
//iterate through instead of all at once to ensure no conflicts
for (Torrent * torrent in torrents)
[torrent startTransferNoQueue];
2007-09-16 01:02:06 +00:00
[self updateTorrentsInQueue];
2007-09-16 01:02:06 +00:00
}
- (void) stopSelectedTorrents: (id) sender
{
[self stopTorrents: [fTableView selectedTorrents]];
2007-09-16 01:02:06 +00:00
}
- (void) stopAllTorrents: (id) sender
{
[self stopTorrents: fTorrents];
}
- (void) stopTorrents: (NSArray *) torrents
{
#warning look into stopping all without starting any
2007-09-16 01:02:06 +00:00
//don't want any of these starting then stopping
/*for (Torrent * torrent in torrents)
[torrent setWaitToStart: NO];*/
2007-09-16 01:02:06 +00:00
[torrents makeObjectsPerformSelector: @selector(stopTransfer)];
[self updateTorrentsInQueue];
2007-09-16 01:02:06 +00:00
}
- (void) removeTorrents: (NSArray *) torrents deleteData: (BOOL) deleteData
2007-09-16 01:02:06 +00:00
{
[torrents retain];
if ([fDefaults boolForKey: @"CheckRemove"])
{
NSUInteger active = 0, downloading = 0;
for (Torrent * torrent in torrents)
2007-09-16 01:02:06 +00:00
if ([torrent isActive])
{
++active;
2007-09-16 01:02:06 +00:00
if (![torrent isSeeding])
++downloading;
2007-09-16 01:02:06 +00:00
}
if ([fDefaults boolForKey: @"CheckRemoveDownloading"] ? downloading > 0 : active > 0)
{
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
2007-12-21 20:39:43 +00:00
torrents, @"Torrents",
[NSNumber numberWithBool: deleteData], @"DeleteData", nil];
2007-12-21 20:39:43 +00:00
2007-09-16 01:02:06 +00:00
NSString * title, * message;
const NSInteger selected = [torrents count];
2007-09-16 01:02:06 +00:00
if (selected == 1)
{
NSString * torrentName = [[torrents objectAtIndex: 0] name];
2007-09-16 01:02:06 +00:00
if (deleteData)
2008-04-23 03:33:04 +00:00
title = [NSString stringWithFormat:
NSLocalizedString(@"Are you sure you want to remove \"%@\" from the transfer list"
" and trash the data file?", "Removal confirm panel -> title"), torrentName];
2007-09-16 01:02:06 +00:00
else
2008-04-23 03:33:04 +00:00
title = [NSString stringWithFormat:
NSLocalizedString(@"Are you sure you want to remove \"%@\" from the transfer list?",
"Removal confirm panel -> title"), torrentName];
2007-09-16 01:02:06 +00:00
message = NSLocalizedString(@"This transfer is active."
" Once removed, continuing the transfer will require the torrent file or magnet link.",
"Removal confirm panel -> message");
2007-09-16 01:02:06 +00:00
}
else
{
if (deleteData)
2008-04-23 03:33:04 +00:00
title = [NSString stringWithFormat:
NSLocalizedString(@"Are you sure you want to remove %@ transfers from the transfer list"
" and trash the data files?", "Removal confirm panel -> title"), [NSString formattedUInteger: selected]];
2007-09-16 01:02:06 +00:00
else
2008-04-23 03:33:04 +00:00
title = [NSString stringWithFormat:
NSLocalizedString(@"Are you sure you want to remove %@ transfers from the transfer list?",
"Removal confirm panel -> title"), [NSString formattedUInteger: selected]];
2007-09-16 01:02:06 +00:00
if (selected == active)
message = [NSString stringWithFormat: NSLocalizedString(@"There are %@ active transfers.",
"Removal confirm panel -> message part 1"), [NSString formattedUInteger: active]];
2007-09-16 01:02:06 +00:00
else
message = [NSString stringWithFormat: NSLocalizedString(@"There are %@ transfers (%@ active).",
"Removal confirm panel -> message part 1"), [NSString formattedUInteger: selected], [NSString formattedUInteger: active]];
2008-04-23 03:33:04 +00:00
message = [message stringByAppendingFormat: @" %@",
NSLocalizedString(@"Once removed, continuing the transfers will require the torrent files or magnet links.",
"Removal confirm panel -> message part 2")];
2007-09-16 01:02:06 +00:00
}
2007-12-21 20:39:43 +00:00
2007-09-16 01:02:06 +00:00
NSBeginAlertSheet(title, NSLocalizedString(@"Remove", "Removal confirm panel -> button"),
NSLocalizedString(@"Cancel", "Removal confirm panel -> button"), nil, fWindow, self,
nil, @selector(removeSheetDidEnd:returnCode:contextInfo:), dict, message);
return;
}
}
[self confirmRemoveTorrents: torrents deleteData: deleteData];
2007-09-16 01:02:06 +00:00
}
2008-11-02 13:52:45 +00:00
- (void) removeSheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (NSDictionary *) dict
2007-09-16 01:02:06 +00:00
{
NSArray * torrents = [dict objectForKey: @"Torrents"];
if (returnCode == NSAlertDefaultReturn)
[self confirmRemoveTorrents: torrents deleteData: [[dict objectForKey: @"DeleteData"] boolValue]];
2007-09-16 01:02:06 +00:00
else
[torrents release];
2007-12-21 20:39:43 +00:00
[dict release];
2007-09-16 01:02:06 +00:00
}
- (void) confirmRemoveTorrents: (NSArray *) torrents deleteData: (BOOL) deleteData
2007-09-16 01:02:06 +00:00
{
NSMutableArray * selectedValues = [NSMutableArray arrayWithArray: [fTableView selectedValues]];
[selectedValues removeObjectsInArray: torrents];
#warning look into stopping all without starting any
2007-09-16 01:02:06 +00:00
//don't want any of these starting then stopping
/*for (Torrent * torrent in torrents)
[torrent setWaitToStart: NO];*/
2007-12-21 20:39:43 +00:00
[fTorrents removeObjectsInArray: torrents];
2009-12-14 23:35:55 +00:00
//if not removed from displayed torrents, updateTorrentsInQueue might cause a crash
if ([fDisplayedTorrents count] > 0)
{
if ([[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
{
for (TorrentGroup * group in fDisplayedTorrents)
[[group torrents] removeObjectsInArray: torrents];
}
else
[fDisplayedTorrents removeObjectsInArray: torrents];
}
for (Torrent * torrent in torrents)
2007-09-16 01:02:06 +00:00
{
//let's expand all groups that have removed items - they either don't exist anymore, are already expanded, or are collapsed (rpc)
[fTableView removeCollapsedGroup: [torrent groupValue]];
//we can't assume the window is active - RPC removal, for example
[fBadger removeTorrent: torrent];
[torrent closeRemoveTorrent: deleteData];
2007-09-16 01:02:06 +00:00
}
2007-12-21 20:39:43 +00:00
#warning why do we need them retained?
2007-09-16 01:02:06 +00:00
[torrents release];
[fTableView selectValues: selectedValues];
2007-09-16 01:02:06 +00:00
2007-12-21 20:39:43 +00:00
[self updateTorrentsInQueue];
2007-09-16 01:02:06 +00:00
}
- (void) removeNoDelete: (id) sender
{
[self removeTorrents: [fTableView selectedTorrents] deleteData: NO];
2007-09-16 01:02:06 +00:00
}
- (void) removeDeleteData: (id) sender
{
[self removeTorrents: [fTableView selectedTorrents] deleteData: YES];
2007-09-16 01:02:06 +00:00
}
- (void) clearCompleted: (id) sender
{
NSMutableArray * torrents = [[NSMutableArray alloc] init];
for (Torrent * torrent in fTorrents)
if ([torrent isFinishedSeeding])
[torrents addObject: torrent];
if ([fDefaults boolForKey: @"WarningRemoveCompleted"])
{
NSString * message, * info;
if ([torrents count] == 1)
{
NSString * torrentName = [[torrents objectAtIndex: 0] name];
message = [NSString stringWithFormat: NSLocalizedString(@"Are you sure you want to remove \"%@\" from the transfer list?",
"Remove completed confirm panel -> title"), torrentName];
info = NSLocalizedString(@"Once removed, continuing the transfer will require the torrent file or magnet link.",
"Remove completed confirm panel -> message");
}
else
{
message = [NSString stringWithFormat: NSLocalizedString(@"Are you sure you want to remove %@ completed transfers from the transfer list?",
"Remove completed confirm panel -> title"), [NSString formattedUInteger: [torrents count]]];
info = NSLocalizedString(@"Once removed, continuing the transfers will require the torrent files or magnet links.",
"Remove completed confirm panel -> message");
}
NSAlert * alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText: message];
[alert setInformativeText: info];
[alert setAlertStyle: NSWarningAlertStyle];
[alert addButtonWithTitle: NSLocalizedString(@"Remove", "Remove completed confirm panel -> button")];
[alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Remove completed confirm panel -> button")];
[alert setShowsSuppressionButton: YES];
const NSInteger returnCode = [alert runModal];
if ([[alert suppressionButton] state])
[fDefaults setBool: NO forKey: @"WarningRemoveCompleted"];
if (returnCode != NSAlertFirstButtonReturn)
return;
}
[self confirmRemoveTorrents: torrents deleteData: NO];
}
- (void) moveDataFilesSelected: (id) sender
{
[self moveDataFiles: [fTableView selectedTorrents]];
}
- (void) moveDataFiles: (NSArray *) torrents
2007-09-16 01:02:06 +00:00
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setPrompt: NSLocalizedString(@"Select", "Move torrent -> prompt")];
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: NO];
[panel setCanChooseDirectories: YES];
[panel setCanCreateDirectories: YES];
torrents = [torrents retain];
2008-11-02 13:52:45 +00:00
NSInteger count = [torrents count];
2007-09-16 01:02:06 +00:00
if (count == 1)
[panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the new folder for \"%@\".",
"Move torrent -> select destination folder"), [[torrents objectAtIndex: 0] name]]];
else
[panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the new folder for %d data files.",
"Move torrent -> select destination folder"), count]];
[panel beginSheetForDirectory: nil file: nil modalForWindow: fWindow modalDelegate: self
didEndSelector: @selector(moveDataFileChoiceClosed:returnCode:contextInfo:) contextInfo: torrents];
}
2008-11-02 13:52:45 +00:00
- (void) moveDataFileChoiceClosed: (NSOpenPanel *) panel returnCode: (NSInteger) code contextInfo: (NSArray *) torrents
2007-09-16 01:02:06 +00:00
{
if (code == NSOKButton)
{
for (Torrent * torrent in torrents)
2007-09-16 01:02:06 +00:00
[torrent moveTorrentDataFileTo: [[panel filenames] objectAtIndex: 0]];
}
[torrents release];
}
- (void) copyTorrentFiles: (id) sender
{
[self copyTorrentFileForTorrents: [[NSMutableArray alloc] initWithArray: [fTableView selectedTorrents]]];
2007-09-16 01:02:06 +00:00
}
- (void) copyTorrentFileForTorrents: (NSMutableArray *) torrents
{
if ([torrents count] == 0)
2007-09-16 01:02:06 +00:00
{
[torrents release];
return;
}
2007-09-16 01:02:06 +00:00
Torrent * torrent = [torrents objectAtIndex: 0];
if (![torrent isMagnet] && [[NSFileManager defaultManager] fileExistsAtPath: [torrent torrentLocation]])
2007-09-16 01:02:06 +00:00
{
NSSavePanel * panel = [NSSavePanel savePanel];
[panel setAllowedFileTypes: [NSArray arrayWithObjects: @"org.bittorrent.torrent", @"torrent", nil]];
[panel setExtensionHidden: NO];
2007-09-16 01:02:06 +00:00
[panel beginSheetForDirectory: nil file: [torrent name] modalForWindow: fWindow modalDelegate: self
didEndSelector: @selector(saveTorrentCopySheetClosed:returnCode:contextInfo:) contextInfo: torrents];
}
else
{
if (![torrent isMagnet])
{
NSAlert * alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent file copy alert -> button")];
[alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Copy of \"%@\" Cannot Be Created",
"Torrent file copy alert -> title"), [torrent name]]];
[alert setInformativeText: [NSString stringWithFormat:
NSLocalizedString(@"The torrent file (%@) cannot be found.", "Torrent file copy alert -> message"),
[torrent torrentLocation]]];
[alert setAlertStyle: NSWarningAlertStyle];
[alert runModal];
[alert release];
}
[torrents removeObjectAtIndex: 0];
[self copyTorrentFileForTorrents: torrents];
}
2007-09-16 01:02:06 +00:00
}
2008-11-02 13:52:45 +00:00
- (void) saveTorrentCopySheetClosed: (NSSavePanel *) panel returnCode: (NSInteger) code contextInfo: (NSMutableArray *) torrents
2007-09-16 01:02:06 +00:00
{
//copy torrent to new location with name of data file
if (code == NSOKButton)
[[torrents objectAtIndex: 0] copyTorrentFileTo: [panel filename]];
[torrents removeObjectAtIndex: 0];
[self performSelectorOnMainThread: @selector(copyTorrentFileForTorrents:) withObject: torrents waitUntilDone: NO];
}
- (void) copyMagnetLinks: (id) sender
{
NSArray * torrents = [fTableView selectedTorrents];
if ([torrents count] <= 0)
return;
NSMutableArray * links = [NSMutableArray arrayWithCapacity: [torrents count]];
for (Torrent * torrent in torrents)
[links addObject: [torrent magnetLink]];
NSString * text = [links componentsJoinedByString: @"\n"];
NSPasteboard * pb = [NSPasteboard generalPasteboard];
if ([NSApp isOnSnowLeopardOrBetter])
{
[pb clearContents];
[pb writeObjects: [NSArray arrayWithObject: text]];
}
else
{
[pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
[pb setString: text forType: NSStringPboardType];
}
}
2007-09-16 01:02:06 +00:00
- (void) revealFile: (id) sender
{
NSArray * selected = [fTableView selectedTorrents];
if ([NSApp isOnSnowLeopardOrBetter])
{
NSMutableArray * paths = [NSMutableArray arrayWithCapacity: [selected count]];
2010-02-16 03:29:31 +00:00
for (Torrent * torrent in selected)
{
NSString * location = [torrent dataLocation];
if (location)
[paths addObject: [NSURL fileURLWithPath: location]];
}
if ([paths count] > 0)
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs: paths];
}
else
{
for (Torrent * torrent in selected)
{
NSString * location = [torrent dataLocation];
if (location)
[[NSWorkspace sharedWorkspace] selectFile: location inFileViewerRootedAtPath: nil];
}
}
2007-09-16 01:02:06 +00:00
}
- (void) announceSelectedTorrents: (id) sender
{
for (Torrent * torrent in [fTableView selectedTorrents])
2007-09-16 01:02:06 +00:00
{
if ([torrent canManualAnnounce])
[torrent manualAnnounce];
}
}
2008-02-27 19:04:38 +00:00
- (void) verifySelectedTorrents: (id) sender
2007-09-16 01:02:06 +00:00
{
2008-02-27 19:04:38 +00:00
[self verifyTorrents: [fTableView selectedTorrents]];
}
- (void) verifyTorrents: (NSArray *) torrents
{
for (Torrent * torrent in torrents)
2007-09-16 01:02:06 +00:00
[torrent resetCache];
2011-02-20 03:58:50 +00:00
[self applyFilter];
2007-09-16 01:02:06 +00:00
}
- (void) showPreferenceWindow: (id) sender
{
NSWindow * window = [fPrefsController window];
if (![window isVisible])
[window center];
[window makeKeyAndOrderFront: nil];
}
- (void) showAboutWindow: (id) sender
{
[[AboutWindowController aboutController] showWindow: nil];
}
- (void) showInfo: (id) sender
{
if ([[fInfoController window] isVisible])
[fInfoController close];
else
{
2007-09-16 01:02:06 +00:00
[fInfoController updateInfoStats];
[[fInfoController window] orderFront: nil];
if ([fInfoController canQuickLook]
&& [QLPreviewPanelSL sharedPreviewPanelExists] && [[QLPreviewPanelSL sharedPreviewPanel] isVisible])
[[QLPreviewPanelSL sharedPreviewPanel] reloadData];
2007-09-16 01:02:06 +00:00
}
[[fWindow toolbar] validateVisibleItems];
2007-09-16 01:02:06 +00:00
}
- (void) resetInfo
{
[fInfoController setInfoForTorrents: [fTableView selectedTorrents]];
if ([NSApp isOnSnowLeopardOrBetter] && [QLPreviewPanelSL sharedPreviewPanelExists]
&& [[QLPreviewPanelSL sharedPreviewPanel] isVisible])
[[QLPreviewPanelSL sharedPreviewPanel] reloadData];
}
2007-09-16 01:02:06 +00:00
- (void) setInfoTab: (id) sender
{
if (sender == fNextInfoTabItem)
[fInfoController setNextTab];
else
[fInfoController setPreviousTab];
}
- (void) showMessageWindow: (id) sender
{
if (!fMessageController)
fMessageController = [[MessageWindowController alloc] init];
2007-09-16 01:02:06 +00:00
[fMessageController showWindow: nil];
}
- (void) showStatsWindow: (id) sender
{
[[StatsWindowController statsWindow: fLib] showWindow: nil];
}
2007-09-16 01:02:06 +00:00
- (void) updateUI
{
[fTorrents makeObjectsPerformSelector: @selector(update)];
//pull the upload and download speeds - most consistent by using current stats
CGFloat dlRate = 0.0, ulRate = 0.0;
BOOL completed = NO;
for (Torrent * torrent in fTorrents)
{
dlRate += [torrent downloadRate];
ulRate += [torrent uploadRate];
completed |= [torrent isFinishedSeeding];
}
if (![NSApp isHidden])
2007-09-16 01:02:06 +00:00
{
if ([fWindow isVisible])
{
[self sortTorrents];
[fStatusBar updateWithDownload: dlRate upload: ulRate];
[fClearCompletedButton setHidden: !completed];
}
2007-09-16 01:02:06 +00:00
//update non-constant parts of info window
if ([[fInfoController window] isVisible])
[fInfoController updateInfoStats];
}
2007-10-19 01:24:42 +00:00
2007-09-16 01:02:06 +00:00
//badge dock
[fBadger updateBadgeWithDownload: dlRate upload: ulRate];
2007-09-16 01:02:06 +00:00
}
2008-03-21 20:28:04 +00:00
- (void) setBottomCountText: (BOOL) filtering
{
NSString * totalTorrentsString;
NSUInteger totalCount = [fTorrents count];
if (totalCount != 1)
totalTorrentsString = [NSString stringWithFormat: NSLocalizedString(@"%@ transfers", "Status bar transfer count"),
[NSString formattedUInteger: totalCount]];
else
totalTorrentsString = NSLocalizedString(@"1 transfer", "Status bar transfer count");
if (filtering)
{
NSUInteger count = [fTableView numberOfRows]; //have to factor in collapsed rows
if (count > 0 && ![[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [Torrent class]])
count -= [fDisplayedTorrents count];
totalTorrentsString = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Status bar transfer count"),
[NSString formattedUInteger: count], totalTorrentsString];
}
[fTotalTorrentsField setStringValue: totalTorrentsString];
}
#warning rename? remove?
2007-09-16 01:02:06 +00:00
- (void) updateTorrentsInQueue
{
[self updateUI];
2011-02-20 03:58:50 +00:00
[self applyFilter];
[[fWindow toolbar] validateVisibleItems];
2007-09-16 01:02:06 +00:00
[self updateTorrentHistory];
}
- (void) torrentFinishedDownloading: (NSNotification *) notification
{
Torrent * torrent = [notification object];
if ([[[notification userInfo] objectForKey: @"WasRunning"] boolValue])
2007-09-16 01:02:06 +00:00
{
if (!fSoundPlaying && [fDefaults boolForKey: @"PlayDownloadSound"])
2007-09-16 01:02:06 +00:00
{
NSSound * sound;
if ((sound = [NSSound soundNamed: [fDefaults stringForKey: @"DownloadSound"]]))
{
[sound setDelegate: self];
fSoundPlaying = YES;
[sound play];
}
2007-09-16 01:02:06 +00:00
}
2009-10-25 19:29:30 +00:00
NSMutableDictionary * clickContext = [NSMutableDictionary dictionaryWithObject: GROWL_DOWNLOAD_COMPLETE forKey: @"Type"];
NSString * location = [torrent dataLocation];
if (location)
[clickContext setObject: location forKey: @"Location"];
[GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Download Complete", "Growl notification title")
description: [torrent name] notificationName: GROWL_DOWNLOAD_COMPLETE
iconData: nil priority: 0 isSticky: NO clickContext: clickContext];
if (![fWindow isMainWindow])
[fBadger addCompletedTorrent: torrent];
//bounce download stack
[[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"com.apple.DownloadFileFinished"
object: [torrent dataLocation]];
2007-09-16 01:02:06 +00:00
}
[self updateTorrentsInQueue];
}
#warning remove?
2007-09-16 01:02:06 +00:00
- (void) torrentRestartedDownloading: (NSNotification *) notification
{
[self updateTorrentsInQueue];
}
- (void) torrentFinishedSeeding: (NSNotification *) notification
{
Torrent * torrent = [notification object];
[self updateTorrentsInQueue];
if ([[fTableView selectedTorrents] containsObject: torrent])
{
[fInfoController updateInfoStats];
[fInfoController updateOptions];
}
if (!fSoundPlaying && [fDefaults boolForKey: @"PlaySeedingSound"])
{
NSSound * sound;
if ((sound = [NSSound soundNamed: [fDefaults stringForKey: @"SeedingSound"]]))
{
[sound setDelegate: self];
fSoundPlaying = YES;
[sound play];
}
}
2009-10-25 19:29:30 +00:00
NSMutableDictionary * clickContext = [NSMutableDictionary dictionaryWithObject: GROWL_SEEDING_COMPLETE forKey: @"Type"];
NSString * location = [torrent dataLocation];
if (location)
[clickContext setObject: location forKey: @"Location"];
[GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Seeding Complete", "Growl notification title")
description: [torrent name] notificationName: GROWL_SEEDING_COMPLETE
iconData: nil priority: 0 isSticky: NO clickContext: clickContext];
}
2007-09-16 01:02:06 +00:00
- (void) updateTorrentHistory
{
NSMutableArray * history = [NSMutableArray arrayWithCapacity: [fTorrents count]];
for (Torrent * torrent in fTorrents)
2007-09-16 01:02:06 +00:00
[history addObject: [torrent history]];
[history writeToFile: [NSHomeDirectory() stringByAppendingPathComponent: TRANSFER_PLIST] atomically: YES];
2007-09-16 01:02:06 +00:00
}
- (void) setSort: (id) sender
{
NSString * sortType;
switch ([sender tag])
{
case SORT_ORDER_TAG:
sortType = SORT_ORDER;
[fDefaults setBool: NO forKey: @"SortReverse"];
break;
case SORT_DATE_TAG:
sortType = SORT_DATE;
break;
case SORT_NAME_TAG:
sortType = SORT_NAME;
break;
case SORT_PROGRESS_TAG:
sortType = SORT_PROGRESS;
break;
case SORT_STATE_TAG:
sortType = SORT_STATE;
break;
case SORT_TRACKER_TAG:
sortType = SORT_TRACKER;
break;
case SORT_ACTIVITY_TAG:
sortType = SORT_ACTIVITY;
break;
2011-07-04 19:47:15 +00:00
case SORT_SIZE_TAG:
sortType = SORT_SIZE;
break;
default:
NSAssert1(NO, @"Unknown sort tag received: %d", [sender tag]);
return;
}
[fDefaults setObject: sortType forKey: @"Sort"];
2011-02-20 03:58:50 +00:00
[self applyFilter]; //better than calling sortTorrents because it will even apply to queue order
}
- (void) setSortByGroup: (id) sender
{
BOOL sortByGroup = ![fDefaults boolForKey: @"SortByGroup"];
[fDefaults setBool: sortByGroup forKey: @"SortByGroup"];
//expand all groups
if (sortByGroup)
[fTableView removeAllCollapsedGroups];
2011-02-20 03:58:50 +00:00
[self applyFilter];
}
- (void) setSortReverse: (id) sender
{
const BOOL setReverse = [sender tag] == SORT_DESC_TAG;
if (setReverse != [fDefaults boolForKey: @"SortReverse"])
{
[fDefaults setBool: setReverse forKey: @"SortReverse"];
[self sortTorrents];
}
}
- (void) sortTorrents
{
NSArray * selectedValues = [fTableView selectedValues];
[self sortTorrentsIgnoreSelected]; //actually sort
2009-10-29 01:47:01 +00:00
[fTableView selectValues: selectedValues];
}
- (void) sortTorrentsIgnoreSelected
2007-09-16 01:02:06 +00:00
{
NSString * sortType = [fDefaults stringForKey: @"Sort"];
if (![sortType isEqualToString: SORT_ORDER])
2008-01-31 00:40:37 +00:00
{
const BOOL asc = ![fDefaults boolForKey: @"SortReverse"];
2008-01-31 00:40:37 +00:00
NSArray * descriptors;
NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: asc
selector: @selector(compareFinder:)] autorelease];
2008-01-31 00:40:37 +00:00
if ([sortType isEqualToString: SORT_STATE])
{
NSSortDescriptor * stateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"stateSortKey" ascending: !asc] autorelease],
* progressDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"progress" ascending: !asc] autorelease],
* ratioDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"ratio" ascending: !asc] autorelease];
descriptors = [[NSArray alloc] initWithObjects: stateDescriptor, progressDescriptor, ratioDescriptor,
nameDescriptor, nil];
}
else if ([sortType isEqualToString: SORT_PROGRESS])
{
NSSortDescriptor * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"progress" ascending: asc] autorelease],
* ratioProgressDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"progressStopRatio"
ascending: asc] autorelease],
* ratioDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"ratio" ascending: asc] autorelease];
descriptors = [[NSArray alloc] initWithObjects: progressDescriptor, ratioProgressDescriptor, ratioDescriptor,
nameDescriptor, nil];
}
else if ([sortType isEqualToString: SORT_TRACKER])
{
NSSortDescriptor * trackerDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"trackerSortKey" ascending: asc
selector: @selector(localizedCaseInsensitiveCompare:)] autorelease];
descriptors = [[NSArray alloc] initWithObjects: trackerDescriptor, nameDescriptor, nil];
}
else if ([sortType isEqualToString: SORT_ACTIVITY])
{
NSSortDescriptor * rateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"totalRate" ascending: !asc] autorelease];
NSSortDescriptor * activityDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"dateActivityOrAdd" ascending: !asc]
autorelease];
descriptors = [[NSArray alloc] initWithObjects: rateDescriptor, activityDescriptor, nameDescriptor, nil];
}
else if ([sortType isEqualToString: SORT_DATE])
{
NSSortDescriptor * dateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"dateAdded" ascending: asc] autorelease];
descriptors = [[NSArray alloc] initWithObjects: dateDescriptor, nameDescriptor, nil];
}
2011-07-04 19:47:15 +00:00
else if ([sortType isEqualToString: SORT_SIZE])
{
NSSortDescriptor * sizeDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"size" ascending: asc] autorelease];
descriptors = [[NSArray alloc] initWithObjects: sizeDescriptor, nameDescriptor, nil];
}
else
descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, nil];
2008-01-31 00:40:37 +00:00
//actually sort
if ([fDefaults boolForKey: @"SortByGroup"])
{
for (TorrentGroup * group in fDisplayedTorrents)
[[group torrents] sortUsingDescriptors: descriptors];
}
else
[fDisplayedTorrents sortUsingDescriptors: descriptors];
[descriptors release];
}
2007-09-16 01:02:06 +00:00
[fTableView reloadData];
}
2011-02-20 03:58:50 +00:00
- (void) applyFilter
2007-09-16 01:02:06 +00:00
{
//get all the torrents in the table
2008-02-10 16:56:13 +00:00
NSMutableArray * previousTorrents;
2008-12-29 02:39:41 +00:00
if ([fDisplayedTorrents count] > 0 && [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
{
2008-02-10 16:56:13 +00:00
previousTorrents = [NSMutableArray array];
for (TorrentGroup * group in fDisplayedTorrents)
[previousTorrents addObjectsFromArray: [group torrents]];
}
else
2008-02-11 00:16:47 +00:00
previousTorrents = fDisplayedTorrents;
NSArray * selectedValues = [fTableView selectedValues];
2007-09-16 01:02:06 +00:00
NSUInteger active = 0, downloading = 0, seeding = 0, paused = 0;
2007-09-16 01:02:06 +00:00
NSString * filterType = [fDefaults stringForKey: @"Filter"];
2007-12-20 04:49:06 +00:00
BOOL filterActive = NO, filterDownload = NO, filterSeed = NO, filterPause = NO, filterStatus = YES;
if ([filterType isEqualToString: FILTER_ACTIVE])
filterActive = YES;
else if ([filterType isEqualToString: FILTER_DOWNLOAD])
2007-12-20 01:06:55 +00:00
filterDownload = YES;
else if ([filterType isEqualToString: FILTER_SEED])
filterSeed = YES;
else if ([filterType isEqualToString: FILTER_PAUSE])
filterPause = YES;
else
filterStatus = NO;
2007-12-20 01:00:03 +00:00
const NSInteger groupFilterValue = [fDefaults integerForKey: @"FilterGroup"];
const BOOL filterGroup = groupFilterValue != GROUP_FILTER_ALL_TAG;
2007-12-20 01:00:03 +00:00
2011-02-21 04:18:58 +00:00
NSString * searchString = [fFilterBar searchString];
if (searchString && [searchString isEqualToString: @""])
searchString = nil;
const BOOL filterTracker = searchString && [[fDefaults stringForKey: @"FilterSearchType"] isEqualToString: FILTER_TYPE_TRACKER];
2007-12-20 01:00:03 +00:00
2008-12-29 02:39:41 +00:00
NSMutableArray * allTorrents = [NSMutableArray arrayWithCapacity: [fTorrents count]];
2007-09-16 01:02:06 +00:00
//get count of each type
for (Torrent * torrent in fTorrents)
2007-09-16 01:02:06 +00:00
{
2007-12-20 01:00:03 +00:00
//check status
if ([torrent isActive] && ![torrent isCheckingWaiting])
2007-09-16 01:02:06 +00:00
{
const BOOL isActive = ![torrent isStalled];
if (isActive)
2011-02-21 04:18:58 +00:00
++active;
2007-09-16 01:02:06 +00:00
if ([torrent isSeeding])
{
2011-02-21 04:18:58 +00:00
++seeding;
if (filterStatus && !((filterActive && isActive) || filterSeed))
2007-12-20 01:00:03 +00:00
continue;
2007-09-16 01:02:06 +00:00
}
else
{
2011-02-21 04:18:58 +00:00
++downloading;
if (filterStatus && !((filterActive && isActive) || filterDownload))
2007-12-20 01:00:03 +00:00
continue;
2007-09-16 01:02:06 +00:00
}
}
else
{
2011-02-21 04:18:58 +00:00
++paused;
2007-12-20 01:00:03 +00:00
if (filterStatus && !filterPause)
continue;
2007-09-16 01:02:06 +00:00
}
2007-12-19 20:46:00 +00:00
2007-12-20 01:00:03 +00:00
//checkGroup
if (filterGroup)
2007-12-20 01:06:55 +00:00
if ([torrent groupValue] != groupFilterValue)
2007-12-20 01:00:03 +00:00
continue;
2007-09-16 01:02:06 +00:00
2007-12-20 01:00:03 +00:00
//check text field
2011-02-21 04:18:58 +00:00
if (searchString)
2007-09-16 01:02:06 +00:00
{
if (filterTracker)
{
BOOL removeTextField = YES;
for (NSString * tracker in [torrent allTrackersFlat])
{
if ([tracker rangeOfString: searchString options:
(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)].location != NSNotFound)
2008-03-21 20:28:04 +00:00
{
removeTextField = NO;
break;
}
}
2007-12-20 01:00:03 +00:00
if (removeTextField)
continue;
}
2007-09-16 01:02:06 +00:00
else
2007-12-20 01:00:03 +00:00
{
if ([[torrent name] rangeOfString: searchString options:
(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)].location == NSNotFound)
2007-12-20 01:00:03 +00:00
continue;
}
2007-09-16 01:02:06 +00:00
}
2007-12-20 01:00:03 +00:00
2008-12-29 02:39:41 +00:00
[allTorrents addObject: torrent];
2007-09-16 01:02:06 +00:00
}
2007-12-20 01:00:03 +00:00
//set button tooltips
if (fFilterBar)
[fFilterBar setCountAll: [fTorrents count] active: active downloading: downloading seeding: seeding paused: paused];
2007-09-16 01:02:06 +00:00
//clear display cache for not-shown torrents
2008-02-11 00:16:47 +00:00
[previousTorrents removeObjectsInArray: allTorrents];
for (Torrent * torrent in previousTorrents)
[torrent setPreviousFinishedPieces: nil];
//place torrents into groups
2009-01-24 19:26:16 +00:00
const BOOL groupRows = [fDefaults boolForKey: @"SortByGroup"];
2008-02-07 16:52:38 +00:00
if (groupRows)
{
NSMutableArray * oldTorrentGroups = [NSMutableArray array];
if ([fDisplayedTorrents count] > 0 && [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]])
[oldTorrentGroups addObjectsFromArray: fDisplayedTorrents];
[fDisplayedTorrents removeAllObjects];
NSSortDescriptor * groupDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"groupOrderValue" ascending: YES] autorelease];
2008-12-29 02:39:41 +00:00
[allTorrents sortUsingDescriptors: [NSArray arrayWithObject: groupDescriptor]];
2009-12-26 00:02:20 +00:00
TorrentGroup * group = nil;
NSInteger lastGroupValue = -2, currentOldGroupIndex = 0;
for (Torrent * torrent in allTorrents)
{
2009-12-26 00:02:20 +00:00
const NSInteger groupValue = [torrent groupValue];
if (groupValue != lastGroupValue)
{
lastGroupValue = groupValue;
2009-12-26 00:02:20 +00:00
group = nil;
//try to see if the group already exists
2011-02-21 04:18:58 +00:00
for (; currentOldGroupIndex < [oldTorrentGroups count]; ++currentOldGroupIndex)
{
TorrentGroup * currentGroup = [oldTorrentGroups objectAtIndex: currentOldGroupIndex];
const NSInteger currentGroupValue = [currentGroup groupIndex];
if (currentGroupValue == groupValue)
{
group = currentGroup;
[[currentGroup torrents] removeAllObjects];
2011-02-21 04:18:58 +00:00
++currentOldGroupIndex;
}
if (currentGroupValue >= groupValue)
break;
}
if (!group)
group = [[[TorrentGroup alloc] initWithGroup: groupValue] autorelease];
[fDisplayedTorrents addObject: group];
}
2009-12-26 00:02:20 +00:00
NSAssert(group != nil, @"No group object to add torrents to");
[[group torrents] addObject: torrent];
}
}
2008-02-11 00:16:47 +00:00
else
[fDisplayedTorrents setArray: allTorrents];
//actually sort
[self sortTorrentsIgnoreSelected];
2007-09-16 01:02:06 +00:00
//reset expanded/collapsed rows
2008-02-07 16:52:38 +00:00
if (groupRows)
{
for (TorrentGroup * group in fDisplayedTorrents)
{
if ([fTableView isGroupCollapsed: [group groupIndex]])
[fTableView collapseItem: group];
else
[fTableView expandItem: group];
}
}
[fTableView selectValues: selectedValues];
[self resetInfo]; //if group is already selected, but the torrents in it change
2011-02-21 04:18:58 +00:00
[self setBottomCountText: groupRows || filterStatus || filterGroup || searchString];
2007-09-16 01:02:06 +00:00
[self setWindowSizeToFit];
}
- (void) switchFilter: (id) sender
{
2011-02-20 03:58:50 +00:00
[fFilterBar switchFilter: sender == fNextFilterItem];
2007-09-16 01:02:06 +00:00
}
- (IBAction) showGlobalPopover: (id) sender
{
if ([NSApp isOnLionOrBetter])
{
if (fGlobalPopoverShown)
return;
NSPopover * popover = [[NSPopoverLion alloc] init];
[popover setBehavior: NSPopoverBehaviorTransient];
GlobalOptionsPopoverViewController * viewController = [[GlobalOptionsPopoverViewController alloc] initWithHandle: [PrefsController handle]];
[popover setContentViewController: viewController];
[popover setDelegate: self];
[popover showRelativeToRect: [sender frame] ofView: sender preferredEdge: NSMaxYEdge];
[viewController release];
[popover release];
}
else
{
//place menu below button
NSRect rect = [sender frame];
NSPoint location = rect.origin;
location.y += NSHeight(rect) + 5.0;
if ([NSApp isOnSnowLeopardOrBetter])
[fActionMenu popUpMenuPositioningItem: nil atLocation: location inView: sender];
else
{
NSEvent * newEvent = [NSEvent mouseEventWithType: NSLeftMouseDown location: location
modifierFlags: NSLeftMouseDownMask timestamp: GetCurrentEventTime() windowNumber: [fWindow windowNumber]
context: nil eventNumber: 1 clickCount: 1 pressure: 1];
[NSMenu popUpContextMenu: fActionMenu withEvent: newEvent forView: sender];
}
}
}
//don't show multiple popovers when clicking the gear button repeatedly
- (void) popoverWillShow: (NSNotification *) notification
{
fGlobalPopoverShown = YES;
}
- (void) popoverWillClose: (NSNotification *) notification
{
fGlobalPopoverShown = NO;
}
2007-12-17 16:06:20 +00:00
- (void) menuNeedsUpdate: (NSMenu *) menu
{
2011-02-20 03:58:50 +00:00
if (menu == fGroupsSetMenu || menu == fGroupsSetContextMenu)
{
2011-02-20 03:58:50 +00:00
for (NSInteger i = [menu numberOfItems]-1; i >= 0; i--)
2007-12-19 20:46:00 +00:00
[menu removeItemAtIndex: i];
2011-02-20 03:58:50 +00:00
NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(setGroup:) isSmall: NO];
2008-12-17 02:13:49 +00:00
const NSInteger groupMenuCount = [groupMenu numberOfItems];
for (NSInteger i = 0; i < groupMenuCount; i++)
{
NSMenuItem * item = [[groupMenu itemAtIndex: 0] retain];
[groupMenu removeItemAtIndex: 0];
[menu addItem: item];
[item release];
}
2007-12-19 20:46:00 +00:00
}
else if (menu == fUploadMenu || menu == fDownloadMenu)
{
if ([menu numberOfItems] > 3)
return;
2009-08-07 23:34:48 +00:00
const NSInteger speedLimitActionValue[] = { 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750, 1000, 1500, 2000, -1 };
NSMenuItem * item;
2008-11-02 13:52:45 +00:00
for (NSInteger i = 0; speedLimitActionValue[i] != -1; i++)
{
item = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: NSLocalizedString(@"%d KB/s",
"Action menu -> upload/download limit"), speedLimitActionValue[i]] action: @selector(setQuickLimitGlobal:)
keyEquivalent: @""];
[item setTarget: self];
[item setRepresentedObject: [NSNumber numberWithInt: speedLimitActionValue[i]]];
[menu addItem: item];
[item release];
}
}
else if (menu == fRatioStopMenu)
{
if ([menu numberOfItems] > 3)
return;
const float ratioLimitActionValue[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, -1 };
NSMenuItem * item;
2008-11-02 13:52:45 +00:00
for (NSInteger i = 0; ratioLimitActionValue[i] != -1; i++)
{
2008-03-24 00:06:54 +00:00
item = [[NSMenuItem alloc] initWithTitle: [NSString localizedStringWithFormat: @"%.2f", ratioLimitActionValue[i]]
2008-01-10 15:53:02 +00:00
action: @selector(setQuickRatioGlobal:) keyEquivalent: @""];
[item setTarget: self];
[item setRepresentedObject: [NSNumber numberWithFloat: ratioLimitActionValue[i]]];
[menu addItem: item];
[item release];
}
}
2007-12-19 20:46:00 +00:00
else;
2007-12-17 16:06:20 +00:00
}
- (void) setGroup: (id) sender
{
for (Torrent * torrent in [fTableView selectedTorrents])
{
[fTableView removeCollapsedGroup: [torrent groupValue]]; //remove old collapsed group
[torrent setGroupValue: [sender tag]];
}
2007-12-17 16:06:20 +00:00
2011-02-20 03:58:50 +00:00
[self applyFilter];
2007-12-17 16:06:20 +00:00
[self updateUI];
[self updateTorrentHistory];
}
2007-09-16 01:02:06 +00:00
- (void) toggleSpeedLimit: (id) sender
{
[fDefaults setBool: ![fDefaults boolForKey: @"SpeedLimit"] forKey: @"SpeedLimit"];
[self speedLimitChanged: sender];
}
- (void) speedLimitChanged: (id) sender
{
tr_sessionUseAltSpeed(fLib, [fDefaults boolForKey: @"SpeedLimit"]);
[fStatusBar updateSpeedFieldsToolTips];
}
//dict has been retained
- (void) altSpeedToggledCallbackIsLimited: (NSDictionary *) dict
{
const BOOL isLimited = [[dict objectForKey: @"Active"] boolValue];
[fDefaults setBool: isLimited forKey: @"SpeedLimit"];
[fStatusBar updateSpeedFieldsToolTips];
if (![[dict objectForKey: @"ByUser"] boolValue])
[GrowlApplicationBridge notifyWithTitle: isLimited
? NSLocalizedString(@"Speed Limit Auto Enabled", "Growl notification title")
: NSLocalizedString(@"Speed Limit Auto Disabled", "Growl notification title")
description: NSLocalizedString(@"Bandwidth settings changed", "Growl notification description")
notificationName: GROWL_AUTO_SPEED_LIMIT iconData: nil priority: 0 isSticky: NO clickContext: nil];
[dict release];
2007-09-16 01:02:06 +00:00
}
- (void) setLimitGlobalEnabled: (id) sender
{
BOOL upload = [sender menu] == fUploadMenu;
[fDefaults setBool: sender == (upload ? fUploadLimitItem : fDownloadLimitItem) forKey: upload ? @"CheckUpload" : @"CheckDownload"];
2007-09-16 01:02:06 +00:00
[fPrefsController applySpeedSettings: nil];
}
- (void) setQuickLimitGlobal: (id) sender
{
BOOL upload = [sender menu] == fUploadMenu;
[fDefaults setInteger: [[sender representedObject] intValue] forKey: upload ? @"UploadLimit" : @"DownloadLimit"];
[fDefaults setBool: YES forKey: upload ? @"CheckUpload" : @"CheckDownload"];
2007-09-16 01:02:06 +00:00
[fPrefsController updateLimitFields];
[fPrefsController applySpeedSettings: nil];
}
- (void) setRatioGlobalEnabled: (id) sender
{
[fDefaults setBool: sender == fCheckRatioItem forKey: @"RatioCheck"];
[fPrefsController applyRatioSetting: nil];
}
2007-09-16 01:02:06 +00:00
- (void) setQuickRatioGlobal: (id) sender
{
[fDefaults setBool: YES forKey: @"RatioCheck"];
[fDefaults setFloat: [[sender representedObject] floatValue] forKey: @"RatioLimit"];
2007-09-16 01:02:06 +00:00
[fPrefsController updateRatioStopFieldOld];
2007-09-16 01:02:06 +00:00
}
- (void) sound: (NSSound *) sound didFinishPlaying: (BOOL) finishedPlaying
{
fSoundPlaying = NO;
}
- (void) watcher: (id<UKFileWatcher>) watcher receivedNotification: (NSString *) notification forPath: (NSString *) path
2007-09-16 01:02:06 +00:00
{
if ([notification isEqualToString: UKFileWatcherWriteNotification])
{
if (![fDefaults boolForKey: @"AutoImport"] || ![fDefaults stringForKey: @"AutoImportDirectory"])
return;
if (fAutoImportTimer)
{
if ([fAutoImportTimer isValid])
[fAutoImportTimer invalidate];
[fAutoImportTimer release];
fAutoImportTimer = nil;
}
//check again in 10 seconds in case torrent file wasn't complete
fAutoImportTimer = [[NSTimer scheduledTimerWithTimeInterval: 10.0 target: self
selector: @selector(checkAutoImportDirectory) userInfo: nil repeats: NO] retain];
[self checkAutoImportDirectory];
}
}
- (void) changeAutoImport
{
if (fAutoImportTimer)
{
if ([fAutoImportTimer isValid])
[fAutoImportTimer invalidate];
[fAutoImportTimer release];
fAutoImportTimer = nil;
}
if (fAutoImportedNames)
{
[fAutoImportedNames release];
fAutoImportedNames = nil;
}
2007-09-16 01:02:06 +00:00
[self checkAutoImportDirectory];
}
- (void) checkAutoImportDirectory
{
NSString * path;
if (![fDefaults boolForKey: @"AutoImport"] || !(path = [fDefaults stringForKey: @"AutoImportDirectory"]))
return;
path = [path stringByExpandingTildeInPath];
NSArray * importedNames;
if (!(importedNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: path error: NULL]))
2007-09-16 01:02:06 +00:00
return;
//only check files that have not been checked yet
NSMutableArray * newNames = [importedNames mutableCopy];
if (fAutoImportedNames)
[newNames removeObjectsInArray: fAutoImportedNames];
else
fAutoImportedNames = [[NSMutableArray alloc] init];
2009-05-23 18:55:08 +00:00
[fAutoImportedNames setArray: importedNames];
2007-09-16 01:02:06 +00:00
for (NSString * file in newNames)
2007-09-16 01:02:06 +00:00
{
if ([file hasPrefix: @"."])
continue;
NSString * fullFile = [path stringByAppendingPathComponent: file];
if (!([[[NSWorkspace sharedWorkspace] typeOfFile: fullFile error: NULL] isEqualToString: @"org.bittorrent.torrent"]
|| [[fullFile pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame))
continue;
tr_ctor * ctor = tr_ctorNew(fLib);
tr_ctorSetMetainfoFromFile(ctor, [fullFile UTF8String]);
switch (tr_torrentParse(ctor, NULL))
2007-09-16 01:02:06 +00:00
{
2009-08-05 02:13:45 +00:00
case TR_PARSE_OK:
[self openFiles: [NSArray arrayWithObject: fullFile] addType: ADD_AUTO forcePath: nil];
2007-12-22 03:39:52 +00:00
[GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Torrent File Auto Added", "Growl notification title")
description: file notificationName: GROWL_AUTO_ADD iconData: nil priority: 0 isSticky: NO
clickContext: nil];
2007-12-22 03:39:52 +00:00
break;
2007-09-16 01:02:06 +00:00
2009-08-05 02:13:45 +00:00
case TR_PARSE_ERR:
[fAutoImportedNames removeObject: file];
break;
case TR_PARSE_DUPLICATE: //let's ignore this (but silence a warning)
break;
2007-09-16 01:02:06 +00:00
}
2007-12-22 03:39:52 +00:00
tr_ctorFree(ctor);
2007-09-16 01:02:06 +00:00
}
[newNames release];
}
- (void) beginCreateFile: (NSNotification *) notification
{
if (![fDefaults boolForKey: @"AutoImport"])
return;
NSString * location = [notification object],
* path = [fDefaults stringForKey: @"AutoImportDirectory"];
if (location && path && [[[location stringByDeletingLastPathComponent] stringByExpandingTildeInPath]
isEqualToString: [path stringByExpandingTildeInPath]])
[fAutoImportedNames addObject: [location lastPathComponent]];
}
- (NSInteger) outlineView: (NSOutlineView *) outlineView numberOfChildrenOfItem: (id) item
2007-09-16 01:02:06 +00:00
{
if (item)
return [[item torrents] count];
else
return [fDisplayedTorrents count];
2007-09-16 01:02:06 +00:00
}
- (id) outlineView: (NSOutlineView *) outlineView child: (NSInteger) index ofItem: (id) item
2007-10-27 02:00:43 +00:00
{
if (item)
return [[item torrents] objectAtIndex: index];
else
return [fDisplayedTorrents objectAtIndex: index];
}
- (BOOL) outlineView: (NSOutlineView *) outlineView isItemExpandable: (id) item
{
return ![item isKindOfClass: [Torrent class]];
2007-10-27 02:00:43 +00:00
}
- (id) outlineView: (NSOutlineView *) outlineView objectValueForTableColumn: (NSTableColumn *) tableColumn byItem: (id) item
{
if ([item isKindOfClass: [Torrent class]]) {
if (tableColumn)
return nil;
return [item hashString];
}
else
{
NSString * ident = [tableColumn identifier];
if ([ident isEqualToString: @"Group"])
{
2008-11-02 13:52:45 +00:00
NSInteger group = [item groupIndex];
return group != -1 ? [[GroupsController groups] nameForIndex: group]
: NSLocalizedString(@"No Group", "Group table row");
}
2008-02-19 01:58:36 +00:00
else if ([ident isEqualToString: @"Color"])
2008-02-19 05:39:56 +00:00
{
2008-11-02 13:52:45 +00:00
NSInteger group = [item groupIndex];
return [[GroupsController groups] imageForIndex: group];
2008-02-19 05:39:56 +00:00
}
else if ([ident isEqualToString: @"DL Image"])
return [NSImage imageNamed: @"DownArrowGroupTemplate.png"];
else if ([ident isEqualToString: @"UL Image"])
return [NSImage imageNamed: [fDefaults boolForKey: @"DisplayGroupRowRatio"]
? @"YingYangGroupTemplate.png" : @"UpArrowGroupTemplate.png"];
else
{
TorrentGroup * group = (TorrentGroup *)item;
if ([fDefaults boolForKey: @"DisplayGroupRowRatio"])
return [NSString stringForRatio: [group ratio]];
else
{
2008-11-02 13:52:45 +00:00
CGFloat rate = [ident isEqualToString: @"UL"] ? [group uploadRate] : [group downloadRate];
return [NSString stringForSpeed: rate];
}
}
}
}
2008-02-07 11:57:24 +00:00
- (BOOL) outlineView: (NSOutlineView *) outlineView writeItems: (NSArray *) items toPasteboard: (NSPasteboard *) pasteboard
2007-09-16 01:02:06 +00:00
{
//only allow reordering of rows if sorting by order
2008-12-26 05:57:51 +00:00
if ([fDefaults boolForKey: @"SortByGroup"] || [[fDefaults stringForKey: @"Sort"] isEqualToString: SORT_ORDER])
2007-09-16 01:02:06 +00:00
{
2008-02-07 11:57:24 +00:00
NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
for (id torrent in items)
{
if (![torrent isKindOfClass: [Torrent class]])
return NO;
2008-02-07 11:57:24 +00:00
[indexSet addIndex: [fTableView rowForItem: torrent]];
}
2008-02-07 11:57:24 +00:00
[pasteboard declareTypes: [NSArray arrayWithObject: TORRENT_TABLE_VIEW_DATA_TYPE] owner: self];
2008-02-07 11:57:24 +00:00
[pasteboard setData: [NSKeyedArchiver archivedDataWithRootObject: indexSet] forType: TORRENT_TABLE_VIEW_DATA_TYPE];
2007-09-16 01:02:06 +00:00
return YES;
}
return NO;
}
2008-02-07 11:57:24 +00:00
- (NSDragOperation) outlineView: (NSOutlineView *) outlineView validateDrop: (id < NSDraggingInfo >) info proposedItem: (id) item
proposedChildIndex: (NSInteger) index
2007-09-16 01:02:06 +00:00
{
NSPasteboard * pasteboard = [info draggingPasteboard];
if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE])
{
if ([fDefaults boolForKey: @"SortByGroup"])
{
if (!item)
return NSDragOperationNone;
if ([[fDefaults stringForKey: @"Sort"] isEqualToString: SORT_ORDER])
{
if ([item isKindOfClass: [Torrent class]])
{
TorrentGroup * group = [fTableView parentForItem: item];
index = [[group torrents] indexOfObject: item] + 1;
item = group;
}
}
else
{
if ([item isKindOfClass: [Torrent class]])
item = [fTableView parentForItem: item];
index = NSOutlineViewDropOnItemIndex;
}
}
else
{
if (item)
{
index = [fTableView rowForItem: item] + 1;
item = nil;
}
}
2008-02-07 11:57:24 +00:00
[fTableView setDropItem: item dropChildIndex: index];
2007-09-16 01:02:06 +00:00
return NSDragOperationGeneric;
}
return NSDragOperationNone;
}
2008-02-07 11:57:24 +00:00
- (BOOL) outlineView: (NSOutlineView *) outlineView acceptDrop: (id < NSDraggingInfo >) info item: (id) item
childIndex: (NSInteger) newRow
2007-09-16 01:02:06 +00:00
{
NSPasteboard * pasteboard = [info draggingPasteboard];
if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE])
{
//remember selected rows
NSArray * selectedValues = [fTableView selectedValues];
2007-09-16 01:02:06 +00:00
NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: TORRENT_TABLE_VIEW_DATA_TYPE]];
2007-09-16 01:02:06 +00:00
//get the torrents to move
NSMutableArray * movingTorrents = [NSMutableArray arrayWithCapacity: [indexes count]];
2008-11-02 13:52:45 +00:00
for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
[movingTorrents addObject: [fTableView itemAtRow: i]];
2008-05-12 01:49:48 +00:00
//reset groups
if (item)
{
2008-05-12 01:49:48 +00:00
//change groups
2008-11-02 13:52:45 +00:00
NSInteger groupValue = [item groupIndex];
for (Torrent * torrent in movingTorrents)
{
//have to reset objects here to avoid weird crash
[[[fTableView parentForItem: torrent] torrents] removeObject: torrent];
[[item torrents] addObject: torrent];
2008-05-12 01:49:48 +00:00
[torrent setGroupValue: groupValue];
}
//part 2 of avoiding weird crash
[fTableView reloadItem: nil reloadChildren: YES];
}
//reorder queue order
if (newRow != NSOutlineViewDropOnItemIndex)
{
//find torrent to place under
NSArray * groupTorrents = item ? [item torrents] : fDisplayedTorrents;
Torrent * topTorrent = nil;
2008-11-02 13:52:45 +00:00
for (NSInteger i = newRow-1; i >= 0; i--)
{
Torrent * tempTorrent = [groupTorrents objectAtIndex: i];
if (![movingTorrents containsObject: tempTorrent])
{
topTorrent = tempTorrent;
break;
}
}
//remove objects to reinsert
[fTorrents removeObjectsInArray: movingTorrents];
//insert objects at new location
NSUInteger insertIndex = topTorrent ? [fTorrents indexOfObject: topTorrent] + 1 : 0;
NSIndexSet * insertIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(insertIndex, [movingTorrents count])];
for (Torrent * torrent in movingTorrents)
[torrent setQueueIndex: insertIndex++];
[fTorrents insertObjects: movingTorrents atIndexes: insertIndexes];
}
2007-09-16 01:02:06 +00:00
2011-02-20 03:58:50 +00:00
[self applyFilter];
[fTableView selectValues: selectedValues];
2007-09-16 01:02:06 +00:00
}
return YES;
}
- (void) torrentTableViewSelectionDidChange: (NSNotification *) notification
{
[self resetInfo];
[[fWindow toolbar] validateVisibleItems];
2007-09-16 01:02:06 +00:00
}
- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) info
{
NSPasteboard * pasteboard = [info draggingPasteboard];
if ([[pasteboard types] containsObject: NSFilenamesPboardType])
{
//check if any torrent files can be added
BOOL torrent = NO;
NSArray * files = [pasteboard propertyListForType: NSFilenamesPboardType];
for (NSString * file in files)
2007-09-16 01:02:06 +00:00
{
if ([[[NSWorkspace sharedWorkspace] typeOfFile: file error: NULL] isEqualToString: @"org.bittorrent.torrent"]
|| [[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
2007-09-16 01:02:06 +00:00
{
torrent = YES;
tr_ctor * ctor = tr_ctorNew(fLib);
tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]);
2009-08-05 02:13:45 +00:00
if (tr_torrentParse(ctor, NULL) == TR_PARSE_OK)
{
if (!fOverlayWindow)
fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
[fOverlayWindow setTorrents: files];
return NSDragOperationCopy;
}
tr_ctorFree(ctor);
2007-09-16 01:02:06 +00:00
}
}
//create a torrent file if a single file
if (!torrent && [files count] == 1)
{
if (!fOverlayWindow)
fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
[fOverlayWindow setFile: [[files objectAtIndex: 0] lastPathComponent]];
return NSDragOperationCopy;
}
}
else if ([[pasteboard types] containsObject: NSURLPboardType])
{
if (!fOverlayWindow)
fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
[fOverlayWindow setURL: [[NSURL URLFromPasteboard: pasteboard] relativeString]];
return NSDragOperationCopy;
}
else;
return NSDragOperationNone;
}
- (void) draggingExited: (id <NSDraggingInfo>) info
{
if (fOverlayWindow)
[fOverlayWindow fadeOut];
}
- (BOOL) performDragOperation: (id <NSDraggingInfo>) info
{
if (fOverlayWindow)
[fOverlayWindow fadeOut];
NSPasteboard * pasteboard = [info draggingPasteboard];
if ([[pasteboard types] containsObject: NSFilenamesPboardType])
{
BOOL torrent = NO, accept = YES;
//create an array of files that can be opened
NSArray * files = [pasteboard propertyListForType: NSFilenamesPboardType];
NSMutableArray * filesToOpen = [NSMutableArray arrayWithCapacity: [files count]];
for (NSString * file in files)
2007-09-16 01:02:06 +00:00
{
if ([[[NSWorkspace sharedWorkspace] typeOfFile: file error: NULL] isEqualToString: @"org.bittorrent.torrent"]
|| [[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
2007-09-16 01:02:06 +00:00
{
torrent = YES;
tr_ctor * ctor = tr_ctorNew(fLib);
tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]);
2009-08-05 02:13:45 +00:00
if (tr_torrentParse(ctor, NULL) == TR_PARSE_OK)
[filesToOpen addObject: file];
tr_ctorFree(ctor);
2007-09-16 01:02:06 +00:00
}
}
if ([filesToOpen count] > 0)
[self application: NSApp openFiles: filesToOpen];
else
{
if (!torrent && [files count] == 1)
[CreatorWindowController createTorrentFile: fLib forFile: [files objectAtIndex: 0]];
else
accept = NO;
}
return accept;
}
else if ([[pasteboard types] containsObject: NSURLPboardType])
{
NSURL * url;
if ((url = [NSURL URLFromPasteboard: pasteboard]))
{
[self openURL: [url absoluteString]];
2007-09-16 01:02:06 +00:00
return YES;
}
}
else;
return NO;
}
- (void) toggleSmallView: (id) sender
{
BOOL makeSmall = ![fDefaults boolForKey: @"SmallView"];
[fDefaults setBool: makeSmall forKey: @"SmallView"];
2007-09-16 01:02:06 +00:00
[fTableView setUsesAlternatingRowBackgroundColors: !makeSmall];
2007-09-16 01:02:06 +00:00
[fTableView setRowHeight: makeSmall ? ROW_HEIGHT_SMALL : ROW_HEIGHT_REGULAR];
[fTableView noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fTableView numberOfRows])]];
2007-09-16 01:02:06 +00:00
//resize for larger min height if not set to auto size
if (![fDefaults boolForKey: @"AutoSize"])
{
const NSSize contentSize = [[fWindow contentView] frame].size;
NSSize contentMinSize = [fWindow contentMinSize];
contentMinSize.height = [self minWindowContentSizeAllowed];
[fWindow setContentMinSize: contentMinSize];
//make sure the window already isn't too small
2007-09-16 01:02:06 +00:00
if (!makeSmall && contentSize.height < contentMinSize.height)
{
NSRect frame = [fWindow frame];
2008-11-02 13:52:45 +00:00
CGFloat heightChange = contentMinSize.height - contentSize.height;
2007-09-16 01:02:06 +00:00
frame.size.height += heightChange;
frame.origin.y -= heightChange;
[fWindow setFrame: frame display: YES];
}
}
else
[self setWindowSizeToFit];
}
- (void) togglePiecesBar: (id) sender
{
[fDefaults setBool: ![fDefaults boolForKey: @"PiecesBar"] forKey: @"PiecesBar"];
[fTableView togglePiecesBar];
}
- (void) toggleAvailabilityBar: (id) sender
{
[fDefaults setBool: ![fDefaults boolForKey: @"DisplayProgressBarAvailable"] forKey: @"DisplayProgressBarAvailable"];
[fTableView display];
}
- (void) toggleStatusString: (id) sender
{
if ([fDefaults boolForKey: @"SmallView"])
[fDefaults setBool: ![fDefaults boolForKey: @"DisplaySmallStatusRegular"] forKey: @"DisplaySmallStatusRegular"];
else
[fDefaults setBool: ![fDefaults boolForKey: @"DisplayStatusProgressSelected"] forKey: @"DisplayStatusProgressSelected"];
[fTableView reloadData];
}
2008-11-02 13:52:45 +00:00
- (NSRect) windowFrameByAddingHeight: (CGFloat) height checkLimits: (BOOL) check
2007-09-16 01:02:06 +00:00
{
NSScrollView * scrollView = [fTableView enclosingScrollView];
2007-09-16 01:02:06 +00:00
//convert pixels to points
NSRect windowFrame = [fWindow frame];
NSSize windowSize = [scrollView convertSize: windowFrame.size fromView: nil];
2007-09-16 01:02:06 +00:00
windowSize.height += height;
if (check)
{
//we can't call minSize, since it might be set to the current size (auto size)
const CGFloat minHeight = [self minWindowContentSizeAllowed]
+ (NSHeight([fWindow frame]) - NSHeight([[fWindow contentView] frame])); //contentView to window
2007-09-16 01:02:06 +00:00
if (windowSize.height < minHeight)
windowSize.height =minHeight;
2007-09-16 01:02:06 +00:00
else
{
NSSize maxSize = [scrollView convertSize: [[fWindow screen] visibleFrame].size fromView: nil];
if (!fStatusBar)
maxSize.height -= STATUS_BAR_HEIGHT;
if (!fFilterBar)
maxSize.height -= FILTER_BAR_HEIGHT;
2007-09-16 01:02:06 +00:00
if (windowSize.height > maxSize.height)
windowSize.height = maxSize.height;
}
}
//convert points to pixels
windowSize = [scrollView convertSize: windowSize toView: nil];
2007-09-16 01:02:06 +00:00
windowFrame.origin.y -= (windowSize.height - windowFrame.size.height);
windowFrame.size.height = windowSize.height;
return windowFrame;
}
- (void) toggleStatusBar: (id) sender
{
const BOOL show = fStatusBar == nil;
[self showStatusBar: show animate: YES];
[fDefaults setBool: show forKey: @"StatusBar"];
}
//doesn't save shown state
2007-09-16 01:02:06 +00:00
- (void) showStatusBar: (BOOL) show animate: (BOOL) animate
{
const BOOL prevShown = fStatusBar != nil;
if (show == prevShown)
2007-09-16 01:02:06 +00:00
return;
2007-09-16 01:02:06 +00:00
if (show)
{
fStatusBar = [[StatusBarController alloc] initWithLib: fLib];
NSView * contentView = [fWindow contentView];
const NSSize windowSize = [contentView convertSize: [fWindow frame].size fromView: nil];
NSRect statusBarFrame = [[fStatusBar view] frame];
statusBarFrame.size.width = windowSize.width;
[[fStatusBar view] setFrame: statusBarFrame];
[contentView addSubview: [fStatusBar view]];
2011-02-20 03:58:50 +00:00
[[fStatusBar view] setFrameOrigin: NSMakePoint(0.0, NSMaxY([contentView frame]))];
}
2007-09-16 01:02:06 +00:00
NSRect frame;
CGFloat heightChange = [[fStatusBar view] frame].size.height;
2007-09-16 01:02:06 +00:00
if (!show)
heightChange *= -1;
//allow bar to show even if not enough room
if (show && ![fDefaults boolForKey: @"AutoSize"])
{
frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
2008-11-02 13:52:45 +00:00
CGFloat change = [[fWindow screen] visibleFrame].size.height - frame.size.height;
2007-09-16 01:02:06 +00:00
if (change < 0.0)
{
frame = [fWindow frame];
frame.size.height += change;
frame.origin.y -= change;
[fWindow setFrame: frame display: NO animate: NO];
}
}
2007-09-16 01:02:06 +00:00
[self updateUI];
NSScrollView * scrollView = [fTableView enclosingScrollView];
2007-09-16 01:02:06 +00:00
//set views to not autoresize
const NSUInteger statsMask = [[fStatusBar view] autoresizingMask];
[[fStatusBar view] setAutoresizingMask: NSViewNotSizable];
2011-02-20 03:58:50 +00:00
NSUInteger filterMask;
if (fFilterBar)
{
filterMask = [[fFilterBar view] autoresizingMask];
[[fFilterBar view] setAutoresizingMask: NSViewNotSizable];
}
const NSUInteger scrollMask = [scrollView autoresizingMask];
[scrollView setAutoresizingMask: NSViewNotSizable];
2007-09-16 01:02:06 +00:00
frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
[fWindow setFrame: frame display: YES animate: animate];
//re-enable autoresize
[[fStatusBar view] setAutoresizingMask: statsMask];
2011-02-20 03:58:50 +00:00
if (fFilterBar)
[[fFilterBar view] setAutoresizingMask: filterMask];
[scrollView setAutoresizingMask: scrollMask];
2007-09-16 01:02:06 +00:00
if (!show)
{
[[fStatusBar view] removeFromSuperviewWithoutNeedingDisplay];
[fStatusBar release];
fStatusBar = nil;
}
if ([fDefaults boolForKey: @"AutoSize"])
[self setWindowMinMaxToCurrent];
else
{
//change min size
NSSize minSize = [fWindow contentMinSize];
minSize.height += heightChange;
[fWindow setContentMinSize: minSize];
}
2007-09-16 01:02:06 +00:00
}
- (void) toggleFilterBar: (id) sender
{
2011-02-20 03:58:50 +00:00
const BOOL show = fFilterBar == nil;
[self showFilterBar: show animate: YES];
[fDefaults setBool: show forKey: @"FilterBar"];
[[fWindow toolbar] validateVisibleItems];
2007-09-16 01:02:06 +00:00
//disable filtering when hiding
2011-02-20 03:58:50 +00:00
if (!show)
2007-09-16 01:02:06 +00:00
{
2011-02-20 03:58:50 +00:00
[[NSUserDefaults standardUserDefaults] setObject: FILTER_NONE forKey: @"Filter"];
[[NSUserDefaults standardUserDefaults] setInteger: GROUP_FILTER_ALL_TAG forKey: @"FilterGroup"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey: @"FilterSearchString"];
2007-09-16 01:02:06 +00:00
}
[self applyFilter]; //do even if showing to ensure tooltips are updated
2007-09-16 01:02:06 +00:00
}
//doesn't save shown state
2007-09-16 01:02:06 +00:00
- (void) showFilterBar: (BOOL) show animate: (BOOL) animate
{
2011-02-20 03:58:50 +00:00
const BOOL prevShown = fFilterBar != nil;
if (show == prevShown)
2007-09-16 01:02:06 +00:00
return;
2011-02-20 03:58:50 +00:00
2007-09-16 01:02:06 +00:00
if (show)
2011-02-20 03:58:50 +00:00
{
fFilterBar = [[FilterBarController alloc] init];
NSView * contentView = [fWindow contentView];
const NSSize windowSize = [contentView convertSize: [fWindow frame].size fromView: nil];
NSRect filterBarFrame = [[fFilterBar view] frame];
filterBarFrame.size.width = windowSize.width;
[[fFilterBar view] setFrame: filterBarFrame];
if (fStatusBar)
[contentView addSubview: [fFilterBar view] positioned: NSWindowBelow relativeTo: [fStatusBar view]];
else
[contentView addSubview: [fFilterBar view]];
const CGFloat originY = fStatusBar ? NSMinY([[fStatusBar view] frame]) : NSMaxY([contentView frame]);
[[fFilterBar view] setFrameOrigin: NSMakePoint(0.0, originY)];
}
else
[fWindow makeFirstResponder: fTableView];
2011-02-20 03:58:50 +00:00
CGFloat heightChange = NSHeight([[fFilterBar view] frame]);
2007-09-16 01:02:06 +00:00
if (!show)
heightChange *= -1;
//allow bar to show even if not enough room
if (show && ![fDefaults boolForKey: @"AutoSize"])
{
2011-02-20 03:58:50 +00:00
NSRect frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
const CGFloat change = NSHeight([[fWindow screen] visibleFrame]) - NSHeight(frame);
2007-09-16 01:02:06 +00:00
if (change < 0.0)
{
frame = [fWindow frame];
frame.size.height += change;
frame.origin.y -= change;
[fWindow setFrame: frame display: NO animate: NO];
}
}
NSScrollView * scrollView = [fTableView enclosingScrollView];
2007-09-16 01:02:06 +00:00
//set views to not autoresize
2011-02-20 03:58:50 +00:00
const NSUInteger filterMask = [[fFilterBar view] autoresizingMask];
const NSUInteger scrollMask = [scrollView autoresizingMask];
[[fFilterBar view] setAutoresizingMask: NSViewNotSizable];
[scrollView setAutoresizingMask: NSViewNotSizable];
2007-09-16 01:02:06 +00:00
2011-02-20 03:58:50 +00:00
const NSRect frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
2007-09-16 01:02:06 +00:00
[fWindow setFrame: frame display: YES animate: animate];
//re-enable autoresize
2011-02-20 03:58:50 +00:00
[[fFilterBar view] setAutoresizingMask: filterMask];
[scrollView setAutoresizingMask: scrollMask];
2007-09-16 01:02:06 +00:00
if (!show)
{
[[fFilterBar view] removeFromSuperviewWithoutNeedingDisplay];
2011-02-20 03:58:50 +00:00
[fFilterBar release];
fFilterBar = nil;
2007-09-16 01:02:06 +00:00
}
if ([fDefaults boolForKey: @"AutoSize"])
[self setWindowMinMaxToCurrent];
else
{
//change min size
NSSize minSize = [fWindow contentMinSize];
minSize.height += heightChange;
[fWindow setContentMinSize: minSize];
}
2007-09-16 01:02:06 +00:00
}
- (void) focusFilterField
{
if (!fFilterBar)
[self toggleFilterBar: self];
[fFilterBar focusSearchField];
}
#warning change from id to QLPreviewPanel
- (BOOL) acceptsPreviewPanelControl: (id) panel
{
return !fQuitting;
}
- (void) beginPreviewPanelControl: (id) panel
{
fPreviewPanel = [panel retain];
[fPreviewPanel setDelegate: self];
[fPreviewPanel setDataSource: self];
}
- (void) endPreviewPanelControl: (id) panel
{
[fPreviewPanel release];
fPreviewPanel = nil;
}
- (NSArray *) quickLookableTorrents
{
NSArray * selectedTorrents = [fTableView selectedTorrents];
NSMutableArray * qlArray = [NSMutableArray arrayWithCapacity: [selectedTorrents count]];
for (Torrent * torrent in selectedTorrents)
if (([torrent isFolder] || [torrent isComplete]) && [torrent dataLocation])
[qlArray addObject: torrent];
return qlArray;
}
- (NSInteger) numberOfPreviewItemsInPreviewPanel: (id) panel
{
if ([fInfoController canQuickLook])
return [[fInfoController quickLookURLs] count];
else
return [[self quickLookableTorrents] count];
}
- (id /*<QLPreviewItem>*/) previewPanel: (id) panel previewItemAtIndex: (NSInteger) index
{
if ([fInfoController canQuickLook])
return [[fInfoController quickLookURLs] objectAtIndex: index];
else
return [[self quickLookableTorrents] objectAtIndex: index];
}
- (BOOL) previewPanel: (id) panel handleEvent: (NSEvent *) event
{
/*if ([event type] == NSKeyDown)
{
[super keyDown: event];
return YES;
}*/
return NO;
}
- (NSRect) previewPanel: (id) panel sourceFrameOnScreenForPreviewItem: (id /*<QLPreviewItem>*/) item
{
if ([fInfoController canQuickLook])
return [fInfoController quickLookSourceFrameForPreviewItem: item];
else
{
if (![fWindow isVisible])
return NSZeroRect;
const NSInteger row = [fTableView rowForItem: item];
if (row == -1)
return NSZeroRect;
NSRect frame = [fTableView iconRectForRow: row];
if (!NSIntersectsRect([fTableView visibleRect], frame))
return NSZeroRect;
frame.origin = [fTableView convertPoint: frame.origin toView: nil];
frame.origin = [fWindow convertBaseToScreen: frame.origin];
frame.origin.y -= frame.size.height;
return frame;
}
}
2007-12-06 20:54:26 +00:00
- (ButtonToolbarItem *) standardToolbarButtonWithIdentifier: (NSString *) ident
2007-09-16 01:02:06 +00:00
{
2007-12-06 20:54:26 +00:00
ButtonToolbarItem * item = [[ButtonToolbarItem alloc] initWithItemIdentifier: ident];
2011-06-28 01:39:30 +00:00
NSButton * button = [[NSButton alloc] init];
[button setBezelStyle: NSTexturedRoundedBezelStyle];
[button setStringValue: @""];
2007-12-04 01:46:27 +00:00
[item setView: button];
[button release];
const NSSize buttonSize = NSMakeSize(36.0, 25.0);
[item setMinSize: buttonSize];
[item setMaxSize: buttonSize];
2007-12-06 20:54:26 +00:00
return [item autorelease];
}
- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
{
2007-09-16 01:02:06 +00:00
if ([ident isEqualToString: TOOLBAR_CREATE])
{
2007-12-06 20:54:26 +00:00
ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
2007-09-16 01:02:06 +00:00
[item setLabel: NSLocalizedString(@"Create", "Create toolbar item -> label")];
[item setPaletteLabel: NSLocalizedString(@"Create Torrent File", "Create toolbar item -> palette label")];
[item setToolTip: NSLocalizedString(@"Create torrent file", "Create toolbar item -> tooltip")];
[item setImage: [NSImage imageNamed: @"ToolbarCreateTemplate.png"]];
2007-09-16 01:02:06 +00:00
[item setTarget: self];
[item setAction: @selector(createFile:)];
[item setAutovalidates: NO];
2007-12-06 20:54:26 +00:00
return item;
2007-09-16 01:02:06 +00:00
}
else if ([ident isEqualToString: TOOLBAR_OPEN_FILE])
2007-09-16 01:02:06 +00:00
{
2007-12-06 20:54:26 +00:00
ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
2007-09-16 01:02:06 +00:00
[item setLabel: NSLocalizedString(@"Open", "Open toolbar item -> label")];
[item setPaletteLabel: NSLocalizedString(@"Open Torrent Files", "Open toolbar item -> palette label")];
[item setToolTip: NSLocalizedString(@"Open torrent files", "Open toolbar item -> tooltip")];
[item setImage: [NSImage imageNamed: @"ToolbarOpenTemplate.png"]];
2007-09-16 01:02:06 +00:00
[item setTarget: self];
[item setAction: @selector(openShowSheet:)];
[item setAutovalidates: NO];
2007-12-06 20:54:26 +00:00
return item;
2007-09-16 01:02:06 +00:00
}
else if ([ident isEqualToString: TOOLBAR_OPEN_WEB])
{
ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
[item setLabel: NSLocalizedString(@"Open Address", "Open address toolbar item -> label")];
[item setPaletteLabel: NSLocalizedString(@"Open Torrent Address", "Open address toolbar item -> palette label")];
[item setToolTip: NSLocalizedString(@"Open torrent web address", "Open address toolbar item -> tooltip")];
[item setImage: [NSImage imageNamed: @"ToolbarOpenWebTemplate.png"]];
[item setTarget: self];
[item setAction: @selector(openURLShowSheet:)];
[item setAutovalidates: NO];
return item;
}
2007-09-16 01:02:06 +00:00
else if ([ident isEqualToString: TOOLBAR_REMOVE])
{
2007-12-06 20:54:26 +00:00
ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
2007-09-16 01:02:06 +00:00
[item setLabel: NSLocalizedString(@"Remove", "Remove toolbar item -> label")];
[item setPaletteLabel: NSLocalizedString(@"Remove Selected", "Remove toolbar item -> palette label")];
[item setToolTip: NSLocalizedString(@"Remove selected transfers", "Remove toolbar item -> tooltip")];
[item setImage: [NSImage imageNamed: @"ToolbarRemoveTemplate.png"]];
2007-09-16 01:02:06 +00:00
[item setTarget: self];
[item setAction: @selector(removeNoDelete:)];
[item setVisibilityPriority: NSToolbarItemVisibilityPriorityHigh];
2007-12-06 20:54:26 +00:00
return item;
2007-09-16 01:02:06 +00:00
}
else if ([ident isEqualToString: TOOLBAR_INFO])
{
2007-12-06 20:54:26 +00:00
ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
[[(NSButton *)[item view] cell] setShowsStateBy: NSContentsCellMask]; //blue when enabled
2007-12-06 20:54:26 +00:00
2007-09-16 01:02:06 +00:00
[item setLabel: NSLocalizedString(@"Inspector", "Inspector toolbar item -> label")];
[item setPaletteLabel: NSLocalizedString(@"Toggle Inspector", "Inspector toolbar item -> palette label")];
[item setToolTip: NSLocalizedString(@"Toggle the torrent inspector", "Inspector toolbar item -> tooltip")];
[item setImage: [NSImage imageNamed: @"ToolbarInfoTemplate.png"]];
2007-09-16 01:02:06 +00:00
[item setTarget: self];
[item setAction: @selector(showInfo:)];
2007-12-06 20:54:26 +00:00
return item;
2007-09-16 01:02:06 +00:00
}
else if ([ident isEqualToString: TOOLBAR_PAUSE_RESUME_ALL])
2007-09-16 01:02:06 +00:00
{
GroupToolbarItem * groupItem = [[GroupToolbarItem alloc] initWithItemIdentifier: ident];
2008-04-22 13:25:53 +00:00
NSSegmentedControl * segmentedControl = [[NSSegmentedControl alloc] initWithFrame: NSZeroRect];
[segmentedControl setCell: [[[ToolbarSegmentedCell alloc] init] autorelease]];
[groupItem setView: segmentedControl];
2007-12-07 03:49:46 +00:00
NSSegmentedCell * segmentedCell = (NSSegmentedCell *)[segmentedControl cell];
[segmentedControl setSegmentCount: 2];
2007-12-07 03:49:46 +00:00
[segmentedCell setTrackingMode: NSSegmentSwitchTrackingMomentary];
const NSSize groupSize = NSMakeSize(72.0, 25.0);
[groupItem setMinSize: groupSize];
[groupItem setMaxSize: groupSize];
[groupItem setLabel: NSLocalizedString(@"Apply All", "All toolbar item -> label")];
[groupItem setPaletteLabel: NSLocalizedString(@"Pause / Resume All", "All toolbar item -> palette label")];
[groupItem setTarget: self];
[groupItem setAction: @selector(allToolbarClicked:)];
[groupItem setIdentifiers: [NSArray arrayWithObjects: TOOLBAR_PAUSE_ALL, TOOLBAR_RESUME_ALL, nil]];
2007-12-07 03:49:46 +00:00
[segmentedCell setTag: TOOLBAR_PAUSE_TAG forSegment: TOOLBAR_PAUSE_TAG];
[segmentedControl setImage: [NSImage imageNamed: @"ToolbarPauseAllTemplate.png"] forSegment: TOOLBAR_PAUSE_TAG];
2007-12-07 03:49:46 +00:00
[segmentedCell setToolTip: NSLocalizedString(@"Pause all transfers",
"All toolbar item -> tooltip") forSegment: TOOLBAR_PAUSE_TAG];
2007-12-07 03:49:46 +00:00
[segmentedCell setTag: TOOLBAR_RESUME_TAG forSegment: TOOLBAR_RESUME_TAG];
[segmentedControl setImage: [NSImage imageNamed: @"ToolbarResumeAllTemplate.png"] forSegment: TOOLBAR_RESUME_TAG];
2007-12-07 03:49:46 +00:00
[segmentedCell setToolTip: NSLocalizedString(@"Resume all transfers",
"All toolbar item -> tooltip") forSegment: TOOLBAR_RESUME_TAG];
2007-12-06 20:46:58 +00:00
[groupItem createMenu: [NSArray arrayWithObjects: NSLocalizedString(@"Pause All", "All toolbar item -> label"),
NSLocalizedString(@"Resume All", "All toolbar item -> label"), nil]];
[segmentedControl release];
[groupItem setVisibilityPriority: NSToolbarItemVisibilityPriorityHigh];
2007-12-06 20:54:26 +00:00
return [groupItem autorelease];
2007-09-16 01:02:06 +00:00
}
else if ([ident isEqualToString: TOOLBAR_PAUSE_RESUME_SELECTED])
2007-09-16 01:02:06 +00:00
{
GroupToolbarItem * groupItem = [[GroupToolbarItem alloc] initWithItemIdentifier: ident];
2008-04-22 13:25:53 +00:00
NSSegmentedControl * segmentedControl = [[NSSegmentedControl alloc] initWithFrame: NSZeroRect];
[segmentedControl setCell: [[[ToolbarSegmentedCell alloc] init] autorelease]];
[groupItem setView: segmentedControl];
2007-12-07 03:49:46 +00:00
NSSegmentedCell * segmentedCell = (NSSegmentedCell *)[segmentedControl cell];
[segmentedControl setSegmentCount: 2];
2007-12-07 03:49:46 +00:00
[segmentedCell setTrackingMode: NSSegmentSwitchTrackingMomentary];
const NSSize groupSize = NSMakeSize(72.0, 25.0);
[groupItem setMinSize: groupSize];
[groupItem setMaxSize: groupSize];
[groupItem setLabel: NSLocalizedString(@"Apply Selected", "Selected toolbar item -> label")];
[groupItem setPaletteLabel: NSLocalizedString(@"Pause / Resume Selected", "Selected toolbar item -> palette label")];
[groupItem setTarget: self];
[groupItem setAction: @selector(selectedToolbarClicked:)];
[groupItem setIdentifiers: [NSArray arrayWithObjects: TOOLBAR_PAUSE_SELECTED, TOOLBAR_RESUME_SELECTED, nil]];
2007-12-07 03:49:46 +00:00
[segmentedCell setTag: TOOLBAR_PAUSE_TAG forSegment: TOOLBAR_PAUSE_TAG];
[segmentedControl setImage: [NSImage imageNamed: @"ToolbarPauseSelectedTemplate.png"] forSegment: TOOLBAR_PAUSE_TAG];
2007-12-07 03:49:46 +00:00
[segmentedCell setToolTip: NSLocalizedString(@"Pause selected transfers",
"Selected toolbar item -> tooltip") forSegment: TOOLBAR_PAUSE_TAG];
2007-12-07 03:49:46 +00:00
[segmentedCell setTag: TOOLBAR_RESUME_TAG forSegment: TOOLBAR_RESUME_TAG];
[segmentedControl setImage: [NSImage imageNamed: @"ToolbarResumeSelectedTemplate.png"] forSegment: TOOLBAR_RESUME_TAG];
2007-12-07 03:49:46 +00:00
[segmentedCell setToolTip: NSLocalizedString(@"Resume selected transfers",
"Selected toolbar item -> tooltip") forSegment: TOOLBAR_RESUME_TAG];
2007-12-06 20:46:58 +00:00
[groupItem createMenu: [NSArray arrayWithObjects: NSLocalizedString(@"Pause Selected", "Selected toolbar item -> label"),
NSLocalizedString(@"Resume Selected", "Selected toolbar item -> label"), nil]];
[segmentedControl release];
[groupItem setVisibilityPriority: NSToolbarItemVisibilityPriorityHigh];
2007-12-06 20:54:26 +00:00
return [groupItem autorelease];
2007-09-16 01:02:06 +00:00
}
else if ([ident isEqualToString: TOOLBAR_FILTER])
{
2007-12-06 20:54:26 +00:00
ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
[[(NSButton *)[item view] cell] setShowsStateBy: NSContentsCellMask]; //blue when enabled
2007-12-06 20:54:26 +00:00
2007-09-16 01:02:06 +00:00
[item setLabel: NSLocalizedString(@"Filter", "Filter toolbar item -> label")];
[item setPaletteLabel: NSLocalizedString(@"Toggle Filter", "Filter toolbar item -> palette label")];
[item setToolTip: NSLocalizedString(@"Toggle the filter bar", "Filter toolbar item -> tooltip")];
[item setImage: [NSImage imageNamed: @"ToolbarFilterTemplate.png"]];
2007-09-16 01:02:06 +00:00
[item setTarget: self];
[item setAction: @selector(toggleFilterBar:)];
2007-12-06 20:54:26 +00:00
return item;
2007-09-16 01:02:06 +00:00
}
2008-05-21 16:02:38 +00:00
else if ([ident isEqualToString: TOOLBAR_QUICKLOOK])
{
ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
[[(NSButton *)[item view] cell] setShowsStateBy: NSContentsCellMask]; //blue when enabled
2008-05-21 16:02:38 +00:00
2008-05-29 00:56:58 +00:00
[item setLabel: NSLocalizedString(@"Quick Look", "QuickLook toolbar item -> label")];
2008-05-21 16:02:38 +00:00
[item setPaletteLabel: NSLocalizedString(@"Quick Look", "QuickLook toolbar item -> palette label")];
[item setToolTip: NSLocalizedString(@"Quick Look", "QuickLook toolbar item -> tooltip")];
[item setImage: [NSImage imageNamed: NSImageNameQuickLookTemplate]];
[item setTarget: self];
2008-05-21 16:28:21 +00:00
[item setAction: @selector(toggleQuickLook:)];
[item setVisibilityPriority: NSToolbarItemVisibilityPriorityLow];
2008-05-21 16:02:38 +00:00
return item;
}
2007-09-16 01:02:06 +00:00
else
return nil;
}
- (void) allToolbarClicked: (id) sender
{
2008-11-02 13:52:45 +00:00
NSInteger tagValue = [sender isKindOfClass: [NSSegmentedControl class]]
? [(NSSegmentedCell *)[sender cell] tagForSegment: [sender selectedSegment]] : [sender tag];
switch (tagValue)
{
case TOOLBAR_PAUSE_TAG:
[self stopAllTorrents: sender];
break;
case TOOLBAR_RESUME_TAG:
[self resumeAllTorrents: sender];
break;
}
}
- (void) selectedToolbarClicked: (id) sender
{
2008-11-02 13:52:45 +00:00
NSInteger tagValue = [sender isKindOfClass: [NSSegmentedControl class]]
? [(NSSegmentedCell *)[sender cell] tagForSegment: [sender selectedSegment]] : [sender tag];
switch (tagValue)
{
case TOOLBAR_PAUSE_TAG:
[self stopSelectedTorrents: sender];
break;
case TOOLBAR_RESUME_TAG:
[self resumeSelectedTorrents: sender];
break;
}
2007-09-16 01:02:06 +00:00
}
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
2007-09-16 01:02:06 +00:00
{
2008-12-26 05:57:51 +00:00
return [NSArray arrayWithObjects:
TOOLBAR_CREATE, TOOLBAR_OPEN_FILE, TOOLBAR_OPEN_WEB, TOOLBAR_REMOVE,
TOOLBAR_PAUSE_RESUME_SELECTED, TOOLBAR_PAUSE_RESUME_ALL,
TOOLBAR_QUICKLOOK, TOOLBAR_FILTER, TOOLBAR_INFO,
NSToolbarSeparatorItemIdentifier,
NSToolbarSpaceItemIdentifier,
NSToolbarFlexibleSpaceItemIdentifier,
NSToolbarCustomizeToolbarItemIdentifier, nil];
2007-09-16 01:02:06 +00:00
}
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
2007-09-16 01:02:06 +00:00
{
2008-12-26 05:57:51 +00:00
return [NSArray arrayWithObjects:
TOOLBAR_CREATE, TOOLBAR_OPEN_FILE, TOOLBAR_REMOVE, NSToolbarSpaceItemIdentifier,
2008-12-26 05:57:51 +00:00
TOOLBAR_PAUSE_RESUME_ALL, NSToolbarFlexibleSpaceItemIdentifier,
TOOLBAR_QUICKLOOK, TOOLBAR_FILTER, TOOLBAR_INFO, nil];
2007-09-16 01:02:06 +00:00
}
- (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
{
NSString * ident = [toolbarItem itemIdentifier];
2007-09-16 01:02:06 +00:00
//enable remove item
if ([ident isEqualToString: TOOLBAR_REMOVE])
return [fTableView numberOfSelectedRows] > 0;
//enable pause all item
if ([ident isEqualToString: TOOLBAR_PAUSE_ALL])
{
for (Torrent * torrent in fTorrents)
2007-09-16 01:02:06 +00:00
if ([torrent isActive] || [torrent waitingToStart])
return YES;
return NO;
}
//enable resume all item
if ([ident isEqualToString: TOOLBAR_RESUME_ALL])
{
for (Torrent * torrent in fTorrents)
if (![torrent isActive] && ![torrent waitingToStart])
2007-09-16 01:02:06 +00:00
return YES;
return NO;
}
//enable pause item
if ([ident isEqualToString: TOOLBAR_PAUSE_SELECTED])
{
for (Torrent * torrent in [fTableView selectedTorrents])
2007-09-16 01:02:06 +00:00
if ([torrent isActive] || [torrent waitingToStart])
return YES;
return NO;
}
//enable resume item
if ([ident isEqualToString: TOOLBAR_RESUME_SELECTED])
{
for (Torrent * torrent in [fTableView selectedTorrents])
if (![torrent isActive] && ![torrent waitingToStart])
2007-09-16 01:02:06 +00:00
return YES;
return NO;
}
//set info item
if ([ident isEqualToString: TOOLBAR_INFO])
{
[(NSButton *)[toolbarItem view] setState: [[fInfoController window] isVisible]];
return YES;
}
//set filter item
if ([ident isEqualToString: TOOLBAR_FILTER])
{
2011-02-20 03:58:50 +00:00
[(NSButton *)[toolbarItem view] setState: fFilterBar != nil];
return YES;
}
//set quick look item
if ([ident isEqualToString: TOOLBAR_QUICKLOOK])
{
[(NSButton *)[toolbarItem view] setState: [NSApp isOnSnowLeopardOrBetter] && [QLPreviewPanelSL sharedPreviewPanelExists]
&& [[QLPreviewPanelSL sharedPreviewPanel] isVisible]];
return [NSApp isOnSnowLeopardOrBetter];
}
2007-09-16 01:02:06 +00:00
return YES;
}
- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
{
SEL action = [menuItem action];
if (action == @selector(toggleSpeedLimit:))
{
[menuItem setState: [fDefaults boolForKey: @"SpeedLimit"] ? NSOnState : NSOffState];
return YES;
}
2007-09-16 01:02:06 +00:00
//only enable some items if it is in a context menu or the window is useable
BOOL canUseTable = [fWindow isKeyWindow] || [[menuItem menu] supermenu] != [NSApp mainMenu];
//enable open items
if (action == @selector(openShowSheet:) || action == @selector(openURLShowSheet:))
return [fWindow attachedSheet] == nil;
2007-11-04 16:45:17 +00:00
//enable sort options
if (action == @selector(setSort:))
{
NSString * sortType;
switch ([menuItem tag])
{
case SORT_ORDER_TAG:
sortType = SORT_ORDER;
break;
case SORT_DATE_TAG:
sortType = SORT_DATE;
break;
case SORT_NAME_TAG:
sortType = SORT_NAME;
break;
case SORT_PROGRESS_TAG:
sortType = SORT_PROGRESS;
break;
case SORT_STATE_TAG:
sortType = SORT_STATE;
break;
case SORT_TRACKER_TAG:
sortType = SORT_TRACKER;
break;
2007-11-21 18:32:41 +00:00
case SORT_ACTIVITY_TAG:
sortType = SORT_ACTIVITY;
break;
2011-07-04 19:47:15 +00:00
case SORT_SIZE_TAG:
sortType = SORT_SIZE;
break;
default:
NSAssert1(NO, @"Unknown sort tag received: %d", [menuItem tag]);
}
[menuItem setState: [sortType isEqualToString: [fDefaults stringForKey: @"Sort"]] ? NSOnState : NSOffState];
return [fWindow isVisible];
}
if (action == @selector(setGroup:))
{
BOOL checked = NO;
2008-11-02 13:52:45 +00:00
NSInteger index = [menuItem tag];
for (Torrent * torrent in [fTableView selectedTorrents])
if (index == [torrent groupValue])
{
checked = YES;
break;
}
[menuItem setState: checked ? NSOnState : NSOffState];
return canUseTable && [fTableView numberOfSelectedRows] > 0;
}
2007-12-19 20:46:00 +00:00
if (action == @selector(toggleSmallView:))
{
[menuItem setState: [fDefaults boolForKey: @"SmallView"] ? NSOnState : NSOffState];
2007-09-16 01:02:06 +00:00
return [fWindow isVisible];
}
if (action == @selector(togglePiecesBar:))
{
[menuItem setState: [fDefaults boolForKey: @"PiecesBar"] ? NSOnState : NSOffState];
return [fWindow isVisible];
}
if (action == @selector(toggleStatusString:))
{
if ([fDefaults boolForKey: @"SmallView"])
{
[menuItem setTitle: NSLocalizedString(@"Remaining Time", "Action menu -> status string toggle")];
[menuItem setState: ![fDefaults boolForKey: @"DisplaySmallStatusRegular"] ? NSOnState : NSOffState];
}
else
{
[menuItem setTitle: NSLocalizedString(@"Status of Selected Files", "Action menu -> status string toggle")];
[menuItem setState: [fDefaults boolForKey: @"DisplayStatusProgressSelected"] ? NSOnState : NSOffState];
}
return [fWindow isVisible];
}
if (action == @selector(toggleAvailabilityBar:))
{
[menuItem setState: [fDefaults boolForKey: @"DisplayProgressBarAvailable"] ? NSOnState : NSOffState];
return [fWindow isVisible];
}
if (action == @selector(setLimitGlobalEnabled:))
{
BOOL upload = [menuItem menu] == fUploadMenu;
BOOL limit = menuItem == (upload ? fUploadLimitItem : fDownloadLimitItem);
if (limit)
[menuItem setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
"Action menu -> upload/download limit"),
[fDefaults integerForKey: upload ? @"UploadLimit" : @"DownloadLimit"]]];
[menuItem setState: [fDefaults boolForKey: upload ? @"CheckUpload" : @"CheckDownload"] ? limit : !limit];
return YES;
}
if (action == @selector(setRatioGlobalEnabled:))
{
BOOL check = menuItem == fCheckRatioItem;
if (check)
2008-03-24 00:06:54 +00:00
[menuItem setTitle: [NSString localizedStringWithFormat: NSLocalizedString(@"Stop at Ratio (%.2f)",
"Action menu -> ratio stop"), [fDefaults floatForKey: @"RatioLimit"]]];
[menuItem setState: [fDefaults boolForKey: @"RatioCheck"] ? check : !check];
return YES;
}
2007-09-16 01:02:06 +00:00
//enable show info
if (action == @selector(showInfo:))
{
NSString * title = [[fInfoController window] isVisible] ? NSLocalizedString(@"Hide Inspector", "View menu -> Inspector")
: NSLocalizedString(@"Show Inspector", "View menu -> Inspector");
[menuItem setTitle: title];
2007-09-16 01:02:06 +00:00
return YES;
}
//enable prev/next inspector tab
if (action == @selector(setInfoTab:))
return [[fInfoController window] isVisible];
//enable toggle status bar
if (action == @selector(toggleStatusBar:))
{
NSString * title = !fStatusBar ? NSLocalizedString(@"Show Status Bar", "View menu -> Status Bar")
2007-09-16 01:02:06 +00:00
: NSLocalizedString(@"Hide Status Bar", "View menu -> Status Bar");
[menuItem setTitle: title];
2007-09-16 01:02:06 +00:00
return [fWindow isVisible];
}
//enable toggle filter bar
if (action == @selector(toggleFilterBar:))
{
2011-02-20 03:58:50 +00:00
NSString * title = !fFilterBar ? NSLocalizedString(@"Show Filter Bar", "View menu -> Filter Bar")
2007-09-16 01:02:06 +00:00
: NSLocalizedString(@"Hide Filter Bar", "View menu -> Filter Bar");
[menuItem setTitle: title];
2007-09-16 01:02:06 +00:00
return [fWindow isVisible];
}
//enable prev/next filter button
if (action == @selector(switchFilter:))
2011-02-20 03:58:50 +00:00
return [fWindow isVisible] && fFilterBar;
2007-09-16 01:02:06 +00:00
//enable reveal in finder
if (action == @selector(revealFile:))
return canUseTable && [fTableView numberOfSelectedRows] > 0;
//enable remove items
if (action == @selector(removeNoDelete:) || action == @selector(removeDeleteData:))
2007-09-16 01:02:06 +00:00
{
BOOL warning = NO;
2007-09-16 01:02:06 +00:00
for (Torrent * torrent in [fTableView selectedTorrents])
2007-09-16 01:02:06 +00:00
{
if ([torrent isActive])
2007-09-16 01:02:06 +00:00
{
if ([fDefaults boolForKey: @"CheckRemoveDownloading"] ? ![torrent isSeeding] : YES)
{
warning = YES;
2007-09-16 01:02:06 +00:00
break;
}
2007-09-16 01:02:06 +00:00
}
}
//append or remove ellipsis when needed
NSString * title = [menuItem title], * ellipsis = [NSString ellipsis];
if (warning && [fDefaults boolForKey: @"CheckRemove"])
{
if (![title hasSuffix: ellipsis])
[menuItem setTitle: [title stringByAppendingEllipsis]];
}
else
{
if ([title hasSuffix: ellipsis])
[menuItem setTitle: [title substringToIndex: [title rangeOfString: ellipsis].location]];
}
return canUseTable && [fTableView numberOfSelectedRows] > 0;
2007-09-16 01:02:06 +00:00
}
//remove all completed transfers item
if (action == @selector(clearCompleted:))
{
//append or remove ellipsis when needed
NSString * title = [menuItem title], * ellipsis = [NSString ellipsis];
if ([fDefaults boolForKey: @"WarningRemoveCompleted"])
{
if (![title hasSuffix: ellipsis])
[menuItem setTitle: [title stringByAppendingEllipsis]];
}
else
{
if ([title hasSuffix: ellipsis])
[menuItem setTitle: [title substringToIndex: [title rangeOfString: ellipsis].location]];
}
for (Torrent * torrent in fTorrents)
if ([torrent isFinishedSeeding])
return YES;
return NO;
}
2007-09-16 01:02:06 +00:00
//enable pause all item
if (action == @selector(stopAllTorrents:))
{
for (Torrent * torrent in fTorrents)
2007-09-16 01:02:06 +00:00
if ([torrent isActive] || [torrent waitingToStart])
return YES;
return NO;
}
//enable resume all item
if (action == @selector(resumeAllTorrents:))
{
for (Torrent * torrent in fTorrents)
if (![torrent isActive] && ![torrent waitingToStart] && ![torrent isFinishedSeeding])
2007-09-16 01:02:06 +00:00
return YES;
return NO;
}
//enable resume all waiting item
if (action == @selector(resumeWaitingTorrents:))
{
if (![fDefaults boolForKey: @"Queue"] && ![fDefaults boolForKey: @"QueueSeed"])
return NO;
for (Torrent * torrent in fTorrents)
if (![torrent isActive] && [torrent waitingToStart])
2007-09-16 01:02:06 +00:00
return YES;
return NO;
}
//enable resume selected waiting item
if (action == @selector(resumeSelectedTorrentsNoWait:))
{
if (!canUseTable)
return NO;
for (Torrent * torrent in [fTableView selectedTorrents])
if (![torrent isActive])
2007-09-16 01:02:06 +00:00
return YES;
return NO;
}
//enable pause item
if (action == @selector(stopSelectedTorrents:))
{
if (!canUseTable)
return NO;
for (Torrent * torrent in [fTableView selectedTorrents])
2007-09-16 01:02:06 +00:00
if ([torrent isActive] || [torrent waitingToStart])
return YES;
return NO;
}
//enable resume item
if (action == @selector(resumeSelectedTorrents:))
{
if (!canUseTable)
return NO;
for (Torrent * torrent in [fTableView selectedTorrents])
if (![torrent isActive] && ![torrent waitingToStart])
2007-09-16 01:02:06 +00:00
return YES;
return NO;
}
//enable manual announce item
if (action == @selector(announceSelectedTorrents:))
{
if (!canUseTable)
return NO;
for (Torrent * torrent in [fTableView selectedTorrents])
2007-09-16 01:02:06 +00:00
if ([torrent canManualAnnounce])
return YES;
return NO;
}
//enable reset cache item
2008-02-27 19:04:38 +00:00
if (action == @selector(verifySelectedTorrents:))
{
if (!canUseTable)
return NO;
for (Torrent * torrent in [fTableView selectedTorrents])
if (![torrent isMagnet])
return YES;
return NO;
}
2007-09-16 01:02:06 +00:00
//enable move torrent file item
if (action == @selector(moveDataFilesSelected:))
2007-09-16 01:02:06 +00:00
return canUseTable && [fTableView numberOfSelectedRows] > 0;
//enable copy torrent file item
if (action == @selector(copyTorrentFiles:))
{
if (!canUseTable)
return NO;
for (Torrent * torrent in [fTableView selectedTorrents])
if (![torrent isMagnet])
return YES;
return NO;
}
2007-09-16 01:02:06 +00:00
//enable copy torrent file item
if (action == @selector(copyMagnetLinks:))
return canUseTable && [fTableView numberOfSelectedRows] > 0;
2007-09-16 01:02:06 +00:00
//enable reverse sort item
if (action == @selector(setSortReverse:))
{
const BOOL isReverse = [menuItem tag] == SORT_DESC_TAG;
[menuItem setState: (isReverse == [fDefaults boolForKey: @"SortReverse"]) ? NSOnState : NSOffState];
return ![[fDefaults stringForKey: @"Sort"] isEqualToString: SORT_ORDER];
}
2007-09-16 01:02:06 +00:00
2007-12-17 19:47:06 +00:00
//enable group sort item
if (action == @selector(setSortByGroup:))
{
[menuItem setState: [fDefaults boolForKey: @"SortByGroup"] ? NSOnState : NSOffState];
return YES;
2007-12-17 19:47:06 +00:00
}
if (action == @selector(toggleQuickLook:))
{
const BOOL visible = [NSApp isOnSnowLeopardOrBetter] && [QLPreviewPanelSL sharedPreviewPanelExists]
&& [[QLPreviewPanelSL sharedPreviewPanel] isVisible];
//text consistent with Finder
NSString * title = !visible ? NSLocalizedString(@"Quick Look", "View menu -> Quick Look")
: NSLocalizedString(@"Close Quick Look", "View menu -> Quick Look");
[menuItem setTitle: title];
return [NSApp isOnSnowLeopardOrBetter];
}
2007-09-16 01:02:06 +00:00
return YES;
}
- (void) sleepCallback: (natural_t) messageType argument: (void *) messageArgument
2007-09-16 01:02:06 +00:00
{
switch (messageType)
{
case kIOMessageSystemWillSleep:
//if there are any running transfers, wait 15 seconds for them to stop
for (Torrent * torrent in fTorrents)
if ([torrent isActive])
2007-09-16 01:02:06 +00:00
{
//stop all transfers (since some are active) before going to sleep and remember to resume when we wake up
for (Torrent * torrent in fTorrents)
[torrent sleep];
sleep(15);
break;
2007-09-16 01:02:06 +00:00
}
IOAllowPowerChange(fRootPort, (long) messageArgument);
break;
case kIOMessageCanSystemSleep:
if ([fDefaults boolForKey: @"SleepPrevent"])
{
//prevent idle sleep unless no torrents are active
for (Torrent * torrent in fTorrents)
2007-09-16 01:02:06 +00:00
if ([torrent isActive] && ![torrent isStalled] && ![torrent isError])
{
IOCancelPowerChange(fRootPort, (long) messageArgument);
return;
2007-09-16 01:02:06 +00:00
}
}
IOAllowPowerChange(fRootPort, (long) messageArgument);
2007-09-16 01:02:06 +00:00
break;
case kIOMessageSystemHasPoweredOn:
//resume sleeping transfers after we wake up
for (Torrent * torrent in fTorrents)
[torrent wakeUp];
2007-09-16 01:02:06 +00:00
break;
}
}
- (NSMenu *) applicationDockMenu: (NSApplication *) sender
{
2008-11-02 13:52:45 +00:00
NSInteger seeding = 0, downloading = 0;
for (Torrent * torrent in fTorrents)
2007-09-16 01:02:06 +00:00
{
if ([torrent isSeeding])
seeding++;
else if ([torrent isActive])
downloading++;
else;
}
NSMenuItem * seedingItem = [fDockMenu itemWithTag: DOCK_SEEDING_TAG],
* downloadingItem = [fDockMenu itemWithTag: DOCK_DOWNLOADING_TAG];
BOOL hasSeparator = seedingItem || downloadingItem;
if (seeding > 0)
{
2008-03-24 00:06:54 +00:00
NSString * title = [NSString stringWithFormat: NSLocalizedString(@"%d Seeding", "Dock item - Seeding"), seeding];
2007-09-16 01:02:06 +00:00
if (!seedingItem)
{
seedingItem = [[[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""] autorelease];
[seedingItem setTag: DOCK_SEEDING_TAG];
[fDockMenu insertItem: seedingItem atIndex: 0];
}
else
[seedingItem setTitle: title];
}
else
{
if (seedingItem)
[fDockMenu removeItem: seedingItem];
}
if (downloading > 0)
{
2008-03-24 00:06:54 +00:00
NSString * title = [NSString stringWithFormat: NSLocalizedString(@"%d Downloading", "Dock item - Downloading"), downloading];
2007-09-16 01:02:06 +00:00
if (!downloadingItem)
{
downloadingItem = [[[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""] autorelease];
[downloadingItem setTag: DOCK_DOWNLOADING_TAG];
[fDockMenu insertItem: downloadingItem atIndex: seeding > 0 ? 1 : 0];
}
else
[downloadingItem setTitle: title];
}
else
{
if (downloadingItem)
[fDockMenu removeItem: downloadingItem];
}
if (seeding > 0 || downloading > 0)
{
if (!hasSeparator)
[fDockMenu insertItem: [NSMenuItem separatorItem] atIndex: (seeding > 0 && downloading > 0) ? 2 : 1];
2007-09-16 01:02:06 +00:00
}
else
{
if (hasSeparator)
[fDockMenu removeItemAtIndex: 0];
}
return fDockMenu;
}
- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
{
//if auto size is enabled, the current frame shouldn't need to change
NSRect frame = [fDefaults boolForKey: @"AutoSize"] ? [window frame] : [self sizedWindowFrame];
frame.size.width = [fDefaults boolForKey: @"SmallView"] ? [fWindow minSize].width : WINDOW_REGULAR_WIDTH;
return frame;
}
- (void) setWindowSizeToFit
{
if ([fDefaults boolForKey: @"AutoSize"])
{
NSScrollView * scrollView = [fTableView enclosingScrollView];
[scrollView setHasVerticalScroller: NO];
2007-09-16 01:02:06 +00:00
[fWindow setFrame: [self sizedWindowFrame] display: YES animate: YES];
[scrollView setHasVerticalScroller: YES];
//hack to ensure scrollbars don't disappear after resizing
2009-12-23 02:02:11 +00:00
if (![NSApp isOnSnowLeopardOrBetter])
{
[scrollView setAutohidesScrollers: NO];
[scrollView setAutohidesScrollers: YES];
}
[self setWindowMinMaxToCurrent];
2007-09-16 01:02:06 +00:00
}
}
- (NSRect) sizedWindowFrame
{
2008-11-02 13:52:45 +00:00
NSInteger groups = ([fDisplayedTorrents count] > 0 && ![[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [Torrent class]])
? [fDisplayedTorrents count] : 0;
2008-11-02 13:52:45 +00:00
CGFloat heightChange = (GROUP_SEPARATOR_HEIGHT + [fTableView intercellSpacing].height) * groups
+ ([fTableView rowHeight] + [fTableView intercellSpacing].height) * ([fTableView numberOfRows] - groups)
- NSHeight([[fTableView enclosingScrollView] frame]);
2007-09-16 01:02:06 +00:00
return [self windowFrameByAddingHeight: heightChange checkLimits: YES];
}
- (void) updateForAutoSize
{
if ([fDefaults boolForKey: @"AutoSize"])
[self setWindowSizeToFit];
else
{
NSSize contentMinSize = [fWindow contentMinSize];
contentMinSize.height = [self minWindowContentSizeAllowed];
[fWindow setContentMinSize: contentMinSize];
NSSize contentMaxSize = [fWindow contentMaxSize];
contentMaxSize.height = FLT_MAX;
[fWindow setContentMaxSize: contentMaxSize];
}
}
- (void) setWindowMinMaxToCurrent
{
const CGFloat height = NSHeight([[fWindow contentView] frame]);
NSSize minSize = [fWindow contentMinSize],
maxSize = [fWindow contentMaxSize];
minSize.height = height;
maxSize.height = height;
[fWindow setContentMinSize: minSize];
[fWindow setContentMaxSize: maxSize];
}
- (CGFloat) minWindowContentSizeAllowed
{
CGFloat contentMinHeight = NSHeight([[fWindow contentView] frame]) - NSHeight([[fTableView enclosingScrollView] frame])
+ [fTableView rowHeight] + [fTableView intercellSpacing].height;
return contentMinHeight;
}
- (void) updateForExpandCollape
{
[self setWindowSizeToFit];
[self setBottomCountText: YES];
}
- (void) showMainWindow: (id) sender
{
[fWindow makeKeyAndOrderFront: nil];
}
- (void) windowDidBecomeMain: (NSNotification *) notification
{
[fBadger clearCompleted];
[self updateUI];
}
- (void) applicationWillUnhide: (NSNotification *) notification
{
[self updateUI];
}
2008-05-21 16:28:21 +00:00
- (void) toggleQuickLook: (id) sender
2008-05-21 16:02:38 +00:00
{
2009-09-07 14:54:22 +00:00
if (![NSApp isOnSnowLeopardOrBetter])
return;
if ([[QLPreviewPanelSL sharedPreviewPanel] isVisible])
[[QLPreviewPanelSL sharedPreviewPanel] orderOut: nil];
else
[[QLPreviewPanelSL sharedPreviewPanel] makeKeyAndOrderFront: nil];
2008-05-21 16:02:38 +00:00
}
2007-09-16 01:02:06 +00:00
- (void) linkHomepage: (id) sender
{
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: WEBSITE_URL]];
}
- (void) linkForums: (id) sender
{
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FORUM_URL]];
}
2008-09-07 14:51:50 +00:00
- (void) linkTrac: (id) sender
{
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: TRAC_URL]];
}
2007-09-16 01:02:06 +00:00
- (void) linkDonate: (id) sender
{
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: DONATE_URL]];
}
2008-08-15 01:44:46 +00:00
- (void) updaterWillRelaunchApplication: (SUUpdater *) updater
2007-09-16 01:02:06 +00:00
{
fQuitRequested = YES;
2007-09-16 01:02:06 +00:00
}
- (NSDictionary *) registrationDictionaryForGrowl
{
NSArray * notifications = [NSArray arrayWithObjects: GROWL_DOWNLOAD_COMPLETE, GROWL_SEEDING_COMPLETE,
GROWL_AUTO_ADD, GROWL_AUTO_SPEED_LIMIT, nil];
return [NSDictionary dictionaryWithObjectsAndKeys: notifications, GROWL_NOTIFICATIONS_ALL,
notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
}
- (void) growlNotificationWasClicked: (id) clickContext
{
if (!clickContext || ![clickContext isKindOfClass: [NSDictionary class]])
return;
NSString * type = [clickContext objectForKey: @"Type"], * location;
if (([type isEqualToString: GROWL_DOWNLOAD_COMPLETE] || [type isEqualToString: GROWL_SEEDING_COMPLETE])
&& (location = [clickContext objectForKey: @"Location"]))
{
if ([NSApp isOnSnowLeopardOrBetter])
{
NSURL * file = [NSURL fileURLWithPath: location];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs: [NSArray arrayWithObject: file]];
}
else
[[NSWorkspace sharedWorkspace] selectFile: location inFileViewerRootedAtPath: nil];
}
2007-09-16 01:02:06 +00:00
}
#warning handle queue state changes
- (void) rpcCallback: (tr_rpc_callback_type) type forTorrentStruct: (struct tr_torrent *) torrentStruct
{
2008-05-25 17:34:02 +00:00
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//get the torrent
Torrent * torrent = nil;
if (torrentStruct != NULL && (type != TR_RPC_TORRENT_ADDED && type != TR_RPC_SESSION_CHANGED && type != TR_RPC_SESSION_CLOSE))
{
for (torrent in fTorrents)
if (torrentStruct == [torrent torrentStruct])
{
[torrent retain];
break;
}
if (!torrent)
{
[pool drain];
NSLog(@"No torrent found matching the given torrent struct from the RPC callback!");
return;
}
}
switch (type)
{
case TR_RPC_TORRENT_ADDED:
[self performSelectorOnMainThread: @selector(rpcAddTorrentStruct:) withObject:
[[NSValue valueWithPointer: torrentStruct] retain] waitUntilDone: NO];
break;
2008-06-03 16:22:31 +00:00
case TR_RPC_TORRENT_STARTED:
case TR_RPC_TORRENT_STOPPED:
[self performSelectorOnMainThread: @selector(rpcStartedStoppedTorrent:) withObject: torrent waitUntilDone: NO];
break;
2008-06-03 16:22:31 +00:00
case TR_RPC_TORRENT_REMOVING:
2008-05-27 19:05:48 +00:00
[self performSelectorOnMainThread: @selector(rpcRemoveTorrent:) withObject: torrent waitUntilDone: NO];
break;
case TR_RPC_TORRENT_TRASHING:
[self performSelectorOnMainThread: @selector(rpcRemoveTorrentDeleteData:) withObject: torrent waitUntilDone: NO];
break;
case TR_RPC_TORRENT_CHANGED:
[self performSelectorOnMainThread: @selector(rpcChangedTorrent:) withObject: torrent waitUntilDone: NO];
break;
case TR_RPC_TORRENT_MOVED:
[self performSelectorOnMainThread: @selector(rpcMovedTorrent:) withObject: torrent waitUntilDone: NO];
break;
case TR_RPC_SESSION_CHANGED:
[fPrefsController performSelectorOnMainThread: @selector(rpcUpdatePrefs) withObject: nil waitUntilDone: NO];
break;
case TR_RPC_SESSION_CLOSE:
fQuitRequested = YES;
[NSApp performSelectorOnMainThread: @selector(terminate:) withObject: self waitUntilDone: NO];
break;
default:
NSAssert1(NO, @"Unknown RPC command received: %d", type);
[torrent release];
}
2008-05-25 17:34:02 +00:00
[pool drain];
}
- (void) rpcAddTorrentStruct: (NSValue *) torrentStructPtr
{
tr_torrent * torrentStruct = (tr_torrent *)[torrentStructPtr pointerValue];
[torrentStructPtr release];
NSString * location = nil;
if (tr_torrentGetDownloadDir(torrentStruct) != NULL)
location = [NSString stringWithUTF8String: tr_torrentGetDownloadDir(torrentStruct)];
Torrent * torrent = [[Torrent alloc] initWithTorrentStruct: torrentStruct location: location lib: fLib];
[torrent update];
[fTorrents addObject: torrent];
[torrent release];
[self updateTorrentsInQueue];
}
2008-05-27 19:05:48 +00:00
- (void) rpcRemoveTorrent: (Torrent *) torrent
{
[self confirmRemoveTorrents: [[NSArray arrayWithObject: torrent] retain] deleteData: NO];
2008-05-27 19:05:48 +00:00
[torrent release];
}
- (void) rpcRemoveTorrentDeleteData: (Torrent *) torrent
{
[self confirmRemoveTorrents: [[NSArray arrayWithObject: torrent] retain] deleteData: YES];
[torrent release];
}
- (void) rpcStartedStoppedTorrent: (Torrent *) torrent
{
[torrent update];
[torrent release];
[self updateUI];
2011-02-20 03:58:50 +00:00
[self applyFilter];
[self updateTorrentHistory];
}
- (void) rpcChangedTorrent: (Torrent *) torrent
{
[torrent update];
if ([[fTableView selectedTorrents] containsObject: torrent])
{
2008-06-03 18:00:51 +00:00
[fInfoController updateInfoStats]; //this will reload the file table
[fInfoController updateOptions];
}
[torrent release];
}
- (void) rpcMovedTorrent: (Torrent *) torrent
{
[torrent update];
2010-11-13 16:11:22 +00:00
[torrent updateTimeMachineExclude];
if ([[fTableView selectedTorrents] containsObject: torrent])
[fInfoController updateInfoStats];
[torrent release];
}
2007-09-16 01:02:06 +00:00
@end