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

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

attempt at getting nat traversal support in the os x gui

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