source: trunk/macosx/PrefsController.m @ 1054

Last change on this file since 1054 was 1054, checked in by livings124, 15 years ago

remove option to save a private torrent copy...just do it automatically

  • Property svn:keywords set to Date Rev Author Id
File size: 18.9 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 1054 2006-11-05 19:08:38Z 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#import "UKKQueue.h"
28
29#define DOWNLOAD_FOLDER     0
30#define DOWNLOAD_TORRENT    2
31#define DOWNLOAD_ASK        3
32
33#define UPDATE_DAILY    0
34#define UPDATE_WEEKLY   1
35#define UPDATE_NEVER    2
36
37#define TOOLBAR_GENERAL     @"TOOLBAR_GENERAL"
38#define TOOLBAR_TRANSFERS   @"TOOLBAR_TRANSFERS"
39#define TOOLBAR_BANDWIDTH   @"TOOLBAR_BANDWIDTH"
40#define TOOLBAR_NETWORK     @"TOOLBAR_NETWORK"
41
42@interface PrefsController (Private)
43
44- (void) setPrefView: (id) sender;
45
46- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
47- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
48- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
49
50@end
51
52@implementation PrefsController
53
54- (id) initWithWindowNibName: (NSString *) name handle: (tr_handle_t *) handle
55{
56    if ((self = [self initWithWindowNibName: name]))
57    {
58        fDefaults = [NSUserDefaults standardUserDefaults];
59        fHandle = handle;
60       
61        //checks for old version upload speed of -1
62        if ([fDefaults integerForKey: @"UploadLimit"] < 0)
63        {
64            [fDefaults setInteger: 20 forKey: @"UploadLimit"];
65            [fDefaults setBool: NO forKey: @"CheckUpload"];
66        }
67       
68        //set auto import
69        if ([fDefaults boolForKey: @"AutoImport"])
70            [[UKKQueue sharedFileWatcher] addPath:
71                [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath]];
72       
73        //set bind port
74        int bindPort = [fDefaults integerForKey: @"BindPort"];
75        tr_setBindPort(fHandle, bindPort);
76       
77        //set NAT
78        if ([fDefaults boolForKey: @"NatTraversal"])
79            tr_natTraversalEnable(fHandle);
80       
81        //actually set bandwidth limits
82        [self applySpeedSettings: nil];
83       
84        //set play sound
85        NSMutableArray * sounds = [NSMutableArray array];
86        NSEnumerator * soundEnumerator,
87                    * soundDirectoriesEnumerator = [[NSArray arrayWithObjects: @"System/Library/Sounds",
88                            [NSHomeDirectory() stringByAppendingPathComponent: @"Library/Sounds"], nil] objectEnumerator];
89        NSString * soundPath, * sound;
90       
91        //get list of all sounds and sort alphabetically
92        while ((soundPath = [soundDirectoriesEnumerator nextObject]))
93            if (soundEnumerator = [[NSFileManager defaultManager] enumeratorAtPath: soundPath])
94                while ((sound = [soundEnumerator nextObject]))
95                {
96                    sound = [sound stringByDeletingPathExtension];
97                    if ([NSSound soundNamed: sound])
98                        [sounds addObject: sound];
99                }
100       
101        fSounds = [[sounds sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)] retain];
102    }
103    return self;
104}
105
106- (void) dealloc
107{
108    if (fNatStatusTimer)
109        [fNatStatusTimer invalidate];
110    [fSounds release];
111   
112    [super dealloc];
113}
114
115- (void) awakeFromNib
116{
117    fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
118    [fToolbar setDelegate: self];
119    [fToolbar setAllowsUserCustomization: NO];
120    [[self window] setToolbar: fToolbar];
121    [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
122    [fToolbar setSizeMode: NSToolbarSizeModeRegular];
123
124    [fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
125    [self setPrefView: nil];
126   
127    //set download folder
128    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
129    if ([downloadChoice isEqualToString: @"Constant"])
130        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
131    else if ([downloadChoice isEqualToString: @"Torrent"])
132        [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
133    else
134        [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
135   
136    [self updatePortStatus];
137   
138    fNatStatus = -1;
139    [self updateNatStatus];
140    fNatStatusTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
141                        selector: @selector(updateNatStatus) userInfo: nil repeats: YES];
142
143    //set update check
144    NSString * updateCheck = [fDefaults stringForKey: @"UpdateCheck"];
145    if ([updateCheck isEqualToString: @"Weekly"])
146        [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
147    else if ([updateCheck isEqualToString: @"Never"])
148        [fUpdatePopUp selectItemAtIndex: UPDATE_NEVER];
149    else
150        [fUpdatePopUp selectItemAtIndex: UPDATE_DAILY];
151}
152
153- (void) setUpdater: (SUUpdater *) updater
154{
155    fUpdater = updater;
156}
157
158- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident
159                    willBeInsertedIntoToolbar: (BOOL) flag
160{
161    NSToolbarItem * item;
162    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
163
164    if ([ident isEqualToString: TOOLBAR_GENERAL])
165    {
166        [item setLabel: NSLocalizedString(@"General", "Preferences -> General toolbar item title")];
167        [item setImage: [NSImage imageNamed: @"Preferences.png"]];
168        [item setTarget: self];
169        [item setAction: @selector(setPrefView:)];
170    }
171    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
172    {
173        [item setLabel: NSLocalizedString(@"Transfers", "Preferences -> Transfers toolbar item title")];
174        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
175        [item setTarget: self];
176        [item setAction: @selector(setPrefView:)];
177    }
178    else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
179    {
180        [item setLabel: NSLocalizedString(@"Bandwidth", "Preferences -> Bandwidth toolbar item title")];
181        [item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
182        [item setTarget: self];
183        [item setAction: @selector(setPrefView:)];
184    }
185    else if ([ident isEqualToString: TOOLBAR_NETWORK])
186    {
187        [item setLabel: NSLocalizedString(@"Network", "Preferences -> Network toolbar item title")];
188        [item setImage: [NSImage imageNamed: @"Network.png"]];
189        [item setTarget: self];
190        [item setAction: @selector(setPrefView:)];
191    }
192    else
193    {
194        [item release];
195        return nil;
196    }
197
198    return item;
199}
200
201- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
202{
203    return [self toolbarDefaultItemIdentifiers: toolbar];
204}
205
206- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
207{
208    return [self toolbarAllowedItemIdentifiers: toolbar];
209}
210
211- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
212{
213    return [NSArray arrayWithObjects: TOOLBAR_GENERAL, TOOLBAR_TRANSFERS,
214                                        TOOLBAR_BANDWIDTH, TOOLBAR_NETWORK, nil];
215}
216
217- (void) setPort: (id) sender
218{
219    tr_setBindPort(fHandle, [fDefaults integerForKey: @"BindPort"]);
220    [self updateNatStatus];
221    [self updatePortStatus];
222}
223
224- (void) updatePortStatus
225{
226    PortChecker * portChecker = [[PortChecker alloc] initWithDelegate: self];
227
228    [fPortStatusField setStringValue: [NSLocalizedString(@"Checking port status",
229                                        "Preferences -> Network -> port status") stringByAppendingEllipsis]];
230    [fPortStatusImage setImage: nil];
231    [fPortStatusProgress startAnimation: self];
232       
233    [portChecker probePort: [fDefaults integerForKey: @"BindPort"]];
234}
235
236- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
237{
238    [fPortStatusProgress stopAnimation: self];
239    switch ([portChecker status])
240    {
241        case PORT_STATUS_OPEN:
242            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Network -> port status")];
243            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.tiff"]];
244            break;
245        case PORT_STATUS_STEALTH:
246            [fPortStatusField setStringValue: NSLocalizedString(@"Port is stealth", "Preferences -> Network -> port status")];
247            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
248            break;
249        case PORT_STATUS_CLOSED:
250            [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Network -> port status")];
251            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
252            break;
253        case PORT_STATUS_ERROR:
254            [fPortStatusField setStringValue: NSLocalizedString(@"Unable to check port status",
255                                                "Preferences -> Network -> port status")];
256            [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.tiff"]];
257            break;
258    }
259    [portChecker release];
260}
261
262- (void) setNat: (id) sender
263{
264    [fDefaults boolForKey: @"NatTraversal"] ? tr_natTraversalEnable(fHandle) : tr_natTraversalDisable(fHandle);
265    [self updateNatStatus];
266}
267
268- (void) updateNatStatus
269{
270    int status = tr_natTraversalStatus(fHandle);
271    if (fNatStatus == status)
272        return;
273    fNatStatus = status;
274   
275    if (status == 2)
276    {
277        [fNatStatusField setStringValue: NSLocalizedString(@"Port successfully mapped",
278                                            "Preferences -> Network -> port map status")];
279        [fNatStatusImage setImage: [NSImage imageNamed: @"GreenDot.tiff"]];
280    }
281    else if (status == 3 || status == 4)
282    {
283        [fNatStatusField setStringValue: NSLocalizedString(@"Error mapping port",
284                                            "Preferences -> Network -> port map status")];
285        [fNatStatusImage setImage: [NSImage imageNamed: @"RedDot.tiff"]];
286    }
287    else
288    {
289        [fNatStatusField setStringValue: @""];
290        [fNatStatusImage setImage: nil];
291    }
292   
293    [self updatePortStatus];
294}
295
296- (void) applySpeedSettings: (id) sender
297{
298    if ([fDefaults boolForKey: @"SpeedLimit"])
299    {
300        tr_setUploadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
301        tr_setDownloadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
302    }
303    else
304    {
305        tr_setUploadLimit(fHandle, [fDefaults boolForKey: @"CheckUpload"]
306                                        ? [fDefaults integerForKey: @"UploadLimit"] : -1);
307        tr_setDownloadLimit(fHandle, [fDefaults boolForKey: @"CheckDownload"]
308                                        ? [fDefaults integerForKey: @"DownloadLimit"] : -1);
309    }
310}
311
312- (void) setAutoSpeedLimit: (id) sender
313{
314    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
315}
316
317- (void) setBadge: (id) sender
318{   
319    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
320}
321
322- (void) setSound: (id) sender
323{
324    //play sound when selecting
325    NSString * soundName = [sender titleOfSelectedItem];
326    NSSound * sound;
327    if ((sound = [NSSound soundNamed: soundName]))
328        [sound play];
329}
330
331- (void) setUpdate: (id) sender
332{
333    int index = [fUpdatePopUp indexOfSelectedItem];
334    NSTimeInterval seconds;
335    if (index == UPDATE_DAILY)
336    {
337        [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"];
338        seconds = 86400;
339    }
340    else if (index == UPDATE_WEEKLY)
341    {
342        [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"];
343        seconds = 604800;
344    }
345    else
346    {
347        [fDefaults setObject: @"Never" forKey: @"UpdateCheck"];
348        seconds = 0;
349    }
350
351    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
352   
353    if (fUpdater)
354        [fUpdater scheduleCheckWithInterval: seconds];
355}
356
357- (void) setQueueNumber: (id) sender
358{
359    [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self];
360}
361
362- (void) setDownloadLocation: (id) sender
363{
364    //download folder
365    switch ([fFolderPopUp indexOfSelectedItem])
366    {
367        case DOWNLOAD_FOLDER:
368            [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
369            break;
370        case DOWNLOAD_TORRENT:
371            [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
372            break;
373        case DOWNLOAD_ASK:
374            [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
375            break;
376    }
377}
378
379- (void) folderSheetShow: (id) sender
380{
381    NSOpenPanel * panel = [NSOpenPanel openPanel];
382
383    [panel setPrompt: @"Select"];
384    [panel setAllowsMultipleSelection: NO];
385    [panel setCanChooseFiles: NO];
386    [panel setCanChooseDirectories: YES];
387    [panel setCanCreateDirectories: YES];
388
389    [panel beginSheetForDirectory: nil file: nil types: nil
390        modalForWindow: [self window] modalDelegate: self didEndSelector:
391        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
392}
393
394- (void) incompleteFolderSheetShow: (id) sender
395{
396    NSOpenPanel * panel = [NSOpenPanel openPanel];
397
398    [panel setPrompt: @"Select"];
399    [panel setAllowsMultipleSelection: NO];
400    [panel setCanChooseFiles: NO];
401    [panel setCanChooseDirectories: YES];
402    [panel setCanCreateDirectories: YES];
403
404    [panel beginSheetForDirectory: nil file: nil types: nil
405        modalForWindow: [self window] modalDelegate: self didEndSelector:
406        @selector(incompleteFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
407}
408
409- (void) setAutoImport: (id) sender
410{
411    if ([fDefaults boolForKey: @"AutoImport"])
412        [[UKKQueue sharedFileWatcher] addPath:
413            [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath]];
414    else
415        [[UKKQueue sharedFileWatcher] removePathFromQueue:
416            [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath]];
417   
418    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
419}
420
421- (void) importFolderSheetShow: (id) sender
422{
423    NSOpenPanel * panel = [NSOpenPanel openPanel];
424
425    [panel setPrompt: @"Select"];
426    [panel setAllowsMultipleSelection: NO];
427    [panel setCanChooseFiles: NO];
428    [panel setCanChooseDirectories: YES];
429    [panel setCanCreateDirectories: YES];
430
431    [panel beginSheetForDirectory: nil file: nil types: nil
432        modalForWindow: [self window] modalDelegate: self didEndSelector:
433        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
434}
435
436- (void) setAutoSize: (id) sender
437{
438    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
439}
440
441- (void) helpForNetwork: (id) sender
442{
443    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PortForwarding"
444        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
445}
446
447@end
448
449@implementation PrefsController (Private)
450
451- (void) setPrefView: (id) sender
452{
453    NSView * view = fGeneralView;
454    if (sender)
455    {
456        NSString * identifier = [sender itemIdentifier];
457        if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
458            view = fTransfersView;
459        else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
460            view = fBandwidthView;
461        else if ([identifier isEqualToString: TOOLBAR_NETWORK])
462            view = fNetworkView;
463        else;
464    }
465   
466    NSWindow * window = [self window];
467    if ([window contentView] == view)
468        return;
469   
470    NSRect windowRect = [window frame];
471    int difference = [view frame].size.height - [[window contentView] frame].size.height;
472    windowRect.origin.y -= difference;
473    windowRect.size.height += difference;
474   
475    [view setHidden: YES];
476    [window setContentView: view];
477    [window setFrame: windowRect display: YES animate: YES];
478    [view setHidden: NO];
479   
480    //set title label
481    if (sender)
482        [window setTitle: [sender label]];
483    else
484    {
485        NSToolbarItem * item;
486        NSEnumerator * enumerator = [[fToolbar items] objectEnumerator];
487        while ((item = [enumerator nextObject]))
488            if ([[item itemIdentifier] isEqualToString: [fToolbar selectedItemIdentifier]])
489            {
490                [window setTitle: [item label]];
491                break;
492            }
493    }
494   
495    //for network view make sure progress indicator hides itself
496    if (view == fNetworkView && [fPortStatusImage image])
497        [fPortStatusProgress setDisplayedWhenStopped: NO];
498}
499
500- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
501{
502    if (code == NSOKButton)
503    {
504        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
505        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"DownloadFolder"];
506        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
507    }
508    else
509    {
510        //reset if cancelled
511        NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
512        if ([downloadChoice isEqualToString: @"Constant"])
513            [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
514        else if ([downloadChoice isEqualToString: @"Torrent"])
515            [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
516        else
517            [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
518    }
519}
520
521- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
522{
523    if (code == NSOKButton)
524        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"IncompleteDownloadFolder"];
525    [fIncompleteFolderPopUp selectItemAtIndex: 0];
526}
527
528- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
529{
530    if (code == NSOKButton)
531    {
532        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
533        [sharedQueue removePathFromQueue: [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath]];
534       
535        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"AutoImportDirectory"];
536       
537        [sharedQueue addPath: [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath]];
538       
539        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
540    }
541    [fImportFolderPopUp selectItemAtIndex: 0];
542}
543
544@end
Note: See TracBrowser for help on using the repository browser.