source: trunk/macosx/PrefsController.m @ 802

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

Make sure the scheduler 24-hour format is double digits.

  • Property svn:keywords set to Date Rev Author Id
File size: 28.9 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 802 2006-08-20 20:46:55Z 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 setStringValue: [NSString stringWithFormat: @"%02d", speedLimitAutoOnHour]];
178    [fSpeedLimitAutoOnField setEnabled: speedLimitAuto];
179   
180    int speedLimitAutoOffHour = [fDefaults integerForKey: @"SpeedLimitAutoOffHour"];
181    [fSpeedLimitAutoOffField setStringValue: [NSString stringWithFormat: @"%02d", 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   
516    //allow numbers under ten in the format 0x
517    if (!([[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", hour]]
518        || [[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%02d", hour]]) || hour < 0 || hour > 23
519        || [fSpeedLimitAutoOnField intValue] == [fSpeedLimitAutoOffField intValue])
520    {
521        NSBeep();
522        hour = [fDefaults integerForKey: key];
523        [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]];
524    }
525    else
526        [fDefaults setInteger: hour forKey: key];
527   
528    [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]]; //ensure number has 2 digits
529   
530    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
531}
532
533- (void) setRatio: (id) sender
534{
535    float ratioLimit = [sender floatValue];
536    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
537            || ratioLimit < 0)
538    {
539        NSBeep();
540        ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
541        [sender setFloatValue: ratioLimit];
542    }
543    else
544        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
545   
546    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
547                                [NSNumber numberWithBool: [fRatioCheck state]], @"Enable",
548                                [NSNumber numberWithFloat: ratioLimit], @"Ratio", nil];
549    [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: dict];
550}
551
552- (void) setRatioCheck: (id) sender
553{
554    BOOL check = [sender state] == NSOnState;
555    [self setRatio: fRatioField];
556    [fRatioField setEnabled: check];
557   
558    [fDefaults setBool: check forKey: @"RatioCheck"];
559}
560
561- (void) setRatioEnabled: (BOOL) enable
562{
563    int state = enable ? NSOnState : NSOffState;
564   
565    [fRatioCheck setState: state];
566    [self setRatioCheck: fRatioCheck];
567}
568
569- (void) setQuickRatio: (float) ratioLimit
570{
571    [fRatioField setFloatValue: ratioLimit];
572   
573    [fRatioCheck setState: NSOnState];
574    [self setRatioCheck: fRatioCheck];
575}
576
577- (void) setShowMessage: (id) sender
578{
579    BOOL state = [sender state];
580
581    if (sender == fQuitCheck)
582    {
583        [fDefaults setBool: state forKey: @"CheckQuit"];
584        [fQuitDownloadingCheck setEnabled: state];
585    }
586    else if (sender == fRemoveCheck)
587    {
588        [fDefaults setBool: state forKey: @"CheckRemove"];
589        [fRemoveDownloadingCheck setEnabled: state];
590    }
591    if (sender == fQuitDownloadingCheck)
592        [fDefaults setBool: state forKey: @"CheckQuitDownloading"];
593    else if (sender == fRemoveDownloadingCheck)
594        [fDefaults setBool: state forKey: @"CheckRemoveDownloading"];
595    else;
596}
597
598- (void) setBadge: (id) sender
599{   
600    if (sender == fBadgeDownloadRateCheck)
601        [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
602    else if (sender == fBadgeUploadRateCheck)
603        [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
604    else;
605   
606    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
607}
608
609- (void) setPlaySound: (id) sender
610{
611    BOOL state = [sender state];
612
613    if (sender == fPlayDownloadSoundCheck)
614    {
615        [fDownloadSoundPopUp setEnabled: state];
616        [fDefaults setBool: state forKey: @"PlayDownloadSound"];
617    }
618    else if (sender == fPlaySeedingSoundCheck)
619    {
620        [fSeedingSoundPopUp setEnabled: state];
621        [fDefaults setBool: state forKey: @"PlaySeedingSound"];
622    }
623    else;
624}
625
626- (void) setSound: (id) sender
627{
628    //play sound when selecting
629    NSString * soundName = [sender titleOfSelectedItem];
630    NSSound * sound;
631    if ((sound = [NSSound soundNamed: soundName]))
632        [sound play];
633
634    if (sender == fDownloadSoundPopUp)
635        [fDefaults setObject: soundName forKey: @"DownloadSound"];
636    else if (sender == fSeedingSoundPopUp)
637        [fDefaults setObject: soundName forKey: @"SeedingSound"];
638    else;
639}
640
641- (void) setUpdate: (id) sender
642{
643    int index = [fUpdatePopUp indexOfSelectedItem];
644    NSTimeInterval seconds;
645    if (index == UPDATE_DAILY)
646    {
647        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
648        seconds = 86400;
649    }
650    else if (index == UPDATE_WEEKLY)
651    {
652        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
653        seconds = 604800;
654    }
655    else
656    {
657        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
658        seconds = 0;
659    }
660
661    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
662    [fUpdater scheduleCheckWithInterval: seconds];
663}
664
665- (void) setStartSetting: (id) sender
666{
667    NSString * startSetting;
668
669    int tag = [[fStartMatrix selectedCell] tag];
670    if (tag == START_YES_CHECK_TAG)
671        startSetting = @"Start";
672    else if (tag == START_WAIT_CHECK_TAG)
673        startSetting = @"Wait";
674    else
675        startSetting = @"Manual";
676   
677    [fDefaults setObject: startSetting forKey: @"StartSetting"];
678   
679    [self setStartNumber: fStartNumberField];
680    [fStartNumberField setEnabled: tag == START_WAIT_CHECK_TAG];
681}
682
683- (void) setStartNumber: (id) sender
684{
685    int waitNumber = [sender intValue];
686    if (![[sender stringValue] isEqualToString: [NSString stringWithInt: waitNumber]] || waitNumber < 1)
687    {
688        NSBeep();
689        waitNumber = [fDefaults floatForKey: @"WaitToStartNumber"];
690        [sender setIntValue: waitNumber];
691    }
692    else
693        [fDefaults setInteger: waitNumber forKey: @"WaitToStartNumber"];
694   
695    [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self];
696}
697
698- (void) setMoveTorrent: (id) sender
699{
700    int state = [sender state];
701    if (sender == fCopyTorrentCheck)
702    {
703        [fDefaults setBool: state forKey: @"SavePrivateTorrent"];
704       
705        [fDeleteOriginalTorrentCheck setEnabled: state];
706        if (state == NSOffState)
707        {
708            [fDeleteOriginalTorrentCheck setState: NSOffState];
709            [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"];
710        }
711    }
712    else
713        [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"];
714}
715
716- (void) setDownloadLocation: (id) sender
717{
718    //Download folder
719    switch ([fFolderPopUp indexOfSelectedItem])
720    {
721        case DOWNLOAD_FOLDER:
722            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
723            break;
724        case DOWNLOAD_TORRENT:
725            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
726            break;
727        case DOWNLOAD_ASK:
728            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
729            break;
730    }
731}
732
733- (void) checkUpdate
734{
735    [fUpdater checkForUpdates: nil];
736}
737
738- (void) folderSheetShow: (id) sender
739{
740    NSOpenPanel * panel = [NSOpenPanel openPanel];
741
742    [panel setPrompt: @"Select"];
743    [panel setAllowsMultipleSelection: NO];
744    [panel setCanChooseFiles: NO];
745    [panel setCanChooseDirectories: YES];
746    [panel setCanCreateDirectories: YES];
747
748    [panel beginSheetForDirectory: nil file: nil types: nil
749        modalForWindow: [self window] modalDelegate: self didEndSelector:
750        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
751}
752
753- (void) setAutoImport: (id) sender
754{
755    int state = [fAutoImportCheck state];
756    [fDefaults setBool: state forKey: @"AutoImport"];
757    [fImportFolderPopUp setEnabled: state];
758   
759    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
760}
761
762- (void) importFolderSheetShow: (id) sender
763{
764    NSOpenPanel * panel = [NSOpenPanel openPanel];
765
766    [panel setPrompt: @"Select"];
767    [panel setAllowsMultipleSelection: NO];
768    [panel setCanChooseFiles: NO];
769    [panel setCanChooseDirectories: YES];
770    [panel setCanCreateDirectories: YES];
771
772    [panel beginSheetForDirectory: nil file: nil types: nil
773        modalForWindow: [self window] modalDelegate: self didEndSelector:
774        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
775}
776
777- (void) setAutoSize: (id) sender
778{
779    [fDefaults setBool: [sender state] forKey: @"AutoSize"];
780   
781    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
782}
783
784- (void) helpForNetwork: (id) sender
785{
786    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PortForwarding"
787        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
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    if ([window contentView] == view)
819        return;
820   
821    NSRect windowRect = [window frame];
822    int difference = [view frame].size.height - [[window contentView] frame].size.height;
823    windowRect.origin.y -= difference;
824    windowRect.size.height += difference;
825
826    [window setTitle: [fToolbar selectedItemIdentifier]];
827   
828    [view setHidden: YES];
829    [window setContentView: view];
830    [window setFrame: windowRect display: YES animate: YES];
831    [view setHidden: NO];
832}
833
834- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
835{
836    if (code == NSOKButton)
837    {
838        [fDownloadFolder release];
839        fDownloadFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
840       
841        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
842        [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
843        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
844       
845        [self updatePopUp];
846    }
847    else
848    {
849        //reset if cancelled
850        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
851        if ([downloadChoice isEqualToString: @"Constant"])
852            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
853        else if ([downloadChoice isEqualToString: @"Torrent"])
854            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
855        else
856            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
857    }
858}
859
860- (void) updatePopUp
861{
862    //get and resize the icon
863    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder];
864    [icon setScalesWhenResized: YES];
865    [icon setSize: NSMakeSize(16.0, 16.0)];
866
867    //update menu item
868    NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
869    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
870    [menuItem setImage: icon];
871}
872
873- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
874{
875   if (code == NSOKButton)
876   {
877       [fImportFolder release];
878       fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
879
880       [fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
881
882       [self updateImportPopUp];
883       
884       [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
885   }
886   [fImportFolderPopUp selectItemAtIndex: 0];
887}
888
889- (void) updateImportPopUp
890{
891    //get and resize the icon
892    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fImportFolder];
893    [icon setScalesWhenResized: YES];
894    [icon setSize: NSMakeSize(16.0, 16.0)];
895
896    //update menu item
897    NSMenuItem * menuItem = (NSMenuItem *) [fImportFolderPopUp itemAtIndex: 0];
898    [menuItem setTitle: [fImportFolder lastPathComponent]];
899    [menuItem setImage: icon];
900}
901
902@end
Note: See TracBrowser for help on using the repository browser.