source: branches/nat-traversal/macosx/PrefsController.m @ 880

Last change on this file since 880 was 880, checked in by livings124, 15 years ago

Simplified queue and autostart prefs

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