source: trunk/macosx/PrefsController.m @ 6361

Last change on this file since 6361 was 6361, checked in by livings124, 14 years ago

add a button to the prefs window to launch the web interface

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