source: trunk/macosx/PrefsController.m @ 19

Last change on this file since 19 was 19, checked in by root, 16 years ago

Update 2005-12-21

File size: 11.6 KB
Line 
1/******************************************************************************
2 * Copyright (c) 2005 Eric Petit
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#include "PrefsController.h"
24
25#define DEFAULT_UPLOAD      @"20"
26#define MIN_PORT            1
27#define MAX_PORT            65535
28
29#define DOWNLOAD_FOLDER     0
30#define DOWNLOAD_TORRENT    1
31#define DOWNLOAD_ASK        2
32
33#define TOOLBAR_GENERAL     @"General"
34#define TOOLBAR_NETWORK     @"Network"
35
36@interface PrefsController (Private)
37
38- (void) showGeneralPref: (id) sender;
39- (void) showNetworkPref: (id) sender;
40
41- (void) setPrefView: (NSView *) view;
42
43- (void) folderSheetClosed: (NSOpenPanel *) s returnCode: (int) code
44                                contextInfo: (void *) info;
45- (void) updatePopUp;
46
47@end
48
49@implementation PrefsController
50
51+ (void) initialize
52{
53    NSDictionary   * appDefaults;
54    NSString       * desktop, * port;
55
56    /* Register defaults settings:
57        - Simple bar
58        - Always download to Desktop
59        - Port TR_DEFAULT_PORT
60        - Upload limit DEFAULT_UPLOAD
61        - Do not limit upload
62        - Ask before quitting
63        - Ask before removing */
64    desktop = [NSHomeDirectory() stringByAppendingString: @"/Desktop"];
65    port = [NSString stringWithFormat: @"%d", TR_DEFAULT_PORT];
66
67    appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
68                    @"NO",          @"UseAdvancedBar",
69                    @"Constant",    @"DownloadChoice",
70                    desktop,        @"DownloadFolder",
71                    port,           @"BindPort",
72                    DEFAULT_UPLOAD, @"UploadLimit",
73                    @"YES",         @"CheckUpload",
74                    @"YES",         @"CheckQuit",
75                    @"YES",         @"CheckRemove",
76                    NULL];
77    [[NSUserDefaults standardUserDefaults] registerDefaults: appDefaults];
78}
79
80- (void)dealloc
81{
82    [fDownloadFolder release];
83    [super dealloc];
84}
85
86- (void) setPrefsWindow: (tr_handle_t *) handle
87{
88    fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
89    [fToolbar setDelegate: self];
90    [fToolbar setAllowsUserCustomization: NO];
91    [fPrefsWindow setToolbar: fToolbar];
92    [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
93    [fToolbar setSizeMode: NSToolbarSizeModeRegular];
94    [fPrefsWindow setShowsToolbarButton: NO];
95   
96    [fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
97    [self setPrefView: fGeneralView];
98
99    fDefaults = [NSUserDefaults standardUserDefaults];
100   
101    //set download folder
102    NSString * downloadChoice  = [fDefaults stringForKey: @"DownloadChoice"];
103    fDownloadFolder = [fDefaults stringForKey: @"DownloadFolder"];
104    [fDownloadFolder retain];
105
106    if( [downloadChoice isEqualToString: @"Constant"] )
107    {
108        [fFolderMatrix selectCellAtRow: DOWNLOAD_FOLDER column: 0];
109    }
110    else if( [downloadChoice isEqualToString: @"Torrent"] )
111    {
112        [fFolderMatrix selectCellAtRow: DOWNLOAD_TORRENT column: 0];
113    }
114    else
115    {
116        [fFolderMatrix selectCellAtRow: DOWNLOAD_ASK column: 0];
117    }
118    [self updatePopUp];
119    [fFolderPopUp setEnabled: [fFolderMatrix selectedRow] == 0];
120
121    //set bind port
122    int bindPort = [fDefaults integerForKey: @"BindPort"];
123    [fPortField setIntValue: bindPort];
124    fHandle = handle;
125    tr_setBindPort( fHandle, bindPort );
126   
127    //checks for old version upload speed of -1
128    if ([fDefaults integerForKey: @"UploadLimit"] < 0)
129    {
130        [fDefaults setObject: DEFAULT_UPLOAD forKey: @"UploadLimit"];
131        [fDefaults setObject: @"NO" forKey: @"CheckUpload"];
132    }
133   
134    //set upload limit
135    BOOL checkUpload = [[fDefaults stringForKey: @"CheckUpload"] isEqualToString:@"YES"];
136    int uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
137   
138    [fUploadCheck setState: checkUpload ? NSOnState : NSOffState];
139    [fUploadField setIntValue: uploadLimit];
140    [fUploadField setEnabled: checkUpload];
141   
142    if (!checkUpload || uploadLimit == 0)
143        uploadLimit = -1;
144    tr_setUploadLimit( fHandle, uploadLimit );
145   
146    //set remove and quit prompts
147    [fQuitCheck setState:([[fDefaults stringForKey: @"CheckQuit"]
148                isEqualToString:@"YES"] ? NSOnState : NSOffState)];
149    [fRemoveCheck setState:([[fDefaults stringForKey: @"CheckRemove"]
150                isEqualToString:@"YES"] ? NSOnState : NSOffState)];
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 stringWithFormat: @"%d", 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 setObject: [NSString stringWithFormat: @"%d", bindPort]
218                    forKey: @"BindPort"];
219    }
220}
221
222- (void) setLimitUploadCheck: (id) sender
223{
224    BOOL checkUpload = [fUploadCheck state] == NSOnState;
225
226    [fDefaults setObject: checkUpload ? @"YES" : @"NO"
227                            forKey: @"CheckUpload"];
228   
229    [self setUploadLimit: sender];
230    [fUploadField setEnabled: checkUpload];
231}
232
233- (void) setUploadLimit: (id) sender
234{
235    int uploadLimit = [fUploadField intValue];
236   
237    //if value entered is not an int or is less than 0 do not change
238    if (![[fUploadField stringValue] isEqualToString:
239            [NSString stringWithFormat: @"%d", uploadLimit]]
240            || uploadLimit < 0)
241    {
242        NSBeep();
243        uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
244        [fUploadField setIntValue: uploadLimit];
245    }
246    else
247    {
248        [fDefaults setObject: [NSString stringWithFormat: @"%d", uploadLimit]
249            forKey: @"UploadLimit"];
250    }
251   
252    if ([fUploadCheck state] == NSOffState || uploadLimit == 0)
253        uploadLimit = -1;
254    tr_setUploadLimit( fHandle, uploadLimit );
255}
256
257- (void) setQuitMessage: (id) sender
258{
259    [fDefaults setObject: ([fQuitCheck state] == NSOnState ? @"YES" : @"NO")
260                forKey: @"CheckQuit"];
261}
262
263- (void) setRemoveMessage: (id) sender
264{
265    [fDefaults setObject: ([fRemoveCheck state] == NSOnState ? @"YES" : @"NO")
266                forKey: @"CheckRemove"];
267}
268
269- (void) setDownloadLocation: (id) sender
270{
271    //Download folder
272    switch( [fFolderMatrix selectedRow] )
273    {
274        case DOWNLOAD_FOLDER:
275            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
276            break;
277        case DOWNLOAD_TORRENT:
278            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
279            break;
280        case DOWNLOAD_ASK:
281            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
282            break;
283    }
284    [fFolderPopUp setEnabled: [fFolderMatrix selectedRow] == 0];
285}
286
287- (void) folderSheetShow: (id) sender
288{
289    NSOpenPanel * panel = [NSOpenPanel openPanel];
290
291    [panel setPrompt:                  @"Select"];
292    [panel setAllowsMultipleSelection:        NO];
293    [panel setCanChooseFiles:                 NO];
294    [panel setCanChooseDirectories:          YES];
295
296    [panel beginSheetForDirectory: NULL file: NULL types: NULL
297        modalForWindow: fPrefsWindow modalDelegate: self didEndSelector:
298        @selector( folderSheetClosed:returnCode:contextInfo: )
299        contextInfo: NULL];
300}
301
302@end // @implementation PrefsController
303
304@implementation PrefsController (Private)
305
306- (void) showGeneralPref: (id) sender
307{
308    [self setPrefView: fGeneralView];
309}
310
311- (void) showNetworkPref: (id) sender
312{
313    [self setPrefView: fNetworkView];
314}
315
316- (void) setPrefView: (NSView *) view
317{
318    NSRect windowRect = [fPrefsWindow frame];
319    int difference = [view frame].size.height - [[fPrefsWindow contentView] frame].size.height;
320
321    windowRect.origin.y -= difference;
322    windowRect.size.height += difference;
323   
324    [fPrefsWindow setTitle: [fToolbar selectedItemIdentifier]];
325    [fPrefsWindow setContentView: fBlankView];
326    [fPrefsWindow setFrame:windowRect display: YES animate: YES];
327    [fPrefsWindow setContentView: view];
328}
329
330- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code
331    contextInfo: (void *) info
332{
333    [fFolderPopUp selectItemAtIndex: 0];
334
335    if (code != NSOKButton)
336        return;
337
338    [fDownloadFolder release];
339    fDownloadFolder = [[openPanel filenames] objectAtIndex: 0];
340    [fDownloadFolder retain];
341   
342    [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
343
344    [self updatePopUp];
345}
346
347- (void) updatePopUp
348{
349    NSMenuItem     * menuItem;
350    NSImage        * image32, * image16;
351
352    // Get the icon for the folder
353    image32 = [[NSWorkspace sharedWorkspace] iconForFile:
354                fDownloadFolder];
355    image16 = [[NSImage alloc] initWithSize: NSMakeSize(16,16)];
356
357    // 32x32 -> 16x16 scaling
358    [image16 lockFocus];
359    [[NSGraphicsContext currentContext]
360        setImageInterpolation: NSImageInterpolationHigh];
361    [image32 drawInRect: NSMakeRect(0,0,16,16)
362        fromRect: NSMakeRect(0,0,32,32) operation: NSCompositeCopy
363        fraction: 1.0];
364    [image16 unlockFocus];
365
366    // Update the menu item
367    menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
368    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
369    [menuItem setImage: image16];
370
371    [image16 release];
372}
373
374@end /* @implementation PrefsController (Private) */
Note: See TracBrowser for help on using the repository browser.