source: trunk/macosx/PrefsController.m @ 256

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

Merged Mitchell's changes, this includes:
Adds stop at ratio,
Adds sorting by name, date or state,
Seperates Info window into its own nib,
Adds option to enable or disabled autostarting downloads,
Seperates Network / Transfers in the Preferences

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