source: trunk/macosx/PrefsController.m @ 700

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

Capitalization change.

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