source: trunk/macosx/PrefsController.m @ 720

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

Make the filter buttons themselves control when they are active/inactive.

  • Property svn:keywords set to Date Rev Author Id
File size: 23.8 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 720 2006-08-05 13:22:30Z 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 start setting
195    NSString * startSetting = [fDefaults stringForKey: @"StartSetting"];
196    int tag;
197    if ([startSetting isEqualToString: @"Start"])
198        tag = START_YES_CHECK_TAG;
199    else if ([startSetting isEqualToString: @"Wait"])
200        tag = START_WAIT_CHECK_TAG;
201    else
202        tag = START_NO_CHECK_TAG;
203   
204    [fStartMatrix selectCellWithTag: tag];
205    [fWaitToStartField setEnabled: tag == START_WAIT_CHECK_TAG];
206    [fWaitToStartField setIntValue: [fDefaults integerForKey: @"WaitToStartNumber"]];
207   
208    //set private torrents
209    BOOL copyTorrents = [fDefaults boolForKey: @"SavePrivateTorrent"];
210    [fCopyTorrentCheck setState: copyTorrents];
211   
212    [fDeleteOriginalTorrentCheck setEnabled: copyTorrents];
213    [fDeleteOriginalTorrentCheck setState: [fDefaults boolForKey: @"DeleteOriginalTorrent"]];
214
215    //set update check
216    NSString * updateCheck = [fDefaults stringForKey: @"UpdateCheck"];
217    if ([updateCheck isEqualToString: @"Weekly"])
218        [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
219    else if ([updateCheck isEqualToString: @"Never"])
220        [fUpdatePopUp selectItemAtIndex: UPDATE_NEVER];
221    else
222        [fUpdatePopUp selectItemAtIndex: UPDATE_DAILY];
223}
224
225- (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier:
226    (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
227{
228    NSToolbarItem * item;
229    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
230
231    if ([ident isEqualToString: TOOLBAR_GENERAL])
232    {
233        [item setLabel: TOOLBAR_GENERAL];
234        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
235        [item setTarget: self];
236        [item setAction: @selector(showGeneralPref:)];
237    }
238    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
239    {
240        [item setLabel: TOOLBAR_TRANSFERS];
241        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
242        [item setTarget: self];
243        [item setAction: @selector(showTransfersPref:)];
244    }
245    else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
246    {
247        [item setLabel: TOOLBAR_BANDWIDTH];
248        [item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
249        [item setTarget: self];
250        [item setAction: @selector(showBandwidthPref:)];
251    }
252    else if ([ident isEqualToString: TOOLBAR_NETWORK])
253    {
254        [item setLabel: TOOLBAR_NETWORK];
255        [item setImage: [NSImage imageNamed: @"Network.png"]];
256        [item setTarget: self];
257        [item setAction: @selector(showNetworkPref:)];
258    }
259    else
260    {
261        [item release];
262        return nil;
263    }
264
265    return item;
266}
267
268- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
269{
270    return [self toolbarDefaultItemIdentifiers: toolbar];
271}
272
273- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
274{
275    return [self toolbarAllowedItemIdentifiers: toolbar];
276}
277
278- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
279{
280    return [NSArray arrayWithObjects:
281            TOOLBAR_GENERAL, TOOLBAR_TRANSFERS,
282            TOOLBAR_BANDWIDTH, TOOLBAR_NETWORK, nil];
283}
284
285- (void) setPort: (id) sender
286{
287    int bindPort = [sender intValue];
288    if (![[NSString stringWithInt: bindPort] isEqualToString: [sender stringValue]]
289            || bindPort < MIN_PORT || bindPort > MAX_PORT)
290    {
291        NSBeep();
292        bindPort = [fDefaults integerForKey: @"BindPort"];
293        [sender setIntValue: bindPort];
294    }
295    else
296    {
297        tr_setBindPort(fHandle, bindPort);
298        [fDefaults setInteger: bindPort forKey: @"BindPort"];
299    }
300}
301
302- (void) setLimit: (id) sender
303{
304    NSString * key;
305    NSButton * check;
306    NSString * type;
307    if (sender == fUploadField)
308    {
309        key = @"UploadLimit";
310        check = fUploadCheck;
311        type = @"Upload";
312    }
313    else
314    {
315        key = @"DownloadLimit";
316        check = fDownloadCheck;
317        type = @"Download";
318    }
319
320    int limit = [sender intValue];
321    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
322    {
323        NSBeep();
324        limit = [fDefaults integerForKey: key];
325        [sender setIntValue: limit];
326    }
327    else
328    {
329        if (![fDefaults boolForKey: @"SpeedLimit"])
330        {
331            if (sender == fUploadField)
332                tr_setUploadLimit(fHandle, [fUploadCheck state] ? limit : -1);
333            else
334                tr_setDownloadLimit(fHandle, [fDownloadCheck state] ? limit : -1);
335        }
336       
337        [fDefaults setInteger: limit forKey: key];
338    }
339   
340    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
341                                    [NSNumber numberWithBool: [check state]], @"Enable",
342                                    [NSNumber numberWithInt: limit], @"Limit",
343                                    type, @"Type", nil];
344    [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange" object: dict];
345}
346
347- (void) setLimitCheck: (id) sender
348{
349    NSString * key;
350    NSTextField * field;
351    if (sender == fUploadCheck)
352    {
353        key = @"CheckUpload";
354        field = fUploadField;
355    }
356    else
357    {
358        key = @"CheckDownload";
359        field = fDownloadField;
360    }
361   
362    BOOL check = [sender state] == NSOnState;
363    [self setLimit: field];
364    [field setEnabled: check];
365   
366    [fDefaults setBool: check forKey: key];
367}
368
369- (void) setLimitEnabled: (BOOL) enable type: (NSString *) type
370{
371    NSButton * check = [type isEqualToString: @"Upload"] ? fUploadCheck : fDownloadCheck;
372    [check setState: enable ? NSOnState : NSOffState];
373    [self setLimitCheck: check];
374}
375
376- (void) setQuickLimit: (int) limit type: (NSString *) type
377{
378    NSButton * check;
379    if ([type isEqualToString: @"Upload"])
380    {
381        [fUploadField setIntValue: limit];
382        check = fUploadCheck;
383    }
384    else
385    {
386        [fDownloadField setIntValue: limit];
387        check = fDownloadCheck;
388    }
389    [check setState: NSOnState];
390    [self setLimitCheck: check];
391}
392
393- (void) enableSpeedLimit: (BOOL) enable
394{
395    if ([fDefaults boolForKey: @"SpeedLimit"] != enable)
396    {
397        [fDefaults setBool: enable forKey: @"SpeedLimit"];
398       
399        if (enable)
400        {
401            tr_setUploadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
402            tr_setDownloadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
403        }
404        else
405        {
406            tr_setUploadLimit(fHandle, [fUploadCheck state] ? [fDefaults integerForKey: @"UploadLimit"] : -1);
407            tr_setDownloadLimit(fHandle, [fDownloadCheck state] ? [fDefaults integerForKey: @"DownloadLimit"] : -1);
408        }
409    }
410}
411
412- (void) setSpeedLimit: (id) sender
413{
414    NSString * key = sender == fSpeedLimitUploadField ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit";
415
416    int limit = [sender intValue];
417    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
418    {
419        NSBeep();
420        limit = [fDefaults integerForKey: key];
421        [sender setIntValue: limit];
422    }
423    else
424    {
425        if ([fDefaults boolForKey: @"SpeedLimit"])
426        {
427            if (sender == fSpeedLimitUploadField)
428                tr_setUploadLimit(fHandle, limit);
429            else
430                tr_setDownloadLimit(fHandle, limit);
431        }
432       
433        [fDefaults setInteger: limit forKey: key];
434    }
435}
436
437- (void) setRatio: (id) sender
438{
439    float ratioLimit = [sender floatValue];
440    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
441            || ratioLimit < 0)
442    {
443        NSBeep();
444        ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
445        [sender setFloatValue: ratioLimit];
446    }
447    else
448        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
449   
450    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
451                                [NSNumber numberWithBool: [fRatioCheck state]], @"Enable",
452                                [NSNumber numberWithFloat: ratioLimit], @"Ratio", nil];
453    [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: dict];
454}
455
456- (void) setRatioCheck: (id) sender
457{
458    BOOL check = [sender state] == NSOnState;
459    [self setRatio: fRatioField];
460    [fRatioField setEnabled: check];
461   
462    [fDefaults setBool: check forKey: @"RatioCheck"];
463}
464
465- (void) setRatioEnabled: (BOOL) enable
466{
467    int state = enable ? NSOnState : NSOffState;
468   
469    [fRatioCheck setState: state];
470    [self setRatioCheck: fRatioCheck];
471}
472
473- (void) setQuickRatio: (float) ratioLimit
474{
475    [fRatioField setFloatValue: ratioLimit];
476   
477    [fRatioCheck setState: NSOnState];
478    [self setRatioCheck: fRatioCheck];
479}
480
481- (void) setShowMessage: (id) sender
482{
483    BOOL state = [sender state];
484
485    if (sender == fQuitCheck)
486    {
487        [fDefaults setBool: state forKey: @"CheckQuit"];
488        [fQuitDownloadingCheck setEnabled: state];
489    }
490    else if (sender == fRemoveCheck)
491    {
492        [fDefaults setBool: state forKey: @"CheckRemove"];
493        [fRemoveDownloadingCheck setEnabled: state];
494    }
495    if (sender == fQuitDownloadingCheck)
496        [fDefaults setBool: state forKey: @"CheckQuitDownloading"];
497    else if (sender == fRemoveDownloadingCheck)
498        [fDefaults setBool: state forKey: @"CheckRemoveDownloading"];
499    else;
500}
501
502- (void) setBadge: (id) sender
503{   
504    if (sender == fBadgeDownloadRateCheck)
505        [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
506    else if (sender == fBadgeUploadRateCheck)
507        [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
508    else;
509   
510    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
511}
512
513- (void) setUpdate: (id) sender
514{
515    int index = [fUpdatePopUp indexOfSelectedItem];
516    NSTimeInterval seconds;
517    if (index == UPDATE_DAILY)
518    {
519        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
520        seconds = 86400;
521    }
522    else if (index == UPDATE_WEEKLY)
523    {
524        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
525        seconds = 604800;
526    }
527    else
528    {
529        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
530        seconds = 0;
531    }
532
533    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
534    [fUpdater scheduleCheckWithInterval: seconds];
535}
536
537- (void) setStartSetting: (id) sender
538{
539    NSString * startSetting;
540
541    int tag = [[fStartMatrix selectedCell] tag];
542    if (tag == START_YES_CHECK_TAG)
543        startSetting = @"Start";
544    else if (tag == START_WAIT_CHECK_TAG)
545        startSetting = @"Wait";
546    else
547        startSetting = @"Manual";
548   
549    [fDefaults setObject: startSetting forKey: @"StartSetting"];
550   
551    [self setWaitToStart: fWaitToStartField];
552    [fWaitToStartField setEnabled: tag == START_WAIT_CHECK_TAG];
553}
554
555- (void) setWaitToStart: (id) sender
556{
557    int waitNumber = [sender intValue];
558    if (![[sender stringValue] isEqualToString: [NSString stringWithInt: waitNumber]] || waitNumber < 1)
559    {
560        NSBeep();
561        waitNumber = [fDefaults floatForKey: @"WaitToStartNumber"];
562        [sender setIntValue: waitNumber];
563    }
564    else
565        [fDefaults setInteger: waitNumber forKey: @"WaitToStartNumber"];
566   
567    [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self];
568}
569
570- (void) setMoveTorrent: (id) sender
571{
572    int state = [sender state];
573    if (sender == fCopyTorrentCheck)
574    {
575        [fDefaults setBool: state forKey: @"SavePrivateTorrent"];
576       
577        [fDeleteOriginalTorrentCheck setEnabled: state];
578        if (state == NSOffState)
579        {
580            [fDeleteOriginalTorrentCheck setState: NSOffState];
581            [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"];
582        }
583    }
584    else
585        [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"];
586}
587
588- (void) setDownloadLocation: (id) sender
589{
590    //Download folder
591    switch ([fFolderPopUp indexOfSelectedItem])
592    {
593        case DOWNLOAD_FOLDER:
594            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
595            break;
596        case DOWNLOAD_TORRENT:
597            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
598            break;
599        case DOWNLOAD_ASK:
600            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
601            break;
602    }
603}
604
605- (void) checkUpdate
606{
607    [fUpdater checkForUpdates: nil];
608}
609
610- (void) folderSheetShow: (id) sender
611{
612    NSOpenPanel * panel = [NSOpenPanel openPanel];
613
614    [panel setPrompt: @"Select"];
615    [panel setAllowsMultipleSelection: NO];
616    [panel setCanChooseFiles: NO];
617    [panel setCanChooseDirectories: YES];
618    [panel setCanCreateDirectories: YES];
619
620    [panel beginSheetForDirectory: nil file: nil types: nil
621        modalForWindow: [self window] modalDelegate: self didEndSelector:
622        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
623}
624
625- (void) setAutoImport: (id) sender
626{
627    int state = [fAutoImportCheck state];
628    [fDefaults setBool: state forKey: @"AutoImport"];
629    [fImportFolderPopUp setEnabled: state];
630   
631    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
632}
633
634- (void) importFolderSheetShow: (id) sender
635{
636    NSOpenPanel * panel = [NSOpenPanel openPanel];
637
638    [panel setPrompt: @"Select"];
639    [panel setAllowsMultipleSelection: NO];
640    [panel setCanChooseFiles: NO];
641    [panel setCanChooseDirectories: YES];
642    [panel setCanCreateDirectories: YES];
643
644    [panel beginSheetForDirectory: nil file: nil types: nil
645        modalForWindow: [self window] modalDelegate: self didEndSelector:
646        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
647}
648
649- (void) setAutoSize: (id) sender
650{
651    [fDefaults setBool: [sender state] forKey: @"AutoSize"];
652   
653    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
654}
655
656- (void) windowWillClose: (NSNotification *) notification
657{
658    [[self window] makeFirstResponder: nil];
659}
660
661@end
662
663@implementation PrefsController (Private)
664
665- (void) showGeneralPref: (id) sender
666{
667    [self setPrefView: fGeneralView];
668}
669
670- (void) showTransfersPref: (id) sender
671{
672    [self setPrefView: fTransfersView];
673}
674
675- (void) showBandwidthPref: (id) sender
676{
677    [self setPrefView: fBandwidthView];
678}
679
680- (void) showNetworkPref: (id) sender
681{
682    [self setPrefView: fNetworkView];
683}
684
685- (void) setPrefView: (NSView *) view
686{
687    NSWindow * window = [self window];
688   
689    NSRect windowRect = [window frame];
690    int difference = [view frame].size.height - [[window contentView] frame].size.height;
691    windowRect.origin.y -= difference;
692    windowRect.size.height += difference;
693
694    [window setTitle: [fToolbar selectedItemIdentifier]];
695   
696    [window setContentView: view];
697    [view setHidden: YES];
698    [window setFrame: windowRect display: YES animate: YES];
699    [view setHidden: NO];
700}
701
702- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
703{
704    if (code == NSOKButton)
705    {
706        [fDownloadFolder release];
707        fDownloadFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
708       
709        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
710        [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
711        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
712       
713        [self updatePopUp];
714    }
715    else
716    {
717        //reset if cancelled
718        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
719        if ([downloadChoice isEqualToString: @"Constant"])
720            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
721        else if ([downloadChoice isEqualToString: @"Torrent"])
722            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
723        else
724            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
725    }
726}
727
728- (void) updatePopUp
729{
730    //get and resize the icon
731    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder];
732    [icon setScalesWhenResized: YES];
733    [icon setSize: NSMakeSize(16.0, 16.0)];
734
735    //update menu item
736    NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
737    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
738    [menuItem setImage: icon];
739}
740
741- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
742{
743   if (code == NSOKButton)
744   {
745       [fImportFolder release];
746       fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
747
748       [fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
749
750       [self updateImportPopUp];
751       
752       [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
753   }
754   [fImportFolderPopUp selectItemAtIndex: 0];
755}
756
757- (void) updateImportPopUp
758{
759    //get and resize the icon
760    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fImportFolder];
761    [icon setScalesWhenResized: YES];
762    [icon setSize: NSMakeSize(16.0, 16.0)];
763
764    //update menu item
765    NSMenuItem * menuItem = (NSMenuItem *) [fImportFolderPopUp itemAtIndex: 0];
766    [menuItem setTitle: [fImportFolder lastPathComponent]];
767    [menuItem setImage: icon];
768}
769
770@end
Note: See TracBrowser for help on using the repository browser.