source: trunk/macosx/PrefsController.m @ 752

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

Scheduler is now more automatic.

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