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

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

retain nat status to avoid constant setting of status

  • Property svn:keywords set to Date Rev Author Id
File size: 30.1 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 890 2006-09-22 15:02:54Z 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    fNatStatus = -1;
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    [self updateNatStatus];
372}
373
374- (void) updateNatStatus
375{
376    int status = tr_natTraversalStatus(fHandle);
377    if (fNatStatus == status)
378        return;
379   
380    if (status == 2)
381    {
382        [fNatStatusField setStringValue: @"Ports have been successfully mapped"];
383        [fNatStatusImage setImage: nil];
384    }
385    else if (status == 3 || status == 4)
386    {
387        [fNatStatusField setStringValue: @"Error mapping ports"];
388        [fNatStatusImage setImage: [NSImage imageNamed: @"Error.tiff"]];
389    }
390    else
391    {
392        [fNatStatusField setStringValue: @""];
393        [fNatStatusImage setImage: nil];
394    }
395}
396
397- (void) setLimit: (id) sender
398{
399    NSString * key;
400    NSButton * check;
401    NSString * type;
402    if (sender == fUploadField)
403    {
404        key = @"UploadLimit";
405        check = fUploadCheck;
406        type = @"Upload";
407    }
408    else
409    {
410        key = @"DownloadLimit";
411        check = fDownloadCheck;
412        type = @"Download";
413    }
414
415    int limit = [sender intValue];
416    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
417    {
418        NSBeep();
419        limit = [fDefaults integerForKey: key];
420        [sender setIntValue: limit];
421    }
422    else
423    {
424        if (![fDefaults boolForKey: @"SpeedLimit"])
425        {
426            int realLimit = [check state] ? limit : -1;
427            if (sender == fUploadField)
428                tr_setUploadLimit(fHandle, realLimit);
429            else
430                tr_setDownloadLimit(fHandle, realLimit);
431        }
432       
433        [fDefaults setInteger: limit forKey: key];
434    }
435   
436    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
437                                    [NSNumber numberWithBool: [check state]], @"Enable",
438                                    [NSNumber numberWithInt: limit], @"Limit",
439                                    type, @"Type", nil];
440    [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange" object: dict];
441}
442
443- (void) setLimitCheck: (id) sender
444{
445    NSString * key;
446    NSTextField * field;
447    if (sender == fUploadCheck)
448    {
449        key = @"CheckUpload";
450        field = fUploadField;
451    }
452    else
453    {
454        key = @"CheckDownload";
455        field = fDownloadField;
456    }
457   
458    BOOL check = [sender state] == NSOnState;
459    [self setLimit: field];
460    [field setEnabled: check];
461   
462    [fDefaults setBool: check forKey: key];
463}
464
465- (void) setLimitEnabled: (BOOL) enable type: (NSString *) type
466{
467    NSButton * check = [type isEqualToString: @"Upload"] ? fUploadCheck : fDownloadCheck;
468    [check setState: enable ? NSOnState : NSOffState];
469    [self setLimitCheck: check];
470}
471
472- (void) setQuickLimit: (int) limit type: (NSString *) type
473{
474    NSButton * check;
475    if ([type isEqualToString: @"Upload"])
476    {
477        [fUploadField setIntValue: limit];
478        check = fUploadCheck;
479    }
480    else
481    {
482        [fDownloadField setIntValue: limit];
483        check = fDownloadCheck;
484    }
485    [check setState: NSOnState];
486    [self setLimitCheck: check];
487}
488
489- (void) enableSpeedLimit: (BOOL) enable
490{
491    if ([fDefaults boolForKey: @"SpeedLimit"] != enable)
492    {
493        [fDefaults setBool: enable forKey: @"SpeedLimit"];
494       
495        if (enable)
496        {
497            tr_setUploadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
498            tr_setDownloadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
499        }
500        else
501        {
502            tr_setUploadLimit(fHandle, [fUploadCheck state] ? [fDefaults integerForKey: @"UploadLimit"] : -1);
503            tr_setDownloadLimit(fHandle, [fDownloadCheck state] ? [fDefaults integerForKey: @"DownloadLimit"] : -1);
504        }
505    }
506}
507
508- (void) setSpeedLimit: (id) sender
509{
510    NSString * key = sender == fSpeedLimitUploadField ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit";
511
512    int limit = [sender intValue];
513    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
514    {
515        NSBeep();
516        limit = [fDefaults integerForKey: key];
517        [sender setIntValue: limit];
518    }
519    else
520    {
521        if ([fDefaults boolForKey: @"SpeedLimit"])
522        {
523            if (sender == fSpeedLimitUploadField)
524                tr_setUploadLimit(fHandle, limit);
525            else
526                tr_setDownloadLimit(fHandle, limit);
527        }
528       
529        [fDefaults setInteger: limit forKey: key];
530    }
531}
532
533- (void) setAutoSpeedLimitCheck: (id) sender
534{
535    BOOL check = [sender state] == NSOnState;
536   
537    [fDefaults setBool: check forKey: @"SpeedLimitAuto"];
538
539    [self setAutoSpeedLimitHour: fSpeedLimitAutoOnField];
540    [fSpeedLimitAutoOnField setEnabled: check];
541   
542    [self setAutoSpeedLimitHour: fSpeedLimitAutoOffField];
543    [fSpeedLimitAutoOffField setEnabled: check];
544}
545
546- (void) setAutoSpeedLimitHour: (id) sender
547{
548    NSString * key = (sender == fSpeedLimitAutoOnField) ? @"SpeedLimitAutoOnHour" : @"SpeedLimitAutoOffHour";
549
550    int hour = [sender intValue];
551   
552    //allow numbers under ten in the format 0x
553    if (!([[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", hour]]
554        || [[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%02d", hour]]) || hour < 0 || hour > 23
555        || [fSpeedLimitAutoOnField intValue] == [fSpeedLimitAutoOffField intValue])
556    {
557        NSBeep();
558        hour = [fDefaults integerForKey: key];
559        [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]];
560    }
561    else
562        [fDefaults setInteger: hour forKey: key];
563   
564    [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]]; //ensure number has 2 digits
565   
566    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
567}
568
569- (void) setRatio: (id) sender
570{
571    float ratioLimit = [sender floatValue];
572    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
573            || ratioLimit < 0)
574    {
575        NSBeep();
576        ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
577        [sender setFloatValue: ratioLimit];
578    }
579    else
580        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
581   
582    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
583                                [NSNumber numberWithBool: [fRatioCheck state]], @"Enable",
584                                [NSNumber numberWithFloat: ratioLimit], @"Ratio", nil];
585    [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: dict];
586}
587
588- (void) setRatioCheck: (id) sender
589{
590    BOOL check = [sender state] == NSOnState;
591    [self setRatio: fRatioField];
592    [fRatioField setEnabled: check];
593   
594    [fDefaults setBool: check forKey: @"RatioCheck"];
595}
596
597- (void) setRatioEnabled: (BOOL) enable
598{
599    int state = enable ? NSOnState : NSOffState;
600   
601    [fRatioCheck setState: state];
602    [self setRatioCheck: fRatioCheck];
603}
604
605- (void) setQuickRatio: (float) ratioLimit
606{
607    [fRatioField setFloatValue: ratioLimit];
608   
609    [fRatioCheck setState: NSOnState];
610    [self setRatioCheck: fRatioCheck];
611}
612
613- (void) setShowMessage: (id) sender
614{
615    BOOL state = [sender state];
616
617    if (sender == fQuitCheck)
618    {
619        [fDefaults setBool: state forKey: @"CheckQuit"];
620        [fQuitDownloadingCheck setEnabled: state];
621    }
622    else if (sender == fRemoveCheck)
623    {
624        [fDefaults setBool: state forKey: @"CheckRemove"];
625        [fRemoveDownloadingCheck setEnabled: state];
626    }
627    if (sender == fQuitDownloadingCheck)
628        [fDefaults setBool: state forKey: @"CheckQuitDownloading"];
629    else if (sender == fRemoveDownloadingCheck)
630        [fDefaults setBool: state forKey: @"CheckRemoveDownloading"];
631    else;
632}
633
634- (void) setBadge: (id) sender
635{   
636    if (sender == fBadgeDownloadRateCheck)
637        [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
638    else if (sender == fBadgeUploadRateCheck)
639        [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
640    else;
641   
642    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
643}
644
645- (void) setPlaySound: (id) sender
646{
647    BOOL state = [sender state];
648
649    if (sender == fPlayDownloadSoundCheck)
650    {
651        [fDownloadSoundPopUp setEnabled: state];
652        [fDefaults setBool: state forKey: @"PlayDownloadSound"];
653    }
654    else if (sender == fPlaySeedingSoundCheck)
655    {
656        [fSeedingSoundPopUp setEnabled: state];
657        [fDefaults setBool: state forKey: @"PlaySeedingSound"];
658    }
659    else;
660}
661
662- (void) setSound: (id) sender
663{
664    //play sound when selecting
665    NSString * soundName = [sender titleOfSelectedItem];
666    NSSound * sound;
667    if ((sound = [NSSound soundNamed: soundName]))
668        [sound play];
669
670    if (sender == fDownloadSoundPopUp)
671        [fDefaults setObject: soundName forKey: @"DownloadSound"];
672    else if (sender == fSeedingSoundPopUp)
673        [fDefaults setObject: soundName forKey: @"SeedingSound"];
674    else;
675}
676
677- (void) setUpdate: (id) sender
678{
679    int index = [fUpdatePopUp indexOfSelectedItem];
680    NSTimeInterval seconds;
681    if (index == UPDATE_DAILY)
682    {
683        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
684        seconds = 86400;
685    }
686    else if (index == UPDATE_WEEKLY)
687    {
688        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
689        seconds = 604800;
690    }
691    else
692    {
693        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
694        seconds = 0;
695    }
696
697    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
698    [fUpdater scheduleCheckWithInterval: seconds];
699}
700
701- (void) setStartAtOpen: (id) sender
702{
703    [fDefaults setBool: [sender state] == NSOnState forKey: @"StartAtOpen"];
704}
705
706- (void) setUseQueue: (id) sender
707{
708    BOOL useQueue = [sender state] == NSOnState;
709   
710    [fDefaults setBool: useQueue forKey: @"Queue"];
711    [self setQueueNumber: fQueueNumberField];
712    [fQueueNumberField setEnabled: useQueue];
713}
714
715- (void) setQueueNumber: (id) sender
716{
717    int queueNumber = [sender intValue];
718    if (![[sender stringValue] isEqualToString: [NSString stringWithInt: queueNumber]] || queueNumber < 1)
719    {
720        NSBeep();
721        queueNumber = [fDefaults integerForKey: @"WaitToStartNumber"];
722        [sender setIntValue: queueNumber];
723    }
724    else
725        [fDefaults setInteger: queueNumber forKey: @"WaitToStartNumber"];
726   
727    [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self];
728}
729
730- (void) setMoveTorrent: (id) sender
731{
732    int state = [sender state];
733    if (sender == fCopyTorrentCheck)
734    {
735        [fDefaults setBool: state forKey: @"SavePrivateTorrent"];
736       
737        [fDeleteOriginalTorrentCheck setEnabled: state];
738        if (state == NSOffState)
739        {
740            [fDeleteOriginalTorrentCheck setState: NSOffState];
741            [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"];
742        }
743    }
744    else
745        [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"];
746}
747
748- (void) setDownloadLocation: (id) sender
749{
750    //Download folder
751    switch ([fFolderPopUp indexOfSelectedItem])
752    {
753        case DOWNLOAD_FOLDER:
754            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
755            break;
756        case DOWNLOAD_TORRENT:
757            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
758            break;
759        case DOWNLOAD_ASK:
760            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
761            break;
762    }
763}
764
765- (void) checkUpdate
766{
767    [fUpdater checkForUpdates: nil];
768}
769
770- (void) folderSheetShow: (id) sender
771{
772    NSOpenPanel * panel = [NSOpenPanel openPanel];
773
774    [panel setPrompt: @"Select"];
775    [panel setAllowsMultipleSelection: NO];
776    [panel setCanChooseFiles: NO];
777    [panel setCanChooseDirectories: YES];
778    [panel setCanCreateDirectories: YES];
779
780    [panel beginSheetForDirectory: nil file: nil types: nil
781        modalForWindow: [self window] modalDelegate: self didEndSelector:
782        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
783}
784
785- (void) setAutoImport: (id) sender
786{
787    int state = [fAutoImportCheck state];
788    [fDefaults setBool: state forKey: @"AutoImport"];
789    [fImportFolderPopUp setEnabled: state];
790   
791    if (state == NSOnState)
792        [[UKKQueue sharedFileWatcher] addPath: fImportFolder];
793    else
794        [[UKKQueue sharedFileWatcher] removePathFromQueue: fImportFolder];
795   
796    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
797}
798
799- (void) importFolderSheetShow: (id) sender
800{
801    NSOpenPanel * panel = [NSOpenPanel openPanel];
802
803    [panel setPrompt: @"Select"];
804    [panel setAllowsMultipleSelection: NO];
805    [panel setCanChooseFiles: NO];
806    [panel setCanChooseDirectories: YES];
807    [panel setCanCreateDirectories: YES];
808
809    [panel beginSheetForDirectory: nil file: nil types: nil
810        modalForWindow: [self window] modalDelegate: self didEndSelector:
811        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
812}
813
814- (void) setAutoSize: (id) sender
815{
816    [fDefaults setBool: [sender state] forKey: @"AutoSize"];
817   
818    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
819}
820
821- (void) helpForNetwork: (id) sender
822{
823    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PortForwarding"
824        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
825}
826
827@end
828
829@implementation PrefsController (Private)
830
831- (void) showGeneralPref: (id) sender
832{
833    [self setPrefView: fGeneralView];
834}
835
836- (void) showTransfersPref: (id) sender
837{
838    [self setPrefView: fTransfersView];
839}
840
841- (void) showBandwidthPref: (id) sender
842{
843    [self setPrefView: fBandwidthView];
844}
845
846- (void) showNetworkPref: (id) sender
847{
848    [self setPrefView: fNetworkView];
849}
850
851- (void) setPrefView: (NSView *) view
852{
853    NSWindow * window = [self window];
854   
855    if ([window contentView] == view)
856        return;
857   
858    NSRect windowRect = [window frame];
859    int difference = [view frame].size.height - [[window contentView] frame].size.height;
860    windowRect.origin.y -= difference;
861    windowRect.size.height += difference;
862
863    [window setTitle: [fToolbar selectedItemIdentifier]];
864   
865    [view setHidden: YES];
866    [window setContentView: view];
867    [window setFrame: windowRect display: YES animate: YES];
868    [view setHidden: NO];
869}
870
871- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
872{
873    if (code == NSOKButton)
874    {
875        [fDownloadFolder release];
876        fDownloadFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
877       
878        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
879        [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
880        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
881       
882        [self updatePopUp];
883    }
884    else
885    {
886        //reset if cancelled
887        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
888        if ([downloadChoice isEqualToString: @"Constant"])
889            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
890        else if ([downloadChoice isEqualToString: @"Torrent"])
891            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
892        else
893            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
894    }
895}
896
897- (void) updatePopUp
898{
899    //get and resize the icon
900    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder];
901    [icon setScalesWhenResized: YES];
902    [icon setSize: NSMakeSize(16.0, 16.0)];
903
904    //update menu item
905    NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
906    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
907    [menuItem setImage: icon];
908}
909
910- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
911{
912    if (code == NSOKButton)
913    {
914        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
915        [sharedQueue removePathFromQueue: fImportFolder];
916       
917        [fImportFolder release];
918        fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
919       
920        [fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
921       
922        [self updateImportPopUp];
923       
924        [sharedQueue addPath: fImportFolder];
925       
926        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
927    }
928    [fImportFolderPopUp selectItemAtIndex: 0];
929}
930
931- (void) updateImportPopUp
932{
933    //get and resize the icon
934    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fImportFolder];
935    [icon setScalesWhenResized: YES];
936    [icon setSize: NSMakeSize(16.0, 16.0)];
937
938    //update menu item
939    NSMenuItem * menuItem = (NSMenuItem *) [fImportFolderPopUp itemAtIndex: 0];
940    [menuItem setTitle: [fImportFolder lastPathComponent]];
941    [menuItem setImage: icon];
942}
943
944@end
Note: See TracBrowser for help on using the repository browser.