transmission/macosx/PrefsController.m

1419 lines
49 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 "PrefsController.h"
#import "BlocklistDownloaderViewController.h"
#import "BlocklistScheduler.h"
2008-11-01 15:22:32 +00:00
#import "PortChecker.h"
#import "BonjourController.h"
#import "NSApplicationAdditions.h"
2007-09-16 01:02:06 +00:00
#import "NSStringAdditions.h"
#import "UKKQueue.h"
#import "transmission.h"
#import "utils.h"
2007-09-16 01:02:06 +00:00
#import <Sparkle/Sparkle.h>
2007-09-16 01:02:06 +00:00
#define DOWNLOAD_FOLDER 0
#define DOWNLOAD_TORRENT 2
#define RPC_IP_ADD_TAG 0
#define RPC_IP_REMOVE_TAG 1
2007-09-16 01:02:06 +00:00
#define TOOLBAR_GENERAL @"TOOLBAR_GENERAL"
#define TOOLBAR_TRANSFERS @"TOOLBAR_TRANSFERS"
#define TOOLBAR_GROUPS @"TOOLBAR_GROUPS"
2007-09-16 01:02:06 +00:00
#define TOOLBAR_BANDWIDTH @"TOOLBAR_BANDWIDTH"
#define TOOLBAR_PEERS @"TOOLBAR_PEERS"
#define TOOLBAR_NETWORK @"TOOLBAR_NETWORK"
2008-06-03 19:11:43 +00:00
#define TOOLBAR_REMOTE @"TOOLBAR_REMOTE"
2007-09-16 01:02:06 +00:00
#define RPC_KEYCHAIN_SERVICE "Transmission:Remote"
#define RPC_KEYCHAIN_NAME "Remote"
#define WEBUI_URL @"http://localhost:%d/transmission/web/"
2007-09-16 01:02:06 +00:00
@interface PrefsController (Private)
- (void) setPrefView: (id) sender;
- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
- (void) doneScriptSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
2007-09-16 01:02:06 +00:00
- (void) setKeychainPassword: (const char *) password forService: (const char *) service username: (const char *) username;
2007-09-16 01:02:06 +00:00
@end
@implementation PrefsController
tr_session * fHandle;
+ (void) setHandle: (tr_session *) handle
{
fHandle = handle;
}
+ (tr_session *) handle
{
return fHandle;
}
- (id) init
2007-09-16 01:02:06 +00:00
{
if ((self = [super initWithWindowNibName: @"PrefsWindow"]))
2007-09-16 01:02:06 +00:00
{
fDefaults = [NSUserDefaults standardUserDefaults];
//check for old version download location (before 1.1)
NSString * choice;
if ((choice = [fDefaults stringForKey: @"DownloadChoice"]))
{
[fDefaults setBool: [choice isEqualToString: @"Constant"] forKey: @"DownloadLocationConstant"];
2008-01-05 04:18:07 +00:00
[fDefaults setBool: YES forKey: @"DownloadAsk"];
[fDefaults removeObjectForKey: @"DownloadChoice"];
}
//check for old version blocklist (before 2.12)
NSDate * blocklistDate;
if ((blocklistDate = [fDefaults objectForKey: @"BlocklistLastUpdate"]))
{
[fDefaults setObject: blocklistDate forKey: @"BlocklistNewLastUpdateSuccess"];
[fDefaults setObject: blocklistDate forKey: @"BlocklistNewLastUpdate"];
[fDefaults removeObjectForKey: @"BlocklistLastUpdate"];
NSString * blocklistDir = [NSHomeDirectory() stringByAppendingPathComponent:
@"/Library/Application Support/Transmission/blocklists/"];
[[NSFileManager defaultManager] moveItemAtPath: [blocklistDir stringByAppendingPathComponent: @"level1.bin"]
2010-11-01 00:21:17 +00:00
toPath: [blocklistDir stringByAppendingPathComponent: [NSString stringWithUTF8String: DEFAULT_BLOCKLIST_FILENAME]]
error: nil];
}
//save a new random port
if ([fDefaults boolForKey: @"RandomPort"])
[fDefaults setInteger: tr_sessionGetPeerPort(fHandle) forKey: @"BindPort"];
2007-09-16 01:02:06 +00:00
//set auto import
NSString * autoPath;
if ([fDefaults boolForKey: @"AutoImport"] && (autoPath = [fDefaults stringForKey: @"AutoImportDirectory"]))
[[UKKQueue sharedFileWatcher] addPath: [autoPath stringByExpandingTildeInPath]];
//set blocklist scheduler
[[BlocklistScheduler scheduler] updateSchedule];
//set encryption
[self setEncryptionMode: nil];
2008-10-01 22:17:25 +00:00
//update rpc whitelist
[self updateRPCPassword];
2008-10-01 22:17:25 +00:00
fRPCWhitelistArray = [[fDefaults arrayForKey: @"RPCWhitelist"] mutableCopy];
if (!fRPCWhitelistArray)
fRPCWhitelistArray = [[NSMutableArray arrayWithObject: @"127.0.0.1"] retain];
[self updateRPCWhitelist];
//reset old Sparkle settings from previous versions
[fDefaults removeObjectForKey: @"SUScheduledCheckInterval"];
if ([fDefaults objectForKey: @"CheckForUpdates"])
{
[[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates: [fDefaults boolForKey: @"CheckForUpdates"]];
[fDefaults removeObjectForKey: @"CheckForUpdates"];
}
[self setAutoUpdateToBeta: nil];
2007-09-16 01:02:06 +00:00
}
2007-09-16 01:02:06 +00:00
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[fPortStatusTimer invalidate];
if (fPortChecker)
{
2007-12-25 19:14:45 +00:00
[fPortChecker cancelProbe];
[fPortChecker release];
}
2007-09-16 01:02:06 +00:00
2008-10-01 22:17:25 +00:00
[fRPCWhitelistArray release];
2008-06-04 03:50:10 +00:00
[fRPCPassword release];
2007-09-16 01:02:06 +00:00
[super dealloc];
}
- (void) awakeFromNib
{
fHasLoaded = YES;
NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
[toolbar setDelegate: self];
[toolbar setAllowsUserCustomization: NO];
[toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
[toolbar setSizeMode: NSToolbarSizeModeRegular];
[toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
[[self window] setToolbar: toolbar];
[toolbar release];
2007-09-16 01:02:06 +00:00
[self setPrefView: nil];
//set download folder
[fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
2007-09-16 01:02:06 +00:00
//set stop ratio
[fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
2007-09-16 01:02:06 +00:00
//set idle seeding minutes
[fIdleStopField setIntegerValue: [fDefaults integerForKey: @"IdleLimitMinutes"]];
2007-09-16 01:02:06 +00:00
//set limits
[self updateLimitFields];
//set speed limit
[fSpeedLimitUploadField setIntValue: [fDefaults integerForKey: @"SpeedLimitUploadLimit"]];
[fSpeedLimitDownloadField setIntValue: [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]];
//set port
[fPortField setIntValue: [fDefaults integerForKey: @"BindPort"]];
fNatStatus = -1;
[self updatePortStatus];
fPortStatusTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
2007-09-16 01:02:06 +00:00
selector: @selector(updatePortStatus) userInfo: nil repeats: YES];
//set peer connections
[fPeersGlobalField setIntValue: [fDefaults integerForKey: @"PeersTotal"]];
[fPeersTorrentField setIntValue: [fDefaults integerForKey: @"PeersTorrent"]];
2007-09-16 01:02:06 +00:00
//set queue values
[fQueueDownloadField setIntValue: [fDefaults integerForKey: @"QueueDownloadNumber"]];
[fQueueSeedField setIntValue: [fDefaults integerForKey: @"QueueSeedNumber"]];
[fStalledField setIntValue: [fDefaults integerForKey: @"StalledMinutes"]];
2008-03-29 23:38:38 +00:00
//set blocklist
NSString * blocklistURL = [fDefaults stringForKey: @"BlocklistURL"];
if (blocklistURL)
[fBlocklistURLField setStringValue: blocklistURL];
[self updateBlocklistButton];
2008-03-29 23:38:38 +00:00
[self updateBlocklistFields];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateBlocklistFields)
name: @"BlocklistUpdated" object: nil];
2008-06-03 19:11:43 +00:00
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateBlocklistURLField)
name: NSControlTextDidChangeNotification object: fBlocklistURLField];
2008-06-03 19:11:43 +00:00
//set rpc port
[fRPCPortField setIntValue: [fDefaults integerForKey: @"RPCPort"]];
2008-06-15 04:56:57 +00:00
//set rpc password
if (fRPCPassword)
[fRPCPasswordField setStringValue: fRPCPassword];
2007-09-16 01:02:06 +00:00
}
- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
2007-09-16 01:02:06 +00:00
{
NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
2007-09-16 01:02:06 +00:00
if ([ident isEqualToString: TOOLBAR_GENERAL])
{
[item setLabel: NSLocalizedString(@"General", "Preferences -> toolbar item title")];
2008-12-26 05:57:51 +00:00
[item setImage: [NSImage imageNamed: NSImageNamePreferencesGeneral]];
2007-09-16 01:02:06 +00:00
[item setTarget: self];
[item setAction: @selector(setPrefView:)];
[item setAutovalidates: NO];
}
else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
{
[item setLabel: NSLocalizedString(@"Transfers", "Preferences -> toolbar item title")];
2007-09-16 01:02:06 +00:00
[item setImage: [NSImage imageNamed: @"Transfers.png"]];
[item setTarget: self];
[item setAction: @selector(setPrefView:)];
[item setAutovalidates: NO];
}
else if ([ident isEqualToString: TOOLBAR_GROUPS])
{
[item setLabel: NSLocalizedString(@"Groups", "Preferences -> toolbar item title")];
[item setImage: [NSImage imageNamed: @"Groups.png"]];
[item setTarget: self];
[item setAction: @selector(setPrefView:)];
[item setAutovalidates: NO];
}
2007-09-16 01:02:06 +00:00
else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
{
[item setLabel: NSLocalizedString(@"Bandwidth", "Preferences -> toolbar item title")];
2007-09-16 01:02:06 +00:00
[item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
[item setTarget: self];
[item setAction: @selector(setPrefView:)];
[item setAutovalidates: NO];
}
else if ([ident isEqualToString: TOOLBAR_PEERS])
2007-09-16 01:02:06 +00:00
{
[item setLabel: NSLocalizedString(@"Peers", "Preferences -> toolbar item title")];
2008-12-26 05:57:51 +00:00
[item setImage: [NSImage imageNamed: NSImageNameUserGroup]];
[item setTarget: self];
[item setAction: @selector(setPrefView:)];
[item setAutovalidates: NO];
}
else if ([ident isEqualToString: TOOLBAR_NETWORK])
{
[item setLabel: NSLocalizedString(@"Network", "Preferences -> toolbar item title")];
2008-12-26 05:57:51 +00:00
[item setImage: [NSImage imageNamed: NSImageNameNetwork]];
2007-09-16 01:02:06 +00:00
[item setTarget: self];
[item setAction: @selector(setPrefView:)];
[item setAutovalidates: NO];
}
2008-06-03 19:11:43 +00:00
else if ([ident isEqualToString: TOOLBAR_REMOTE])
{
[item setLabel: NSLocalizedString(@"Remote", "Preferences -> toolbar item title")];
2008-06-13 15:38:02 +00:00
[item setImage: [NSImage imageNamed: @"Remote.png"]];
2008-06-03 19:11:43 +00:00
[item setTarget: self];
[item setAction: @selector(setPrefView:)];
[item setAutovalidates: NO];
}
2007-09-16 01:02:06 +00:00
else
{
[item release];
return nil;
}
return [item autorelease];
}
2009-10-31 02:09:21 +00:00
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
2007-09-16 01:02:06 +00:00
{
2009-10-31 02:09:21 +00:00
return [NSArray arrayWithObjects: TOOLBAR_GENERAL, TOOLBAR_TRANSFERS, TOOLBAR_GROUPS, TOOLBAR_BANDWIDTH,
TOOLBAR_PEERS, TOOLBAR_NETWORK, TOOLBAR_REMOTE, nil];
2007-09-16 01:02:06 +00:00
}
2009-10-31 02:09:21 +00:00
- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
2007-09-16 01:02:06 +00:00
{
return [self toolbarAllowedItemIdentifiers: toolbar];
}
2009-10-31 02:09:21 +00:00
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
2007-09-16 01:02:06 +00:00
{
2009-10-31 02:09:21 +00:00
return [self toolbarAllowedItemIdentifiers: toolbar];
2007-09-16 01:02:06 +00:00
}
//for a beta release, always use the beta appcast
#if defined(TR_BETA_RELEASE)
#define SPARKLE_TAG YES
#else
#define SPARKLE_TAG [fDefaults boolForKey: @"AutoUpdateBeta"]
#endif
- (void) setAutoUpdateToBeta: (id) sender
{
[[SUUpdater sharedUpdater] setAllowedTags: SPARKLE_TAG ? [NSSet setWithObject: @"beta"] : nil];
}
2007-09-16 01:02:06 +00:00
- (void) setPort: (id) sender
{
const tr_port port = [sender intValue];
[fDefaults setInteger: port forKey: @"BindPort"];
tr_sessionSetPeerPort(fHandle, port);
2007-09-16 01:02:06 +00:00
fPeerPort = -1;
2007-09-16 01:02:06 +00:00
[self updatePortStatus];
}
- (void) randomPort: (id) sender
{
const tr_port port = tr_sessionSetPeerPortRandom(fHandle);
[fDefaults setInteger: port forKey: @"BindPort"];
[fPortField setIntValue: port];
fPeerPort = -1;
[self updatePortStatus];
}
- (void) setRandomPortOnStart: (id) sender
{
tr_sessionSetPeerPortRandomOnStart(fHandle, [sender state] == NSOnState);
}
2007-09-16 01:02:06 +00:00
- (void) setNat: (id) sender
{
2008-05-12 16:39:32 +00:00
tr_sessionSetPortForwardingEnabled(fHandle, [fDefaults boolForKey: @"NatTraversal"]);
fNatStatus = -1;
2007-09-16 01:02:06 +00:00
[self updatePortStatus];
}
- (void) updatePortStatus
{
const tr_port_forwarding fwd = tr_sessionGetPortForwarding(fHandle);
const int port = tr_sessionGetPeerPort(fHandle);
BOOL natStatusChanged = (fNatStatus != fwd);
BOOL peerPortChanged = (fPeerPort != port);
if (natStatusChanged || peerPortChanged)
2007-09-16 01:02:06 +00:00
{
fNatStatus = fwd;
fPeerPort = port;
2007-09-16 01:02:06 +00:00
[fPortStatusField setStringValue: @""];
2007-09-16 01:02:06 +00:00
[fPortStatusImage setImage: nil];
[fPortStatusProgress startAnimation: self];
if (fPortChecker)
{
2007-12-25 19:14:45 +00:00
[fPortChecker cancelProbe];
[fPortChecker release];
}
BOOL delay = natStatusChanged || tr_sessionIsPortForwardingEnabled(fHandle);
fPortChecker = [[PortChecker alloc] initForPort: fPeerPort delay: delay withDelegate: self];
2007-09-16 01:02:06 +00:00
}
}
- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
{
[fPortStatusProgress stopAnimation: self];
switch ([fPortChecker status])
2007-09-16 01:02:06 +00:00
{
case PORT_STATUS_OPEN:
[fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Network -> port status")];
2007-10-28 03:29:20 +00:00
[fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]];
2007-09-16 01:02:06 +00:00
break;
case PORT_STATUS_CLOSED:
[fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Network -> port status")];
2007-10-28 03:29:20 +00:00
[fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
2007-09-16 01:02:06 +00:00
break;
case PORT_STATUS_ERROR:
[fPortStatusField setStringValue: NSLocalizedString(@"Port check site is down", "Preferences -> Network -> port status")];
2007-10-28 03:29:20 +00:00
[fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.png"]];
2007-09-16 01:02:06 +00:00
break;
2010-12-20 14:25:20 +00:00
default:
NSAssert1(NO, @"Port checker returned invalid status: %d", [fPortChecker status]);
break;
2007-09-16 01:02:06 +00:00
}
[fPortChecker release];
fPortChecker = nil;
2007-09-16 01:02:06 +00:00
}
- (NSArray *) sounds
{
NSMutableArray * sounds = [NSMutableArray array];
NSArray * directories = NSSearchPathForDirectoriesInDomains(NSAllLibrariesDirectory,
NSUserDomainMask | NSLocalDomainMask | NSSystemDomainMask, YES);
2008-12-26 07:25:17 +00:00
for (NSString * directory in directories)
{
directory = [directory stringByAppendingPathComponent: @"Sounds"];
2008-12-26 07:25:17 +00:00
BOOL isDirectory;
if ([[NSFileManager defaultManager] fileExistsAtPath: directory isDirectory: &isDirectory] && isDirectory)
{
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: directory error: NULL];
2008-12-26 07:25:17 +00:00
for (NSString * sound in directoryContents)
{
sound = [sound stringByDeletingPathExtension];
if ([NSSound soundNamed: sound])
[sounds addObject: sound];
}
}
2008-12-26 07:25:17 +00:00
}
return sounds;
}
- (void) setSound: (id) sender
{
//play sound when selecting
NSSound * sound;
if ((sound = [NSSound soundNamed: [sender titleOfSelectedItem]]))
[sound play];
}
- (void) setPeersGlobal: (id) sender
{
const int count = [sender intValue];
[fDefaults setInteger: count forKey: @"PeersTotal"];
2008-05-12 16:39:32 +00:00
tr_sessionSetPeerLimit(fHandle, count);
}
- (void) setPeersTorrent: (id) sender
{
const int count = [sender intValue];
[fDefaults setInteger: count forKey: @"PeersTorrent"];
tr_sessionSetPeerLimitPerTorrent(fHandle, count);
}
2007-12-24 05:05:56 +00:00
- (void) setPEX: (id) sender
{
2008-05-12 16:39:32 +00:00
tr_sessionSetPexEnabled(fHandle, [fDefaults boolForKey: @"PEXGlobal"]);
2007-12-24 05:05:56 +00:00
}
2009-05-21 04:35:40 +00:00
- (void) setDHT: (id) sender
{
tr_sessionSetDHTEnabled(fHandle, [fDefaults boolForKey: @"DHTGlobal"]);
}
2010-05-08 08:54:45 +00:00
- (void) setLPD: (id) sender
{
2010-05-08 08:54:45 +00:00
tr_sessionSetLPDEnabled(fHandle, [fDefaults boolForKey: @"LocalPeerDiscoveryGlobal"]);
}
- (void) setEncryptionMode: (id) sender
{
const tr_encryption_mode mode = [fDefaults boolForKey: @"EncryptionPrefer"] ?
([fDefaults boolForKey: @"EncryptionRequire"] ? TR_ENCRYPTION_REQUIRED : TR_ENCRYPTION_PREFERRED) : TR_CLEAR_PREFERRED;
tr_sessionSetEncryption(fHandle, mode);
}
2008-03-29 23:38:38 +00:00
- (void) setBlocklistEnabled: (id) sender
{
tr_blocklistSetEnabled(fHandle, [fDefaults boolForKey: @"BlocklistNew"]);
[[BlocklistScheduler scheduler] updateSchedule];
[self updateBlocklistButton];
}
2008-03-29 23:38:38 +00:00
- (void) updateBlocklist: (id) sender
{
[BlocklistDownloaderViewController downloadWithPrefsController: self];
2008-03-29 23:38:38 +00:00
}
- (void) setBlocklistAutoUpdate: (id) sender
{
[[BlocklistScheduler scheduler] updateSchedule];
}
2008-03-29 23:38:38 +00:00
- (void) updateBlocklistFields
{
const BOOL exists = tr_blocklistExists(fHandle);
2008-03-29 23:38:38 +00:00
if (exists)
{
NSString * countString = [NSString formattedUInteger: tr_blocklistGetRuleCount(fHandle)];
[fBlocklistMessageField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ IP address rules in list",
"Prefs -> blocklist -> message"), countString]];
}
else
[fBlocklistMessageField setStringValue: NSLocalizedString(@"A blocklist must first be downloaded",
"Prefs -> blocklist -> message")];
2008-03-29 23:38:38 +00:00
NSString * updatedDateString;
if (exists)
{
NSDate * updatedDate = [fDefaults objectForKey: @"BlocklistNewLastUpdateSuccess"];
if (updatedDate)
{
if ([NSApp isOnSnowLeopardOrBetter])
updatedDateString = [NSDateFormatter localizedStringFromDate: updatedDate dateStyle: NSDateFormatterFullStyle
timeStyle: NSDateFormatterShortStyle];
else
{
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle: NSDateFormatterFullStyle];
[dateFormatter setTimeStyle: NSDateFormatterShortStyle];
updatedDateString = [dateFormatter stringFromDate: updatedDate];
[dateFormatter release];
}
}
else
updatedDateString = NSLocalizedString(@"N/A", "Prefs -> blocklist -> message");
}
else
updatedDateString = NSLocalizedString(@"Never", "Prefs -> blocklist -> message");
[fBlocklistDateField setStringValue: [NSString stringWithFormat: @"%@: %@",
NSLocalizedString(@"Last updated", "Prefs -> blocklist -> message"), updatedDateString]];
2008-03-29 23:38:38 +00:00
}
- (void) updateBlocklistURLField
{
NSString * blocklistString = [fBlocklistURLField stringValue];
[fDefaults setObject: blocklistString forKey: @"BlocklistURL"];
tr_blocklistSetURL(fHandle, [blocklistString UTF8String]);
[self updateBlocklistButton];
}
- (void) updateBlocklistButton
{
NSString * blocklistString = [fDefaults objectForKey: @"BlocklistURL"];
const BOOL enable = (blocklistString && ![blocklistString isEqualToString: @""])
&& [fDefaults boolForKey: @"BlocklistNew"];
[fBlocklistButton setEnabled: enable];
}
- (void) setAutoStartDownloads: (id) sender
{
tr_sessionSetPaused(fHandle, ![fDefaults boolForKey: @"AutoStartDownload"]);
}
2007-09-16 01:02:06 +00:00
- (void) applySpeedSettings: (id) sender
{
tr_sessionLimitSpeed(fHandle, TR_UP, [fDefaults boolForKey: @"CheckUpload"]);
2010-07-06 03:31:17 +00:00
tr_sessionSetSpeedLimit_KBps(fHandle, TR_UP, [fDefaults integerForKey: @"UploadLimit"]);
tr_sessionLimitSpeed(fHandle, TR_DOWN, [fDefaults boolForKey: @"CheckDownload"]);
2010-07-06 03:31:17 +00:00
tr_sessionSetSpeedLimit_KBps(fHandle, TR_DOWN, [fDefaults integerForKey: @"DownloadLimit"]);
2008-12-03 02:55:38 +00:00
[[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
2007-09-16 01:02:06 +00:00
}
- (void) applyAltSpeedSettings
{
2010-07-06 03:31:17 +00:00
tr_sessionSetAltSpeed_KBps(fHandle, TR_UP, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
tr_sessionSetAltSpeed_KBps(fHandle, TR_DOWN, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
[[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
}
2007-09-16 01:02:06 +00:00
- (void) applyRatioSetting: (id) sender
{
tr_sessionSetRatioLimited(fHandle, [fDefaults boolForKey: @"RatioCheck"]);
tr_sessionSetRatioLimit(fHandle, [fDefaults floatForKey: @"RatioLimit"]);
2007-09-16 01:02:06 +00:00
}
- (void) setRatioStop: (id) sender
{
[fDefaults setFloat: [sender floatValue] forKey: @"RatioLimit"];
[self applyRatioSetting: nil];
}
2007-09-16 01:02:06 +00:00
- (void) updateRatioStopField
{
if (fHasLoaded)
[fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
2007-09-16 01:02:06 +00:00
[self applyRatioSetting: nil];
}
- (void) applyIdleStopSetting: (id) sender
2007-09-16 01:02:06 +00:00
{
tr_sessionSetIdleLimited(fHandle, [fDefaults boolForKey: @"IdleLimitCheck"]);
tr_sessionSetIdleLimit(fHandle, [fDefaults integerForKey: @"IdleLimitMinutes"]);
}
- (void) setIdleStop: (id) sender
{
[fDefaults setInteger: [sender integerValue] forKey: @"IdleLimitMinutes"];
[self applyIdleStopSetting: nil];
2007-09-16 01:02:06 +00:00
}
- (void) updateLimitFields
{
if (!fHasLoaded)
return;
[fUploadField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
[fDownloadField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
}
- (void) setGlobalLimit: (id) sender
{
[fDefaults setInteger: [sender intValue] forKey: sender == fUploadField ? @"UploadLimit" : @"DownloadLimit"];
2007-09-16 01:02:06 +00:00
[self applySpeedSettings: self];
}
- (void) setSpeedLimit: (id) sender
{
[fDefaults setInteger: [sender intValue] forKey: sender == fSpeedLimitUploadField
? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"];
[self applyAltSpeedSettings];
2007-09-16 01:02:06 +00:00
}
- (void) setAutoSpeedLimit: (id) sender
{
tr_sessionUseAltSpeedTime(fHandle, [fDefaults boolForKey: @"SpeedLimitAuto"]);
}
- (void) setAutoSpeedLimitTime: (id) sender
{
tr_sessionSetAltSpeedBegin(fHandle, [PrefsController dateToTimeSum: [fDefaults objectForKey: @"SpeedLimitAutoOnDate"]]);
tr_sessionSetAltSpeedEnd(fHandle, [PrefsController dateToTimeSum: [fDefaults objectForKey: @"SpeedLimitAutoOffDate"]]);
}
- (void) setAutoSpeedLimitDay: (id) sender
{
tr_sessionSetAltSpeedDay(fHandle, [[sender selectedItem] tag]);
}
+ (NSInteger) dateToTimeSum: (NSDate *) date
{
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents * components = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: date];
return [components hour] * 60 + [components minute];
2007-09-16 01:02:06 +00:00
}
2009-04-05 15:55:20 +00:00
+ (NSDate *) timeSumToDate: (NSInteger) sum
{
NSDateComponents * comps = [[[NSDateComponents alloc] init] autorelease];
[comps setHour: sum / 60];
[comps setMinute: sum % 60];
return [[NSCalendar currentCalendar] dateFromComponents: comps];
}
- (BOOL) control: (NSControl *) control textShouldBeginEditing: (NSText *) fieldEditor
{
2007-12-06 01:07:56 +00:00
[fInitialString release];
fInitialString = [[control stringValue] retain];
2007-12-06 01:07:56 +00:00
return YES;
}
- (BOOL) control: (NSControl *) control didFailToFormatString: (NSString *) string errorDescription: (NSString *) error
{
NSBeep();
if (fInitialString)
2007-12-06 01:07:56 +00:00
{
[control setStringValue: fInitialString];
2007-12-06 01:07:56 +00:00
[fInitialString release];
fInitialString = nil;
}
return NO;
}
2007-09-16 01:02:06 +00:00
- (void) setBadge: (id) sender
2008-03-29 23:38:38 +00:00
{
2007-09-16 01:02:06 +00:00
[[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
}
- (void) resetWarnings: (id) sender
{
2008-08-15 01:44:46 +00:00
[fDefaults removeObjectForKey: @"WarningDuplicate"];
[fDefaults removeObjectForKey: @"WarningRemainingSpace"];
[fDefaults removeObjectForKey: @"WarningFolderDataSameName"];
[fDefaults removeObjectForKey: @"WarningResetStats"];
[fDefaults removeObjectForKey: @"WarningCreatorBlankAddress"];
[fDefaults removeObjectForKey: @"WarningCreatorPrivateBlankAddress"];
[fDefaults removeObjectForKey: @"WarningRemoveTrackers"];
2008-08-15 01:44:46 +00:00
[fDefaults removeObjectForKey: @"WarningInvalidOpen"];
[fDefaults removeObjectForKey: @"WarningDonate"];
//[fDefaults removeObjectForKey: @"WarningLegal"];
2007-09-16 01:02:06 +00:00
}
- (void) setDefaultForMagnets: (id) sender
{
NSString * bundleID = [[NSBundle mainBundle] bundleIdentifier];
const OSStatus result = LSSetDefaultHandlerForURLScheme((CFStringRef)@"magnet", (CFStringRef)bundleID);
if (result != noErr)
NSLog(@"Failed setting default magnet link handler");
}
2007-09-16 01:02:06 +00:00
- (void) setQueue: (id) sender
{
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
}
- (void) setQueueNumber: (id) sender
{
[fDefaults setInteger: [sender intValue] forKey: sender == fQueueDownloadField ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
2007-09-16 01:02:06 +00:00
[self setQueue: nil];
}
- (void) setStalled: (id) sender
{
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
2007-09-16 01:02:06 +00:00
}
- (void) setStalledMinutes: (id) sender
{
[fDefaults setInteger: [sender intValue] forKey: @"StalledMinutes"];
2007-09-16 01:02:06 +00:00
[self setStalled: nil];
}
- (void) setDownloadLocation: (id) sender
{
[fDefaults setBool: [fFolderPopUp indexOfSelectedItem] == DOWNLOAD_FOLDER forKey: @"DownloadLocationConstant"];
2007-09-16 01:02:06 +00:00
}
- (void) folderSheetShow: (id) sender
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
2008-01-03 06:36:38 +00:00
[panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
2007-09-16 01:02:06 +00:00
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: NO];
[panel setCanChooseDirectories: YES];
[panel setCanCreateDirectories: YES];
[panel beginSheetForDirectory: nil file: nil types: nil
modalForWindow: [self window] modalDelegate: self didEndSelector:
@selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
}
- (void) incompleteFolderSheetShow: (id) sender
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
2008-01-03 06:36:38 +00:00
[panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
2007-09-16 01:02:06 +00:00
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: NO];
[panel setCanChooseDirectories: YES];
[panel setCanCreateDirectories: YES];
[panel beginSheetForDirectory: nil file: nil types: nil
modalForWindow: [self window] modalDelegate: self didEndSelector:
@selector(incompleteFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
}
- (void) doneScriptSheetShow:(id)sender
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: YES];
[panel setCanChooseDirectories: NO];
[panel setCanCreateDirectories: NO];
[panel beginSheetForDirectory: nil file: nil types: nil
modalForWindow: [self window] modalDelegate: self didEndSelector:
@selector(doneScriptSheetClosed:returnCode:contextInfo:) contextInfo: nil];
}
- (void) setUseIncompleteFolder: (id) sender
{
tr_sessionSetIncompleteDirEnabled(fHandle, [fDefaults boolForKey: @"UseIncompleteDownloadFolder"]);
}
- (void) setRenamePartialFiles: (id) sender
{
tr_sessionSetIncompleteFileNamingEnabled(fHandle, [fDefaults boolForKey: @"RenamePartialFiles"]);
}
- (void) setDoneScriptEnabled: (id) sender
{
if ([fDefaults boolForKey: @"DoneScriptEnabled"] && ![[NSFileManager defaultManager] fileExistsAtPath: [fDefaults stringForKey:@"DoneScriptPath"]])
{
// enabled is set but script file doesn't exist, so prompt for one and disable until they pick one
[fDefaults setBool: NO forKey: @"DoneScriptEnabled"];
[self doneScriptSheetShow: sender];
}
tr_sessionSetTorrentDoneScriptEnabled(fHandle, [fDefaults boolForKey: @"DoneScriptEnabled"]);
}
2007-09-16 01:02:06 +00:00
- (void) setAutoImport: (id) sender
{
NSString * path;
if ((path = [fDefaults stringForKey: @"AutoImportDirectory"]))
{
path = [path stringByExpandingTildeInPath];
if ([fDefaults boolForKey: @"AutoImport"])
[[UKKQueue sharedFileWatcher] addPath: path];
else
[[UKKQueue sharedFileWatcher] removePathFromQueue: path];
[[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
}
else
[self importFolderSheetShow: nil];
}
- (void) importFolderSheetShow: (id) sender
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
2008-01-03 06:36:38 +00:00
[panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
2007-09-16 01:02:06 +00:00
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: NO];
[panel setCanChooseDirectories: YES];
[panel setCanCreateDirectories: YES];
[panel beginSheetForDirectory: nil file: nil types: nil
modalForWindow: [self window] modalDelegate: self didEndSelector:
@selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
}
- (void) setAutoSize: (id) sender
{
[[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
}
2008-06-03 19:11:43 +00:00
- (void) setRPCEnabled: (id) sender
{
BOOL enable = [fDefaults boolForKey: @"RPC"];
tr_sessionSetRPCEnabled(fHandle, enable);
[self setRPCWebUIDiscovery: nil];
2008-06-03 19:11:43 +00:00
}
- (void) linkWebUI: (id) sender
{
NSString * urlString = [NSString stringWithFormat: WEBUI_URL, [fDefaults integerForKey: @"RPCPort"]];
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: urlString]];
}
- (void) setRPCAuthorize: (id) sender
{
tr_sessionSetRPCPasswordEnabled(fHandle, [fDefaults boolForKey: @"RPCAuthorize"]);
}
- (void) setRPCUsername: (id) sender
{
tr_sessionSetRPCUsername(fHandle, [[fDefaults stringForKey: @"RPCUsername"] UTF8String]);
}
- (void) setRPCPassword: (id) sender
{
[fRPCPassword release];
fRPCPassword = [[sender stringValue] copy];
const char * password = [[sender stringValue] UTF8String];
[self setKeychainPassword: password forService: RPC_KEYCHAIN_SERVICE username: RPC_KEYCHAIN_NAME];
tr_sessionSetRPCPassword(fHandle, password);
}
- (void) updateRPCPassword
{
UInt32 passwordLength;
2008-06-15 20:48:54 +00:00
const char * password = nil;
SecKeychainFindGenericPassword(NULL, strlen(RPC_KEYCHAIN_SERVICE), RPC_KEYCHAIN_SERVICE,
strlen(RPC_KEYCHAIN_NAME), RPC_KEYCHAIN_NAME, &passwordLength, (void **)&password, NULL);
[fRPCPassword release];
if (password != NULL)
{
char fullPassword[passwordLength+1];
strncpy(fullPassword, password, passwordLength);
fullPassword[passwordLength] = '\0';
SecKeychainItemFreeContent(NULL, (void *)password);
tr_sessionSetRPCPassword(fHandle, fullPassword);
fRPCPassword = [[NSString alloc] initWithUTF8String: fullPassword];
[fRPCPasswordField setStringValue: fRPCPassword];
}
else
fRPCPassword = nil;
}
2008-06-03 19:11:43 +00:00
- (void) setRPCPort: (id) sender
{
int port = [sender intValue];
[fDefaults setInteger: port forKey: @"RPCPort"];
tr_sessionSetRPCPort(fHandle, port);
[self setRPCWebUIDiscovery: nil];
2008-06-03 19:11:43 +00:00
}
- (void) setRPCUseWhitelist: (id) sender
{
tr_sessionSetRPCWhitelistEnabled(fHandle, [fDefaults boolForKey: @"RPCUseWhitelist"]);
}
- (void) setRPCWebUIDiscovery: (id) sender
{
if ([fDefaults boolForKey:@"RPC"] && [fDefaults boolForKey: @"RPCWebDiscovery"])
[[BonjourController defaultController] startWithPort: [fDefaults integerForKey: @"RPCPort"]];
else
[[BonjourController defaultController] stop];
}
2008-10-01 22:17:25 +00:00
- (void) updateRPCWhitelist
2008-06-04 03:50:10 +00:00
{
2008-10-01 22:17:25 +00:00
NSString * string = [fRPCWhitelistArray componentsJoinedByString: @","];
tr_sessionSetRPCWhitelist(fHandle, [string UTF8String]);
2008-06-04 03:50:10 +00:00
}
- (void) addRemoveRPCIP: (id) sender
{
//don't allow add/remove when currently adding - it leads to weird results
2008-10-01 22:17:25 +00:00
if ([fRPCWhitelistTable editedRow] != -1)
return;
if ([[sender cell] tagForSegment: [sender selectedSegment]] == RPC_IP_REMOVE_TAG)
{
2008-10-01 22:17:25 +00:00
[fRPCWhitelistArray removeObjectsAtIndexes: [fRPCWhitelistTable selectedRowIndexes]];
[fRPCWhitelistTable deselectAll: self];
[fRPCWhitelistTable reloadData];
2008-10-01 22:17:25 +00:00
[fDefaults setObject: fRPCWhitelistArray forKey: @"RPCWhitelist"];
[self updateRPCWhitelist];
}
else
{
2008-10-01 22:17:25 +00:00
[fRPCWhitelistArray addObject: @""];
[fRPCWhitelistTable reloadData];
const int row = [fRPCWhitelistArray count] - 1;
[fRPCWhitelistTable selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
2008-10-01 22:17:25 +00:00
[fRPCWhitelistTable editColumn: 0 row: row withEvent: nil select: YES];
}
}
2008-06-04 03:50:10 +00:00
- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
{
2008-10-01 22:17:25 +00:00
return [fRPCWhitelistArray count];
2008-06-04 03:50:10 +00:00
}
- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
{
2008-10-01 22:17:25 +00:00
return [fRPCWhitelistArray objectAtIndex: row];
2008-06-04 03:50:10 +00:00
}
- (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn
row: (NSInteger) row
{
2008-10-01 22:17:25 +00:00
NSArray * components = [object componentsSeparatedByString: @"."];
NSMutableArray * newComponents = [NSMutableArray arrayWithCapacity: 4];
2008-06-04 03:50:10 +00:00
2008-10-01 22:17:25 +00:00
//create better-formatted ip string
BOOL valid = false;
if ([components count] == 4)
{
valid = true;
2008-12-26 07:25:17 +00:00
for (NSString * component in components)
2008-06-04 03:50:10 +00:00
{
2008-10-01 22:17:25 +00:00
if ([component isEqualToString: @"*"])
[newComponents addObject: component];
else
2008-06-04 03:50:10 +00:00
{
2008-10-01 22:17:25 +00:00
int num = [component intValue];
if (num >= 0 && num < 256)
[newComponents addObject: [[NSNumber numberWithInt: num] stringValue]];
2008-06-04 03:50:10 +00:00
else
{
2008-10-01 22:17:25 +00:00
valid = false;
break;
}
2008-06-04 03:50:10 +00:00
}
}
2008-10-01 22:17:25 +00:00
}
NSString * newIP;
if (valid)
{
newIP = [newComponents componentsJoinedByString: @"."];
2008-06-04 03:50:10 +00:00
2008-10-01 22:17:25 +00:00
//don't allow the same ip address
if ([fRPCWhitelistArray containsObject: newIP] && ![[fRPCWhitelistArray objectAtIndex: row] isEqualToString: newIP])
valid = false;
2008-06-04 03:50:10 +00:00
}
2008-10-01 22:17:25 +00:00
if (valid)
{
[fRPCWhitelistArray replaceObjectAtIndex: row withObject: newIP];
[fRPCWhitelistArray sortUsingSelector: @selector(compareNumeric:)];
}
else
{
NSBeep();
if ([[fRPCWhitelistArray objectAtIndex: row] isEqualToString: @""])
[fRPCWhitelistArray removeObjectAtIndex: row];
}
[fRPCWhitelistTable deselectAll: self];
[fRPCWhitelistTable reloadData];
[fDefaults setObject: fRPCWhitelistArray forKey: @"RPCWhitelist"];
[self updateRPCWhitelist];
2008-06-04 03:50:10 +00:00
}
- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
2008-10-01 22:17:25 +00:00
[fRPCAddRemoveControl setEnabled: [fRPCWhitelistTable numberOfSelectedRows] > 0 forSegment: RPC_IP_REMOVE_TAG];
}
- (void) helpForScript: (id) sender
{
[[NSHelpManager sharedHelpManager] openHelpAnchor: @"script"
inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
}
- (void) helpForPeers: (id) sender
{
2010-06-20 03:50:44 +00:00
[[NSHelpManager sharedHelpManager] openHelpAnchor: @"peers"
inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
}
2007-09-16 01:02:06 +00:00
- (void) helpForNetwork: (id) sender
{
2010-06-20 03:50:44 +00:00
[[NSHelpManager sharedHelpManager] openHelpAnchor: @"network"
2007-09-16 01:02:06 +00:00
inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
}
2008-06-11 03:28:14 +00:00
- (void) helpForRemote: (id) sender
{
2010-06-20 03:50:44 +00:00
[[NSHelpManager sharedHelpManager] openHelpAnchor: @"remote"
2008-06-11 03:28:14 +00:00
inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
}
- (void) rpcUpdatePrefs
{
//encryption
const tr_encryption_mode encryptionMode = tr_sessionGetEncryption(fHandle);
[fDefaults setBool: encryptionMode != TR_CLEAR_PREFERRED forKey: @"EncryptionPrefer"];
[fDefaults setBool: encryptionMode == TR_ENCRYPTION_REQUIRED forKey: @"EncryptionRequire"];
//download directory
NSString * downloadLocation = [[NSString stringWithUTF8String: tr_sessionGetDownloadDir(fHandle)] stringByStandardizingPath];
[fDefaults setObject: downloadLocation forKey: @"DownloadFolder"];
NSString * incompleteLocation = [[NSString stringWithUTF8String: tr_sessionGetIncompleteDir(fHandle)] stringByStandardizingPath];
[fDefaults setObject: incompleteLocation forKey: @"IncompleteDownloadFolder"];
const BOOL useIncomplete = tr_sessionIsIncompleteDirEnabled(fHandle);
[fDefaults setBool: useIncomplete forKey: @"UseIncompleteDownloadFolder"];
const BOOL usePartialFileRanaming = tr_sessionIsIncompleteFileNamingEnabled(fHandle);
[fDefaults setBool: usePartialFileRanaming forKey: @"RenamePartialFiles"];
//peers
const uint16_t peersTotal = tr_sessionGetPeerLimit(fHandle);
[fDefaults setInteger: peersTotal forKey: @"PeersTotal"];
const uint16_t peersTorrent = tr_sessionGetPeerLimitPerTorrent(fHandle);
[fDefaults setInteger: peersTorrent forKey: @"PeersTorrent"];
//pex
const BOOL pex = tr_sessionIsPexEnabled(fHandle);
[fDefaults setBool: pex forKey: @"PEXGlobal"];
2009-05-21 04:35:40 +00:00
//dht
const BOOL dht = tr_sessionIsDHTEnabled(fHandle);
[fDefaults setBool: dht forKey: @"DHTGlobal"];
//dht
2010-05-19 23:08:14 +00:00
const BOOL lpd = tr_sessionIsLPDEnabled(fHandle);
2010-08-03 23:54:02 +00:00
[fDefaults setBool: lpd forKey: @"LocalPeerDiscoveryGlobal"];
//auto start
const BOOL autoStart = !tr_sessionGetPaused(fHandle);
[fDefaults setBool: autoStart forKey: @"AutoStartDownload"];
//port
const tr_port port = tr_sessionGetPeerPort(fHandle);
[fDefaults setInteger: port forKey: @"BindPort"];
const BOOL nat = tr_sessionIsPortForwardingEnabled(fHandle);
[fDefaults setBool: nat forKey: @"NatTraversal"];
fPeerPort = -1;
fNatStatus = -1;
[self updatePortStatus];
const BOOL randomPort = tr_sessionGetPeerPortRandomOnStart(fHandle);
[fDefaults setBool: randomPort forKey: @"RandomPort"];
//speed limit - down
const BOOL downLimitEnabled = tr_sessionIsSpeedLimited(fHandle, TR_DOWN);
[fDefaults setBool: downLimitEnabled forKey: @"CheckDownload"];
2010-07-06 03:31:17 +00:00
const int downLimit = tr_sessionGetSpeedLimit_KBps(fHandle, TR_DOWN);
[fDefaults setInteger: downLimit forKey: @"DownloadLimit"];
//speed limit - up
const BOOL upLimitEnabled = tr_sessionIsSpeedLimited(fHandle, TR_UP);
[fDefaults setBool: upLimitEnabled forKey: @"CheckUpload"];
2010-07-06 03:31:17 +00:00
const int upLimit = tr_sessionGetSpeedLimit_KBps(fHandle, TR_UP);
[fDefaults setInteger: upLimit forKey: @"UploadLimit"];
//alt speed limit enabled
2009-04-05 15:55:20 +00:00
const BOOL useAltSpeed = tr_sessionUsesAltSpeed(fHandle);
[fDefaults setBool: useAltSpeed forKey: @"SpeedLimit"];
//alt speed limit - down
2010-07-06 03:31:17 +00:00
const int downLimitAlt = tr_sessionGetAltSpeed_KBps(fHandle, TR_DOWN);
[fDefaults setInteger: downLimitAlt forKey: @"SpeedLimitDownloadLimit"];
//alt speed limit - up
2010-07-06 03:31:17 +00:00
const int upLimitAlt = tr_sessionGetAltSpeed_KBps(fHandle, TR_UP);
[fDefaults setInteger: upLimitAlt forKey: @"SpeedLimitUploadLimit"];
//alt speed limit schedule
const BOOL useAltSpeedSched = tr_sessionUsesAltSpeedTime(fHandle);
[fDefaults setBool: useAltSpeedSched forKey: @"SpeedLimitAuto"];
2009-04-05 15:55:20 +00:00
NSDate * limitStartDate = [PrefsController timeSumToDate: tr_sessionGetAltSpeedBegin(fHandle)];
[fDefaults setObject: limitStartDate forKey: @"SpeedLimitAutoOnDate"];
NSDate * limitEndDate = [PrefsController timeSumToDate: tr_sessionGetAltSpeedEnd(fHandle)];
2009-04-05 15:55:20 +00:00
[fDefaults setObject: limitEndDate forKey: @"SpeedLimitAutoOffDate"];
const int limitDay = tr_sessionGetAltSpeedDay(fHandle);
[fDefaults setInteger: limitDay forKey: @"SpeedLimitAutoDay"];
//blocklist
const BOOL blocklist = tr_blocklistIsEnabled(fHandle);
[fDefaults setBool: blocklist forKey: @"BlocklistNew"];
2010-11-05 22:45:38 +00:00
NSString * blocklistURL = [NSString stringWithUTF8String: tr_blocklistGetURL(fHandle)];
[fDefaults setObject: blocklistURL forKey: @"BlocklistURL"];
//seed ratio
const BOOL ratioLimited = tr_sessionIsRatioLimited(fHandle);
[fDefaults setBool: ratioLimited forKey: @"RatioCheck"];
const float ratioLimit = tr_sessionGetRatioLimit(fHandle);
[fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
//Idle seed limit
const BOOL idleLimited = tr_sessionIsIdleLimited(fHandle);
[fDefaults setBool: idleLimited forKey: @"IdleLimitCheck"];
const NSUInteger idleLimitMin = tr_sessionGetIdleLimit(fHandle);
[fDefaults setInteger: idleLimitMin forKey: @"IdleLimitMinutes"];
//done script
const BOOL doneScriptEnabled = tr_sessionIsTorrentDoneScriptEnabled(fHandle);
[fDefaults setBool: doneScriptEnabled forKey: @"DoneScriptEnabled"];
NSString * doneScriptPath = [NSString stringWithUTF8String: tr_sessionGetTorrentDoneScript(fHandle)];
[fDefaults setObject: doneScriptPath forKey: @"DoneScriptPath"];
2008-06-03 18:00:51 +00:00
//update gui if loaded
if (fHasLoaded)
{
//encryption handled by bindings
//download directory handled by bindings
[fPeersGlobalField setIntValue: peersTotal];
[fPeersTorrentField setIntValue: peersTorrent];
//pex handled by bindings
2009-05-21 04:35:40 +00:00
//dht handled by bindings
2010-05-19 23:08:14 +00:00
//lpd handled by bindings
[fPortField setIntValue: port];
//port forwarding (nat) handled by bindings
//random port handled by bindings
//limit check handled by bindings
[fDownloadField setIntValue: downLimit];
//limit check handled by bindings
[fUploadField setIntValue: upLimit];
[fSpeedLimitDownloadField setIntValue: downLimitAlt];
[fSpeedLimitUploadField setIntValue: upLimitAlt];
//speed limit schedule handled by bindings
//speed limit schedule times and day handled by bindings
[fBlocklistURLField setStringValue: blocklistURL];
[self updateBlocklistButton];
[self updateBlocklistFields];
//ratio limit enabled handled by bindings
[fRatioStopField setFloatValue: ratioLimit];
//idle limit enabled handled by bindings
[fIdleStopField setIntegerValue: idleLimitMin];
}
2009-04-05 15:55:20 +00:00
[[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
}
2007-09-16 01:02:06 +00:00
@end
@implementation PrefsController (Private)
- (void) setPrefView: (id) sender
{
NSString * identifier;
2007-09-16 01:02:06 +00:00
if (sender)
{
identifier = [sender itemIdentifier];
[[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"SelectedPrefView"];
2007-09-16 01:02:06 +00:00
}
else
identifier = [[NSUserDefaults standardUserDefaults] stringForKey: @"SelectedPrefView"];
NSView * view;
if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
view = fTransfersView;
else if ([identifier isEqualToString: TOOLBAR_GROUPS])
view = fGroupsView;
else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
view = fBandwidthView;
else if ([identifier isEqualToString: TOOLBAR_PEERS])
view = fPeersView;
else if ([identifier isEqualToString: TOOLBAR_NETWORK])
view = fNetworkView;
else if ([identifier isEqualToString: TOOLBAR_REMOTE])
view = fRemoteView;
else
{
identifier = TOOLBAR_GENERAL; //general view is the default selected
view = fGeneralView;
}
[[[self window] toolbar] setSelectedItemIdentifier: identifier];
2007-09-16 01:02:06 +00:00
NSWindow * window = [self window];
if ([window contentView] == view)
return;
NSRect windowRect = [window frame];
float difference = ([view frame].size.height - [[window contentView] frame].size.height) * [window userSpaceScaleFactor];
windowRect.origin.y -= difference;
windowRect.size.height += difference;
[view setHidden: YES];
[window setContentView: view];
[window setFrame: windowRect display: YES animate: YES];
[view setHidden: NO];
//set title label
if (sender)
[window setTitle: [sender label]];
else
{
NSToolbar * toolbar = [window toolbar];
NSString * itemIdentifier = [toolbar selectedItemIdentifier];
2008-12-26 07:25:17 +00:00
for (NSToolbarItem * item in [toolbar items])
2007-09-16 01:02:06 +00:00
if ([[item itemIdentifier] isEqualToString: itemIdentifier])
{
[window setTitle: [item label]];
break;
}
}
}
- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
{
if (code == NSOKButton)
{
[fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
2009-04-05 17:48:03 +00:00
NSString * folder = [[openPanel filenames] objectAtIndex: 0];
[fDefaults setObject: folder forKey: @"DownloadFolder"];
2007-09-16 01:02:06 +00:00
[fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
2009-04-05 17:48:03 +00:00
tr_sessionSetDownloadDir(fHandle, [folder UTF8String]);
2007-09-16 01:02:06 +00:00
}
else
{
//reset if cancelled
[fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
2007-09-16 01:02:06 +00:00
}
}
- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
{
if (code == NSOKButton)
{
NSString * folder = [[openPanel filenames] objectAtIndex: 0];
[fDefaults setObject: folder forKey: @"IncompleteDownloadFolder"];
tr_sessionSetIncompleteDir(fHandle, [folder UTF8String]);
}
2007-09-16 01:02:06 +00:00
[fIncompleteFolderPopUp selectItemAtIndex: 0];
}
- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
{
NSString * path = [fDefaults stringForKey: @"AutoImportDirectory"];
if (code == NSOKButton)
{
UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
if (path)
[sharedQueue removePathFromQueue: [path stringByExpandingTildeInPath]];
path = [[openPanel filenames] objectAtIndex: 0];
[fDefaults setObject: path forKey: @"AutoImportDirectory"];
[sharedQueue addPath: [path stringByExpandingTildeInPath]];
[[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
}
else if (!path)
[fDefaults setBool: NO forKey: @"AutoImport"];
[fImportFolderPopUp selectItemAtIndex: 0];
}
- (void) doneScriptSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
{
if (code == NSOKButton)
{
NSString * filePath = [[openPanel filenames] objectAtIndex: 0];
if ([[NSFileManager defaultManager] fileExistsAtPath: filePath]) // script file exists
{
[fDefaults setObject: filePath forKey: @"DoneScriptPath"];
[fDefaults setBool: YES forKey: @"DoneScriptEnabled"];
}
else // script file doesn't exist so don't enable
{
[fDefaults setObject: nil forKey:@"DoneScriptPath"];
[fDefaults setBool: NO forKey: @"DoneScriptEnabled"];
}
tr_sessionSetTorrentDoneScript(fHandle, [[fDefaults stringForKey:@"DoneScriptPath"] UTF8String]);
tr_sessionSetTorrentDoneScriptEnabled(fHandle, [fDefaults boolForKey:@"DoneScriptEnabled"]);
}
[fDoneScriptPopUp selectItemAtIndex: 0];
}
- (void) setKeychainPassword: (const char *) password forService: (const char *) service username: (const char *) username
{
SecKeychainItemRef item = NULL;
NSUInteger passwordLength = strlen(password);
OSStatus result = SecKeychainFindGenericPassword(NULL, strlen(service), service, strlen(username), username, NULL, NULL, &item);
if (result == noErr && item)
{
if (passwordLength > 0) //found, so update
{
result = SecKeychainItemModifyAttributesAndData(item, NULL, passwordLength, (const void *)password);
if (result != noErr)
NSLog(@"Problem updating Keychain item: %s", GetMacOSStatusErrorString(result));
}
else //remove the item
{
result = SecKeychainItemDelete(item);
if (result != noErr)
NSLog(@"Problem removing Keychain item: %s", GetMacOSStatusErrorString(result));
}
}
else if (result == errSecItemNotFound) //not found, so add
{
if (passwordLength > 0)
{
result = SecKeychainAddGenericPassword(NULL, strlen(service), service, strlen(username), username,
passwordLength, (const void *)password, NULL);
if (result != noErr)
NSLog(@"Problem adding Keychain item: %s", GetMacOSStatusErrorString(result));
}
}
else
NSLog(@"Problem accessing Keychain: %s", GetMacOSStatusErrorString(result));
}
2007-09-16 01:02:06 +00:00
@end