source: trunk/macosx/PrefsController.m @ 7352

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

#1157 ability to pick a random port in libtransmission; mac preference to pick a random port on launch and on button click

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