source: trunk/macosx/PrefsController.m @ 1033

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

remove some redundant warnings

  • Property svn:keywords set to Date Rev Author Id
File size: 35.0 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 1033 2006-10-25 13:36:30Z 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    [fDefaults setBool: check forKey: key];
510   
511    [self setLimit: field];
512    [field setEnabled: check];
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    if (fToolbar)
539    {
540        NSButton * check;
541        if ([type isEqualToString: @"Upload"])
542        {
543            [fUploadField setIntValue: limit];
544            check = fUploadCheck;
545        }
546        else
547        {
548            [fDownloadField setIntValue: limit];
549            check = fDownloadCheck;
550        }
551        [check setState: NSOnState];
552        [self setLimitCheck: check];
553    }
554    else
555    {
556        BOOL upload = [type isEqualToString: @"Upload"];
557        [fDefaults setBool: YES forKey: upload ? @"CheckUpload" : @"CheckDownload"];
558        [fDefaults setInteger: limit forKey: upload ? @"UploadLimit" : @"DownloadLimit"];
559       
560        [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange"
561                                                object: [[NSNumber alloc] initWithBool: upload]];
562    }
563}
564
565- (void) enableSpeedLimit: (BOOL) enable
566{
567    if ([fDefaults boolForKey: @"SpeedLimit"] != enable)
568    {
569        [fDefaults setBool: enable forKey: @"SpeedLimit"];
570       
571        if (enable)
572        {
573            tr_setUploadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
574            tr_setDownloadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
575        }
576        else
577        {
578            tr_setUploadLimit(fHandle, [fDefaults boolForKey: @"CheckUpload"]
579                                        ? [fDefaults integerForKey: @"UploadLimit"] : -1);
580            tr_setDownloadLimit(fHandle, [fDefaults boolForKey: @"CheckDownload"]
581                                            ? [fDefaults integerForKey: @"DownloadLimit"] : -1);
582        }
583    }
584}
585
586- (void) setSpeedLimit: (id) sender
587{
588    NSString * key = sender == fSpeedLimitUploadField ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit";
589
590    int limit = [sender intValue];
591    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0)
592    {
593        NSBeep();
594        limit = [fDefaults integerForKey: key];
595        [sender setIntValue: limit];
596    }
597    else
598    {
599        if ([fDefaults boolForKey: @"SpeedLimit"])
600        {
601            if (sender == fSpeedLimitUploadField)
602                tr_setUploadLimit(fHandle, limit);
603            else
604                tr_setDownloadLimit(fHandle, limit);
605        }
606       
607        [fDefaults setInteger: limit forKey: key];
608    }
609}
610
611- (void) setAutoSpeedLimitCheck: (id) sender
612{
613    BOOL check = [sender state] == NSOnState;
614   
615    [fDefaults setBool: check forKey: @"SpeedLimitAuto"];
616
617    [self setAutoSpeedLimitHour: fSpeedLimitAutoOnField];
618    [fSpeedLimitAutoOnField setEnabled: check];
619   
620    [self setAutoSpeedLimitHour: fSpeedLimitAutoOffField];
621    [fSpeedLimitAutoOffField setEnabled: check];
622}
623
624- (void) setAutoSpeedLimitHour: (id) sender
625{
626    NSString * key = (sender == fSpeedLimitAutoOnField) ? @"SpeedLimitAutoOnHour" : @"SpeedLimitAutoOffHour";
627
628    int hour = [sender intValue];
629   
630    //allow numbers under ten in the format 0x
631    if (!([[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", hour]]
632        || [[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%02d", hour]]) || hour < 0 || hour > 23
633        || [fSpeedLimitAutoOnField intValue] == [fSpeedLimitAutoOffField intValue])
634    {
635        NSBeep();
636        hour = [fDefaults integerForKey: key];
637        [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]];
638    }
639    else
640        [fDefaults setInteger: hour forKey: key];
641   
642    [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]]; //ensure number has 2 digits
643   
644    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
645}
646
647- (void) setRatio: (id) sender
648{
649    float ratioLimit = [sender floatValue];
650    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
651            || ratioLimit < 0)
652    {
653        NSBeep();
654        ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
655        [sender setFloatValue: ratioLimit];
656    }
657    else
658        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
659   
660    [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: nil];
661}
662
663- (void) setRatioCheck: (id) sender
664{
665    BOOL check = [sender state] == NSOnState;
666    [fDefaults setBool: check forKey: @"RatioCheck"];
667   
668    [self setRatio: fRatioField];
669    [fRatioField setEnabled: check];
670}
671
672- (void) setQuickRatioEnabled: (BOOL) enable
673{
674    //if interface is loaded, perform change as if user performed it
675    if (fToolbar)
676    {
677        int state = enable ? NSOnState : NSOffState;
678       
679        [fRatioCheck setState: state];
680        [self setRatioCheck: fRatioCheck];
681    }
682    else
683    {
684        [fDefaults setBool: enable forKey: @"RatioCheck"];
685       
686        [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: nil];
687    }
688}
689
690- (void) setQuickRatio: (float) ratioLimit
691{
692    //if interface is loaded, perform change as if user performed it
693    if (fToolbar)
694    {
695        [fRatioField setFloatValue: ratioLimit];
696       
697        [fRatioCheck setState: NSOnState];
698        [self setRatioCheck: fRatioCheck];
699    }
700    else
701    {
702        [fDefaults setBool: YES forKey: @"RatioCheck"];
703        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
704       
705        [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: nil];
706    }
707}
708
709- (void) setShowMessage: (id) sender
710{
711    BOOL state = [sender state];
712
713    if (sender == fQuitCheck)
714    {
715        [fDefaults setBool: state forKey: @"CheckQuit"];
716        [fQuitDownloadingCheck setEnabled: state];
717    }
718    else if (sender == fRemoveCheck)
719    {
720        [fDefaults setBool: state forKey: @"CheckRemove"];
721        [fRemoveDownloadingCheck setEnabled: state];
722    }
723    if (sender == fQuitDownloadingCheck)
724        [fDefaults setBool: state forKey: @"CheckQuitDownloading"];
725    else if (sender == fRemoveDownloadingCheck)
726        [fDefaults setBool: state forKey: @"CheckRemoveDownloading"];
727    else;
728}
729
730- (void) setBadge: (id) sender
731{   
732    if (sender == fBadgeDownloadRateCheck)
733        [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
734    else if (sender == fBadgeUploadRateCheck)
735        [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
736    else;
737   
738    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
739}
740
741- (void) setPlaySound: (id) sender
742{
743    BOOL state = [sender state];
744
745    if (sender == fPlayDownloadSoundCheck)
746    {
747        [fDownloadSoundPopUp setEnabled: state];
748        [fDefaults setBool: state forKey: @"PlayDownloadSound"];
749    }
750    else if (sender == fPlaySeedingSoundCheck)
751    {
752        [fSeedingSoundPopUp setEnabled: state];
753        [fDefaults setBool: state forKey: @"PlaySeedingSound"];
754    }
755    else;
756}
757
758- (void) setSound: (id) sender
759{
760    //play sound when selecting
761    NSString * soundName = [sender titleOfSelectedItem];
762    NSSound * sound;
763    if ((sound = [NSSound soundNamed: soundName]))
764        [sound play];
765
766    if (sender == fDownloadSoundPopUp)
767        [fDefaults setObject: soundName forKey: @"DownloadSound"];
768    else if (sender == fSeedingSoundPopUp)
769        [fDefaults setObject: soundName forKey: @"SeedingSound"];
770    else;
771}
772
773- (void) setUpdate: (id) sender
774{
775    int index = [fUpdatePopUp indexOfSelectedItem];
776    NSTimeInterval seconds;
777    if (index == UPDATE_DAILY)
778    {
779        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
780        seconds = 86400;
781    }
782    else if (index == UPDATE_WEEKLY)
783    {
784        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
785        seconds = 604800;
786    }
787    else
788    {
789        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
790        seconds = 0;
791    }
792
793    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
794   
795    if (fUpdater)
796        [fUpdater scheduleCheckWithInterval: seconds];
797}
798
799- (void) setStartAtOpen: (id) sender
800{
801    [fDefaults setBool: [sender state] == NSOnState forKey: @"AutoStartDownload"];
802}
803
804- (void) setUseQueue: (id) sender
805{
806    BOOL useQueue = [sender state] == NSOnState;
807   
808    [fDefaults setBool: useQueue forKey: @"Queue"];
809    [self setQueueNumber: fQueueNumberField];
810    [fQueueNumberField setEnabled: useQueue];
811}
812
813- (void) setQueueNumber: (id) sender
814{
815    int queueNumber = [sender intValue];
816    if (![[sender stringValue] isEqualToString: [NSString stringWithInt: queueNumber]] || queueNumber < 1)
817    {
818        NSBeep();
819        queueNumber = [fDefaults integerForKey: @"QueueDownloadNumber"];
820        [sender setIntValue: queueNumber];
821    }
822    else
823        [fDefaults setInteger: queueNumber forKey: @"QueueDownloadNumber"];
824   
825    [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self];
826}
827
828- (void) setMoveTorrent: (id) sender
829{
830    int state = [sender state];
831    if (sender == fCopyTorrentCheck)
832    {
833        [fDefaults setBool: state forKey: @"SavePrivateTorrent"];
834       
835        [fDeleteOriginalTorrentCheck setEnabled: state];
836        if (state == NSOffState)
837        {
838            [fDeleteOriginalTorrentCheck setState: NSOffState];
839            [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"];
840        }
841    }
842    else
843        [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"];
844}
845
846- (void) setDownloadLocation: (id) sender
847{
848    //Download folder
849    switch ([fFolderPopUp indexOfSelectedItem])
850    {
851        case DOWNLOAD_FOLDER:
852            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
853            break;
854        case DOWNLOAD_TORRENT:
855            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
856            break;
857        case DOWNLOAD_ASK:
858            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
859            break;
860    }
861}
862
863- (void) folderSheetShow: (id) sender
864{
865    NSOpenPanel * panel = [NSOpenPanel openPanel];
866
867    [panel setPrompt: @"Select"];
868    [panel setAllowsMultipleSelection: NO];
869    [panel setCanChooseFiles: NO];
870    [panel setCanChooseDirectories: YES];
871    [panel setCanCreateDirectories: YES];
872
873    [panel beginSheetForDirectory: nil file: nil types: nil
874        modalForWindow: [self window] modalDelegate: self didEndSelector:
875        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
876}
877
878- (void) setAutoImport: (id) sender
879{
880    int state = [fAutoImportCheck state];
881    [fDefaults setBool: state forKey: @"AutoImport"];
882    [fImportFolderPopUp setEnabled: state];
883   
884    if (state == NSOnState)
885        [[UKKQueue sharedFileWatcher] addPath: fImportFolder];
886    else
887        [[UKKQueue sharedFileWatcher] removePathFromQueue: fImportFolder];
888   
889    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
890}
891
892- (void) importFolderSheetShow: (id) sender
893{
894    NSOpenPanel * panel = [NSOpenPanel openPanel];
895
896    [panel setPrompt: @"Select"];
897    [panel setAllowsMultipleSelection: NO];
898    [panel setCanChooseFiles: NO];
899    [panel setCanChooseDirectories: YES];
900    [panel setCanCreateDirectories: YES];
901
902    [panel beginSheetForDirectory: nil file: nil types: nil
903        modalForWindow: [self window] modalDelegate: self didEndSelector:
904        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
905}
906
907- (void) setAutoSize: (id) sender
908{
909    [fDefaults setBool: [sender state] forKey: @"AutoSize"];
910   
911    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
912}
913
914- (void) helpForNetwork: (id) sender
915{
916    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PortForwarding"
917        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
918}
919
920@end
921
922@implementation PrefsController (Private)
923
924- (void) setPrefView: (id) sender
925{
926    NSView * view = fGeneralView;
927    if (sender)
928    {
929        NSString * identifier = [sender itemIdentifier];
930        if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
931            view = fTransfersView;
932        else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
933            view = fBandwidthView;
934        else if ([identifier isEqualToString: TOOLBAR_NETWORK])
935            view = fNetworkView;
936        else;
937    }
938   
939    NSWindow * window = [self window];
940    if ([window contentView] == view)
941        return;
942   
943    NSRect windowRect = [window frame];
944    int difference = [view frame].size.height - [[window contentView] frame].size.height;
945    windowRect.origin.y -= difference;
946    windowRect.size.height += difference;
947   
948    [view setHidden: YES];
949    [window setContentView: view];
950    [window setFrame: windowRect display: YES animate: YES];
951    [view setHidden: NO];
952   
953    //set title label
954    if (sender)
955        [window setTitle: [sender label]];
956    else
957    {
958        NSToolbarItem * item;
959        NSEnumerator * enumerator = [[fToolbar items] objectEnumerator];
960        while ((item = [enumerator nextObject]))
961            if ([[item itemIdentifier] isEqualToString: [fToolbar selectedItemIdentifier]])
962            {
963                [window setTitle: [item label]];
964                break;
965            }
966    }
967   
968    //for network view make sure progress indicator hides itself
969    if (view == fNetworkView && [fPortStatusImage image])
970        [fPortStatusProgress setDisplayedWhenStopped: NO];
971}
972
973- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
974{
975    if (code == NSOKButton)
976    {
977        [fDownloadFolder release];
978        fDownloadFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
979       
980        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
981        [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
982        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
983       
984        [self updatePopUp];
985    }
986    else
987    {
988        //reset if cancelled
989        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
990        if ([downloadChoice isEqualToString: @"Constant"])
991            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
992        else if ([downloadChoice isEqualToString: @"Torrent"])
993            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
994        else
995            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
996    }
997}
998
999- (void) updatePopUp
1000{
1001    //get and resize the icon
1002    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder];
1003    [icon setScalesWhenResized: YES];
1004    [icon setSize: NSMakeSize(16.0, 16.0)];
1005
1006    //update menu item
1007    NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
1008    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
1009    [menuItem setImage: icon];
1010}
1011
1012- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
1013{
1014    if (code == NSOKButton)
1015    {
1016        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
1017        [sharedQueue removePathFromQueue: fImportFolder];
1018       
1019        [fImportFolder release];
1020        fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
1021       
1022        [fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
1023       
1024        [self updateImportPopUp];
1025       
1026        [sharedQueue addPath: fImportFolder];
1027       
1028        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
1029    }
1030    [fImportFolderPopUp selectItemAtIndex: 0];
1031}
1032
1033- (void) updateImportPopUp
1034{
1035    //get and resize the icon
1036    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fImportFolder];
1037    [icon setScalesWhenResized: YES];
1038    [icon setSize: NSMakeSize(16.0, 16.0)];
1039
1040    //update menu item
1041    NSMenuItem * menuItem = (NSMenuItem *) [fImportFolderPopUp itemAtIndex: 0];
1042    [menuItem setTitle: [fImportFolder lastPathComponent]];
1043    [menuItem setImage: icon];
1044}
1045
1046@end
Note: See TracBrowser for help on using the repository browser.