source: trunk/macosx/PrefsController.m @ 737

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

Use tabs in Transfers section of prefs.

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