source: trunk/macosx/PrefsController.m @ 421

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

A lot of cleanup; remove cantfindaname once and for all.

  • Property svn:keywords set to Date Rev Author Id
File size: 16.4 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 421 2006-06-21 14:07:56Z 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
28#define MIN_PORT    1
29#define MAX_PORT    65535
30
31#define DOWNLOAD_FOLDER     0
32#define DOWNLOAD_TORRENT    2
33#define DOWNLOAD_ASK        3
34
35#define UPDATE_DAILY    0
36#define UPDATE_WEEKLY   1
37#define UPDATE_NEVER    2
38
39#define TOOLBAR_GENERAL     @"General"
40#define TOOLBAR_TRANSFERS   @"Transfers"
41#define TOOLBAR_NETWORK     @"Network"
42
43@interface PrefsController (Private)
44
45- (void) showGeneralPref: (id) sender;
46- (void) showTransfersPref: (id) sender;
47- (void) showNetworkPref: (id) sender;
48
49- (void) setPrefView: (NSView *) view;
50
51- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
52- (void) updatePopUp;
53
54@end
55
56@implementation PrefsController
57
58+ (void) initialize
59{
60    [[NSUserDefaults standardUserDefaults] registerDefaults:
61        [NSDictionary dictionaryWithContentsOfFile:
62        [[NSBundle mainBundle] pathForResource: @"Defaults" ofType: @"plist"]]];
63}
64
65- (void) dealloc
66{
67    [fDownloadFolder release];
68    [super dealloc];
69}
70
71- (void) setPrefs: (tr_handle_t *) handle
72{
73    fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
74    [fToolbar setDelegate: self];
75    [fToolbar setAllowsUserCustomization: NO];
76    [[self window] setToolbar: fToolbar];
77    [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
78    [fToolbar setSizeMode: NSToolbarSizeModeRegular];
79
80    [fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
81    [self showGeneralPref: nil];
82
83    fDefaults = [NSUserDefaults standardUserDefaults];
84    fHandle = handle;
85   
86    //set download folder
87    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
88    fDownloadFolder = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
89    [fDownloadFolder retain];
90
91    if ([downloadChoice isEqualToString: @"Constant"])
92        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
93    else if ([downloadChoice isEqualToString: @"Torrent"])
94        [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
95    else
96        [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
97    [self updatePopUp];
98
99    //set bind port
100    int bindPort = [fDefaults integerForKey: @"BindPort"];
101    [fPortField setIntValue: bindPort];
102    tr_setBindPort(fHandle, bindPort);
103   
104    //checks for old version upload speed of -1
105    if ([fDefaults integerForKey: @"UploadLimit"] < 0)
106    {
107        [fDefaults setInteger: 20 forKey: @"UploadLimit"];
108        [fDefaults setBool: NO forKey: @"CheckUpload"];
109    }
110   
111    //set upload limit
112    BOOL checkUpload = [fDefaults boolForKey: @"CheckUpload"];
113    int uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
114   
115    [fUploadCheck setState: checkUpload];
116    [fUploadField setIntValue: uploadLimit];
117    [fUploadField setEnabled: checkUpload];
118   
119    tr_setUploadLimit(fHandle, checkUpload ? uploadLimit : -1);
120
121        //set download limit
122    BOOL checkDownload = [fDefaults boolForKey: @"CheckDownload"];
123    int downloadLimit = [fDefaults integerForKey: @"DownloadLimit"];
124   
125    [fDownloadCheck setState: checkDownload];
126    [fDownloadField setIntValue: downloadLimit];
127    [fDownloadField setEnabled: checkDownload];
128   
129    tr_setDownloadLimit(fHandle, checkDownload ? downloadLimit : -1);
130   
131    //set ratio limit
132    BOOL ratioCheck = [fDefaults boolForKey: @"RatioCheck"];
133    [fRatioCheck setState: ratioCheck];
134    [fRatioField setEnabled: ratioCheck];
135    [fRatioField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
136   
137    //set remove and quit prompts
138    [fQuitCheck setState: [fDefaults boolForKey: @"CheckQuit"]];
139    [fRemoveCheck setState: [fDefaults boolForKey: @"CheckRemove"]];
140
141    //set dock badging
142    [fBadgeDownloadRateCheck setState: [fDefaults boolForKey: @"BadgeDownloadRate"]];
143    [fBadgeUploadRateCheck setState: [fDefaults boolForKey: @"BadgeUploadRate"]];
144   
145    //set auto start
146    [fAutoStartCheck setState: [fDefaults boolForKey: @"AutoStartDownload"]];
147   
148    //set private torrents
149    BOOL copyTorrents = [fDefaults boolForKey: @"SavePrivateTorrent"];
150    [fCopyTorrentCheck setState: copyTorrents];
151   
152    [fDeleteOriginalTorrentCheck setEnabled: copyTorrents];
153    [fDeleteOriginalTorrentCheck setState: [fDefaults boolForKey: @"DeleteOriginalTorrent"]];
154
155    //set update check
156    NSString * updateCheck = [fDefaults stringForKey: @"UpdateCheck"];
157    if ([updateCheck isEqualToString: @"Weekly"])
158        [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
159    else if ([updateCheck isEqualToString: @"Never"])
160        [fUpdatePopUp selectItemAtIndex: UPDATE_NEVER];
161    else
162        [fUpdatePopUp selectItemAtIndex: UPDATE_DAILY];
163}
164
165- (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier:
166    (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
167{
168    NSToolbarItem * item;
169    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
170
171    if ([ident isEqualToString: TOOLBAR_GENERAL])
172    {
173        [item setLabel: TOOLBAR_GENERAL];
174        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
175        [item setTarget: self];
176        [item setAction: @selector( showGeneralPref: )];
177    }
178    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
179    {
180        [item setLabel: TOOLBAR_TRANSFERS];
181        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
182        [item setTarget: self];
183        [item setAction: @selector( showTransfersPref: )];
184    }
185    else if ([ident isEqualToString: TOOLBAR_NETWORK])
186    {
187        [item setLabel: TOOLBAR_NETWORK];
188        [item setImage: [NSImage imageNamed: @"Network.png"]];
189        [item setTarget: self];
190        [item setAction: @selector( showNetworkPref: )];
191    }
192    else
193    {
194        [item release];
195        return nil;
196    }
197
198    return item;
199}
200
201- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
202{
203    return [self toolbarDefaultItemIdentifiers: nil];
204}
205
206- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
207{
208    return [self toolbarAllowedItemIdentifiers: nil];
209}
210
211- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
212{
213    return [NSArray arrayWithObjects:
214            TOOLBAR_GENERAL, TOOLBAR_TRANSFERS,
215            TOOLBAR_NETWORK, nil];
216}
217
218- (void) setPort: (id) sender
219{
220    int bindPort = [sender intValue];
221    if (![[NSString stringWithInt: bindPort] isEqualToString: [sender stringValue]]
222            || bindPort < MIN_PORT || bindPort > MAX_PORT)
223    {
224        NSBeep();
225        bindPort = [fDefaults integerForKey: @"BindPort"];
226        [sender setIntValue: bindPort];
227    }
228    else
229    {
230        tr_setBindPort( fHandle, bindPort );
231        [fDefaults setInteger: bindPort forKey: @"BindPort"];
232    }
233}
234
235- (void) setLimit: (id) sender
236{
237    NSString * key;
238    NSButton * check;
239    NSString * type;
240    if (sender == fUploadField)
241    {
242        key = @"UploadLimit";
243        check = fUploadCheck;
244        type = @"Upload";
245    }
246    else
247    {
248        key = @"DownloadLimit";
249        check = fDownloadCheck;
250        type = @"Download";
251    }
252
253    int limit = [sender intValue];
254    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]]
255            || limit < 0)
256    {
257        NSBeep();
258        limit = [fDefaults integerForKey: key];
259        [sender setIntValue: limit];
260    }
261    else
262    {
263        if (sender == fUploadField)
264            tr_setUploadLimit(fHandle, [fUploadCheck state] == NSOffState ? -1 : limit);
265        else
266            tr_setDownloadLimit(fHandle, [fDownloadCheck state] == NSOffState ? -1 : limit);
267       
268        [fDefaults setInteger: limit forKey: key];
269    }
270   
271    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
272                                    [NSNumber numberWithBool: [check state]], @"Enable",
273                                    [NSNumber numberWithInt: limit], @"Limit",
274                                    type, @"Type", nil];
275    [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange" object: dict];
276}
277
278- (void) setLimitCheck: (id) sender
279{
280    NSString * key;
281    NSTextField * field;
282    if (sender == fUploadCheck)
283    {
284        key = @"CheckUpload";
285        field = fUploadField;
286    }
287    else
288    {
289        key = @"CheckDownload";
290        field = fDownloadField;
291    }
292   
293    BOOL check = [sender state] == NSOnState;
294    [self setLimit: field];
295    [field setEnabled: check];
296   
297    [fDefaults setBool: check forKey: key];
298}
299
300- (void) setLimitEnabled: (BOOL) enable type: (NSString *) type
301{
302    NSButton * check = [type isEqualToString: @"Upload"] ? fUploadCheck : fDownloadCheck;
303    [check setState: enable ? NSOnState : NSOffState];
304    [self setLimitCheck: check];
305}
306
307- (void) setQuickLimit: (int) limit type: (NSString *) type
308{
309    NSButton * check;
310    if ([type isEqualToString: @"Upload"])
311    {
312        [fUploadField setIntValue: limit];
313        check = fUploadCheck;
314    }
315    else
316    {
317        [fDownloadField setIntValue: limit];
318        check = fDownloadCheck;
319    }
320    [check setState: NSOnState];
321    [self setLimitCheck: check];
322}
323
324- (void) setRatio: (id) sender
325{
326    float ratioLimit = [sender floatValue];
327    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
328            || ratioLimit < 0)
329    {
330        NSBeep();
331        ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
332        [sender setFloatValue: ratioLimit];
333    }
334    else
335        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
336   
337    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
338                                    [NSNumber numberWithBool: [fRatioCheck state]], @"Enable",
339                                    [NSNumber numberWithFloat: ratioLimit], @"Ratio", nil];
340    [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: dict];
341}
342
343- (void) setRatioCheck: (id) sender
344{
345    BOOL check = [sender state] == NSOnState;
346    [self setRatio: fRatioField];
347    [fRatioField setEnabled: check];
348   
349    [fDefaults setBool: check forKey: @"RatioCheck"];
350}
351
352- (void) setRatioEnabled: (BOOL) enable
353{
354    int state = enable ? NSOnState : NSOffState;
355   
356    [fRatioCheck setState: state];
357    [self setRatioCheck: fRatioCheck];
358}
359
360- (void) setQuickRatio: (float) ratioLimit
361{
362    [fRatioField setFloatValue: ratioLimit];
363   
364    [fRatioCheck setState: NSOnState];
365    [self setRatioCheck: fRatioCheck];
366}
367
368- (void) setShowMessage: (id) sender
369{
370    if (sender == fQuitCheck)
371        [fDefaults setBool: [sender state] forKey: @"CheckQuit"];
372    else if (sender == fRemoveCheck)
373        [fDefaults setBool: [fRemoveCheck state] forKey: @"CheckRemove"];
374    else;
375}
376
377- (void) setBadge: (id) sender
378{   
379    if (sender == fBadgeDownloadRateCheck)
380        [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
381    else if (sender == fBadgeUploadRateCheck)
382        [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
383    else;
384}
385
386- (void) setUpdate: (id) sender
387{
388    int index = [fUpdatePopUp indexOfSelectedItem];
389    NSTimeInterval seconds;
390    if (index == UPDATE_DAILY)
391    {
392        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
393        seconds = 86400;
394    }
395    else if (index == UPDATE_WEEKLY)
396    {
397        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
398        seconds = 604800;
399    }
400    else
401    {
402        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
403        seconds = 0;
404    }
405
406    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
407    [fUpdater scheduleCheckWithInterval: seconds];
408}
409
410- (void) setAutoStart: (id) sender
411{
412    [fDefaults setBool: [sender state] forKey: @"AutoStartDownload"];
413}
414
415- (void) setMoveTorrent: (id) sender
416{
417    int state = [sender state];
418    if (sender == fCopyTorrentCheck)
419    {
420        [fDefaults setBool: state forKey: @"SavePrivateTorrent"];
421       
422        [fDeleteOriginalTorrentCheck setEnabled: state];
423        if (state == NSOffState)
424        {
425            [fDeleteOriginalTorrentCheck setState: NSOffState];
426            [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"];
427        }
428    }
429    else
430        [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"];
431}
432
433- (void) setDownloadLocation: (id) sender
434{
435    //Download folder
436    switch ([fFolderPopUp indexOfSelectedItem])
437    {
438        case DOWNLOAD_FOLDER:
439            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
440            break;
441        case DOWNLOAD_TORRENT:
442            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
443            break;
444        case DOWNLOAD_ASK:
445            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
446            break;
447    }
448}
449
450- (void) checkUpdate
451{
452    [fUpdater checkForUpdates: nil];
453}
454
455- (void) folderSheetShow: (id) sender
456{
457    NSOpenPanel * panel = [NSOpenPanel openPanel];
458
459    [panel setPrompt: @"Select"];
460    [panel setAllowsMultipleSelection: NO];
461    [panel setCanChooseFiles: NO];
462    [panel setCanChooseDirectories: YES];
463
464    [panel beginSheetForDirectory: nil file: nil types: nil
465        modalForWindow: [self window] modalDelegate: self didEndSelector:
466        @selector( folderSheetClosed:returnCode:contextInfo: )
467        contextInfo: nil];
468}
469
470@end
471
472@implementation PrefsController (Private)
473
474- (void) showGeneralPref: (id) sender
475{
476    [self setPrefView: fGeneralView];
477}
478
479- (void) showTransfersPref: (id) sender
480{
481    [self setPrefView: fTransfersView];
482}
483
484- (void) showNetworkPref: (id) sender
485{
486    [self setPrefView: fNetworkView];
487}
488
489- (void) setPrefView: (NSView *) view
490{
491    NSWindow * window = [self window];
492   
493    NSRect windowRect = [window frame];
494    int difference = [view frame].size.height - [[window contentView] frame].size.height;
495    windowRect.origin.y -= difference;
496    windowRect.size.height += difference;
497
498    [window setTitle: [fToolbar selectedItemIdentifier]];
499   
500    [window setContentView: view];
501    [view setHidden: YES];
502    [window setFrame: windowRect display: YES animate: YES];
503    [view setHidden: NO];
504}
505
506- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
507{
508   if (code == NSOKButton)
509   {
510       [fDownloadFolder release];
511       fDownloadFolder = [[openPanel filenames] objectAtIndex: 0];
512       [fDownloadFolder retain];
513
514       [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
515       [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
516       [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
517
518       [self updatePopUp];
519   }
520   else
521   {
522       //reset if cancelled
523       NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
524       if ([downloadChoice isEqualToString: @"Constant"])
525           [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
526       else if ([downloadChoice isEqualToString: @"Torrent"])
527           [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
528       else
529           [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
530   }
531}
532
533- (void) updatePopUp
534{
535    //get and resize the icon
536    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder];
537    [icon setScalesWhenResized: YES];
538    [icon setSize: NSMakeSize(16.0, 16.0)];
539
540    //update menu item
541    NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
542    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
543    [menuItem setImage: icon];
544}
545
546@end
Note: See TracBrowser for help on using the repository browser.