source: trunk/macosx/PrefsController.m @ 484

Last change on this file since 484 was 484, checked in by livings124, 16 years ago

Added an option to only show the remove/quit warning if transfers are downloading (not seeding). Also change the word "torrent" to "transfer" in the prefs window.

  • Property svn:keywords set to Date Rev Author Id
File size: 18.5 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 484 2006-06-26 05:52:51Z livings124 $
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
28#define MIN_PORT    1
29#define MAX_PORT    65535
30
31#define DOWNLOAD_FOLDER     0
32#define DOWNLOAD_TORRENT    2
33#define DOWNLOAD_ASK    3
34
35#define START_YES_CHECK_TAG     0
36#define START_WAIT_CHECK_TAG    1
37#define START_NO_CHECK_TAG      2
38
39#define UPDATE_DAILY    0
40#define UPDATE_WEEKLY   1
41#define UPDATE_NEVER    2
42
43#define TOOLBAR_GENERAL     @"General"
44#define TOOLBAR_TRANSFERS   @"Transfers"
45#define TOOLBAR_NETWORK     @"Network"
46
47@interface PrefsController (Private)
48
49- (void) showGeneralPref: (id) sender;
50- (void) showTransfersPref: (id) sender;
51- (void) showNetworkPref: (id) sender;
52
53- (void) setPrefView: (NSView *) view;
54
55- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
56- (void) updatePopUp;
57
58@end
59
60@implementation PrefsController
61
62+ (void) initialize
63{
64    [[NSUserDefaults standardUserDefaults] registerDefaults:
65        [NSDictionary dictionaryWithContentsOfFile:
66        [[NSBundle mainBundle] pathForResource: @"Defaults" ofType: @"plist"]]];
67}
68
69- (void) dealloc
70{
71    [fDownloadFolder release];
72    [super dealloc];
73}
74
75- (void) setPrefs: (tr_handle_t *) handle
76{
77    fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
78    [fToolbar setDelegate: self];
79    [fToolbar setAllowsUserCustomization: NO];
80    [[self window] setToolbar: fToolbar];
81    [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
82    [fToolbar setSizeMode: NSToolbarSizeModeRegular];
83
84    [fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
85    [self showGeneralPref: nil];
86
87    fDefaults = [NSUserDefaults standardUserDefaults];
88    fHandle = handle;
89   
90    //set download folder
91    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
92    fDownloadFolder = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
93    [fDownloadFolder retain];
94
95    if ([downloadChoice isEqualToString: @"Constant"])
96        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
97    else if ([downloadChoice isEqualToString: @"Torrent"])
98        [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
99    else
100        [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
101    [self updatePopUp];
102
103    //set bind port
104    int bindPort = [fDefaults integerForKey: @"BindPort"];
105    [fPortField setIntValue: bindPort];
106    tr_setBindPort(fHandle, bindPort);
107   
108    //checks for old version upload speed of -1
109    if ([fDefaults integerForKey: @"UploadLimit"] < 0)
110    {
111        [fDefaults setInteger: 20 forKey: @"UploadLimit"];
112        [fDefaults setBool: NO forKey: @"CheckUpload"];
113    }
114   
115    //set upload limit
116    BOOL checkUpload = [fDefaults boolForKey: @"CheckUpload"];
117    int uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
118   
119    [fUploadCheck setState: checkUpload];
120    [fUploadField setIntValue: uploadLimit];
121    [fUploadField setEnabled: checkUpload];
122   
123    tr_setUploadLimit(fHandle, checkUpload ? uploadLimit : -1);
124
125        //set download limit
126    BOOL checkDownload = [fDefaults boolForKey: @"CheckDownload"];
127    int downloadLimit = [fDefaults integerForKey: @"DownloadLimit"];
128   
129    [fDownloadCheck setState: checkDownload];
130    [fDownloadField setIntValue: downloadLimit];
131    [fDownloadField setEnabled: checkDownload];
132   
133    tr_setDownloadLimit(fHandle, checkDownload ? downloadLimit : -1);
134   
135    //set ratio limit
136    BOOL ratioCheck = [fDefaults boolForKey: @"RatioCheck"];
137    [fRatioCheck setState: ratioCheck];
138    [fRatioField setEnabled: ratioCheck];
139    [fRatioField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
140   
141    //set remove and quit prompts
142    BOOL isQuitCheck = [fDefaults boolForKey: @"CheckQuit"],
143        isRemoveCheck = [fDefaults boolForKey: @"CheckRemove"];
144   
145    [fQuitCheck setState: isQuitCheck];
146    [fRemoveCheck setState: isRemoveCheck];
147   
148    [fQuitDownloadingCheck setState: [fDefaults boolForKey: @"CheckQuitDownloading"]];
149    [fQuitDownloadingCheck setEnabled: isQuitCheck];
150    [fRemoveDownloadingCheck setState: [fDefaults boolForKey: @"CheckRemoveDownloading"]];
151    [fRemoveDownloadingCheck setEnabled: isRemoveCheck];
152
153    //set dock badging
154    [fBadgeDownloadRateCheck setState: [fDefaults boolForKey: @"BadgeDownloadRate"]];
155    [fBadgeUploadRateCheck setState: [fDefaults boolForKey: @"BadgeUploadRate"]];
156   
157    //set start setting
158    NSString * startSetting = [fDefaults stringForKey: @"StartSetting"];
159    int tag;
160    if ([startSetting isEqualToString: @"Start"])
161        tag = START_YES_CHECK_TAG;
162    else if ([startSetting isEqualToString: @"Wait"])
163        tag = START_WAIT_CHECK_TAG;
164    else
165        tag = START_NO_CHECK_TAG;
166   
167    [fStartMatrix selectCellWithTag: tag];
168    [fWaitToStartField setEnabled: tag == START_WAIT_CHECK_TAG];
169    [fWaitToStartField setIntValue: [fDefaults integerForKey: @"WaitToStartNumber"]];
170   
171    //set private torrents
172    BOOL copyTorrents = [fDefaults boolForKey: @"SavePrivateTorrent"];
173    [fCopyTorrentCheck setState: copyTorrents];
174   
175    [fDeleteOriginalTorrentCheck setEnabled: copyTorrents];
176    [fDeleteOriginalTorrentCheck setState: [fDefaults boolForKey: @"DeleteOriginalTorrent"]];
177
178    //set update check
179    NSString * updateCheck = [fDefaults stringForKey: @"UpdateCheck"];
180    if ([updateCheck isEqualToString: @"Weekly"])
181        [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
182    else if ([updateCheck isEqualToString: @"Never"])
183        [fUpdatePopUp selectItemAtIndex: UPDATE_NEVER];
184    else
185        [fUpdatePopUp selectItemAtIndex: UPDATE_DAILY];
186}
187
188- (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier:
189    (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
190{
191    NSToolbarItem * item;
192    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
193
194    if ([ident isEqualToString: TOOLBAR_GENERAL])
195    {
196        [item setLabel: TOOLBAR_GENERAL];
197        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
198        [item setTarget: self];
199        [item setAction: @selector( showGeneralPref: )];
200    }
201    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
202    {
203        [item setLabel: TOOLBAR_TRANSFERS];
204        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
205        [item setTarget: self];
206        [item setAction: @selector( showTransfersPref: )];
207    }
208    else if ([ident isEqualToString: TOOLBAR_NETWORK])
209    {
210        [item setLabel: TOOLBAR_NETWORK];
211        [item setImage: [NSImage imageNamed: @"Network.png"]];
212        [item setTarget: self];
213        [item setAction: @selector( showNetworkPref: )];
214    }
215    else
216    {
217        [item release];
218        return nil;
219    }
220
221    return item;
222}
223
224- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
225{
226    return [self toolbarDefaultItemIdentifiers: nil];
227}
228
229- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
230{
231    return [self toolbarAllowedItemIdentifiers: nil];
232}
233
234- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
235{
236    return [NSArray arrayWithObjects:
237            TOOLBAR_GENERAL, TOOLBAR_TRANSFERS,
238            TOOLBAR_NETWORK, nil];
239}
240
241- (void) setPort: (id) sender
242{
243    int bindPort = [sender intValue];
244    if (![[NSString stringWithInt: bindPort] isEqualToString: [sender stringValue]]
245            || bindPort < MIN_PORT || bindPort > MAX_PORT)
246    {
247        NSBeep();
248        bindPort = [fDefaults integerForKey: @"BindPort"];
249        [sender setIntValue: bindPort];
250    }
251    else
252    {
253        tr_setBindPort( fHandle, bindPort );
254        [fDefaults setInteger: bindPort forKey: @"BindPort"];
255    }
256}
257
258- (void) setLimit: (id) sender
259{
260    NSString * key;
261    NSButton * check;
262    NSString * type;
263    if (sender == fUploadField)
264    {
265        key = @"UploadLimit";
266        check = fUploadCheck;
267        type = @"Upload";
268    }
269    else
270    {
271        key = @"DownloadLimit";
272        check = fDownloadCheck;
273        type = @"Download";
274    }
275
276    int limit = [sender intValue];
277    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]]
278            || limit < 0)
279    {
280        NSBeep();
281        limit = [fDefaults integerForKey: key];
282        [sender setIntValue: limit];
283    }
284    else
285    {
286        if (sender == fUploadField)
287            tr_setUploadLimit(fHandle, [fUploadCheck state] == NSOffState ? -1 : limit);
288        else
289            tr_setDownloadLimit(fHandle, [fDownloadCheck state] == NSOffState ? -1 : limit);
290       
291        [fDefaults setInteger: limit forKey: key];
292    }
293   
294    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
295                                    [NSNumber numberWithBool: [check state]], @"Enable",
296                                    [NSNumber numberWithInt: limit], @"Limit",
297                                    type, @"Type", nil];
298    [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange" object: dict];
299}
300
301- (void) setLimitCheck: (id) sender
302{
303    NSString * key;
304    NSTextField * field;
305    if (sender == fUploadCheck)
306    {
307        key = @"CheckUpload";
308        field = fUploadField;
309    }
310    else
311    {
312        key = @"CheckDownload";
313        field = fDownloadField;
314    }
315   
316    BOOL check = [sender state] == NSOnState;
317    [self setLimit: field];
318    [field setEnabled: check];
319   
320    [fDefaults setBool: check forKey: key];
321}
322
323- (void) setLimitEnabled: (BOOL) enable type: (NSString *) type
324{
325    NSButton * check = [type isEqualToString: @"Upload"] ? fUploadCheck : fDownloadCheck;
326    [check setState: enable ? NSOnState : NSOffState];
327    [self setLimitCheck: check];
328}
329
330- (void) setQuickLimit: (int) limit type: (NSString *) type
331{
332    NSButton * check;
333    if ([type isEqualToString: @"Upload"])
334    {
335        [fUploadField setIntValue: limit];
336        check = fUploadCheck;
337    }
338    else
339    {
340        [fDownloadField setIntValue: limit];
341        check = fDownloadCheck;
342    }
343    [check setState: NSOnState];
344    [self setLimitCheck: check];
345}
346
347- (void) setRatio: (id) sender
348{
349    float ratioLimit = [sender floatValue];
350    if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]]
351            || ratioLimit < 0)
352    {
353        NSBeep();
354        ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
355        [sender setFloatValue: ratioLimit];
356    }
357    else
358        [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"];
359   
360    NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
361                                    [NSNumber numberWithBool: [fRatioCheck state]], @"Enable",
362                                    [NSNumber numberWithFloat: ratioLimit], @"Ratio", nil];
363    [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: dict];
364}
365
366- (void) setRatioCheck: (id) sender
367{
368    BOOL check = [sender state] == NSOnState;
369    [self setRatio: fRatioField];
370    [fRatioField setEnabled: check];
371   
372    [fDefaults setBool: check forKey: @"RatioCheck"];
373}
374
375- (void) setRatioEnabled: (BOOL) enable
376{
377    int state = enable ? NSOnState : NSOffState;
378   
379    [fRatioCheck setState: state];
380    [self setRatioCheck: fRatioCheck];
381}
382
383- (void) setQuickRatio: (float) ratioLimit
384{
385    [fRatioField setFloatValue: ratioLimit];
386   
387    [fRatioCheck setState: NSOnState];
388    [self setRatioCheck: fRatioCheck];
389}
390
391- (void) setShowMessage: (id) sender
392{
393    BOOL state = [sender state];
394
395    if (sender == fQuitCheck)
396    {
397        [fDefaults setBool: state forKey: @"CheckQuit"];
398        [fQuitDownloadingCheck setEnabled: state];
399    }
400    else if (sender == fRemoveCheck)
401    {
402        [fDefaults setBool: state forKey: @"CheckRemove"];
403        [fRemoveDownloadingCheck setEnabled: state];
404    }
405    if (sender == fQuitDownloadingCheck)
406        [fDefaults setBool: state forKey: @"CheckQuitDownloading"];
407    else if (sender == fRemoveDownloadingCheck)
408        [fDefaults setBool: state forKey: @"CheckRemoveDownloading"];
409    else;
410}
411
412- (void) setBadge: (id) sender
413{   
414    if (sender == fBadgeDownloadRateCheck)
415        [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
416    else if (sender == fBadgeUploadRateCheck)
417        [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
418    else;
419}
420
421- (void) setUpdate: (id) sender
422{
423    int index = [fUpdatePopUp indexOfSelectedItem];
424    NSTimeInterval seconds;
425    if (index == UPDATE_DAILY)
426    {
427        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
428        seconds = 86400;
429    }
430    else if (index == UPDATE_WEEKLY)
431    {
432        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
433        seconds = 604800;
434    }
435    else
436    {
437        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
438        seconds = 0;
439    }
440
441    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
442    [fUpdater scheduleCheckWithInterval: seconds];
443}
444
445- (void) setStartSetting: (id) sender
446{
447    NSString * startSetting;
448
449    int tag = [[fStartMatrix selectedCell] tag];
450    if (tag == START_YES_CHECK_TAG)
451        startSetting = @"Start";
452    else if (tag == START_WAIT_CHECK_TAG)
453        startSetting = @"Wait";
454    else
455        startSetting = @"Manual";
456   
457    [fDefaults setObject: startSetting forKey: @"StartSetting"];
458   
459    [self setWaitToStart: fWaitToStartField];
460    [fWaitToStartField setEnabled: tag == START_WAIT_CHECK_TAG];
461}
462
463- (void) setWaitToStart: (id) sender
464{
465    int waitNumber = [sender intValue];
466    if (![[sender stringValue] isEqualToString: [NSString stringWithInt: waitNumber]] || waitNumber < 1)
467    {
468        NSBeep();
469        waitNumber = [fDefaults floatForKey: @"WaitToStartNumber"];
470        [sender setIntValue: waitNumber];
471    }
472    else
473        [fDefaults setInteger: waitNumber forKey: @"WaitToStartNumber"];
474   
475    [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self];
476}
477
478- (void) setMoveTorrent: (id) sender
479{
480    int state = [sender state];
481    if (sender == fCopyTorrentCheck)
482    {
483        [fDefaults setBool: state forKey: @"SavePrivateTorrent"];
484       
485        [fDeleteOriginalTorrentCheck setEnabled: state];
486        if (state == NSOffState)
487        {
488            [fDeleteOriginalTorrentCheck setState: NSOffState];
489            [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"];
490        }
491    }
492    else
493        [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"];
494}
495
496- (void) setDownloadLocation: (id) sender
497{
498    //Download folder
499    switch ([fFolderPopUp indexOfSelectedItem])
500    {
501        case DOWNLOAD_FOLDER:
502            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
503            break;
504        case DOWNLOAD_TORRENT:
505            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
506            break;
507        case DOWNLOAD_ASK:
508            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
509            break;
510    }
511}
512
513- (void) checkUpdate
514{
515    [fUpdater checkForUpdates: nil];
516}
517
518- (void) folderSheetShow: (id) sender
519{
520    NSOpenPanel * panel = [NSOpenPanel openPanel];
521
522    [panel setPrompt: @"Select"];
523    [panel setAllowsMultipleSelection: NO];
524    [panel setCanChooseFiles: NO];
525    [panel setCanChooseDirectories: YES];
526    [panel setCanCreateDirectories: YES];
527
528    [panel beginSheetForDirectory: nil file: nil types: nil
529        modalForWindow: [self window] modalDelegate: self didEndSelector:
530        @selector( folderSheetClosed:returnCode:contextInfo: )
531        contextInfo: nil];
532}
533
534@end
535
536@implementation PrefsController (Private)
537
538- (void) showGeneralPref: (id) sender
539{
540    [self setPrefView: fGeneralView];
541}
542
543- (void) showTransfersPref: (id) sender
544{
545    [self setPrefView: fTransfersView];
546}
547
548- (void) showNetworkPref: (id) sender
549{
550    [self setPrefView: fNetworkView];
551}
552
553- (void) setPrefView: (NSView *) view
554{
555    NSWindow * window = [self window];
556   
557    NSRect windowRect = [window frame];
558    int difference = [view frame].size.height - [[window contentView] frame].size.height;
559    windowRect.origin.y -= difference;
560    windowRect.size.height += difference;
561
562    [window setTitle: [fToolbar selectedItemIdentifier]];
563   
564    [window setContentView: view];
565    [view setHidden: YES];
566    [window setFrame: windowRect display: YES animate: YES];
567    [view setHidden: NO];
568}
569
570- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
571{
572   if (code == NSOKButton)
573   {
574       [fDownloadFolder release];
575       fDownloadFolder = [[openPanel filenames] objectAtIndex: 0];
576       [fDownloadFolder retain];
577
578       [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
579       [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
580       [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
581
582       [self updatePopUp];
583   }
584   else
585   {
586       //reset if cancelled
587       NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
588       if ([downloadChoice isEqualToString: @"Constant"])
589           [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
590       else if ([downloadChoice isEqualToString: @"Torrent"])
591           [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
592       else
593           [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
594   }
595}
596
597- (void) updatePopUp
598{
599    //get and resize the icon
600    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder];
601    [icon setScalesWhenResized: YES];
602    [icon setSize: NSMakeSize(16.0, 16.0)];
603
604    //update menu item
605    NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
606    [menuItem setTitle: [fDownloadFolder lastPathComponent]];
607    [menuItem setImage: icon];
608}
609
610@end
Note: See TracBrowser for help on using the repository browser.