source: trunk/macosx/PrefsController.m @ 746

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

Add help button to Network prefs that goes straight to portforwarding help.

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