source: trunk/macosx/PrefsController.m @ 11421

Last change on this file since 11421 was 11421, checked in by livings124, 11 years ago

#3742 entering a blocklist URL should register while typing (and support undo) - boo for glue code replacing bindings!

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