source: trunk/macosx/PrefsController.m @ 981

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

correct image file extension

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