source: trunk/macosx/PrefsController.m @ 10424

Last change on this file since 10424 was 10424, checked in by livings124, 12 years ago

#1295 Sync "Start when added" with libtransmission

  • Property svn:keywords set to Date Rev Author Id
File size: 47.5 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 10424 2010-03-26 02:28:41Z livings124 $
3 *
4 * Copyright (c) 2005-2010 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "PrefsController.h"
26#import "BlocklistDownloaderViewController.h"
27#import "BlocklistScheduler.h"
28#import "PortChecker.h"
29#import "BonjourController.h"
30#import "NSApplicationAdditions.h"
31#import "NSStringAdditions.h"
32#import "UKKQueue.h"
33#import "utils.h"
34
35#import <Sparkle/Sparkle.h>
36
37#define DOWNLOAD_FOLDER     0
38#define DOWNLOAD_TORRENT    2
39
40#define PROXY_HTTP      0
41#define PROXY_SOCKS4    1
42#define PROXY_SOCKS5    2
43
44#define RPC_IP_ADD_TAG      0
45#define RPC_IP_REMOVE_TAG   1
46
47#define TOOLBAR_GENERAL     @"TOOLBAR_GENERAL"
48#define TOOLBAR_TRANSFERS   @"TOOLBAR_TRANSFERS"
49#define TOOLBAR_GROUPS      @"TOOLBAR_GROUPS"
50#define TOOLBAR_BANDWIDTH   @"TOOLBAR_BANDWIDTH"
51#define TOOLBAR_PEERS       @"TOOLBAR_PEERS"
52#define TOOLBAR_NETWORK     @"TOOLBAR_NETWORK"
53#define TOOLBAR_REMOTE      @"TOOLBAR_REMOTE"
54
55#define PROXY_KEYCHAIN_SERVICE  "Transmission:Proxy"
56#define PROXY_KEYCHAIN_NAME     "Proxy"
57
58#define RPC_KEYCHAIN_SERVICE    "Transmission:Remote"
59#define RPC_KEYCHAIN_NAME       "Remote"
60
61#define WEBUI_URL   @"http://localhost:%d/transmission/web/"
62
63@interface PrefsController (Private)
64
65- (void) setPrefView: (id) sender;
66
67- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
68- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
69- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
70
71- (void) setKeychainPassword: (const char *) password forService: (const char *) service username: (const char *) username;
72
73@end
74
75@implementation PrefsController
76
77tr_session * fHandle;
78+ (void) setHandle: (tr_session *) handle
79{
80    fHandle = handle;
81}
82
83+ (tr_session *) handle
84{
85    return fHandle;
86}
87
88- (id) init
89{
90    if ((self = [super initWithWindowNibName: @"PrefsWindow"]))
91    {
92        fDefaults = [NSUserDefaults standardUserDefaults];
93       
94        //check for old version download location (before 1.1)
95        NSString * choice;
96        if ((choice = [fDefaults stringForKey: @"DownloadChoice"]))
97        {
98            [fDefaults setBool: [choice isEqualToString: @"Constant"] forKey: @"DownloadLocationConstant"];
99            [fDefaults setBool: YES forKey: @"DownloadAsk"];
100           
101            [fDefaults removeObjectForKey: @"DownloadChoice"];
102        }
103       
104        //save a new random port
105        if ([fDefaults boolForKey: @"RandomPort"])
106            [fDefaults setInteger: tr_sessionGetPeerPort(fHandle) forKey: @"BindPort"];
107       
108        //set auto import
109        NSString * autoPath;
110        if ([fDefaults boolForKey: @"AutoImport"] && (autoPath = [fDefaults stringForKey: @"AutoImportDirectory"]))
111            [[UKKQueue sharedFileWatcher] addPath: [autoPath stringByExpandingTildeInPath]];
112       
113        //set blocklist scheduler
114        [[BlocklistScheduler scheduler] updateSchedule];
115       
116        //set encryption
117        [self setEncryptionMode: nil];
118       
119        //set proxy type
120        [self updateProxyType];
121        [self updateProxyPassword];
122       
123        //update rpc whitelist
124        [self updateRPCPassword];
125       
126        fRPCWhitelistArray = [[fDefaults arrayForKey: @"RPCWhitelist"] mutableCopy];
127        if (!fRPCWhitelistArray)
128            fRPCWhitelistArray = [[NSMutableArray arrayWithObject: @"127.0.0.1"] retain];
129        [self updateRPCWhitelist];
130       
131        //reset old Sparkle settings from previous versions
132        [fDefaults removeObjectForKey: @"SUScheduledCheckInterval"];
133        if ([fDefaults objectForKey: @"CheckForUpdates"])
134        {
135            [[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates: [fDefaults boolForKey: @"CheckForUpdates"]];
136            [fDefaults removeObjectForKey: @"CheckForUpdates"];
137        }
138       
139        [self setAutoUpdateToBeta: nil];
140    }
141   
142    return self;
143}
144
145- (void) dealloc
146{
147    [[NSNotificationCenter defaultCenter] removeObserver: self];
148   
149    [fPortStatusTimer invalidate];
150    if (fPortChecker)
151    {
152        [fPortChecker cancelProbe];
153        [fPortChecker release];
154    }
155   
156    [fRPCWhitelistArray release];
157   
158    [fRPCPassword release];
159   
160    [super dealloc];
161}
162
163- (void) awakeFromNib
164{
165    fHasLoaded = YES;
166   
167    NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
168    [toolbar setDelegate: self];
169    [toolbar setAllowsUserCustomization: NO];
170    [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
171    [toolbar setSizeMode: NSToolbarSizeModeRegular];
172    [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
173    [[self window] setToolbar: toolbar];
174    [toolbar release];
175   
176    [self setPrefView: nil];
177   
178    //set download folder
179    [fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
180   
181    //set stop ratio
182    [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
183   
184    //set limits
185    [self updateLimitFields];
186   
187    //set speed limit
188    [fSpeedLimitUploadField setIntValue: [fDefaults integerForKey: @"SpeedLimitUploadLimit"]];
189    [fSpeedLimitDownloadField setIntValue: [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]];
190   
191    //set port
192    [fPortField setIntValue: [fDefaults integerForKey: @"BindPort"]];
193    fNatStatus = -1;
194   
195    [self updatePortStatus];
196    fPortStatusTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
197                        selector: @selector(updatePortStatus) userInfo: nil repeats: YES];
198   
199    //set peer connections
200    [fPeersGlobalField setIntValue: [fDefaults integerForKey: @"PeersTotal"]];
201    [fPeersTorrentField setIntValue: [fDefaults integerForKey: @"PeersTorrent"]];
202   
203    //set queue values
204    [fQueueDownloadField setIntValue: [fDefaults integerForKey: @"QueueDownloadNumber"]];
205    [fQueueSeedField setIntValue: [fDefaults integerForKey: @"QueueSeedNumber"]];
206    [fStalledField setIntValue: [fDefaults integerForKey: @"StalledMinutes"]];
207   
208    //set proxy type
209    [fProxyAddressField setStringValue: [fDefaults stringForKey: @"ProxyAddress"]];
210    NSInteger proxyType;
211    switch (tr_sessionGetProxyType(fHandle))
212    {
213        case TR_PROXY_SOCKS4:
214            proxyType = PROXY_SOCKS4;
215            break;
216        case TR_PROXY_SOCKS5:
217            proxyType = PROXY_SOCKS5;
218            break;
219        case TR_PROXY_HTTP:
220            proxyType = PROXY_HTTP;
221            break;
222        default:
223            NSAssert(NO, @"Unknown proxy type received");
224    }
225    [fProxyTypePopUp selectItemAtIndex: proxyType];
226   
227    //set proxy password - does NOT need to be released
228    [fProxyPasswordField setStringValue: [NSString stringWithUTF8String: tr_sessionGetProxyPassword(fHandle)]];
229   
230    //set proxy port
231    [fProxyPortField setIntValue: [fDefaults integerForKey: @"ProxyPort"]];
232   
233    //set blocklist
234    [self updateBlocklistFields];
235    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateBlocklistFields)
236        name: @"BlocklistUpdated" object: nil];
237   
238    //set rpc port
239    [fRPCPortField setIntValue: [fDefaults integerForKey: @"RPCPort"]];
240   
241    //set rpc password
242    if (fRPCPassword)
243        [fRPCPasswordField setStringValue: fRPCPassword];
244}
245
246- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
247{
248    NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
249
250    if ([ident isEqualToString: TOOLBAR_GENERAL])
251    {
252        [item setLabel: NSLocalizedString(@"General", "Preferences -> toolbar item title")];
253        [item setImage: [NSImage imageNamed: NSImageNamePreferencesGeneral]];
254        [item setTarget: self];
255        [item setAction: @selector(setPrefView:)];
256        [item setAutovalidates: NO];
257    }
258    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
259    {
260        [item setLabel: NSLocalizedString(@"Transfers", "Preferences -> toolbar item title")];
261        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
262        [item setTarget: self];
263        [item setAction: @selector(setPrefView:)];
264        [item setAutovalidates: NO];
265    }
266    else if ([ident isEqualToString: TOOLBAR_GROUPS])
267    {
268        [item setLabel: NSLocalizedString(@"Groups", "Preferences -> toolbar item title")];
269        [item setImage: [NSImage imageNamed: @"Groups.png"]];
270        [item setTarget: self];
271        [item setAction: @selector(setPrefView:)];
272        [item setAutovalidates: NO];
273    }
274    else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
275    {
276        [item setLabel: NSLocalizedString(@"Bandwidth", "Preferences -> toolbar item title")];
277        [item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
278        [item setTarget: self];
279        [item setAction: @selector(setPrefView:)];
280        [item setAutovalidates: NO];
281    }
282    else if ([ident isEqualToString: TOOLBAR_PEERS])
283    {
284        [item setLabel: NSLocalizedString(@"Peers", "Preferences -> toolbar item title")];
285        [item setImage: [NSImage imageNamed: NSImageNameUserGroup]];
286        [item setTarget: self];
287        [item setAction: @selector(setPrefView:)];
288        [item setAutovalidates: NO];
289    }
290    else if ([ident isEqualToString: TOOLBAR_NETWORK])
291    {
292        [item setLabel: NSLocalizedString(@"Network", "Preferences -> toolbar item title")];
293        [item setImage: [NSImage imageNamed: NSImageNameNetwork]];
294        [item setTarget: self];
295        [item setAction: @selector(setPrefView:)];
296        [item setAutovalidates: NO];
297    }
298    else if ([ident isEqualToString: TOOLBAR_REMOTE])
299    {
300        [item setLabel: NSLocalizedString(@"Remote", "Preferences -> toolbar item title")];
301        [item setImage: [NSImage imageNamed: @"Remote.png"]];
302        [item setTarget: self];
303        [item setAction: @selector(setPrefView:)];
304        [item setAutovalidates: NO];
305    }
306    else
307    {
308        [item release];
309        return nil;
310    }
311
312    return [item autorelease];
313}
314
315- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
316{
317    return [NSArray arrayWithObjects: TOOLBAR_GENERAL, TOOLBAR_TRANSFERS, TOOLBAR_GROUPS, TOOLBAR_BANDWIDTH,
318                                        TOOLBAR_PEERS, TOOLBAR_NETWORK, TOOLBAR_REMOTE, nil];
319}
320
321- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
322{
323    return [self toolbarAllowedItemIdentifiers: toolbar];
324}
325
326- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
327{
328    return [self toolbarAllowedItemIdentifiers: toolbar];
329}
330
331//for a beta release, always use the beta appcast
332#if defined(TR_BETA_RELEASE)
333#define SPARKLE_TAG YES
334#else
335#define SPARKLE_TAG [fDefaults boolForKey: @"AutoUpdateBeta"]
336#endif
337- (void) setAutoUpdateToBeta: (id) sender
338{
339    [[SUUpdater sharedUpdater] setAllowedTags: SPARKLE_TAG ? [NSSet setWithObject: @"beta"] : nil];
340}
341
342- (void) setPort: (id) sender
343{
344    const tr_port port = [sender intValue];
345    [fDefaults setInteger: port forKey: @"BindPort"];
346    tr_sessionSetPeerPort(fHandle, port);
347   
348    fPeerPort = -1;
349    [self updatePortStatus];
350}
351
352- (void) randomPort: (id) sender
353{
354    const tr_port port = tr_sessionSetPeerPortRandom(fHandle);
355    [fDefaults setInteger: port forKey: @"BindPort"];
356    [fPortField setIntValue: port];
357   
358    fPeerPort = -1;
359    [self updatePortStatus];
360}
361
362- (void) setRandomPortOnStart: (id) sender
363{
364    tr_sessionSetPeerPortRandomOnStart(fHandle, [sender state] == NSOnState);
365}
366
367- (void) setNat: (id) sender
368{
369    tr_sessionSetPortForwardingEnabled(fHandle, [fDefaults boolForKey: @"NatTraversal"]);
370   
371    fNatStatus = -1;
372    [self updatePortStatus];
373}
374
375- (void) updatePortStatus
376{
377    const tr_port_forwarding fwd = tr_sessionGetPortForwarding(fHandle);
378    const int port = tr_sessionGetPeerPort(fHandle);
379    BOOL natStatusChanged = (fNatStatus != fwd);
380    BOOL peerPortChanged = (fPeerPort != port);
381
382    if (natStatusChanged || peerPortChanged)
383    {
384        fNatStatus = fwd;
385        fPeerPort = port;
386       
387        [fPortStatusField setStringValue: @""];
388        [fPortStatusImage setImage: nil];
389        [fPortStatusProgress startAnimation: self];
390       
391        if (fPortChecker)
392        {
393            [fPortChecker cancelProbe];
394            [fPortChecker release];
395        }
396        BOOL delay = natStatusChanged || tr_sessionIsPortForwardingEnabled(fHandle);
397        fPortChecker = [[PortChecker alloc] initForPort: fPeerPort delay: delay withDelegate: self];
398    }
399}
400
401- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
402{
403    [fPortStatusProgress stopAnimation: self];
404    switch ([fPortChecker status])
405    {
406        case PORT_STATUS_OPEN:
407            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Network -> port status")];
408            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]];
409            break;
410        case PORT_STATUS_CLOSED:
411            [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Network -> port status")];
412            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
413            break;
414        case PORT_STATUS_ERROR:
415            [fPortStatusField setStringValue: NSLocalizedString(@"Port check site is down", "Preferences -> Network -> port status")];
416            [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.png"]];
417            break;
418    }
419    [fPortChecker release];
420    fPortChecker = nil;
421}
422
423- (NSArray *) sounds
424{
425    NSMutableArray * sounds = [NSMutableArray array];
426   
427    NSArray * directories = NSSearchPathForDirectoriesInDomains(NSAllLibrariesDirectory,
428                                NSUserDomainMask | NSLocalDomainMask | NSSystemDomainMask, YES);
429   
430    for (NSString * directory in directories)
431    {
432        directory = [directory stringByAppendingPathComponent: @"Sounds"];
433       
434        BOOL isDirectory;
435        if ([[NSFileManager defaultManager] fileExistsAtPath: directory isDirectory: &isDirectory] && isDirectory)
436        {
437            NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: directory error: NULL];
438            for (NSString * sound in directoryContents)
439            {
440                sound = [sound stringByDeletingPathExtension];
441                if ([NSSound soundNamed: sound])
442                    [sounds addObject: sound];
443            }
444        }
445    }
446   
447    return sounds;
448}
449
450- (void) setSound: (id) sender
451{
452    //play sound when selecting
453    NSSound * sound;
454    if ((sound = [NSSound soundNamed: [sender titleOfSelectedItem]]))
455        [sound play];
456}
457
458- (void) setPeersGlobal: (id) sender
459{
460    const int count = [sender intValue];
461    [fDefaults setInteger: count forKey: @"PeersTotal"];
462    tr_sessionSetPeerLimit(fHandle, count);
463}
464
465- (void) setPeersTorrent: (id) sender
466{
467    const int count = [sender intValue];
468    [fDefaults setInteger: count forKey: @"PeersTorrent"];
469    tr_sessionSetPeerLimitPerTorrent(fHandle, count);
470}
471
472- (void) setPEX: (id) sender
473{
474    tr_sessionSetPexEnabled(fHandle, [fDefaults boolForKey: @"PEXGlobal"]);
475}
476
477- (void) setDHT: (id) sender
478{
479    tr_sessionSetDHTEnabled(fHandle, [fDefaults boolForKey: @"DHTGlobal"]);
480}
481
482- (void) setEncryptionMode: (id) sender
483{
484    const tr_encryption_mode mode = [fDefaults boolForKey: @"EncryptionPrefer"] ?
485        ([fDefaults boolForKey: @"EncryptionRequire"] ? TR_ENCRYPTION_REQUIRED : TR_ENCRYPTION_PREFERRED) : TR_CLEAR_PREFERRED;
486    tr_sessionSetEncryption(fHandle, mode);
487}
488
489- (void) setBlocklistEnabled: (id) sender
490{
491    const BOOL enable = [sender state] == NSOnState;
492    [fDefaults setBool: enable forKey: @"Blocklist"];
493    tr_blocklistSetEnabled(fHandle, enable);
494   
495    [[BlocklistScheduler scheduler] updateSchedule];
496}
497
498- (void) updateBlocklist: (id) sender
499{
500    [BlocklistDownloaderViewController downloadWithPrefsController: self];
501}
502
503- (void) setBlocklistAutoUpdate: (id) sender
504{
505    [[BlocklistScheduler scheduler] updateSchedule];
506}
507
508- (void) updateBlocklistFields
509{
510    const BOOL exists = tr_blocklistExists(fHandle);
511   
512    if (exists)
513    {
514        NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
515        [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
516        [numberFormatter setMaximumFractionDigits: 0];
517        NSString * countString = [numberFormatter stringFromNumber: [NSNumber numberWithInt: tr_blocklistGetRuleCount(fHandle)]];
518        [numberFormatter release];
519       
520        [fBlocklistMessageField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ IP address rules in list",
521            "Prefs -> blocklist -> message"), countString]];
522    }
523    else
524        [fBlocklistMessageField setStringValue: NSLocalizedString(@"A blocklist must first be downloaded",
525            "Prefs -> blocklist -> message")];
526   
527    [fBlocklistEnableCheck setEnabled: exists];
528    [fBlocklistEnableCheck setState: exists && [fDefaults boolForKey: @"Blocklist"]];
529   
530    NSString * updatedDateString;
531    if (exists)
532    {
533        NSDate * updatedDate = [fDefaults objectForKey: @"BlocklistLastUpdate"];
534        if (updatedDate)
535        {
536            if ([NSApp isOnSnowLeopardOrBetter])
537                updatedDateString = [NSDateFormatter localizedStringFromDate: updatedDate dateStyle: NSDateFormatterFullStyle
538                                        timeStyle: NSDateFormatterShortStyle];
539            else
540            {
541                NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
542                [dateFormatter setDateStyle: NSDateFormatterFullStyle];
543                [dateFormatter setTimeStyle: NSDateFormatterShortStyle];
544               
545                updatedDateString = [dateFormatter stringFromDate: updatedDate];
546                [dateFormatter release];
547            }
548        }
549        else
550            updatedDateString = NSLocalizedString(@"N/A", "Prefs -> blocklist -> message");
551    }
552    else
553        updatedDateString = NSLocalizedString(@"Never", "Prefs -> blocklist -> message");
554   
555    [fBlocklistDateField setStringValue: [NSString stringWithFormat: @"%@: %@",
556        NSLocalizedString(@"Last updated", "Prefs -> blocklist -> message"), updatedDateString]];
557}
558
559- (void) setAutoStartDownloads: (id) sender
560{
561    tr_sessionSetPaused(fHandle, ![fDefaults boolForKey: @"AutoStartDownload"]);
562}
563
564- (void) applySpeedSettings: (id) sender
565{
566    tr_sessionLimitSpeed(fHandle, TR_UP, [fDefaults boolForKey: @"CheckUpload"]);
567    tr_sessionSetSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"UploadLimit"]);
568   
569    tr_sessionLimitSpeed(fHandle, TR_DOWN, [fDefaults boolForKey: @"CheckDownload"]);
570    tr_sessionSetSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"DownloadLimit"]);
571   
572    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
573}
574
575- (void) applyAltSpeedSettings
576{
577    tr_sessionSetAltSpeed(fHandle, TR_UP, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
578    tr_sessionSetAltSpeed(fHandle, TR_DOWN, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
579       
580    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
581}
582
583- (void) applyRatioSetting: (id) sender
584{
585    //[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
586    tr_sessionSetRatioLimited(fHandle, [fDefaults boolForKey: @"RatioCheck"]);
587    tr_sessionSetRatioLimit(fHandle, [fDefaults floatForKey: @"RatioLimit"]);
588}
589
590- (void) updateRatioStopField
591{
592    if (fHasLoaded)
593        [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
594   
595    [self applyRatioSetting: nil];
596}
597
598- (void) setRatioStop: (id) sender
599{
600    [fDefaults setFloat: [sender floatValue] forKey: @"RatioLimit"];
601   
602    [self applyRatioSetting: nil];
603}
604
605- (void) updateLimitFields
606{
607    if (!fHasLoaded)
608        return;
609   
610    [fUploadField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
611    [fDownloadField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
612}
613
614- (void) setGlobalLimit: (id) sender
615{
616    [fDefaults setInteger: [sender intValue] forKey: sender == fUploadField ? @"UploadLimit" : @"DownloadLimit"];
617    [self applySpeedSettings: self];
618}
619
620- (void) setSpeedLimit: (id) sender
621{
622    [fDefaults setInteger: [sender intValue] forKey: sender == fSpeedLimitUploadField
623                                                        ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"];
624    [self applyAltSpeedSettings];
625}
626
627- (void) setAutoSpeedLimit: (id) sender
628{
629    tr_sessionUseAltSpeedTime(fHandle, [fDefaults boolForKey: @"SpeedLimitAuto"]);
630}
631
632- (void) setAutoSpeedLimitTime: (id) sender
633{
634    tr_sessionSetAltSpeedBegin(fHandle, [PrefsController dateToTimeSum: [fDefaults objectForKey: @"SpeedLimitAutoOnDate"]]);
635    tr_sessionSetAltSpeedEnd(fHandle, [PrefsController dateToTimeSum: [fDefaults objectForKey: @"SpeedLimitAutoOffDate"]]);
636}
637
638- (void) setAutoSpeedLimitDay: (id) sender
639{
640    tr_sessionSetAltSpeedDay(fHandle, [[sender selectedItem] tag]);
641}
642
643+ (NSInteger) dateToTimeSum: (NSDate *) date
644{
645    NSCalendar * calendar = [NSCalendar currentCalendar];
646    NSDateComponents * components = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: date];
647    return [components hour] * 60 + [components minute];
648}
649
650+ (NSDate *) timeSumToDate: (NSInteger) sum
651{
652    NSDateComponents * comps = [[[NSDateComponents alloc] init] autorelease];
653    [comps setHour: sum / 60];
654    [comps setMinute: sum % 60];
655   
656    return [[NSCalendar currentCalendar] dateFromComponents: comps];
657}
658
659- (BOOL) control: (NSControl *) control textShouldBeginEditing: (NSText *) fieldEditor
660{
661    [fInitialString release];
662    fInitialString = [[control stringValue] retain];
663   
664    return YES;
665}
666
667- (BOOL) control: (NSControl *) control didFailToFormatString: (NSString *) string errorDescription: (NSString *) error
668{
669    NSBeep();
670    if (fInitialString)
671    {
672        [control setStringValue: fInitialString];
673        [fInitialString release];
674        fInitialString = nil;
675    }
676    return NO;
677}
678
679- (void) setBadge: (id) sender
680{
681    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
682}
683
684- (void) resetWarnings: (id) sender
685{
686    [fDefaults removeObjectForKey: @"WarningDuplicate"];
687    [fDefaults removeObjectForKey: @"WarningRemainingSpace"];
688    [fDefaults removeObjectForKey: @"WarningFolderDataSameName"];
689    [fDefaults removeObjectForKey: @"WarningResetStats"];
690    [fDefaults removeObjectForKey: @"WarningCreatorBlankAddress"];
691    [fDefaults removeObjectForKey: @"WarningCreatorPrivateBlankAddress"];
692    [fDefaults removeObjectForKey: @"WarningRemoveTrackers"];
693    [fDefaults removeObjectForKey: @"WarningInvalidOpen"];
694    [fDefaults removeObjectForKey: @"WarningDonate"];
695    //[fDefaults removeObjectForKey: @"WarningLegal"];
696}
697
698- (void) setDefaultForMagnets: (id) sender
699{
700    NSString * bundleID = [[NSBundle mainBundle] bundleIdentifier];
701    const OSStatus result = LSSetDefaultHandlerForURLScheme((CFStringRef)@"magnet", (CFStringRef)bundleID);
702    if (result != noErr)
703        NSLog(@"Failed setting default magnet link handler");
704}
705
706- (void) setQueue: (id) sender
707{
708    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
709}
710
711- (void) setQueueNumber: (id) sender
712{
713    [fDefaults setInteger: [sender intValue] forKey: sender == fQueueDownloadField ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
714    [self setQueue: nil];
715}
716
717- (void) setStalled: (id) sender
718{
719    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
720}
721
722- (void) setStalledMinutes: (id) sender
723{
724    [fDefaults setInteger: [sender intValue] forKey: @"StalledMinutes"];
725    [self setStalled: nil];
726}
727
728- (void) setDownloadLocation: (id) sender
729{
730    [fDefaults setBool: [fFolderPopUp indexOfSelectedItem] == DOWNLOAD_FOLDER forKey: @"DownloadLocationConstant"];
731}
732
733- (void) folderSheetShow: (id) sender
734{
735    NSOpenPanel * panel = [NSOpenPanel openPanel];
736
737    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
738    [panel setAllowsMultipleSelection: NO];
739    [panel setCanChooseFiles: NO];
740    [panel setCanChooseDirectories: YES];
741    [panel setCanCreateDirectories: YES];
742
743    [panel beginSheetForDirectory: nil file: nil types: nil
744        modalForWindow: [self window] modalDelegate: self didEndSelector:
745        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
746}
747
748- (void) incompleteFolderSheetShow: (id) sender
749{
750    NSOpenPanel * panel = [NSOpenPanel openPanel];
751
752    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
753    [panel setAllowsMultipleSelection: NO];
754    [panel setCanChooseFiles: NO];
755    [panel setCanChooseDirectories: YES];
756    [panel setCanCreateDirectories: YES];
757
758    [panel beginSheetForDirectory: nil file: nil types: nil
759        modalForWindow: [self window] modalDelegate: self didEndSelector:
760        @selector(incompleteFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
761}
762
763- (void) setUseIncompleteFolder: (id) sender
764{
765    tr_sessionSetIncompleteDirEnabled(fHandle, [fDefaults boolForKey: @"UseIncompleteDownloadFolder"]);
766}
767
768- (void) setRenamePartialFiles: (id) sender
769{
770    tr_sessionSetIncompleteFileNamingEnabled(fHandle, [fDefaults boolForKey: @"RenamePartialFiles"]);
771}
772
773- (void) setAutoImport: (id) sender
774{
775    NSString * path;
776    if ((path = [fDefaults stringForKey: @"AutoImportDirectory"]))
777    {
778        path = [path stringByExpandingTildeInPath];
779        if ([fDefaults boolForKey: @"AutoImport"])
780            [[UKKQueue sharedFileWatcher] addPath: path];
781        else
782            [[UKKQueue sharedFileWatcher] removePathFromQueue: path];
783       
784        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
785    }
786    else
787        [self importFolderSheetShow: nil];
788}
789
790- (void) importFolderSheetShow: (id) sender
791{
792    NSOpenPanel * panel = [NSOpenPanel openPanel];
793
794    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
795    [panel setAllowsMultipleSelection: NO];
796    [panel setCanChooseFiles: NO];
797    [panel setCanChooseDirectories: YES];
798    [panel setCanCreateDirectories: YES];
799
800    [panel beginSheetForDirectory: nil file: nil types: nil
801        modalForWindow: [self window] modalDelegate: self didEndSelector:
802        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
803}
804
805- (void) setAutoSize: (id) sender
806{
807    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
808}
809
810- (void) setProxyEnabled: (id) sender
811{
812    tr_sessionSetProxyEnabled(fHandle, [fDefaults boolForKey: @"Proxy"]);
813}
814
815- (void) setProxyAddress: (id) sender
816{
817    NSString * address = [sender stringValue];
818    tr_sessionSetProxy(fHandle, [address UTF8String]);
819    [fDefaults setObject: address forKey: @"ProxyAddress"];
820}
821
822- (void) setProxyPort: (id) sender
823{
824    int port = [sender intValue];
825    [fDefaults setInteger: port forKey: @"ProxyPort"];
826    tr_sessionSetProxyPort(fHandle, port);
827}
828
829- (void) setProxyType: (id) sender
830{
831    NSString * type;
832    switch ([sender indexOfSelectedItem])
833    {
834        case PROXY_HTTP:
835            type = @"HTTP";
836            break;
837        case PROXY_SOCKS4:
838            type = @"SOCKS4";
839            break;
840        case PROXY_SOCKS5:
841            type = @"SOCKS5";
842            break;
843        default:
844            NSAssert1(NO, @"Unknown index %d received for proxy type", [sender indexOfSelectedItem]);
845    }
846   
847    [fDefaults setObject: type forKey: @"ProxyType"];
848    [self updateProxyType];
849}
850
851- (void) updateProxyType
852{
853    NSString * typeString = [fDefaults stringForKey: @"ProxyType"];
854    tr_proxy_type type;
855    if ([typeString isEqualToString: @"SOCKS4"])
856        type = TR_PROXY_SOCKS4;
857    else if ([typeString isEqualToString: @"SOCKS5"])
858        type = TR_PROXY_SOCKS5;
859    else
860    {
861        //safety
862        if (![typeString isEqualToString: @"HTTP"])
863        {
864            typeString = @"HTTP";
865            [fDefaults setObject: typeString forKey: @"ProxyType"];
866        }
867        type = TR_PROXY_HTTP;
868    }
869   
870    tr_sessionSetProxyType(fHandle, type);
871}
872
873- (void) setProxyAuthorize: (id) sender
874{
875    BOOL enable = [fDefaults boolForKey: @"ProxyAuthorize"];
876    tr_sessionSetProxyAuthEnabled(fHandle, enable);
877}
878
879- (void) setProxyUsername: (id) sender
880{
881    tr_sessionSetProxyUsername(fHandle, [[fDefaults stringForKey: @"ProxyUsername"] UTF8String]);
882}
883
884- (void) setProxyPassword: (id) sender
885{
886    const char * password = [[sender stringValue] UTF8String];
887    [self setKeychainPassword: password forService: PROXY_KEYCHAIN_SERVICE username: PROXY_KEYCHAIN_NAME];
888   
889    tr_sessionSetProxyPassword(fHandle, password);
890}
891
892- (void) updateProxyPassword
893{
894    UInt32 passwordLength;
895    const char * password = nil;
896    SecKeychainFindGenericPassword(NULL, strlen(PROXY_KEYCHAIN_SERVICE), PROXY_KEYCHAIN_SERVICE,
897        strlen(PROXY_KEYCHAIN_NAME), PROXY_KEYCHAIN_NAME, &passwordLength, (void **)&password, NULL);
898   
899    if (password != NULL)
900    {
901        char fullPassword[passwordLength+1];
902        strncpy(fullPassword, password, passwordLength);
903        fullPassword[passwordLength] = '\0';
904        SecKeychainItemFreeContent(NULL, (void *)password);
905       
906        tr_sessionSetProxyPassword(fHandle, fullPassword);
907        [fProxyPasswordField setStringValue: [NSString stringWithUTF8String: fullPassword]];
908    }
909}
910
911- (void) setRPCEnabled: (id) sender
912{
913    BOOL enable = [fDefaults boolForKey: @"RPC"];
914    tr_sessionSetRPCEnabled(fHandle, enable);
915   
916    [self setRPCWebUIDiscovery: nil];
917}
918
919- (void) linkWebUI: (id) sender
920{
921    NSString * urlString = [NSString stringWithFormat: WEBUI_URL, [fDefaults integerForKey: @"RPCPort"]];
922    [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: urlString]];
923}
924
925- (void) setRPCAuthorize: (id) sender
926{
927    tr_sessionSetRPCPasswordEnabled(fHandle, [fDefaults boolForKey: @"RPCAuthorize"]);
928}
929
930- (void) setRPCUsername: (id) sender
931{
932    tr_sessionSetRPCUsername(fHandle, [[fDefaults stringForKey: @"RPCUsername"] UTF8String]);
933}
934
935- (void) setRPCPassword: (id) sender
936{
937    [fRPCPassword release];
938    fRPCPassword = [[sender stringValue] copy];
939   
940    const char * password = [[sender stringValue] UTF8String];
941    [self setKeychainPassword: password forService: RPC_KEYCHAIN_SERVICE username: RPC_KEYCHAIN_NAME];
942   
943    tr_sessionSetRPCPassword(fHandle, password);
944}
945
946- (void) updateRPCPassword
947{
948    UInt32 passwordLength;
949    const char * password = nil;
950    SecKeychainFindGenericPassword(NULL, strlen(RPC_KEYCHAIN_SERVICE), RPC_KEYCHAIN_SERVICE,
951        strlen(RPC_KEYCHAIN_NAME), RPC_KEYCHAIN_NAME, &passwordLength, (void **)&password, NULL);
952   
953    [fRPCPassword release];
954    if (password != NULL)
955    {
956        char fullPassword[passwordLength+1];
957        strncpy(fullPassword, password, passwordLength);
958        fullPassword[passwordLength] = '\0';
959        SecKeychainItemFreeContent(NULL, (void *)password);
960       
961        tr_sessionSetRPCPassword(fHandle, fullPassword);
962       
963        fRPCPassword = [[NSString alloc] initWithUTF8String: fullPassword];
964        [fRPCPasswordField setStringValue: fRPCPassword];
965    }
966    else
967        fRPCPassword = nil;
968}
969
970- (void) setRPCPort: (id) sender
971{
972    int port = [sender intValue];
973    [fDefaults setInteger: port forKey: @"RPCPort"];
974    tr_sessionSetRPCPort(fHandle, port);
975   
976    [self setRPCWebUIDiscovery: nil];
977}
978
979- (void) setRPCUseWhitelist: (id) sender
980{
981    tr_sessionSetRPCWhitelistEnabled(fHandle, [fDefaults boolForKey: @"RPCUseWhitelist"]);
982}
983
984- (void) setRPCWebUIDiscovery: (id) sender
985{
986    if ([fDefaults boolForKey:@"RPC"] && [fDefaults boolForKey: @"RPCWebDiscovery"])
987        [[BonjourController defaultController] startWithPort: [fDefaults integerForKey: @"RPCPort"]];
988    else
989        [[BonjourController defaultController] stop];
990}
991
992- (void) updateRPCWhitelist
993{
994    NSString * string = [fRPCWhitelistArray componentsJoinedByString: @","];
995    tr_sessionSetRPCWhitelist(fHandle, [string UTF8String]);
996}
997
998- (void) addRemoveRPCIP: (id) sender
999{
1000    //don't allow add/remove when currently adding - it leads to weird results
1001    if ([fRPCWhitelistTable editedRow] != -1)
1002        return;
1003   
1004    if ([[sender cell] tagForSegment: [sender selectedSegment]] == RPC_IP_REMOVE_TAG)
1005    {
1006        [fRPCWhitelistArray removeObjectsAtIndexes: [fRPCWhitelistTable selectedRowIndexes]];
1007        [fRPCWhitelistTable deselectAll: self];
1008        [fRPCWhitelistTable reloadData];
1009       
1010        [fDefaults setObject: fRPCWhitelistArray forKey: @"RPCWhitelist"];
1011        [self updateRPCWhitelist];
1012    }
1013    else
1014    {
1015        [fRPCWhitelistArray addObject: @""];
1016        [fRPCWhitelistTable reloadData];
1017       
1018        const int row = [fRPCWhitelistArray count] - 1;
1019        [fRPCWhitelistTable selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
1020        [fRPCWhitelistTable editColumn: 0 row: row withEvent: nil select: YES];
1021    }
1022}
1023
1024- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
1025{
1026    return [fRPCWhitelistArray count];
1027}
1028
1029- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
1030{
1031    return [fRPCWhitelistArray objectAtIndex: row];
1032}
1033
1034- (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn
1035    row: (NSInteger) row
1036{
1037    NSArray * components = [object componentsSeparatedByString: @"."];
1038    NSMutableArray * newComponents = [NSMutableArray arrayWithCapacity: 4];
1039       
1040    //create better-formatted ip string
1041    BOOL valid = false;
1042    if ([components count] == 4)
1043    {
1044        valid = true;
1045        for (NSString * component in components)
1046        {
1047            if ([component isEqualToString: @"*"])
1048                [newComponents addObject: component];
1049            else
1050            {
1051                int num = [component intValue];
1052                if (num >= 0 && num < 256)
1053                    [newComponents addObject: [[NSNumber numberWithInt: num] stringValue]];
1054                else
1055                {
1056                    valid = false;
1057                    break;
1058                }
1059            }
1060        }
1061    }
1062   
1063    NSString * newIP;
1064    if (valid)
1065    {
1066        newIP = [newComponents componentsJoinedByString: @"."];
1067       
1068        //don't allow the same ip address
1069        if ([fRPCWhitelistArray containsObject: newIP] && ![[fRPCWhitelistArray objectAtIndex: row] isEqualToString: newIP])
1070            valid = false;
1071    }
1072   
1073    if (valid)
1074    {
1075        [fRPCWhitelistArray replaceObjectAtIndex: row withObject: newIP];
1076        [fRPCWhitelistArray sortUsingSelector: @selector(compareNumeric:)];
1077    }
1078    else
1079    {
1080        NSBeep();
1081        if ([[fRPCWhitelistArray objectAtIndex: row] isEqualToString: @""])
1082            [fRPCWhitelistArray removeObjectAtIndex: row];
1083    }
1084       
1085    [fRPCWhitelistTable deselectAll: self];
1086    [fRPCWhitelistTable reloadData];
1087   
1088    [fDefaults setObject: fRPCWhitelistArray forKey: @"RPCWhitelist"];
1089    [self updateRPCWhitelist];
1090}
1091
1092- (void) tableViewSelectionDidChange: (NSNotification *) notification
1093{
1094    [fRPCAddRemoveControl setEnabled: [fRPCWhitelistTable numberOfSelectedRows] > 0 forSegment: RPC_IP_REMOVE_TAG];
1095}
1096
1097- (void) helpForPeers: (id) sender
1098{
1099    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PeersPrefs"
1100        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
1101}
1102
1103- (void) helpForNetwork: (id) sender
1104{
1105    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"NetworkPrefs"
1106        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
1107}
1108
1109- (void) helpForRemote: (id) sender
1110{
1111    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"RemotePrefs"
1112        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
1113}
1114
1115- (void) rpcUpdatePrefs
1116{
1117    //encryption
1118    const tr_encryption_mode encryptionMode = tr_sessionGetEncryption(fHandle);
1119    [fDefaults setBool: encryptionMode != TR_CLEAR_PREFERRED forKey: @"EncryptionPrefer"];
1120    [fDefaults setBool: encryptionMode == TR_ENCRYPTION_REQUIRED forKey: @"EncryptionRequire"];
1121   
1122    //download directory
1123    NSString * downloadLocation = [[NSString stringWithUTF8String: tr_sessionGetDownloadDir(fHandle)] stringByStandardizingPath];
1124    [fDefaults setObject: downloadLocation forKey: @"DownloadFolder"];
1125   
1126    NSString * incompleteLocation = [[NSString stringWithUTF8String: tr_sessionGetIncompleteDir(fHandle)] stringByStandardizingPath];
1127    [fDefaults setObject: incompleteLocation forKey: @"IncompleteDownloadFolder"];
1128   
1129    const BOOL useIncomplete = tr_sessionIsIncompleteDirEnabled(fHandle);
1130    [fDefaults setBool: useIncomplete forKey: @"UseIncompleteDownloadFolder"];
1131   
1132    const BOOL usePartialFileRanaming = tr_sessionIsIncompleteFileNamingEnabled(fHandle);
1133    [fDefaults setBool: usePartialFileRanaming forKey: @"RenamePartialFiles"];
1134   
1135    //peers
1136    const uint16_t peersTotal = tr_sessionGetPeerLimit(fHandle);
1137    [fDefaults setInteger: peersTotal forKey: @"PeersTotal"];
1138   
1139    const uint16_t peersTorrent = tr_sessionGetPeerLimitPerTorrent(fHandle);
1140    [fDefaults setInteger: peersTorrent forKey: @"PeersTorrent"];
1141   
1142    //pex
1143    const BOOL pex = tr_sessionIsPexEnabled(fHandle);
1144    [fDefaults setBool: pex forKey: @"PEXGlobal"];
1145   
1146    //dht
1147    const BOOL dht = tr_sessionIsDHTEnabled(fHandle);
1148    [fDefaults setBool: dht forKey: @"DHTGlobal"];
1149   
1150    //auto start
1151    const BOOL autoStart = !tr_sessionGetPaused(fHandle);
1152    [fDefaults setBool: autoStart forKey: @"AutoStartDownload"];
1153   
1154    //port
1155    const tr_port port = tr_sessionGetPeerPort(fHandle);
1156    [fDefaults setInteger: port forKey: @"BindPort"];
1157   
1158    const BOOL nat = tr_sessionIsPortForwardingEnabled(fHandle);
1159    [fDefaults setBool: nat forKey: @"NatTraversal"];
1160   
1161    fPeerPort = -1;
1162    fNatStatus = -1;
1163    [self updatePortStatus];
1164   
1165    const BOOL randomPort = tr_sessionGetPeerPortRandomOnStart(fHandle);
1166    [fDefaults setBool: randomPort forKey: @"RandomPort"];
1167   
1168    //speed limit - down
1169    const BOOL downLimitEnabled = tr_sessionIsSpeedLimited(fHandle, TR_DOWN);
1170    [fDefaults setBool: downLimitEnabled forKey: @"CheckDownload"];
1171   
1172    const int downLimit = tr_sessionGetSpeedLimit(fHandle, TR_DOWN);
1173    [fDefaults setInteger: downLimit forKey: @"DownloadLimit"];
1174   
1175    //speed limit - up
1176    const BOOL upLimitEnabled = tr_sessionIsSpeedLimited(fHandle, TR_UP);
1177    [fDefaults setBool: upLimitEnabled forKey: @"CheckUpload"];
1178   
1179    const int upLimit = tr_sessionGetSpeedLimit(fHandle, TR_UP);
1180    [fDefaults setInteger: upLimit forKey: @"UploadLimit"];
1181   
1182    //alt speed limit enabled
1183    const BOOL useAltSpeed = tr_sessionUsesAltSpeed(fHandle);
1184    [fDefaults setBool: useAltSpeed forKey: @"SpeedLimit"];
1185   
1186    //alt speed limit - down
1187    const int downLimitAlt = tr_sessionGetAltSpeed(fHandle, TR_DOWN);
1188    [fDefaults setInteger: downLimitAlt forKey: @"SpeedLimitDownloadLimit"];
1189   
1190    //alt speed limit - up
1191    const int upLimitAlt = tr_sessionGetAltSpeed(fHandle, TR_UP);
1192    [fDefaults setInteger: upLimitAlt forKey: @"SpeedLimitUploadLimit"];
1193   
1194    //alt speed limit schedule
1195    const BOOL useAltSpeedSched = tr_sessionUsesAltSpeedTime(fHandle);
1196    [fDefaults setBool: useAltSpeedSched forKey: @"SpeedLimitAuto"];
1197   
1198    NSDate * limitStartDate = [PrefsController timeSumToDate: tr_sessionGetAltSpeedBegin(fHandle)];
1199    [fDefaults setObject: limitStartDate forKey: @"SpeedLimitAutoOnDate"];
1200   
1201    NSDate * limitEndDate = [PrefsController timeSumToDate: tr_sessionGetAltSpeedEnd(fHandle)];
1202    [fDefaults setObject: limitEndDate forKey: @"SpeedLimitAutoOffDate"];
1203   
1204    const int limitDay = tr_sessionGetAltSpeedDay(fHandle);
1205    [fDefaults setInteger: limitDay forKey: @"SpeedLimitAutoDay"];
1206   
1207    //blocklist
1208    const BOOL blocklist = tr_blocklistIsEnabled(fHandle);
1209    [fDefaults setBool: blocklist forKey: @"Blocklist"];
1210   
1211    //seed ratio
1212    const BOOL ratioLimited = tr_sessionIsRatioLimited(fHandle);
1213    [fDefaults setBool: ratioLimited forKey: @"RatioCheck"];
1214   
1215    const float ratioLimit = tr_sessionGetRatioLimit(fHandle);
1216    [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
1217   
1218    //update gui if loaded
1219    if (fHasLoaded)
1220    {
1221        //encryption handled by bindings
1222       
1223        //download directory handled by bindings
1224       
1225        [fPeersGlobalField setIntValue: peersTotal];
1226        [fPeersTorrentField setIntValue: peersTorrent];
1227       
1228        //pex handled by bindings
1229       
1230        //dht handled by bindings
1231       
1232        [fPortField setIntValue: port];
1233        //port forwarding (nat) handled by bindings
1234        //random port handled by bindings
1235       
1236        //limit check handled by bindings
1237        [fDownloadField setIntValue: downLimit];
1238       
1239        //limit check handled by bindings
1240        [fUploadField setIntValue: upLimit];
1241       
1242        [fSpeedLimitDownloadField setIntValue: downLimitAlt];
1243       
1244        [fSpeedLimitUploadField setIntValue: upLimitAlt];
1245       
1246        //speed limit schedule handled by bindings
1247       
1248        //speed limit schedule times and day handled by bindings
1249       
1250        [self updateBlocklistFields];
1251       
1252        //ratio limit enabled handled by bindings
1253        [fRatioStopField setFloatValue: ratioLimit];
1254    }
1255   
1256    [[NSNotificationCenter defaultCenter] postNotificationName: @"SpeedLimitUpdate" object: nil];
1257}
1258
1259@end
1260
1261@implementation PrefsController (Private)
1262
1263- (void) setPrefView: (id) sender
1264{
1265    NSString * identifier;
1266    if (sender)
1267    {
1268        identifier = [sender itemIdentifier];
1269        [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"SelectedPrefView"];
1270    }
1271    else
1272        identifier = [[NSUserDefaults standardUserDefaults] stringForKey: @"SelectedPrefView"];
1273   
1274    NSView * view;
1275    if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
1276        view = fTransfersView;
1277    else if ([identifier isEqualToString: TOOLBAR_GROUPS])
1278        view = fGroupsView;
1279    else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
1280        view = fBandwidthView;
1281    else if ([identifier isEqualToString: TOOLBAR_PEERS])
1282        view = fPeersView;
1283    else if ([identifier isEqualToString: TOOLBAR_NETWORK])
1284        view = fNetworkView;
1285    else if ([identifier isEqualToString: TOOLBAR_REMOTE])
1286        view = fRemoteView;
1287    else
1288    {
1289        identifier = TOOLBAR_GENERAL; //general view is the default selected
1290        view = fGeneralView;
1291    }
1292   
1293    [[[self window] toolbar] setSelectedItemIdentifier: identifier];
1294   
1295    NSWindow * window = [self window];
1296    if ([window contentView] == view)
1297        return;
1298   
1299    NSRect windowRect = [window frame];
1300    float difference = ([view frame].size.height - [[window contentView] frame].size.height) * [window userSpaceScaleFactor];
1301    windowRect.origin.y -= difference;
1302    windowRect.size.height += difference;
1303   
1304    [view setHidden: YES];
1305    [window setContentView: view];
1306    [window setFrame: windowRect display: YES animate: YES];
1307    [view setHidden: NO];
1308   
1309    //set title label
1310    if (sender)
1311        [window setTitle: [sender label]];
1312    else
1313    {
1314        NSToolbar * toolbar = [window toolbar];
1315        NSString * itemIdentifier = [toolbar selectedItemIdentifier];
1316        for (NSToolbarItem * item in [toolbar items])
1317            if ([[item itemIdentifier] isEqualToString: itemIdentifier])
1318            {
1319                [window setTitle: [item label]];
1320                break;
1321            }
1322    }
1323}
1324
1325- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
1326{
1327    if (code == NSOKButton)
1328    {
1329        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
1330       
1331        NSString * folder = [[openPanel filenames] objectAtIndex: 0];
1332        [fDefaults setObject: folder forKey: @"DownloadFolder"];
1333        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
1334       
1335        tr_sessionSetDownloadDir(fHandle, [folder UTF8String]);
1336    }
1337    else
1338    {
1339        //reset if cancelled
1340        [fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
1341    }
1342}
1343
1344- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
1345{
1346    if (code == NSOKButton)
1347    {
1348        NSString * folder = [[openPanel filenames] objectAtIndex: 0];
1349        [fDefaults setObject: folder forKey: @"IncompleteDownloadFolder"];
1350       
1351        tr_sessionSetIncompleteDir(fHandle, [folder UTF8String]);
1352    }
1353    [fIncompleteFolderPopUp selectItemAtIndex: 0];
1354}
1355
1356- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
1357{
1358    NSString * path = [fDefaults stringForKey: @"AutoImportDirectory"];
1359    if (code == NSOKButton)
1360    {
1361        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
1362        if (path)
1363            [sharedQueue removePathFromQueue: [path stringByExpandingTildeInPath]];
1364       
1365        path = [[openPanel filenames] objectAtIndex: 0];
1366        [fDefaults setObject: path forKey: @"AutoImportDirectory"];
1367        [sharedQueue addPath: [path stringByExpandingTildeInPath]];
1368       
1369        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
1370    }
1371    else if (!path)
1372        [fDefaults setBool: NO forKey: @"AutoImport"];
1373   
1374    [fImportFolderPopUp selectItemAtIndex: 0];
1375}
1376
1377- (void) setKeychainPassword: (const char *) password forService: (const char *) service username: (const char *) username
1378{
1379    SecKeychainItemRef item = NULL;
1380    NSUInteger passwordLength = strlen(password);
1381   
1382    OSStatus result = SecKeychainFindGenericPassword(NULL, strlen(service), service, strlen(username), username, NULL, NULL, &item);
1383    if (result == noErr && item)
1384    {
1385        if (passwordLength > 0) //found, so update
1386        {
1387            result = SecKeychainItemModifyAttributesAndData(item, NULL, passwordLength, (const void *)password);
1388            if (result != noErr)
1389                NSLog(@"Problem updating Keychain item: %s", GetMacOSStatusErrorString(result));
1390        }
1391        else //remove the item
1392        {
1393            result = SecKeychainItemDelete(item);
1394            if (result != noErr)
1395                NSLog(@"Problem removing Keychain item: %s", GetMacOSStatusErrorString(result));
1396        }
1397    }
1398    else if (result == errSecItemNotFound) //not found, so add
1399    {
1400        if (passwordLength > 0)
1401        {
1402            result = SecKeychainAddGenericPassword(NULL, strlen(service), service, strlen(username), username,
1403                        passwordLength, (const void *)password, NULL);
1404            if (result != noErr)
1405                NSLog(@"Problem adding Keychain item: %s", GetMacOSStatusErrorString(result));
1406        }
1407    }
1408    else
1409        NSLog(@"Problem accessing Keychain: %s", GetMacOSStatusErrorString(result));
1410}
1411
1412@end
Note: See TracBrowser for help on using the repository browser.