source: trunk/macosx/PrefsController.m @ 3326

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

switch-ify more code

  • Property svn:keywords set to Date Rev Author Id
File size: 23.7 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 3326 2007-10-08 04:28:15Z 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        switch (fNatStatus)
248        {
249            case TR_NAT_TRAVERSAL_MAPPED:
250                [fNatStatusField setStringValue: NSLocalizedString(@"Port successfully mapped",
251                                                    "Preferences -> Advanced -> port map status")];
252                [fNatStatusImage setImage: [NSImage imageNamed: @"GreenDot.tiff"]];
253                break;
254           
255            case TR_NAT_TRAVERSAL_NOTFOUND:
256            case TR_NAT_TRAVERSAL_ERROR:
257                [fNatStatusField setStringValue: NSLocalizedString(@"Error mapping port",
258                                                    "Preferences -> Advanced -> port map status")];
259                [fNatStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
260                break;
261           
262            default:
263                [fNatStatusField setStringValue: @""];
264                [fNatStatusImage setImage: nil];
265        }
266    }
267   
268    if (change || fPublicPort != stat->publicPort)
269    {
270        fPublicPort = stat->publicPort;
271       
272        [fPortStatusField setStringValue: [NSLocalizedString(@"Checking port status",
273                                            "Preferences -> Advanced -> port status") stringByAppendingEllipsis]];
274        [fPortStatusImage setImage: nil];
275        [fPortStatusProgress startAnimation: self];
276       
277        PortChecker * portChecker = [[PortChecker alloc] initWithDelegate: self];
278        [portChecker probePort: fPublicPort];
279    }
280}
281
282- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
283{
284    [fPortStatusProgress stopAnimation: self];
285    switch ([portChecker status])
286    {
287        case PORT_STATUS_OPEN:
288            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Advanced -> port status")];
289            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.tiff"]];
290            break;
291        case PORT_STATUS_STEALTH:
292            [fPortStatusField setStringValue: NSLocalizedString(@"Port is stealth", "Preferences -> Advanced -> port status")];
293            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
294            break;
295        case PORT_STATUS_CLOSED:
296            [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Advanced -> port status")];
297            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
298            break;
299        case PORT_STATUS_ERROR:
300            [fPortStatusField setStringValue: NSLocalizedString(@"Unable to check port status",
301                                                "Preferences -> Advanced -> port status")];
302            [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.tiff"]];
303            break;
304    }
305    [portChecker release];
306}
307
308- (NSArray *) sounds
309{
310    NSMutableArray * sounds = [[NSMutableArray alloc] init];
311   
312    NSArray * directories = [NSArray arrayWithObjects: @"/System/Library/Sounds", @"/Library/Sounds",
313                                [NSHomeDirectory() stringByAppendingPathComponent: @"Library/Sounds"], nil];
314    BOOL isDirectory;
315    NSEnumerator * soundEnumerator;
316    NSString * sound;
317   
318    NSString * directory;
319    NSEnumerator * enumerator = [directories objectEnumerator];
320    while ((directory = [enumerator nextObject]))
321        if ([[NSFileManager defaultManager] fileExistsAtPath: directory isDirectory: &isDirectory] && isDirectory)
322        {
323                soundEnumerator = [[[NSFileManager defaultManager] directoryContentsAtPath: directory] objectEnumerator];
324                while ((sound = [soundEnumerator nextObject]))
325                {
326                    sound = [sound stringByDeletingPathExtension];
327                    if ([NSSound soundNamed: sound])
328                        [sounds addObject: sound];
329                }
330        }
331   
332    return sounds;
333}
334
335- (void) setSound: (id) sender
336{
337    //play sound when selecting
338    NSSound * sound;
339    if ((sound = [NSSound soundNamed: [sender titleOfSelectedItem]]))
340        [sound play];
341}
342
343- (void) setEncryptionRequired: (id) sender
344{
345    tr_setEncryptionMode(fHandle, [fDefaults boolForKey: @"EncryptionRequire"] ? TR_ENCRYPTION_REQUIRED : TR_ENCRYPTION_PREFERRED);
346}
347
348- (void) applySpeedSettings: (id) sender
349{
350    if ([fDefaults boolForKey: @"SpeedLimit"])
351    {
352        tr_setUseGlobalSpeedLimit(fHandle, TR_UP, 1);
353        tr_setGlobalSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
354       
355        tr_setUseGlobalSpeedLimit(fHandle, TR_DOWN, 1);
356        tr_setGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
357    }
358    else
359    {
360        tr_setUseGlobalSpeedLimit(fHandle, TR_UP, [fDefaults boolForKey: @"CheckUpload"]);
361        tr_setGlobalSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"UploadLimit"]);
362       
363        tr_setUseGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults boolForKey: @"CheckDownload"]);
364        tr_setGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"DownloadLimit"]);
365    }
366}
367
368- (void) applyRatioSetting: (id) sender
369{
370    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
371}
372
373- (void) updateRatioStopField
374{
375    if (!fHasLoaded)
376        return;
377   
378    [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
379   
380    [self applyRatioSetting: nil];
381}
382
383- (void) setRatioStop: (id) sender
384{
385    float ratio = [sender floatValue];
386    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratio]] || ratio < 0)
387    {
388        NSBeep();
389        [sender setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
390        return;
391    }
392   
393    [fDefaults setFloat: ratio forKey: @"RatioLimit"];
394   
395    [self applyRatioSetting: nil];
396}
397
398- (void) updateLimitFields
399{
400    if (!fHasLoaded)
401        return;
402   
403    [fUploadField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
404    [fDownloadField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
405}
406
407- (void) setGlobalLimit: (id) sender
408{
409    BOOL upload = sender == fUploadField;
410   
411    int limit = [sender intValue];
412    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
413    {
414        NSBeep();
415        [sender setIntValue: [fDefaults integerForKey: upload ? @"UploadLimit" : @"DownloadLimit"]];
416        return;
417    }
418   
419    [fDefaults setInteger: limit forKey: upload ? @"UploadLimit" : @"DownloadLimit"];
420   
421    [self applySpeedSettings: self];
422}
423
424- (void) setSpeedLimit: (id) sender
425{
426    BOOL upload = sender == fSpeedLimitUploadField;
427   
428    int limit = [sender intValue];
429    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]])
430    {
431        NSBeep();
432        [sender setIntValue: [fDefaults integerForKey: upload ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"]];
433        return;
434    }
435   
436    [fDefaults setInteger: limit forKey: upload ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"];
437   
438    [self applySpeedSettings: self];
439}
440
441- (void) setAutoSpeedLimit: (id) sender
442{
443    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
444}
445
446- (void) setBadge: (id) sender
447{   
448    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
449}
450
451- (void) resetWarnings: (id) sender
452{
453    [fDefaults setBool: YES forKey: @"WarningDebug"];
454    [fDefaults setBool: YES forKey: @"WarningDuplicate"];
455    [fDefaults setBool: YES forKey: @"WarningRemainingSpace"];
456}
457
458- (void) setCheckForUpdate: (id) sender
459{
460    NSTimeInterval seconds = [fDefaults boolForKey: @"CheckForUpdates"] ? UPDATE_SECONDS : 0;
461    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
462    if (fUpdater)
463        [fUpdater scheduleCheckWithInterval: seconds];
464}
465
466- (void) setQueue: (id) sender
467{
468    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
469}
470
471- (void) setQueueNumber: (id) sender
472{
473    BOOL download = sender == fQueueDownloadField;
474   
475    int limit = [sender intValue];
476    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 1)
477    {
478        NSBeep();
479        [sender setIntValue: [fDefaults integerForKey: download ? @"QueueDownloadNumber" : @"QueueSeedNumber"]];
480        return;
481    }
482   
483    [fDefaults setInteger: limit forKey: download ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
484    [self setQueue: nil];
485}
486
487- (void) setStalled: (id) sender
488{
489    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: self];
490}
491
492- (void) setStalledMinutes: (id) sender
493{
494    int minutes = [sender intValue];
495    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", minutes]] || minutes < 1)
496    {
497        NSBeep();
498        [sender setIntValue: [fDefaults integerForKey: @"StalledMinutes"]];
499        return;
500    }
501   
502    [fDefaults setInteger: minutes forKey: @"StalledMinutes"];
503    [self setStalled: nil];
504}
505
506- (void) setDownloadLocation: (id) sender
507{
508    switch ([fFolderPopUp indexOfSelectedItem])
509    {
510        case DOWNLOAD_FOLDER:
511            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
512            break;
513        case DOWNLOAD_TORRENT:
514            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
515            break;
516        case DOWNLOAD_ASK:
517            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
518            break;
519    }
520}
521
522- (void) folderSheetShow: (id) sender
523{
524    NSOpenPanel * panel = [NSOpenPanel openPanel];
525
526    [panel setPrompt: @"Select"];
527    [panel setAllowsMultipleSelection: NO];
528    [panel setCanChooseFiles: NO];
529    [panel setCanChooseDirectories: YES];
530    [panel setCanCreateDirectories: YES];
531
532    [panel beginSheetForDirectory: nil file: nil types: nil
533        modalForWindow: [self window] modalDelegate: self didEndSelector:
534        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
535}
536
537- (void) incompleteFolderSheetShow: (id) sender
538{
539    NSOpenPanel * panel = [NSOpenPanel openPanel];
540
541    [panel setPrompt: @"Select"];
542    [panel setAllowsMultipleSelection: NO];
543    [panel setCanChooseFiles: NO];
544    [panel setCanChooseDirectories: YES];
545    [panel setCanCreateDirectories: YES];
546
547    [panel beginSheetForDirectory: nil file: nil types: nil
548        modalForWindow: [self window] modalDelegate: self didEndSelector:
549        @selector(incompleteFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
550}
551
552- (void) setAutoImport: (id) sender
553{
554    NSString * path;
555    if ((path = [fDefaults stringForKey: @"AutoImportDirectory"]))
556    {
557        path = [path stringByExpandingTildeInPath];
558        if ([fDefaults boolForKey: @"AutoImport"])
559            [[UKKQueue sharedFileWatcher] addPath: path];
560        else
561            [[UKKQueue sharedFileWatcher] removePathFromQueue: path];
562       
563        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
564    }
565    else
566        [self importFolderSheetShow: nil];
567}
568
569- (void) importFolderSheetShow: (id) sender
570{
571    NSOpenPanel * panel = [NSOpenPanel openPanel];
572
573    [panel setPrompt: @"Select"];
574    [panel setAllowsMultipleSelection: NO];
575    [panel setCanChooseFiles: NO];
576    [panel setCanChooseDirectories: YES];
577    [panel setCanCreateDirectories: YES];
578
579    [panel beginSheetForDirectory: nil file: nil types: nil
580        modalForWindow: [self window] modalDelegate: self didEndSelector:
581        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
582}
583
584- (void) setAutoSize: (id) sender
585{
586    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
587}
588
589- (void) helpForNetwork: (id) sender
590{
591    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PortForwarding"
592        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
593}
594
595@end
596
597@implementation PrefsController (Private)
598
599- (void) setPrefView: (id) sender
600{
601    NSView * view = fGeneralView;
602    if (sender)
603    {
604        NSString * identifier = [sender itemIdentifier];
605        if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
606            view = fTransfersView;
607        else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
608            view = fBandwidthView;
609        else if ([identifier isEqualToString: TOOLBAR_ADVANCED])
610            view = fAdvancedView;
611        else;
612    }
613   
614    NSWindow * window = [self window];
615    if ([window contentView] == view)
616        return;
617   
618    NSRect windowRect = [window frame];
619    float difference = ([view frame].size.height - [[window contentView] frame].size.height) * [window userSpaceScaleFactor];
620    windowRect.origin.y -= difference;
621    windowRect.size.height += difference;
622   
623    [view setHidden: YES];
624    [window setContentView: view];
625    [window setFrame: windowRect display: YES animate: YES];
626    [view setHidden: NO];
627   
628    //set title label
629    if (sender)
630        [window setTitle: [sender label]];
631    else
632    {
633        NSToolbar * toolbar = [window toolbar];
634        NSString * itemIdentifier = [toolbar selectedItemIdentifier];
635        NSEnumerator * enumerator = [[toolbar items] objectEnumerator];
636        NSToolbarItem * item;
637        while ((item = [enumerator nextObject]))
638            if ([[item itemIdentifier] isEqualToString: itemIdentifier])
639            {
640                [window setTitle: [item label]];
641                break;
642            }
643    }
644   
645    //for advanced view make sure progress indicator hides itself
646    if (view == fAdvancedView && [fPortStatusImage image])
647        [fPortStatusProgress setDisplayedWhenStopped: NO];
648}
649
650- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
651{
652    if (code == NSOKButton)
653    {
654        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
655        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"DownloadFolder"];
656        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
657    }
658    else
659    {
660        //reset if cancelled
661        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
662        if ([downloadChoice isEqualToString: @"Constant"])
663            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
664        else if ([downloadChoice isEqualToString: @"Torrent"])
665            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
666        else
667            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
668    }
669}
670
671- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
672{
673    if (code == NSOKButton)
674        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"IncompleteDownloadFolder"];
675    [fIncompleteFolderPopUp selectItemAtIndex: 0];
676}
677
678- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
679{
680    NSString * path = [fDefaults stringForKey: @"AutoImportDirectory"];
681    if (code == NSOKButton)
682    {
683        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
684        if (path)
685            [sharedQueue removePathFromQueue: [path stringByExpandingTildeInPath]];
686       
687        path = [[openPanel filenames] objectAtIndex: 0];
688        [fDefaults setObject: path forKey: @"AutoImportDirectory"];
689        [sharedQueue addPath: [path stringByExpandingTildeInPath]];
690       
691        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
692    }
693    else if (!path)
694        [fDefaults setBool: NO forKey: @"AutoImport"];
695   
696    [fImportFolderPopUp selectItemAtIndex: 0];
697}
698
699@end
Note: See TracBrowser for help on using the repository browser.