source: trunk/macosx/PrefsController.m @ 655

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

New prefs setting to keep the window sized perfectly for the current number of transfers.

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