source: trunk/macosx/PrefsController.m @ 757

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

Some changes for the last commit plus don't allow the same time for turning speed limit on and off.

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