source: trunk/macosx/PrefsController.m @ 7892

Last change on this file since 7892 was 7892, checked in by livings124, 13 years ago

have the mac ui use libT's ratio settings (attempt 1); when seed ratio is reached in libT, set the seed ratio setting to "seed forever"

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