source: trunk/macosx/PrefsController.m @ 394

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

A little cleanup, a slightly more straight-forward way of checking the Growl path, and the method to register Growl won't be called through Interface Builder.

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