source: trunk/macosx/PrefsController.m @ 3314

Last change on this file since 3314 was 3314, checked in by livings124, 15 years ago

change some initializers so that they are aware of their own nib's name

  • Property svn:keywords set to Date Rev Author Id
File size: 23.6 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 3314 2007-10-07 12:24:26Z livings124 $
3 *
4 * Copyright (c) 2005-2007 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 "NSStringAdditions.h"
27#import "UKKQueue.h"
28
29#define DOWNLOAD_FOLDER     0
30#define DOWNLOAD_TORRENT    2
31#define DOWNLOAD_ASK        3
32
33#define UPDATE_SECONDS 86400
34
35#define TOOLBAR_GENERAL     @"TOOLBAR_GENERAL"
36#define TOOLBAR_TRANSFERS   @"TOOLBAR_TRANSFERS"
37#define TOOLBAR_BANDWIDTH   @"TOOLBAR_BANDWIDTH"
38#define TOOLBAR_ADVANCED    @"TOOLBAR_ADVANCED"
39
40@interface PrefsController (Private)
41
42- (void) setPrefView: (id) sender;
43
44- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
45- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
46- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
47
48@end
49
50@implementation PrefsController
51
52- (id) initWithHandle: (tr_handle *) handle
53{
54    if ((self = [super initWithWindowNibName: @"PrefsWindow"]))
55    {
56        fDefaults = [NSUserDefaults standardUserDefaults];
57        fHandle = handle;
58       
59        //checks for old version upload speed of -1
60        if ([fDefaults integerForKey: @"UploadLimit"] < 0)
61        {
62            [fDefaults setInteger: 20 forKey: @"UploadLimit"];
63            [fDefaults setBool: NO forKey: @"CheckUpload"];
64        }
65       
66        //set check for update to right value
67        [self setCheckForUpdate: nil];
68       
69        //set auto import
70        NSString * autoPath;
71        if ([fDefaults boolForKey: @"AutoImport"] && (autoPath = [fDefaults stringForKey: @"AutoImportDirectory"]))
72            [[UKKQueue sharedFileWatcher] addPath: [autoPath stringByExpandingTildeInPath]];
73       
74        //set bind port
75        int bindPort = [fDefaults integerForKey: @"BindPort"];
76        tr_setBindPort(fHandle, bindPort);
77       
78        //set NAT
79        tr_natTraversalEnable(fHandle, [fDefaults boolForKey: @"NatTraversal"]);
80       
81        //set encryption
82        tr_setEncryptionMode(fHandle, [fDefaults boolForKey: @"EncryptionRequire"] ? TR_ENCRYPTION_REQUIRED : TR_ENCRYPTION_PREFERRED);
83       
84        //actually set bandwidth limits
85        [self applySpeedSettings: nil];
86    }
87   
88    return self;
89}
90
91- (void) dealloc
92{
93    if (fNatStatusTimer)
94        [fNatStatusTimer invalidate];
95   
96    [super dealloc];
97}
98
99- (void) awakeFromNib
100{
101    fHasLoaded = YES;
102   
103    NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
104    [toolbar setDelegate: self];
105    [toolbar setAllowsUserCustomization: NO];
106    [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
107    [toolbar setSizeMode: NSToolbarSizeModeRegular];
108    [[self window] setToolbar: toolbar];
109
110    [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
111    [self setPrefView: nil];
112   
113    //set download folder
114    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
115    if ([downloadChoice isEqualToString: @"Constant"])
116        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
117    else if ([downloadChoice isEqualToString: @"Torrent"])
118        [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
119    else
120        [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
121   
122    //set stop ratio
123    [self updateRatioStopField];
124   
125    //set limits
126    [self updateLimitFields];
127   
128    //set speed limit
129    [fSpeedLimitUploadField setIntValue: [fDefaults integerForKey: @"SpeedLimitUploadLimit"]];
130    [fSpeedLimitDownloadField setIntValue: [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]];
131   
132    //set port
133    [fPortField setIntValue: [fDefaults integerForKey: @"BindPort"]];
134    fNatStatus = -1;
135   
136    [self updatePortStatus];
137    fNatStatusTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
138                        selector: @selector(updatePortStatus) userInfo: nil repeats: YES];
139   
140    //set queue values
141    [fQueueDownloadField setIntValue: [fDefaults integerForKey: @"QueueDownloadNumber"]];
142    [fQueueSeedField setIntValue: [fDefaults integerForKey: @"QueueSeedNumber"]];
143   
144    //set stalled value
145    [fStalledField setIntValue: [fDefaults integerForKey: @"StalledMinutes"]];
146}
147
148- (void) setUpdater: (SUUpdater *) updater
149{
150    fUpdater = updater;
151}
152
153- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
154{
155    NSToolbarItem * item;
156    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
157
158    if ([ident isEqualToString: TOOLBAR_GENERAL])
159    {
160        [item setLabel: NSLocalizedString(@"General", "Preferences -> General toolbar item title")];
161        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
162        [item setTarget: self];
163        [item setAction: @selector(setPrefView:)];
164        [item setAutovalidates: NO];
165    }
166    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
167    {
168        [item setLabel: NSLocalizedString(@"Transfers", "Preferences -> Transfers toolbar item title")];
169        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
170        [item setTarget: self];
171        [item setAction: @selector(setPrefView:)];
172        [item setAutovalidates: NO];
173    }
174    else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
175    {
176        [item setLabel: NSLocalizedString(@"Bandwidth", "Preferences -> Bandwidth toolbar item title")];
177        [item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
178        [item setTarget: self];
179        [item setAction: @selector(setPrefView:)];
180        [item setAutovalidates: NO];
181    }
182    else if ([ident isEqualToString: TOOLBAR_ADVANCED])
183    {
184        [item setLabel: NSLocalizedString(@"Advanced", "Preferences -> Advanced toolbar item title")];
185        [item setImage: [NSImage imageNamed: @"Advanced.png"]];
186        [item setTarget: self];
187        [item setAction: @selector(setPrefView:)];
188        [item setAutovalidates: NO];
189    }
190    else
191    {
192        [item release];
193        return nil;
194    }
195
196    return [item autorelease];
197}
198
199- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
200{
201    return [self toolbarDefaultItemIdentifiers: toolbar];
202}
203
204- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
205{
206    return [self toolbarAllowedItemIdentifiers: toolbar];
207}
208
209- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
210{
211    return [NSArray arrayWithObjects: TOOLBAR_GENERAL, TOOLBAR_TRANSFERS,
212                                        TOOLBAR_BANDWIDTH, TOOLBAR_ADVANCED, nil];
213}
214
215- (void) setPort: (id) sender
216{
217    int port = [sender intValue];
218    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", port]])
219    {
220        NSBeep();
221        [sender setIntValue: [fDefaults integerForKey: @"BindPort"]];
222        return;
223    }
224   
225    [fDefaults setInteger: fPublicPort forKey: @"BindPort"];
226   
227    tr_setBindPort(fHandle, port);
228   
229    fPublicPort = -1;
230    [self updatePortStatus];
231}
232
233- (void) setNat: (id) sender
234{
235    tr_natTraversalEnable(fHandle, [fDefaults boolForKey: @"NatTraversal"]);
236    [self updatePortStatus];
237}
238
239- (void) updatePortStatus
240{
241    tr_handle_status * stat = tr_handleStatus(fHandle);
242   
243    BOOL change;
244    if (change = (fNatStatus != stat->natTraversalStatus))
245    {
246        fNatStatus = stat->natTraversalStatus;
247       
248        if (fNatStatus == TR_NAT_TRAVERSAL_MAPPED)
249        {
250            [fNatStatusField setStringValue: NSLocalizedString(@"Port successfully mapped",
251                                                "Preferences -> Advanced -> port map status")];
252            [fNatStatusImage setImage: [NSImage imageNamed: @"GreenDot.tiff"]];
253        }
254        else if (fNatStatus == TR_NAT_TRAVERSAL_NOTFOUND || fNatStatus == TR_NAT_TRAVERSAL_ERROR)
255        {
256            [fNatStatusField setStringValue: NSLocalizedString(@"Error mapping port",
257                                                "Preferences -> Advanced -> port map status")];
258            [fNatStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
259        }
260        else
261        {
262            [fNatStatusField setStringValue: @""];
263            [fNatStatusImage setImage: nil];
264        }
265    }
266   
267    if (change || fPublicPort != stat->publicPort)
268    {
269        fPublicPort = stat->publicPort;
270       
271        [fPortStatusField setStringValue: [NSLocalizedString(@"Checking port status",
272                                            "Preferences -> Advanced -> port status") stringByAppendingEllipsis]];
273        [fPortStatusImage setImage: nil];
274        [fPortStatusProgress startAnimation: self];
275       
276        PortChecker * portChecker = [[PortChecker alloc] initWithDelegate: self];
277        [portChecker probePort: fPublicPort];
278    }
279}
280
281- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
282{
283    [fPortStatusProgress stopAnimation: self];
284    switch ([portChecker status])
285    {
286        case PORT_STATUS_OPEN:
287            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Advanced -> port status")];
288            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.tiff"]];
289            break;
290        case PORT_STATUS_STEALTH:
291            [fPortStatusField setStringValue: NSLocalizedString(@"Port is stealth", "Preferences -> Advanced -> port status")];
292            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
293            break;
294        case PORT_STATUS_CLOSED:
295            [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Advanced -> port status")];
296            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
297            break;
298        case PORT_STATUS_ERROR:
299            [fPortStatusField setStringValue: NSLocalizedString(@"Unable to check port status",
300                                                "Preferences -> Advanced -> port status")];
301            [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.tiff"]];
302            break;
303    }
304    [portChecker release];
305}
306
307- (NSArray *) sounds
308{
309    NSMutableArray * sounds = [[NSMutableArray alloc] init];
310   
311    NSArray * directories = [NSArray arrayWithObjects: @"/System/Library/Sounds", @"/Library/Sounds",
312                                [NSHomeDirectory() stringByAppendingPathComponent: @"Library/Sounds"], nil];
313    BOOL isDirectory;
314    NSEnumerator * soundEnumerator;
315    NSString * sound;
316   
317    NSString * directory;
318    NSEnumerator * enumerator = [directories objectEnumerator];
319    while ((directory = [enumerator nextObject]))
320        if ([[NSFileManager defaultManager] fileExistsAtPath: directory isDirectory: &isDirectory] && isDirectory)
321        {
322                soundEnumerator = [[[NSFileManager defaultManager] directoryContentsAtPath: directory] objectEnumerator];
323                while ((sound = [soundEnumerator nextObject]))
324                {
325                    sound = [sound stringByDeletingPathExtension];
326                    if ([NSSound soundNamed: sound])
327                        [sounds addObject: sound];
328                }
329        }
330   
331    return sounds;
332}
333
334- (void) setSound: (id) sender
335{
336    //play sound when selecting
337    NSSound * sound;
338    if ((sound = [NSSound soundNamed: [sender titleOfSelectedItem]]))
339        [sound play];
340}
341
342- (void) setEncryptionRequired: (id) sender
343{
344    tr_setEncryptionMode(fHandle, [fDefaults boolForKey: @"EncryptionRequire"] ? TR_ENCRYPTION_REQUIRED : TR_ENCRYPTION_PREFERRED);
345}
346
347- (void) applySpeedSettings: (id) sender
348{
349    if ([fDefaults boolForKey: @"SpeedLimit"])
350    {
351        tr_setUseGlobalSpeedLimit(fHandle, TR_UP, 1);
352        tr_setGlobalSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
353       
354        tr_setUseGlobalSpeedLimit(fHandle, TR_DOWN, 1);
355        tr_setGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
356    }
357    else
358    {
359        tr_setUseGlobalSpeedLimit(fHandle, TR_UP, [fDefaults boolForKey: @"CheckUpload"]);
360        tr_setGlobalSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"UploadLimit"]);
361       
362        tr_setUseGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults boolForKey: @"CheckDownload"]);
363        tr_setGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"DownloadLimit"]);
364    }
365}
366
367- (void) applyRatioSetting: (id) sender
368{
369    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
370}
371
372- (void) updateRatioStopField
373{
374    if (!fHasLoaded)
375        return;
376   
377    [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
378   
379    [self applyRatioSetting: nil];
380}
381
382- (void) setRatioStop: (id) sender
383{
384    float ratio = [sender floatValue];
385    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratio]] || ratio < 0)
386    {
387        NSBeep();
388        [sender setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
389        return;
390    }
391   
392    [fDefaults setFloat: ratio forKey: @"RatioLimit"];
393   
394    [self applyRatioSetting: nil];
395}
396
397- (void) updateLimitFields
398{
399    if (!fHasLoaded)
400        return;
401   
402    [fUploadField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
403    [fDownloadField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
404}
405
406- (void) setGlobalLimit: (id) sender
407{
408    BOOL upload = sender == fUploadField;
409   
410    int limit = [sender intValue];
411    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
412    {
413        NSBeep();
414        [sender setIntValue: [fDefaults integerForKey: upload ? @"UploadLimit" : @"DownloadLimit"]];
415        return;
416    }
417   
418    [fDefaults setInteger: limit forKey: upload ? @"UploadLimit" : @"DownloadLimit"];
419   
420    [self applySpeedSettings: self];
421}
422
423- (void) setSpeedLimit: (id) sender
424{
425    BOOL upload = sender == fSpeedLimitUploadField;
426   
427    int limit = [sender intValue];
428    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]])
429    {
430        NSBeep();
431        [sender setIntValue: [fDefaults integerForKey: upload ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"]];
432        return;
433    }
434   
435    [fDefaults setInteger: limit forKey: upload ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"];
436   
437    [self applySpeedSettings: self];
438}
439
440- (void) setAutoSpeedLimit: (id) sender
441{
442    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
443}
444
445- (void) setBadge: (id) sender
446{   
447    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
448}
449
450- (void) resetWarnings: (id) sender
451{
452    [fDefaults setBool: YES forKey: @"WarningDebug"];
453    [fDefaults setBool: YES forKey: @"WarningDuplicate"];
454    [fDefaults setBool: YES forKey: @"WarningRemainingSpace"];
455}
456
457- (void) setCheckForUpdate: (id) sender
458{
459    NSTimeInterval seconds = [fDefaults boolForKey: @"CheckForUpdates"] ? UPDATE_SECONDS : 0;
460    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
461    if (fUpdater)
462        [fUpdater scheduleCheckWithInterval: seconds];
463}
464
465- (void) setQueue: (id) sender
466{
467    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
468}
469
470- (void) setQueueNumber: (id) sender
471{
472    BOOL download = sender == fQueueDownloadField;
473   
474    int limit = [sender intValue];
475    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 1)
476    {
477        NSBeep();
478        [sender setIntValue: [fDefaults integerForKey: download ? @"QueueDownloadNumber" : @"QueueSeedNumber"]];
479        return;
480    }
481   
482    [fDefaults setInteger: limit forKey: download ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
483    [self setQueue: nil];
484}
485
486- (void) setStalled: (id) sender
487{
488    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: self];
489}
490
491- (void) setStalledMinutes: (id) sender
492{
493    int minutes = [sender intValue];
494    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", minutes]] || minutes < 1)
495    {
496        NSBeep();
497        [sender setIntValue: [fDefaults integerForKey: @"StalledMinutes"]];
498        return;
499    }
500   
501    [fDefaults setInteger: minutes forKey: @"StalledMinutes"];
502    [self setStalled: nil];
503}
504
505- (void) setDownloadLocation: (id) sender
506{
507    switch ([fFolderPopUp indexOfSelectedItem])
508    {
509        case DOWNLOAD_FOLDER:
510            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
511            break;
512        case DOWNLOAD_TORRENT:
513            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
514            break;
515        case DOWNLOAD_ASK:
516            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
517            break;
518    }
519}
520
521- (void) folderSheetShow: (id) sender
522{
523    NSOpenPanel * panel = [NSOpenPanel openPanel];
524
525    [panel setPrompt: @"Select"];
526    [panel setAllowsMultipleSelection: NO];
527    [panel setCanChooseFiles: NO];
528    [panel setCanChooseDirectories: YES];
529    [panel setCanCreateDirectories: YES];
530
531    [panel beginSheetForDirectory: nil file: nil types: nil
532        modalForWindow: [self window] modalDelegate: self didEndSelector:
533        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
534}
535
536- (void) incompleteFolderSheetShow: (id) sender
537{
538    NSOpenPanel * panel = [NSOpenPanel openPanel];
539
540    [panel setPrompt: @"Select"];
541    [panel setAllowsMultipleSelection: NO];
542    [panel setCanChooseFiles: NO];
543    [panel setCanChooseDirectories: YES];
544    [panel setCanCreateDirectories: YES];
545
546    [panel beginSheetForDirectory: nil file: nil types: nil
547        modalForWindow: [self window] modalDelegate: self didEndSelector:
548        @selector(incompleteFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
549}
550
551- (void) setAutoImport: (id) sender
552{
553    NSString * path;
554    if ((path = [fDefaults stringForKey: @"AutoImportDirectory"]))
555    {
556        path = [path stringByExpandingTildeInPath];
557        if ([fDefaults boolForKey: @"AutoImport"])
558            [[UKKQueue sharedFileWatcher] addPath: path];
559        else
560            [[UKKQueue sharedFileWatcher] removePathFromQueue: path];
561       
562        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
563    }
564    else
565        [self importFolderSheetShow: nil];
566}
567
568- (void) importFolderSheetShow: (id) sender
569{
570    NSOpenPanel * panel = [NSOpenPanel openPanel];
571
572    [panel setPrompt: @"Select"];
573    [panel setAllowsMultipleSelection: NO];
574    [panel setCanChooseFiles: NO];
575    [panel setCanChooseDirectories: YES];
576    [panel setCanCreateDirectories: YES];
577
578    [panel beginSheetForDirectory: nil file: nil types: nil
579        modalForWindow: [self window] modalDelegate: self didEndSelector:
580        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
581}
582
583- (void) setAutoSize: (id) sender
584{
585    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
586}
587
588- (void) helpForNetwork: (id) sender
589{
590    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PortForwarding"
591        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
592}
593
594@end
595
596@implementation PrefsController (Private)
597
598- (void) setPrefView: (id) sender
599{
600    NSView * view = fGeneralView;
601    if (sender)
602    {
603        NSString * identifier = [sender itemIdentifier];
604        if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
605            view = fTransfersView;
606        else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
607            view = fBandwidthView;
608        else if ([identifier isEqualToString: TOOLBAR_ADVANCED])
609            view = fAdvancedView;
610        else;
611    }
612   
613    NSWindow * window = [self window];
614    if ([window contentView] == view)
615        return;
616   
617    NSRect windowRect = [window frame];
618    float difference = ([view frame].size.height - [[window contentView] frame].size.height) * [window userSpaceScaleFactor];
619    windowRect.origin.y -= difference;
620    windowRect.size.height += difference;
621   
622    [view setHidden: YES];
623    [window setContentView: view];
624    [window setFrame: windowRect display: YES animate: YES];
625    [view setHidden: NO];
626   
627    //set title label
628    if (sender)
629        [window setTitle: [sender label]];
630    else
631    {
632        NSToolbar * toolbar = [window toolbar];
633        NSString * itemIdentifier = [toolbar selectedItemIdentifier];
634        NSEnumerator * enumerator = [[toolbar items] objectEnumerator];
635        NSToolbarItem * item;
636        while ((item = [enumerator nextObject]))
637            if ([[item itemIdentifier] isEqualToString: itemIdentifier])
638            {
639                [window setTitle: [item label]];
640                break;
641            }
642    }
643   
644    //for advanced view make sure progress indicator hides itself
645    if (view == fAdvancedView && [fPortStatusImage image])
646        [fPortStatusProgress setDisplayedWhenStopped: NO];
647}
648
649- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
650{
651    if (code == NSOKButton)
652    {
653        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
654        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"DownloadFolder"];
655        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
656    }
657    else
658    {
659        //reset if cancelled
660        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
661        if ([downloadChoice isEqualToString: @"Constant"])
662            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
663        else if ([downloadChoice isEqualToString: @"Torrent"])
664            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
665        else
666            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
667    }
668}
669
670- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
671{
672    if (code == NSOKButton)
673        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"IncompleteDownloadFolder"];
674    [fIncompleteFolderPopUp selectItemAtIndex: 0];
675}
676
677- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
678{
679    NSString * path = [fDefaults stringForKey: @"AutoImportDirectory"];
680    if (code == NSOKButton)
681    {
682        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
683        if (path)
684            [sharedQueue removePathFromQueue: [path stringByExpandingTildeInPath]];
685       
686        path = [[openPanel filenames] objectAtIndex: 0];
687        [fDefaults setObject: path forKey: @"AutoImportDirectory"];
688        [sharedQueue addPath: [path stringByExpandingTildeInPath]];
689       
690        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
691    }
692    else if (!path)
693        [fDefaults setBool: NO forKey: @"AutoImport"];
694   
695    [fImportFolderPopUp selectItemAtIndex: 0];
696}
697
698@end
Note: See TracBrowser for help on using the repository browser.