source: trunk/macosx/PrefsController.m @ 847

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

Use UKKQueue for auto import which gets rid of polling and makes the process instantaneous.

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