source: trunk/macosx/PrefsController.m @ 261

Last change on this file since 261 was 261, checked in by titer, 16 years ago

Updated svn:keywords

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