source: trunk/macosx/PrefsController.m @ 558

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

no more double release

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