source: trunk/macosx/PrefsController.m @ 7508

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

so long Tiger support

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