source: trunk/macosx/PrefsController.m @ 1030

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

load prefs when first used as opposed to at launch

  • Property svn:keywords set to Date Rev Author Id
File size: 35.0 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 1030 2006-10-25 02:57:00Z 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     @"TOOLBAR_GENERAL"
41#define TOOLBAR_TRANSFERS   @"TOOLBAR_TRANSFERS"
42#define TOOLBAR_BANDWIDTH   @"TOOLBAR_BANDWIDTH"
43#define TOOLBAR_NETWORK     @"TOOLBAR_NETWORK"
44
45@interface PrefsController (Private)
46
47- (void) setPrefView: (id) sender;
48
49- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
50- (void) updatePopUp;
51
52- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
53- (void) updateImportPopUp;
54
55@end
56
57@implementation PrefsController
58
59- (id) initWithWindowNibName: (NSString *) name handle: (tr_handle_t *) handle
60{
61    if ((self = [self initWithWindowNibName: name]))
62    {
63        fDefaults = [NSUserDefaults standardUserDefaults];
64        fHandle = handle;
65       
66        //checks for old version upload speed of -1
67        if ([fDefaults integerForKey: @"UploadLimit"] < 0)
68        {
69            [fDefaults setInteger: 20 forKey: @"UploadLimit"];
70            [fDefaults setBool: NO forKey: @"CheckUpload"];
71        }
72       
73        //set download folder and import folder
74        fDownloadFolder = [[[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath] retain];
75        fImportFolder = [[[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath] retain];
76       
77        //set auto import
78        if ([fDefaults boolForKey: @"AutoImport"])
79            [[UKKQueue sharedFileWatcher] addPath: fImportFolder];
80       
81        //set bind port
82        int bindPort = [fDefaults integerForKey: @"BindPort"];
83        tr_setBindPort(fHandle, bindPort);
84       
85        //set NAT
86        if ([fDefaults boolForKey: @"NatTraversal"])
87            tr_natTraversalEnable(fHandle);
88       
89        //actually set bandwidth limits
90        if ([fDefaults boolForKey: @"SpeedLimit"])
91        {
92            tr_setUploadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
93            tr_setDownloadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
94        }
95        else
96        {
97            tr_setUploadLimit(fHandle, [fDefaults boolForKey: @"CheckUpload"]
98                                            ? [fDefaults integerForKey: @"UploadLimit"] : -1);
99            tr_setDownloadLimit(fHandle, [fDefaults boolForKey: @"CheckDownload"]
100                                            ? [fDefaults integerForKey: @"DownloadLimit"] : -1);
101        }
102    }
103    return self;
104}
105
106- (void) dealloc
107{
108    if (fNatStatusTimer)
109        [fNatStatusTimer invalidate];
110
111    [fDownloadFolder release];
112    [fImportFolder release];
113    [super dealloc];
114}
115
116- (void) awakeFromNib
117{
118    fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
119    [fToolbar setDelegate: self];
120    [fToolbar setAllowsUserCustomization: NO];
121    [[self window] setToolbar: fToolbar];
122    [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
123    [fToolbar setSizeMode: NSToolbarSizeModeRegular];
124
125    [fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
126    [self setPrefView: nil];
127   
128    //set download folder
129    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
130    if ([downloadChoice isEqualToString: @"Constant"])
131        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
132    else if ([downloadChoice isEqualToString: @"Torrent"])
133        [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
134    else
135        [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
136    [self updatePopUp];
137   
138    //set auto import
139    [self updateImportPopUp];
140 
141    BOOL autoImport = [fDefaults boolForKey: @"AutoImport"];
142    [fAutoImportCheck setState: autoImport];
143    [fImportFolderPopUp setEnabled: autoImport];
144   
145    //set auto size
146    [fAutoSizeCheck setState: [fDefaults boolForKey: @"AutoSize"]];
147   
148    //set bind port
149    int bindPort = [fDefaults integerForKey: @"BindPort"];
150    [fPortField setIntValue: bindPort];
151    [self updatePortStatus];
152   
153    //set NAT
154    [fNatCheck setState: [fDefaults boolForKey: @"NatTraversal"]];
155   
156    fNatStatus = -1;
157    [self updateNatStatus];
158    fNatStatusTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
159                        selector: @selector(updateNatStatus) userInfo: nil repeats: YES];
160   
161    //set upload limit
162    BOOL checkUpload = [fDefaults boolForKey: @"CheckUpload"];
163    [fUploadCheck setState: checkUpload];
164    [fUploadField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
165    [fUploadField setEnabled: checkUpload];
166
167        //set download limit
168    BOOL checkDownload = [fDefaults boolForKey: @"CheckDownload"];
169    [fDownloadCheck setState: checkDownload];
170    [fDownloadField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
171    [fDownloadField setEnabled: checkDownload];
172   
173    //set speed limit
174    [fSpeedLimitUploadField setIntValue: [fDefaults integerForKey: @"SpeedLimitUploadLimit"]];
175    [fSpeedLimitDownloadField setIntValue: [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]];
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 settings
259    BOOL useQueue = [fDefaults boolForKey: @"Queue"];
260    [fQueueCheck setState: useQueue];
261    [fQueueNumberField setEnabled: useQueue];
262    [fQueueNumberField setIntValue: [fDefaults integerForKey: @"QueueDownloadNumber"]];
263   
264    [fStartAtOpenCheck setState: [fDefaults boolForKey: @"AutoStartDownload"]];
265   
266    //set private torrents
267    BOOL copyTorrents = [fDefaults boolForKey: @"SavePrivateTorrent"];
268    [fCopyTorrentCheck setState: copyTorrents];
269   
270    [fDeleteOriginalTorrentCheck setEnabled: copyTorrents];
271    [fDeleteOriginalTorrentCheck setState: [fDefaults boolForKey: @"DeleteOriginalTorrent"]];
272
273    //set update check
274    NSString * updateCheck = [fDefaults stringForKey: @"UpdateCheck"];
275    if ([updateCheck isEqualToString: @"Weekly"])
276        [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
277    else if ([updateCheck isEqualToString: @"Never"])
278        [fUpdatePopUp selectItemAtIndex: UPDATE_NEVER];
279    else
280        [fUpdatePopUp selectItemAtIndex: UPDATE_DAILY];
281}
282
283- (void) setUpdater: (SUUpdater *) updater
284{
285    fUpdater = updater;
286}
287
288- (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier:
289    (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
290{
291    NSToolbarItem * item;
292    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
293
294    if ([ident isEqualToString: TOOLBAR_GENERAL])
295    {
296        [item setLabel: NSLocalizedString(@"General", "Preferences -> General toolbar item title")];
297        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
298        [item setTarget: self];
299        [item setAction: @selector(setPrefView:)];
300    }
301    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
302    {
303        [item setLabel: NSLocalizedString(@"Transfers", "Preferences -> Transfers toolbar item title")];
304        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
305        [item setTarget: self];
306        [item setAction: @selector(setPrefView:)];
307    }
308    else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
309    {
310        [item setLabel: NSLocalizedString(@"Bandwidth", "Preferences -> Bandwidth toolbar item title")];
311        [item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
312        [item setTarget: self];
313        [item setAction: @selector(setPrefView:)];
314    }
315    else if ([ident isEqualToString: TOOLBAR_NETWORK])
316    {
317        [item setLabel: NSLocalizedString(@"Network", "Preferences -> Network toolbar item title")];
318        [item setImage: [NSImage imageNamed: @"Network.png"]];
319        [item setTarget: self];
320        [item setAction: @selector(setPrefView:)];
321    }
322    else
323    {
324        [item release];
325        return nil;
326    }
327
328    return item;
329}
330
331- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
332{
333    return [self toolbarDefaultItemIdentifiers: toolbar];
334}
335
336- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
337{
338    return [self toolbarAllowedItemIdentifiers: toolbar];
339}
340
341- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
342{
343    return [NSArray arrayWithObjects: TOOLBAR_GENERAL, TOOLBAR_TRANSFERS,
344                                        TOOLBAR_BANDWIDTH, TOOLBAR_NETWORK, nil];
345}
346
347- (void) setPort: (id) sender
348{
349    int bindPort = [sender intValue];
350    if (![[NSString stringWithInt: bindPort] isEqualToString: [sender stringValue]]
351            || bindPort < MIN_PORT || bindPort > MAX_PORT)
352    {
353        NSBeep();
354        bindPort = [fDefaults integerForKey: @"BindPort"];
355        [sender setIntValue: bindPort];
356    }
357    else
358    {
359        tr_setBindPort(fHandle, bindPort);
360        [fDefaults setInteger: bindPort forKey: @"BindPort"];
361       
362        [self updateNatStatus];
363        [self updatePortStatus];
364    }
365}
366
367- (void) updatePortStatus
368{
369    long sytemVersion;
370    [fPortStatusField setStringValue: @""];
371    [fPortStatusImage setImage: nil];
372   
373    Gestalt('sysv', & sytemVersion);
374    if (sytemVersion >= 0x1040)
375    {
376        //NSXML features are unfortunately only available since Mac OS X v10.4
377        PortChecker * checker = [[PortChecker alloc] initWithDelegate: self];
378
379        [fPortStatusField setStringValue: [NSLocalizedString(@"Checking port status",
380                                            "Preferences -> Network -> port status") stringByAppendingEllipsis]];
381        [fPortStatusProgress startAnimation: self];
382       
383        [checker probePort: [fDefaults integerForKey: @"BindPort"]];
384    }
385}
386
387- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
388{
389    [fPortStatusProgress stopAnimation: self];
390    switch ([portChecker status])
391    {
392        case PORT_STATUS_OPEN:
393            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Network -> port status")];
394            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.tiff"]];
395            break;
396        case PORT_STATUS_STEALTH:
397            [fPortStatusField setStringValue: NSLocalizedString(@"Port is stealth", "Preferences -> Network -> port status")];
398            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
399            break;
400        case PORT_STATUS_CLOSED:
401            [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Network -> port status")];
402            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
403            break;
404        case PORT_STATUS_ERROR:
405            [fPortStatusField setStringValue: NSLocalizedString(@"Unable to check port status",
406                                                "Preferences -> Network -> port status")];
407            [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.tiff"]];
408            break;
409    }
410    [portChecker release];
411}
412
413- (void) setNat: (id) sender
414{
415    BOOL enable = [sender state] == NSOnState;
416    enable ? tr_natTraversalEnable(fHandle) : tr_natTraversalDisable(fHandle);
417    [fDefaults setBool: enable forKey: @"NatTraversal"];
418   
419    [self updateNatStatus];
420}
421
422- (void) updateNatStatus
423{
424    int status = tr_natTraversalStatus(fHandle);
425    if (fNatStatus == status)
426        return;
427    fNatStatus = status;
428   
429    if (status == 2)
430    {
431        [fNatStatusField setStringValue: NSLocalizedString(@"Port successfully mapped",
432                                            "Preferences -> Network -> port map status")];
433        [fNatStatusImage setImage: [NSImage imageNamed: @"GreenDot.tiff"]];
434    }
435    else if (status == 3 || status == 4)
436    {
437        [fNatStatusField setStringValue: NSLocalizedString(@"Error mapping port",
438                                            "Preferences -> Network -> port map status")];
439        [fNatStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
440    }
441    else
442    {
443        [fNatStatusField setStringValue: @""];
444        [fNatStatusImage setImage: nil];
445    }
446   
447    [self updatePortStatus];
448}
449
450- (void) setLimit: (id) sender
451{
452    NSString * key;
453    NSButton * check;
454    NSString * type;
455    if (sender == fUploadField)
456    {
457        key = @"UploadLimit";
458        check = fUploadCheck;
459        type = @"Upload";
460    }
461    else
462    {
463        key = @"DownloadLimit";
464        check = fDownloadCheck;
465        type = @"Download";
466    }
467
468    int limit = [sender intValue];
469    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
470    {
471        NSBeep();
472        limit = [fDefaults integerForKey: key];
473        [sender setIntValue: limit];
474    }
475    else
476    {
477        if (![fDefaults boolForKey: @"SpeedLimit"])
478        {
479            int realLimit = [check state] ? limit : -1;
480            if (sender == fUploadField)
481                tr_setUploadLimit(fHandle, realLimit);
482            else
483                tr_setDownloadLimit(fHandle, realLimit);
484        }
485       
486        [fDefaults setInteger: limit forKey: key];
487    }
488   
489    [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange"
490                                            object: [[NSNumber alloc] initWithBool: sender == fUploadField]];
491}
492
493- (void) setLimitCheck: (id) sender
494{
495    NSString * key;
496    NSTextField * field;
497    if (sender == fUploadCheck)
498    {
499        key = @"CheckUpload";
500        field = fUploadField;
501    }
502    else
503    {
504        key = @"CheckDownload";
505        field = fDownloadField;
506    }
507   
508    BOOL check = [sender state] == NSOnState;
509    [self setLimit: field];
510    [field setEnabled: check];
511   
512    [fDefaults setBool: check forKey: key];
513}
514
515- (void) setQuickLimitEnabled: (BOOL) enable type: (NSString *) type
516{
517    //if interface is loaded, perform change as if user performed it
518    #warning better way?
519    if (fToolbar)
520    {
521        NSButton * check = [type isEqualToString: @"Upload"] ? fUploadCheck : fDownloadCheck;
522        [check setState: enable ? NSOnState : NSOffState];
523        [self setLimitCheck: check];
524    }
525    else
526    {
527        BOOL upload = [type isEqualToString: @"Upload"];
528        [fDefaults setBool: enable forKey: upload ? @"CheckUpload" : @"CheckDownload"];
529       
530        [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange"
531                                                object: [[NSNumber alloc] initWithBool: upload]];
532    }
533}
534
535- (void) setQuickLimit: (int) limit type: (NSString *) type
536{
537    //if interface is loaded, perform change as if user performed it
538    #warning better way?
539    if (fToolbar)
540    {
541        NSButton * check;
542        if ([type isEqualToString: @"Upload"])
543        {
544            [fUploadField setIntValue: limit];
545            check = fUploadCheck;
546        }
547        else
548        {
549            [fDownloadField setIntValue: limit];
550            check = fDownloadCheck;
551        }
552        [check setState: NSOnState];
553        [self setLimitCheck: check];
554    }
555    else
556    {
557        BOOL upload = [type isEqualToString: @"Upload"];
558        [fDefaults setBool: YES forKey: upload ? @"CheckUpload" : @"CheckDownload"];
559        [fDefaults setInteger: limit forKey: upload ? @"UploadLimit" : @"DownloadLimit"];
560       
561        [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange"
562                                                object: [[NSNumber alloc] initWithBool: upload]];
563    }
564}
565
566- (void) enableSpeedLimit: (BOOL) enable
567{
568    if ([fDefaults boolForKey: @"SpeedLimit"] != enable)
569    {
570        [fDefaults setBool: enable forKey: @"SpeedLimit"];
571       
572        if (enable)
573        {
574            tr_setUploadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
575            tr_setDownloadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
576        }
577        else
578        {
579            tr_setUploadLimit(fHandle, [fDefaults boolForKey: @"CheckUpload"]
580                                        ? [fDefaults integerForKey: @"UploadLimit"] : -1);
581            tr_setDownloadLimit(fHandle, [fDefaults boolForKey: @"CheckDownload"]
582                                            ? [fDefaults integerForKey: @"DownloadLimit"] : -1);
583        }
584    }
585}
586
587- (void) setSpeedLimit: (id) sender
588{
589    NSString * key = sender == fSpeedLimitUploadField ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit";
590
591    int limit = [sender intValue];
592    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
593    {
594        NSBeep();
595        limit = [fDefaults integerForKey: key];
596        [sender setIntValue: limit];
597    }
598    else
599    {
600        if ([fDefaults boolForKey: @"SpeedLimit"])
601        {
602            if (sender == fSpeedLimitUploadField)
603                tr_setUploadLimit(fHandle, limit);
604            else
605                tr_setDownloadLimit(fHandle, limit);
606        }
607       
608        [fDefaults setInteger: limit forKey: key];
609    }
610}
611
612- (void) setAutoSpeedLimitCheck: (id) sender
613{
614    BOOL check = [sender state] == NSOnState;
615   
616    [fDefaults setBool: check forKey: @"SpeedLimitAuto"];
617
618    [self setAutoSpeedLimitHour: fSpeedLimitAutoOnField];
619    [fSpeedLimitAutoOnField setEnabled: check];
620   
621    [self setAutoSpeedLimitHour: fSpeedLimitAutoOffField];
622    [fSpeedLimitAutoOffField setEnabled: check];
623}
624
625- (void) setAutoSpeedLimitHour: (id) sender
626{
627    NSString * key = (sender == fSpeedLimitAutoOnField) ? @"SpeedLimitAutoOnHour" : @"SpeedLimitAutoOffHour";
628
629    int hour = [sender intValue];
630   
631    //allow numbers under ten in the format 0x
632    if (!([[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", hour]]
633        || [[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%02d", hour]]) || hour < 0 || hour > 23
634        || [fSpeedLimitAutoOnField intValue] == [fSpeedLimitAutoOffField intValue])
635    {
636        NSBeep();
637        hour = [fDefaults integerForKey: key];
638        [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]];
639    }
640    else
641        [fDefaults setInteger: hour forKey: key];
642   
643    [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]]; //ensure number has 2 digits
644   
645    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
646}
647
648- (void) setRatio: (id) sender
649{
650    float ratioLimit = [sender floatValue];
651    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
652            || ratioLimit < 0)
653    {
654        NSBeep();
655        ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
656        [sender setFloatValue: ratioLimit];
657    }
658    else
659        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
660   
661    [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: nil];
662}
663
664- (void) setRatioCheck: (id) sender
665{
666    BOOL check = [sender state] == NSOnState;
667    [self setRatio: fRatioField];
668    [fRatioField setEnabled: check];
669   
670    [fDefaults setBool: check forKey: @"RatioCheck"];
671}
672
673- (void) setQuickRatioEnabled: (BOOL) enable
674{
675    //if interface is loaded, perform change as if user performed it
676    #warning better way?
677    if (fToolbar)
678    {
679        int state = enable ? NSOnState : NSOffState;
680       
681        [fRatioCheck setState: state];
682        [self setRatioCheck: fRatioCheck];
683    }
684    else
685    {
686        [fDefaults setBool: enable forKey: @"RatioCheck"];
687       
688        [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: nil];
689    }
690}
691
692- (void) setQuickRatio: (float) ratioLimit
693{
694    //if interface is loaded, perform change as if user performed it
695    #warning better way?
696    if (fToolbar)
697    {
698        [fRatioField setFloatValue: ratioLimit];
699       
700        [fRatioCheck setState: NSOnState];
701        [self setRatioCheck: fRatioCheck];
702    }
703    else
704    {
705        [fDefaults setBool: YES forKey: @"RatioCheck"];
706        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
707       
708        [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: nil];
709    }
710}
711
712- (void) setShowMessage: (id) sender
713{
714    BOOL state = [sender state];
715
716    if (sender == fQuitCheck)
717    {
718        [fDefaults setBool: state forKey: @"CheckQuit"];
719        [fQuitDownloadingCheck setEnabled: state];
720    }
721    else if (sender == fRemoveCheck)
722    {
723        [fDefaults setBool: state forKey: @"CheckRemove"];
724        [fRemoveDownloadingCheck setEnabled: state];
725    }
726    if (sender == fQuitDownloadingCheck)
727        [fDefaults setBool: state forKey: @"CheckQuitDownloading"];
728    else if (sender == fRemoveDownloadingCheck)
729        [fDefaults setBool: state forKey: @"CheckRemoveDownloading"];
730    else;
731}
732
733- (void) setBadge: (id) sender
734{   
735    if (sender == fBadgeDownloadRateCheck)
736        [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
737    else if (sender == fBadgeUploadRateCheck)
738        [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
739    else;
740   
741    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
742}
743
744- (void) setPlaySound: (id) sender
745{
746    BOOL state = [sender state];
747
748    if (sender == fPlayDownloadSoundCheck)
749    {
750        [fDownloadSoundPopUp setEnabled: state];
751        [fDefaults setBool: state forKey: @"PlayDownloadSound"];
752    }
753    else if (sender == fPlaySeedingSoundCheck)
754    {
755        [fSeedingSoundPopUp setEnabled: state];
756        [fDefaults setBool: state forKey: @"PlaySeedingSound"];
757    }
758    else;
759}
760
761- (void) setSound: (id) sender
762{
763    //play sound when selecting
764    NSString * soundName = [sender titleOfSelectedItem];
765    NSSound * sound;
766    if ((sound = [NSSound soundNamed: soundName]))
767        [sound play];
768
769    if (sender == fDownloadSoundPopUp)
770        [fDefaults setObject: soundName forKey: @"DownloadSound"];
771    else if (sender == fSeedingSoundPopUp)
772        [fDefaults setObject: soundName forKey: @"SeedingSound"];
773    else;
774}
775
776- (void) setUpdate: (id) sender
777{
778    int index = [fUpdatePopUp indexOfSelectedItem];
779    NSTimeInterval seconds;
780    if (index == UPDATE_DAILY)
781    {
782        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
783        seconds = 86400;
784    }
785    else if (index == UPDATE_WEEKLY)
786    {
787        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
788        seconds = 604800;
789    }
790    else
791    {
792        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
793        seconds = 0;
794    }
795
796    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
797   
798    if (fUpdater)
799        [fUpdater scheduleCheckWithInterval: seconds];
800}
801
802- (void) setStartAtOpen: (id) sender
803{
804    [fDefaults setBool: [sender state] == NSOnState forKey: @"AutoStartDownload"];
805}
806
807- (void) setUseQueue: (id) sender
808{
809    BOOL useQueue = [sender state] == NSOnState;
810   
811    [fDefaults setBool: useQueue forKey: @"Queue"];
812    [self setQueueNumber: fQueueNumberField];
813    [fQueueNumberField setEnabled: useQueue];
814}
815
816- (void) setQueueNumber: (id) sender
817{
818    int queueNumber = [sender intValue];
819    if (![[sender stringValue] isEqualToString: [NSString stringWithInt: queueNumber]] || queueNumber < 1)
820    {
821        NSBeep();
822        queueNumber = [fDefaults integerForKey: @"QueueDownloadNumber"];
823        [sender setIntValue: queueNumber];
824    }
825    else
826        [fDefaults setInteger: queueNumber forKey: @"QueueDownloadNumber"];
827   
828    [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self];
829}
830
831- (void) setMoveTorrent: (id) sender
832{
833    int state = [sender state];
834    if (sender == fCopyTorrentCheck)
835    {
836        [fDefaults setBool: state forKey: @"SavePrivateTorrent"];
837       
838        [fDeleteOriginalTorrentCheck setEnabled: state];
839        if (state == NSOffState)
840        {
841            [fDeleteOriginalTorrentCheck setState: NSOffState];
842            [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"];
843        }
844    }
845    else
846        [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"];
847}
848
849- (void) setDownloadLocation: (id) sender
850{
851    //Download folder
852    switch ([fFolderPopUp indexOfSelectedItem])
853    {
854        case DOWNLOAD_FOLDER:
855            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
856            break;
857        case DOWNLOAD_TORRENT:
858            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
859            break;
860        case DOWNLOAD_ASK:
861            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
862            break;
863    }
864}
865
866- (void) folderSheetShow: (id) sender
867{
868    NSOpenPanel * panel = [NSOpenPanel openPanel];
869
870    [panel setPrompt: @"Select"];
871    [panel setAllowsMultipleSelection: NO];
872    [panel setCanChooseFiles: NO];
873    [panel setCanChooseDirectories: YES];
874    [panel setCanCreateDirectories: YES];
875
876    [panel beginSheetForDirectory: nil file: nil types: nil
877        modalForWindow: [self window] modalDelegate: self didEndSelector:
878        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
879}
880
881- (void) setAutoImport: (id) sender
882{
883    int state = [fAutoImportCheck state];
884    [fDefaults setBool: state forKey: @"AutoImport"];
885    [fImportFolderPopUp setEnabled: state];
886   
887    if (state == NSOnState)
888        [[UKKQueue sharedFileWatcher] addPath: fImportFolder];
889    else
890        [[UKKQueue sharedFileWatcher] removePathFromQueue: fImportFolder];
891   
892    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
893}
894
895- (void) importFolderSheetShow: (id) sender
896{
897    NSOpenPanel * panel = [NSOpenPanel openPanel];
898
899    [panel setPrompt: @"Select"];
900    [panel setAllowsMultipleSelection: NO];
901    [panel setCanChooseFiles: NO];
902    [panel setCanChooseDirectories: YES];
903    [panel setCanCreateDirectories: YES];
904
905    [panel beginSheetForDirectory: nil file: nil types: nil
906        modalForWindow: [self window] modalDelegate: self didEndSelector:
907        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
908}
909
910- (void) setAutoSize: (id) sender
911{
912    [fDefaults setBool: [sender state] forKey: @"AutoSize"];
913   
914    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
915}
916
917- (void) helpForNetwork: (id) sender
918{
919    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PortForwarding"
920        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
921}
922
923@end
924
925@implementation PrefsController (Private)
926
927- (void) setPrefView: (id) sender
928{
929    NSView * view = fGeneralView;
930    if (sender)
931    {
932        NSString * identifier = [sender itemIdentifier];
933        if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
934            view = fTransfersView;
935        else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
936            view = fBandwidthView;
937        else if ([identifier isEqualToString: TOOLBAR_NETWORK])
938            view = fNetworkView;
939        else;
940    }
941   
942    NSWindow * window = [self window];
943    if ([window contentView] == view)
944        return;
945   
946    NSRect windowRect = [window frame];
947    int difference = [view frame].size.height - [[window contentView] frame].size.height;
948    windowRect.origin.y -= difference;
949    windowRect.size.height += difference;
950   
951    [view setHidden: YES];
952    [window setContentView: view];
953    [window setFrame: windowRect display: YES animate: YES];
954    [view setHidden: NO];
955   
956    //set title label
957    if (sender)
958        [window setTitle: [sender label]];
959    else
960    {
961        NSToolbarItem * item;
962        NSEnumerator * enumerator = [[fToolbar items] objectEnumerator];
963        while ((item = [enumerator nextObject]))
964            if ([[item itemIdentifier] isEqualToString: [fToolbar selectedItemIdentifier]])
965            {
966                [window setTitle: [item label]];
967                break;
968            }
969    }
970   
971    //for network view make sure progress indicator hides itself
972    if (view == fNetworkView && [fPortStatusImage image])
973        [fPortStatusProgress setDisplayedWhenStopped: NO];
974}
975
976- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
977{
978    if (code == NSOKButton)
979    {
980        [fDownloadFolder release];
981        fDownloadFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
982       
983        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
984        [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
985        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
986       
987        [self updatePopUp];
988    }
989    else
990    {
991        //reset if cancelled
992        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
993        if ([downloadChoice isEqualToString: @"Constant"])
994            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
995        else if ([downloadChoice isEqualToString: @"Torrent"])
996            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
997        else
998            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
999    }
1000}
1001
1002- (void) updatePopUp
1003{
1004    //get and resize the icon
1005    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder];
1006    [icon setScalesWhenResized: YES];
1007    [icon setSize: NSMakeSize(16.0, 16.0)];
1008
1009    //update menu item
1010    NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
1011    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
1012    [menuItem setImage: icon];
1013}
1014
1015- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
1016{
1017    if (code == NSOKButton)
1018    {
1019        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
1020        [sharedQueue removePathFromQueue: fImportFolder];
1021       
1022        [fImportFolder release];
1023        fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
1024       
1025        [fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
1026       
1027        [self updateImportPopUp];
1028       
1029        [sharedQueue addPath: fImportFolder];
1030       
1031        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
1032    }
1033    [fImportFolderPopUp selectItemAtIndex: 0];
1034}
1035
1036- (void) updateImportPopUp
1037{
1038    //get and resize the icon
1039    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fImportFolder];
1040    [icon setScalesWhenResized: YES];
1041    [icon setSize: NSMakeSize(16.0, 16.0)];
1042
1043    //update menu item
1044    NSMenuItem * menuItem = (NSMenuItem *) [fImportFolderPopUp itemAtIndex: 0];
1045    [menuItem setTitle: [fImportFolder lastPathComponent]];
1046    [menuItem setImage: icon];
1047}
1048
1049@end
Note: See TracBrowser for help on using the repository browser.