source: trunk/macosx/PrefsController.m @ 734

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

Play sound when seeding completes.

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