source: trunk/macosx/PrefsController.m @ 20

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

Update 2005-12-25

File size: 12.0 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    2
31#define DOWNLOAD_ASK        3
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        - 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        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
109    }
110    else if( [downloadChoice isEqualToString: @"Torrent"] )
111    {
112        [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
113    }
114    else
115    {
116        [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
117    }
118    [self updatePopUp];
119
120    //set bind port
121    int bindPort = [fDefaults integerForKey: @"BindPort"];
122    [fPortField setIntValue: bindPort];
123    fHandle = handle;
124    tr_setBindPort( fHandle, bindPort );
125   
126    //checks for old version upload speed of -1
127    if ([fDefaults integerForKey: @"UploadLimit"] < 0)
128    {
129        [fDefaults setObject: DEFAULT_UPLOAD forKey: @"UploadLimit"];
130        [fDefaults setObject: @"NO" forKey: @"CheckUpload"];
131    }
132   
133    //set upload limit
134    BOOL checkUpload = [[fDefaults stringForKey: @"CheckUpload"] isEqualToString:@"YES"];
135    int uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
136   
137    [fUploadCheck setState: checkUpload ? NSOnState : NSOffState];
138    [fUploadField setIntValue: uploadLimit];
139    [fUploadField setEnabled: checkUpload];
140   
141    if (!checkUpload || uploadLimit == 0)
142        uploadLimit = -1;
143    tr_setUploadLimit( fHandle, uploadLimit );
144   
145    //set remove and quit prompts
146    [fQuitCheck setState:([[fDefaults stringForKey: @"CheckQuit"]
147                isEqualToString:@"YES"] ? NSOnState : NSOffState)];
148    [fRemoveCheck setState:([[fDefaults stringForKey: @"CheckRemove"]
149                isEqualToString:@"YES"] ? NSOnState : NSOffState)];
150}
151
152- (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier:
153    (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
154{
155    NSToolbarItem * item;
156    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
157
158    if ([ident isEqualToString: TOOLBAR_GENERAL])
159    {
160        [item setLabel: TOOLBAR_GENERAL];
161        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
162        [item setTarget: self];
163        [item setAction: @selector( showGeneralPref: )];
164    }
165    else if ([ident isEqualToString: TOOLBAR_NETWORK])
166    {
167        [item setLabel: TOOLBAR_NETWORK];
168        [item setImage: [NSImage imageNamed: @"Network.png"]];
169        [item setTarget: self];
170        [item setAction: @selector( showNetworkPref: )];
171    }
172    else
173    {
174        [item release];
175        return nil;
176    }
177
178    return item;
179}
180
181- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
182{
183    return [self toolbarDefaultItemIdentifiers: nil];
184}
185
186- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
187{
188    return [self toolbarAllowedItemIdentifiers: nil];
189}
190
191- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
192{
193    return [NSArray arrayWithObjects:
194            TOOLBAR_GENERAL,
195            TOOLBAR_NETWORK,
196            nil];
197}
198
199- (void) setPort: (id) sender
200{
201    int bindPort = [fPortField intValue];
202   
203    //if value entered is not an int or is not in range do not change
204    if (![[fPortField stringValue] isEqualToString:
205            [NSString stringWithFormat: @"%d", bindPort]]
206            || bindPort < MIN_PORT
207            || bindPort > MAX_PORT)
208    {
209        NSBeep();
210        bindPort = [fDefaults integerForKey: @"BindPort"];
211        [fPortField setIntValue: bindPort];
212    }
213    else
214    {
215        tr_setBindPort( fHandle, bindPort );
216        [fDefaults setObject: [NSString stringWithFormat: @"%d", bindPort]
217                    forKey: @"BindPort"];
218    }
219}
220
221- (void) setLimitUploadCheck: (id) sender
222{
223    BOOL checkUpload = [fUploadCheck state] == NSOnState;
224
225    [fDefaults setObject: checkUpload ? @"YES" : @"NO"
226                            forKey: @"CheckUpload"];
227   
228    [self setUploadLimit: sender];
229    [fUploadField setEnabled: checkUpload];
230}
231
232- (void) setUploadLimit: (id) sender
233{
234    int uploadLimit = [fUploadField intValue];
235   
236    //if value entered is not an int or is less than 0 do not change
237    if (![[fUploadField stringValue] isEqualToString:
238            [NSString stringWithFormat: @"%d", uploadLimit]]
239            || uploadLimit < 0)
240    {
241        NSBeep();
242        uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
243        [fUploadField setIntValue: uploadLimit];
244    }
245    else
246    {
247        [fDefaults setObject: [NSString stringWithFormat: @"%d", uploadLimit]
248            forKey: @"UploadLimit"];
249    }
250   
251    if ([fUploadCheck state] == NSOffState || uploadLimit == 0)
252        uploadLimit = -1;
253    tr_setUploadLimit( fHandle, uploadLimit );
254}
255
256- (void) setQuitMessage: (id) sender
257{
258    [fDefaults setObject: ([fQuitCheck state] == NSOnState ? @"YES" : @"NO")
259                forKey: @"CheckQuit"];
260}
261
262- (void) setRemoveMessage: (id) sender
263{
264    [fDefaults setObject: ([fRemoveCheck state] == NSOnState ? @"YES" : @"NO")
265                forKey: @"CheckRemove"];
266}
267
268- (void) setDownloadLocation: (id) sender
269{
270    //Download folder
271    switch( [fFolderPopUp indexOfSelectedItem] )
272    {
273        case DOWNLOAD_FOLDER:
274            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
275            break;
276        case DOWNLOAD_TORRENT:
277            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
278            break;
279        case DOWNLOAD_ASK:
280            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
281            break;
282    }
283}
284
285- (void) folderSheetShow: (id) sender
286{
287    NSOpenPanel * panel = [NSOpenPanel openPanel];
288
289    [panel setPrompt:                  @"Select"];
290    [panel setAllowsMultipleSelection:        NO];
291    [panel setCanChooseFiles:                 NO];
292    [panel setCanChooseDirectories:          YES];
293
294    [panel beginSheetForDirectory: NULL file: NULL types: NULL
295        modalForWindow: fPrefsWindow modalDelegate: self didEndSelector:
296        @selector( folderSheetClosed:returnCode:contextInfo: )
297        contextInfo: NULL];
298}
299
300@end // @implementation PrefsController
301
302@implementation PrefsController (Private)
303
304- (void) showGeneralPref: (id) sender
305{
306    [self setPrefView: fGeneralView];
307}
308
309- (void) showNetworkPref: (id) sender
310{
311    [self setPrefView: fNetworkView];
312}
313
314- (void) setPrefView: (NSView *) view
315{
316    NSRect windowRect = [fPrefsWindow frame];
317    int difference = [view frame].size.height - [[fPrefsWindow contentView] frame].size.height;
318
319    windowRect.origin.y -= difference;
320    windowRect.size.height += difference;
321   
322    [fPrefsWindow setTitle: [fToolbar selectedItemIdentifier]];
323    [fPrefsWindow setContentView: fBlankView];
324    [fPrefsWindow setFrame:windowRect display: YES animate: YES];
325    [fPrefsWindow setContentView: view];
326}
327
328- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code
329    contextInfo: (void *) info
330{
331   if (code == NSOKButton)
332   {
333       [fDownloadFolder release];
334       fDownloadFolder = [[openPanel filenames] objectAtIndex: 0];
335       [fDownloadFolder retain];
336
337       [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
338       [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
339       [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
340
341       [self updatePopUp];
342   }
343   else
344   {
345       //reset if cancelled
346       NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
347       if( [downloadChoice isEqualToString: @"Constant"] )
348       {
349           [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
350       }
351       else if( [downloadChoice isEqualToString: @"Torrent"] )
352       {
353           [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
354       }
355       else
356       {
357           [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
358       }
359   }
360}
361
362- (void) updatePopUp
363{
364    NSMenuItem     * menuItem;
365    NSImage        * image32, * image16;
366
367    // Get the icon for the folder
368    image32 = [[NSWorkspace sharedWorkspace] iconForFile:
369                fDownloadFolder];
370    image16 = [[NSImage alloc] initWithSize: NSMakeSize(16,16)];
371
372    // 32x32 -> 16x16 scaling
373    [image16 lockFocus];
374    [[NSGraphicsContext currentContext]
375        setImageInterpolation: NSImageInterpolationHigh];
376    [image32 drawInRect: NSMakeRect(0,0,16,16)
377        fromRect: NSMakeRect(0,0,32,32) operation: NSCompositeCopy
378        fraction: 1.0];
379    [image16 unlockFocus];
380
381    // Update the menu item
382    menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
383    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
384    [menuItem setImage: image16];
385
386    [image16 release];
387}
388
389@end /* @implementation PrefsController (Private) */
Note: See TracBrowser for help on using the repository browser.