source: branches/new_api/macosx/PrefsController.m @ 156

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

Added [NSString stringWithInt:] for convenience.
UI cosmetic: bigger margin left from the pause/resume button

File size: 12.5 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 MIN_PORT            1
28#define MAX_PORT            65535
29
30#define DOWNLOAD_FOLDER     0
31#define DOWNLOAD_TORRENT    2
32#define DOWNLOAD_ASK        3
33
34#define UPDATE_DAILY        0
35#define UPDATE_WEEKLY       1
36#define UPDATE_NEVER        2
37
38#define TOOLBAR_GENERAL     @"General"
39#define TOOLBAR_NETWORK     @"Network"
40
41@interface PrefsController (Private)
42
43- (void) showGeneralPref: (id) sender;
44- (void) showNetworkPref: (id) sender;
45
46- (void) setPrefView: (NSView *) view;
47
48- (void) folderSheetClosed: (NSOpenPanel *) s returnCode: (int) code
49                                contextInfo: (void *) info;
50- (void) updatePopUp;
51
52@end
53
54@implementation PrefsController
55
56+ (void) initialize
57{
58    [[NSUserDefaults standardUserDefaults] registerDefaults:
59        [NSDictionary dictionaryWithContentsOfFile:
60            [[NSBundle mainBundle] pathForResource: @"Defaults"
61                ofType: @"plist"]]];
62}
63
64- (void)dealloc
65{
66    [fDownloadFolder release];
67    [super dealloc];
68}
69
70- (void) setPrefsWindow: (tr_handle_t *) handle
71{
72    fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
73    [fToolbar setDelegate: self];
74    [fToolbar setAllowsUserCustomization: NO];
75    [fPrefsWindow setToolbar: fToolbar];
76    [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
77    [fToolbar setSizeMode: NSToolbarSizeModeRegular];
78    [[fPrefsWindow standardWindowButton: NSWindowToolbarButton]
79        setFrame: NSZeroRect];
80   
81    [fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
82    [self setPrefView: fGeneralView];
83
84    fDefaults = [NSUserDefaults standardUserDefaults];
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    tr_setUploadLimit( fHandle, checkUpload ? uploadLimit : -1 );
127   
128    //set remove and quit prompts
129    [fQuitCheck setState: [fDefaults boolForKey: @"CheckQuit"] ?
130        NSOnState : NSOffState];
131    [fRemoveCheck setState: [fDefaults boolForKey: @"CheckRemove"] ?
132        NSOnState : NSOffState];
133
134    //set dock badging
135    [fBadgeDownloadRateCheck setState: [fDefaults boolForKey: @"BadgeDownloadRate"]];
136    [fBadgeUploadRateCheck setState: [fDefaults boolForKey: @"BadgeUploadRate"]];
137
138    /* Check for update */
139    NSString * versionCheck  = [fDefaults stringForKey: @"VersionCheck"];
140    if( [versionCheck isEqualToString: @"Daily"] )
141        [fUpdatePopUp selectItemAtIndex: UPDATE_DAILY];
142    else if( [versionCheck isEqualToString: @"Weekly"] )
143        [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
144    else if( [versionCheck isEqualToString: @"Never"] )
145        [fUpdatePopUp selectItemAtIndex: UPDATE_NEVER];
146    else
147    {
148        [fDefaults setObject: @"Weekly" forKey: @"VersionCheck"];
149        [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
150    }
151}
152
153- (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier:
154    (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
155{
156    NSToolbarItem * item;
157    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
158
159    if ([ident isEqualToString: TOOLBAR_GENERAL])
160    {
161        [item setLabel: TOOLBAR_GENERAL];
162        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
163        [item setTarget: self];
164        [item setAction: @selector( showGeneralPref: )];
165    }
166    else if ([ident isEqualToString: TOOLBAR_NETWORK])
167    {
168        [item setLabel: TOOLBAR_NETWORK];
169        [item setImage: [NSImage imageNamed: @"Network.png"]];
170        [item setTarget: self];
171        [item setAction: @selector( showNetworkPref: )];
172    }
173    else
174    {
175        [item release];
176        return nil;
177    }
178
179    return item;
180}
181
182- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
183{
184    return [self toolbarDefaultItemIdentifiers: nil];
185}
186
187- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
188{
189    return [self toolbarAllowedItemIdentifiers: nil];
190}
191
192- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
193{
194    return [NSArray arrayWithObjects:
195            TOOLBAR_GENERAL,
196            TOOLBAR_NETWORK,
197            nil];
198}
199
200- (void) setPort: (id) sender
201{
202    int bindPort = [fPortField intValue];
203   
204    //if value entered is not an int or is not in range do not change
205    if (![[fPortField stringValue] isEqualToString:
206            [NSString stringWithInt: bindPort]]
207            || bindPort < MIN_PORT
208            || bindPort > MAX_PORT)
209    {
210        NSBeep();
211        bindPort = [fDefaults integerForKey: @"BindPort"];
212        [fPortField setIntValue: bindPort];
213    }
214    else
215    {
216        tr_setBindPort( fHandle, bindPort );
217        [fDefaults setInteger: bindPort forKey: @"BindPort"];
218    }
219}
220
221- (void) setLimitUploadCheck: (id) sender
222{
223    BOOL checkUpload = [fUploadCheck state] == NSOnState;
224
225    [fDefaults setBool: checkUpload forKey: @"CheckUpload"];
226   
227    [self setUploadLimit: nil];
228    [fUploadField setEnabled: checkUpload];
229}
230
231- (void) setUploadLimit: (id) sender
232{
233    int uploadLimit = [fUploadField intValue];
234   
235    //if value entered is not an int or is less than 0 do not change
236    if (![[fUploadField stringValue] isEqualToString:
237            [NSString stringWithInt: uploadLimit]] || uploadLimit < 0)
238    {
239        NSBeep();
240        uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
241        [fUploadField setIntValue: uploadLimit];
242    }
243    else
244    {
245        [fDefaults setInteger: uploadLimit forKey: @"UploadLimit"];
246    }
247   
248    if ([fUploadCheck state] == NSOffState || uploadLimit == 0)
249        uploadLimit = -1;
250    tr_setUploadLimit( fHandle, uploadLimit );
251}
252
253- (void) setQuitMessage: (id) sender
254{
255    [fDefaults setBool: ( [fQuitCheck state] == NSOnState )
256        forKey: @"CheckQuit"];
257}
258
259- (void) setRemoveMessage: (id) sender
260{
261    [fDefaults setBool: ( [fRemoveCheck state] == NSOnState )
262        forKey: @"CheckRemove"];
263}
264
265- (void) setBadge: (id) sender
266{   
267    BOOL state = [sender state];
268   
269    if (sender == fBadgeDownloadRateCheck)
270        [fDefaults setBool: state forKey: @"BadgeDownloadRate"];
271    else if (sender == fBadgeUploadRateCheck)
272        [fDefaults setBool: state forKey: @"BadgeUploadRate"];
273    else;
274}
275
276- (void) setUpdate: (id) sender
277{
278    switch( [fUpdatePopUp indexOfSelectedItem] )
279    {
280        case UPDATE_DAILY:
281            [fDefaults setObject: @"Daily" forKey: @"VersionCheck"];
282            break;
283        case UPDATE_WEEKLY:
284            [fDefaults setObject: @"Weekly" forKey: @"VersionCheck"];
285            break;
286        case UPDATE_NEVER:
287            [fDefaults setObject: @"Never" forKey: @"VersionCheck"];
288            break;
289    }
290}
291
292- (void) setDownloadLocation: (id) sender
293{
294    //Download folder
295    switch( [fFolderPopUp indexOfSelectedItem] )
296    {
297        case DOWNLOAD_FOLDER:
298            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
299            break;
300        case DOWNLOAD_TORRENT:
301            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
302            break;
303        case DOWNLOAD_ASK:
304            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
305            break;
306    }
307}
308
309- (void) folderSheetShow: (id) sender
310{
311    NSOpenPanel * panel = [NSOpenPanel openPanel];
312
313    [panel setPrompt:                  @"Select"];
314    [panel setAllowsMultipleSelection:        NO];
315    [panel setCanChooseFiles:                 NO];
316    [panel setCanChooseDirectories:          YES];
317
318    [panel beginSheetForDirectory: NULL file: NULL types: NULL
319        modalForWindow: fPrefsWindow modalDelegate: self didEndSelector:
320        @selector( folderSheetClosed:returnCode:contextInfo: )
321        contextInfo: NULL];
322}
323
324@end // @implementation PrefsController
325
326@implementation PrefsController (Private)
327
328- (void) showGeneralPref: (id) sender
329{
330    [self setPrefView: fGeneralView];
331}
332
333- (void) showNetworkPref: (id) sender
334{
335    [self setPrefView: fNetworkView];
336}
337
338- (void) setPrefView: (NSView *) view
339{
340    NSRect windowRect = [fPrefsWindow frame];
341    int difference = [view frame].size.height - [[fPrefsWindow contentView] frame].size.height;
342
343    windowRect.origin.y -= difference;
344    windowRect.size.height += difference;
345   
346    [fPrefsWindow setTitle: [fToolbar selectedItemIdentifier]];
347    [fPrefsWindow setContentView: fBlankView];
348    [fPrefsWindow setFrame:windowRect display: YES animate: YES];
349    [fPrefsWindow setContentView: view];
350}
351
352- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code
353    contextInfo: (void *) info
354{
355   if (code == NSOKButton)
356   {
357       [fDownloadFolder release];
358       fDownloadFolder = [[openPanel filenames] objectAtIndex: 0];
359       [fDownloadFolder retain];
360
361       [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
362       [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
363       [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
364
365       [self updatePopUp];
366   }
367   else
368   {
369       //reset if cancelled
370       NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
371       if( [downloadChoice isEqualToString: @"Constant"] )
372       {
373           [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
374       }
375       else if( [downloadChoice isEqualToString: @"Torrent"] )
376       {
377           [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
378       }
379       else
380       {
381           [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
382       }
383   }
384}
385
386- (void) updatePopUp
387{
388    NSMenuItem     * menuItem;
389    NSImage        * image32, * image16;
390
391    // Get the icon for the folder
392    image32 = [[NSWorkspace sharedWorkspace] iconForFile:
393                fDownloadFolder];
394    image16 = [[NSImage alloc] initWithSize: NSMakeSize(16,16)];
395
396    // 32x32 -> 16x16 scaling
397    [image16 lockFocus];
398    [[NSGraphicsContext currentContext]
399        setImageInterpolation: NSImageInterpolationHigh];
400    [image32 drawInRect: NSMakeRect(0,0,16,16)
401        fromRect: NSMakeRect(0,0,32,32) operation: NSCompositeCopy
402        fraction: 1.0];
403    [image16 unlockFocus];
404
405    // Update the menu item
406    menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
407    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
408    [menuItem setImage: image16];
409
410    [image16 release];
411}
412
413@end /* @implementation PrefsController (Private) */
Note: See TracBrowser for help on using the repository browser.