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

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

show nat status in prefs

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