source: trunk/macosx/PrefsController.m @ 740

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

Allow scheduling of speed limit.
Preference window saves when closing again.

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