source: trunk/macosx/PrefsController.m @ 733

Last change on this file since 733 was 733, checked in by livings124, 16 years ago

Option to play sound when downloads finish (for now the sound is "Glass").

  • Property svn:keywords set to Date Rev Author Id
File size: 24.0 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 733 2006-08-07 00:46:47Z livings124 $
3 *
4 * Copyright (c) 2005-2006 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 "StringAdditions.h"
27
28#define MIN_PORT    1
29#define MAX_PORT    65535
30
31#define DOWNLOAD_FOLDER     0
32#define DOWNLOAD_TORRENT    2
33#define DOWNLOAD_ASK        3
34
35#define START_YES_CHECK_TAG     0
36#define START_WAIT_CHECK_TAG    1
37#define START_NO_CHECK_TAG      2
38
39#define UPDATE_DAILY    0
40#define UPDATE_WEEKLY   1
41#define UPDATE_NEVER    2
42
43#define TOOLBAR_GENERAL     @"General"
44#define TOOLBAR_TRANSFERS   @"Transfers"
45#define TOOLBAR_BANDWIDTH   @"Bandwidth"
46#define TOOLBAR_NETWORK     @"Network"
47
48@interface PrefsController (Private)
49
50- (void) showGeneralPref: (id) sender;
51- (void) showTransfersPref: (id) sender;
52- (void) showBandwidthPref: (id) sender;
53- (void) showNetworkPref: (id) sender;
54
55- (void) setPrefView: (NSView *) view;
56
57- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
58- (void) updatePopUp;
59
60- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
61- (void) updateImportPopUp;
62
63@end
64
65@implementation PrefsController
66
67+ (void) initialize
68{
69    [[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithContentsOfFile:
70        [[NSBundle mainBundle] pathForResource: @"Defaults" ofType: @"plist"]]];
71}
72
73- (id) initWithWindowNibName: (NSString *) name handle: (tr_handle_t *) handle
74{
75    if ((self = [self initWithWindowNibName: name]))
76    {
77        fDefaults = [NSUserDefaults standardUserDefaults];
78        fHandle = handle;
79       
80        [[self window] update]; //make sure nib is loaded right away
81    }
82    return self;
83}
84
85- (void) dealloc
86{
87    [fDownloadFolder release];
88    [super dealloc];
89}
90
91- (void) awakeFromNib
92{
93    fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
94    [fToolbar setDelegate: self];
95    [fToolbar setAllowsUserCustomization: NO];
96    [[self window] setToolbar: fToolbar];
97    [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
98    [fToolbar setSizeMode: NSToolbarSizeModeRegular];
99
100    [fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
101    [self showGeneralPref: nil];
102   
103    //set download folder
104    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
105    fDownloadFolder = [[[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath] retain];
106    if ([downloadChoice isEqualToString: @"Constant"])
107        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
108    else if ([downloadChoice isEqualToString: @"Torrent"])
109        [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
110    else
111        [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
112    [self updatePopUp];
113   
114    //set auto import
115    fImportFolder = [[[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath] retain];
116    [self updateImportPopUp];
117 
118    BOOL autoImport = [fDefaults boolForKey: @"AutoImport"];
119    [fAutoImportCheck setState: autoImport];
120    [fImportFolderPopUp setEnabled: autoImport];
121   
122    //set auto size
123    [fAutoSizeCheck setState: [fDefaults boolForKey: @"AutoSize"]];
124   
125    //set bind port
126    int bindPort = [fDefaults integerForKey: @"BindPort"];
127    [fPortField setIntValue: bindPort];
128    tr_setBindPort(fHandle, bindPort);
129   
130    //checks for old version upload speed of -1
131    if ([fDefaults integerForKey: @"UploadLimit"] < 0)
132    {
133        [fDefaults setInteger: 20 forKey: @"UploadLimit"];
134        [fDefaults setBool: NO forKey: @"CheckUpload"];
135    }
136   
137    //set upload limit
138    BOOL checkUpload = [fDefaults boolForKey: @"CheckUpload"];
139    int uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
140   
141    [fUploadCheck setState: checkUpload];
142    [fUploadField setIntValue: uploadLimit];
143    [fUploadField setEnabled: checkUpload];
144
145        //set download limit
146    BOOL checkDownload = [fDefaults boolForKey: @"CheckDownload"];
147    int downloadLimit = [fDefaults integerForKey: @"DownloadLimit"];
148   
149    [fDownloadCheck setState: checkDownload];
150    [fDownloadField setIntValue: downloadLimit];
151    [fDownloadField setEnabled: checkDownload];
152   
153    //set speed limit
154    int speedLimitUploadLimit = [fDefaults integerForKey: @"SpeedLimitUploadLimit"];
155    [fSpeedLimitUploadField setIntValue: speedLimitUploadLimit];
156   
157    int speedLimitDownloadLimit = [fDefaults integerForKey: @"SpeedLimitDownloadLimit"];
158    [fSpeedLimitDownloadField setIntValue: speedLimitDownloadLimit];
159   
160    //actually set bandwidth limits
161    if ([fDefaults boolForKey: @"SpeedLimit"])
162    {
163        tr_setUploadLimit(fHandle, speedLimitUploadLimit);
164        tr_setDownloadLimit(fHandle, speedLimitDownloadLimit);
165    }
166    else
167    {
168        tr_setUploadLimit(fHandle, checkUpload ? uploadLimit : -1);
169        tr_setDownloadLimit(fHandle, checkDownload ? downloadLimit : -1);
170    }
171   
172    //set ratio limit
173    BOOL ratioCheck = [fDefaults boolForKey: @"RatioCheck"];
174    [fRatioCheck setState: ratioCheck];
175    [fRatioField setEnabled: ratioCheck];
176    [fRatioField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
177   
178    //set remove and quit prompts
179    BOOL isQuitCheck = [fDefaults boolForKey: @"CheckQuit"],
180        isRemoveCheck = [fDefaults boolForKey: @"CheckRemove"];
181   
182    [fQuitCheck setState: isQuitCheck];
183    [fRemoveCheck setState: isRemoveCheck];
184   
185    [fQuitDownloadingCheck setState: [fDefaults boolForKey: @"CheckQuitDownloading"]];
186    [fQuitDownloadingCheck setEnabled: isQuitCheck];
187    [fRemoveDownloadingCheck setState: [fDefaults boolForKey: @"CheckRemoveDownloading"]];
188    [fRemoveDownloadingCheck setEnabled: isRemoveCheck];
189
190    //set dock badging
191    [fBadgeDownloadRateCheck setState: [fDefaults boolForKey: @"BadgeDownloadRate"]];
192    [fBadgeUploadRateCheck setState: [fDefaults boolForKey: @"BadgeUploadRate"]];
193   
194    //set play sound
195    [fPlayDownloadSoundCheck setState: [fDefaults boolForKey: @"PlayDownloadSound"]];
196   
197    //set start setting
198    NSString * startSetting = [fDefaults stringForKey: @"StartSetting"];
199    int tag;
200    if ([startSetting isEqualToString: @"Start"])
201        tag = START_YES_CHECK_TAG;
202    else if ([startSetting isEqualToString: @"Wait"])
203        tag = START_WAIT_CHECK_TAG;
204    else
205        tag = START_NO_CHECK_TAG;
206   
207    [fStartMatrix selectCellWithTag: tag];
208    [fWaitToStartField setEnabled: tag == START_WAIT_CHECK_TAG];
209    [fWaitToStartField setIntValue: [fDefaults integerForKey: @"WaitToStartNumber"]];
210   
211    //set private torrents
212    BOOL copyTorrents = [fDefaults boolForKey: @"SavePrivateTorrent"];
213    [fCopyTorrentCheck setState: copyTorrents];
214   
215    [fDeleteOriginalTorrentCheck setEnabled: copyTorrents];
216    [fDeleteOriginalTorrentCheck setState: [fDefaults boolForKey: @"DeleteOriginalTorrent"]];
217
218    //set update check
219    NSString * updateCheck = [fDefaults stringForKey: @"UpdateCheck"];
220    if ([updateCheck isEqualToString: @"Weekly"])
221        [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
222    else if ([updateCheck isEqualToString: @"Never"])
223        [fUpdatePopUp selectItemAtIndex: UPDATE_NEVER];
224    else
225        [fUpdatePopUp selectItemAtIndex: UPDATE_DAILY];
226}
227
228- (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier:
229    (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
230{
231    NSToolbarItem * item;
232    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
233
234    if ([ident isEqualToString: TOOLBAR_GENERAL])
235    {
236        [item setLabel: TOOLBAR_GENERAL];
237        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
238        [item setTarget: self];
239        [item setAction: @selector(showGeneralPref:)];
240    }
241    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
242    {
243        [item setLabel: TOOLBAR_TRANSFERS];
244        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
245        [item setTarget: self];
246        [item setAction: @selector(showTransfersPref:)];
247    }
248    else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
249    {
250        [item setLabel: TOOLBAR_BANDWIDTH];
251        [item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
252        [item setTarget: self];
253        [item setAction: @selector(showBandwidthPref:)];
254    }
255    else if ([ident isEqualToString: TOOLBAR_NETWORK])
256    {
257        [item setLabel: TOOLBAR_NETWORK];
258        [item setImage: [NSImage imageNamed: @"Network.png"]];
259        [item setTarget: self];
260        [item setAction: @selector(showNetworkPref:)];
261    }
262    else
263    {
264        [item release];
265        return nil;
266    }
267
268    return item;
269}
270
271- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
272{
273    return [self toolbarDefaultItemIdentifiers: toolbar];
274}
275
276- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
277{
278    return [self toolbarAllowedItemIdentifiers: toolbar];
279}
280
281- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
282{
283    return [NSArray arrayWithObjects:
284            TOOLBAR_GENERAL, TOOLBAR_TRANSFERS,
285            TOOLBAR_BANDWIDTH, TOOLBAR_NETWORK, nil];
286}
287
288- (void) setPort: (id) sender
289{
290    int bindPort = [sender intValue];
291    if (![[NSString stringWithInt: bindPort] isEqualToString: [sender stringValue]]
292            || bindPort < MIN_PORT || bindPort > MAX_PORT)
293    {
294        NSBeep();
295        bindPort = [fDefaults integerForKey: @"BindPort"];
296        [sender setIntValue: bindPort];
297    }
298    else
299    {
300        tr_setBindPort(fHandle, bindPort);
301        [fDefaults setInteger: bindPort forKey: @"BindPort"];
302    }
303}
304
305- (void) setLimit: (id) sender
306{
307    NSString * key;
308    NSButton * check;
309    NSString * type;
310    if (sender == fUploadField)
311    {
312        key = @"UploadLimit";
313        check = fUploadCheck;
314        type = @"Upload";
315    }
316    else
317    {
318        key = @"DownloadLimit";
319        check = fDownloadCheck;
320        type = @"Download";
321    }
322
323    int limit = [sender intValue];
324    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
325    {
326        NSBeep();
327        limit = [fDefaults integerForKey: key];
328        [sender setIntValue: limit];
329    }
330    else
331    {
332        if (![fDefaults boolForKey: @"SpeedLimit"])
333        {
334            if (sender == fUploadField)
335                tr_setUploadLimit(fHandle, [fUploadCheck state] ? limit : -1);
336            else
337                tr_setDownloadLimit(fHandle, [fDownloadCheck state] ? limit : -1);
338        }
339       
340        [fDefaults setInteger: limit forKey: key];
341    }
342   
343    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
344                                    [NSNumber numberWithBool: [check state]], @"Enable",
345                                    [NSNumber numberWithInt: limit], @"Limit",
346                                    type, @"Type", nil];
347    [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange" object: dict];
348}
349
350- (void) setLimitCheck: (id) sender
351{
352    NSString * key;
353    NSTextField * field;
354    if (sender == fUploadCheck)
355    {
356        key = @"CheckUpload";
357        field = fUploadField;
358    }
359    else
360    {
361        key = @"CheckDownload";
362        field = fDownloadField;
363    }
364   
365    BOOL check = [sender state] == NSOnState;
366    [self setLimit: field];
367    [field setEnabled: check];
368   
369    [fDefaults setBool: check forKey: key];
370}
371
372- (void) setLimitEnabled: (BOOL) enable type: (NSString *) type
373{
374    NSButton * check = [type isEqualToString: @"Upload"] ? fUploadCheck : fDownloadCheck;
375    [check setState: enable ? NSOnState : NSOffState];
376    [self setLimitCheck: check];
377}
378
379- (void) setQuickLimit: (int) limit type: (NSString *) type
380{
381    NSButton * check;
382    if ([type isEqualToString: @"Upload"])
383    {
384        [fUploadField setIntValue: limit];
385        check = fUploadCheck;
386    }
387    else
388    {
389        [fDownloadField setIntValue: limit];
390        check = fDownloadCheck;
391    }
392    [check setState: NSOnState];
393    [self setLimitCheck: check];
394}
395
396- (void) enableSpeedLimit: (BOOL) enable
397{
398    if ([fDefaults boolForKey: @"SpeedLimit"] != enable)
399    {
400        [fDefaults setBool: enable forKey: @"SpeedLimit"];
401       
402        if (enable)
403        {
404            tr_setUploadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
405            tr_setDownloadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
406        }
407        else
408        {
409            tr_setUploadLimit(fHandle, [fUploadCheck state] ? [fDefaults integerForKey: @"UploadLimit"] : -1);
410            tr_setDownloadLimit(fHandle, [fDownloadCheck state] ? [fDefaults integerForKey: @"DownloadLimit"] : -1);
411        }
412    }
413}
414
415- (void) setSpeedLimit: (id) sender
416{
417    NSString * key = sender == fSpeedLimitUploadField ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit";
418
419    int limit = [sender intValue];
420    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
421    {
422        NSBeep();
423        limit = [fDefaults integerForKey: key];
424        [sender setIntValue: limit];
425    }
426    else
427    {
428        if ([fDefaults boolForKey: @"SpeedLimit"])
429        {
430            if (sender == fSpeedLimitUploadField)
431                tr_setUploadLimit(fHandle, limit);
432            else
433                tr_setDownloadLimit(fHandle, limit);
434        }
435       
436        [fDefaults setInteger: limit forKey: key];
437    }
438}
439
440- (void) setRatio: (id) sender
441{
442    float ratioLimit = [sender floatValue];
443    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
444            || ratioLimit < 0)
445    {
446        NSBeep();
447        ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
448        [sender setFloatValue: ratioLimit];
449    }
450    else
451        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
452   
453    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
454                                [NSNumber numberWithBool: [fRatioCheck state]], @"Enable",
455                                [NSNumber numberWithFloat: ratioLimit], @"Ratio", nil];
456    [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: dict];
457}
458
459- (void) setRatioCheck: (id) sender
460{
461    BOOL check = [sender state] == NSOnState;
462    [self setRatio: fRatioField];
463    [fRatioField setEnabled: check];
464   
465    [fDefaults setBool: check forKey: @"RatioCheck"];
466}
467
468- (void) setRatioEnabled: (BOOL) enable
469{
470    int state = enable ? NSOnState : NSOffState;
471   
472    [fRatioCheck setState: state];
473    [self setRatioCheck: fRatioCheck];
474}
475
476- (void) setQuickRatio: (float) ratioLimit
477{
478    [fRatioField setFloatValue: ratioLimit];
479   
480    [fRatioCheck setState: NSOnState];
481    [self setRatioCheck: fRatioCheck];
482}
483
484- (void) setShowMessage: (id) sender
485{
486    BOOL state = [sender state];
487
488    if (sender == fQuitCheck)
489    {
490        [fDefaults setBool: state forKey: @"CheckQuit"];
491        [fQuitDownloadingCheck setEnabled: state];
492    }
493    else if (sender == fRemoveCheck)
494    {
495        [fDefaults setBool: state forKey: @"CheckRemove"];
496        [fRemoveDownloadingCheck setEnabled: state];
497    }
498    if (sender == fQuitDownloadingCheck)
499        [fDefaults setBool: state forKey: @"CheckQuitDownloading"];
500    else if (sender == fRemoveDownloadingCheck)
501        [fDefaults setBool: state forKey: @"CheckRemoveDownloading"];
502    else;
503}
504
505- (void) setBadge: (id) sender
506{   
507    if (sender == fBadgeDownloadRateCheck)
508        [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
509    else if (sender == fBadgeUploadRateCheck)
510        [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
511    else;
512   
513    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
514}
515
516- (void) setPlaySound: (id) sender
517{
518    [fDefaults setBool: [sender state] forKey: @"PlayDownloadSound"];
519}
520
521- (void) setUpdate: (id) sender
522{
523    int index = [fUpdatePopUp indexOfSelectedItem];
524    NSTimeInterval seconds;
525    if (index == UPDATE_DAILY)
526    {
527        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
528        seconds = 86400;
529    }
530    else if (index == UPDATE_WEEKLY)
531    {
532        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
533        seconds = 604800;
534    }
535    else
536    {
537        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
538        seconds = 0;
539    }
540
541    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
542    [fUpdater scheduleCheckWithInterval: seconds];
543}
544
545- (void) setStartSetting: (id) sender
546{
547    NSString * startSetting;
548
549    int tag = [[fStartMatrix selectedCell] tag];
550    if (tag == START_YES_CHECK_TAG)
551        startSetting = @"Start";
552    else if (tag == START_WAIT_CHECK_TAG)
553        startSetting = @"Wait";
554    else
555        startSetting = @"Manual";
556   
557    [fDefaults setObject: startSetting forKey: @"StartSetting"];
558   
559    [self setWaitToStart: fWaitToStartField];
560    [fWaitToStartField setEnabled: tag == START_WAIT_CHECK_TAG];
561}
562
563- (void) setWaitToStart: (id) sender
564{
565    int waitNumber = [sender intValue];
566    if (![[sender stringValue] isEqualToString: [NSString stringWithInt: waitNumber]] || waitNumber < 1)
567    {
568        NSBeep();
569        waitNumber = [fDefaults floatForKey: @"WaitToStartNumber"];
570        [sender setIntValue: waitNumber];
571    }
572    else
573        [fDefaults setInteger: waitNumber forKey: @"WaitToStartNumber"];
574   
575    [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self];
576}
577
578- (void) setMoveTorrent: (id) sender
579{
580    int state = [sender state];
581    if (sender == fCopyTorrentCheck)
582    {
583        [fDefaults setBool: state forKey: @"SavePrivateTorrent"];
584       
585        [fDeleteOriginalTorrentCheck setEnabled: state];
586        if (state == NSOffState)
587        {
588            [fDeleteOriginalTorrentCheck setState: NSOffState];
589            [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"];
590        }
591    }
592    else
593        [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"];
594}
595
596- (void) setDownloadLocation: (id) sender
597{
598    //Download folder
599    switch ([fFolderPopUp indexOfSelectedItem])
600    {
601        case DOWNLOAD_FOLDER:
602            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
603            break;
604        case DOWNLOAD_TORRENT:
605            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
606            break;
607        case DOWNLOAD_ASK:
608            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
609            break;
610    }
611}
612
613- (void) checkUpdate
614{
615    [fUpdater checkForUpdates: nil];
616}
617
618- (void) folderSheetShow: (id) sender
619{
620    NSOpenPanel * panel = [NSOpenPanel openPanel];
621
622    [panel setPrompt: @"Select"];
623    [panel setAllowsMultipleSelection: NO];
624    [panel setCanChooseFiles: NO];
625    [panel setCanChooseDirectories: YES];
626    [panel setCanCreateDirectories: YES];
627
628    [panel beginSheetForDirectory: nil file: nil types: nil
629        modalForWindow: [self window] modalDelegate: self didEndSelector:
630        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
631}
632
633- (void) setAutoImport: (id) sender
634{
635    int state = [fAutoImportCheck state];
636    [fDefaults setBool: state forKey: @"AutoImport"];
637    [fImportFolderPopUp setEnabled: state];
638   
639    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
640}
641
642- (void) importFolderSheetShow: (id) sender
643{
644    NSOpenPanel * panel = [NSOpenPanel openPanel];
645
646    [panel setPrompt: @"Select"];
647    [panel setAllowsMultipleSelection: NO];
648    [panel setCanChooseFiles: NO];
649    [panel setCanChooseDirectories: YES];
650    [panel setCanCreateDirectories: YES];
651
652    [panel beginSheetForDirectory: nil file: nil types: nil
653        modalForWindow: [self window] modalDelegate: self didEndSelector:
654        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
655}
656
657- (void) setAutoSize: (id) sender
658{
659    [fDefaults setBool: [sender state] forKey: @"AutoSize"];
660   
661    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
662}
663
664- (void) windowWillClose: (NSNotification *) notification
665{
666    [[self window] makeFirstResponder: nil];
667}
668
669@end
670
671@implementation PrefsController (Private)
672
673- (void) showGeneralPref: (id) sender
674{
675    [self setPrefView: fGeneralView];
676}
677
678- (void) showTransfersPref: (id) sender
679{
680    [self setPrefView: fTransfersView];
681}
682
683- (void) showBandwidthPref: (id) sender
684{
685    [self setPrefView: fBandwidthView];
686}
687
688- (void) showNetworkPref: (id) sender
689{
690    [self setPrefView: fNetworkView];
691}
692
693- (void) setPrefView: (NSView *) view
694{
695    NSWindow * window = [self window];
696   
697    NSRect windowRect = [window frame];
698    int difference = [view frame].size.height - [[window contentView] frame].size.height;
699    windowRect.origin.y -= difference;
700    windowRect.size.height += difference;
701
702    [window setTitle: [fToolbar selectedItemIdentifier]];
703   
704    [window setContentView: view];
705    [view setHidden: YES];
706    [window setFrame: windowRect display: YES animate: YES];
707    [view setHidden: NO];
708}
709
710- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
711{
712    if (code == NSOKButton)
713    {
714        [fDownloadFolder release];
715        fDownloadFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
716       
717        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
718        [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
719        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
720       
721        [self updatePopUp];
722    }
723    else
724    {
725        //reset if cancelled
726        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
727        if ([downloadChoice isEqualToString: @"Constant"])
728            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
729        else if ([downloadChoice isEqualToString: @"Torrent"])
730            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
731        else
732            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
733    }
734}
735
736- (void) updatePopUp
737{
738    //get and resize the icon
739    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder];
740    [icon setScalesWhenResized: YES];
741    [icon setSize: NSMakeSize(16.0, 16.0)];
742
743    //update menu item
744    NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
745    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
746    [menuItem setImage: icon];
747}
748
749- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
750{
751   if (code == NSOKButton)
752   {
753       [fImportFolder release];
754       fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
755
756       [fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
757
758       [self updateImportPopUp];
759       
760       [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
761   }
762   [fImportFolderPopUp selectItemAtIndex: 0];
763}
764
765- (void) updateImportPopUp
766{
767    //get and resize the icon
768    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fImportFolder];
769    [icon setScalesWhenResized: YES];
770    [icon setSize: NSMakeSize(16.0, 16.0)];
771
772    //update menu item
773    NSMenuItem * menuItem = (NSMenuItem *) [fImportFolderPopUp itemAtIndex: 0];
774    [menuItem setTitle: [fImportFolder lastPathComponent]];
775    [menuItem setImage: icon];
776}
777
778@end
Note: See TracBrowser for help on using the repository browser.