source: trunk/macosx/PrefsController.m @ 5534

Last change on this file since 5534 was 5534, checked in by livings124, 14 years ago

trivial changes

  • Property svn:keywords set to Date Rev Author Id
File size: 24.5 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 5534 2008-04-06 13:10:19Z livings124 $
3 *
4 * Copyright (c) 2005-2008 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 "BlocklistDownloader.h"
27#import "NSApplicationAdditions.h"
28#import "NSStringAdditions.h"
29#import "UKKQueue.h"
30
31#define DOWNLOAD_FOLDER     0
32#define DOWNLOAD_TORRENT    2
33
34#define UPDATE_SECONDS 86400
35
36#define TOOLBAR_GENERAL     @"TOOLBAR_GENERAL"
37#define TOOLBAR_TRANSFERS   @"TOOLBAR_TRANSFERS"
38#define TOOLBAR_BANDWIDTH   @"TOOLBAR_BANDWIDTH"
39#define TOOLBAR_PEERS       @"TOOLBAR_PEERS"
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) initWithHandle: (tr_handle *) handle
55{
56    if ((self = [super initWithWindowNibName: @"PrefsWindow"]))
57    {
58        fDefaults = [NSUserDefaults standardUserDefaults];
59        fHandle = handle;
60       
61        //checks for old version speeds of -1
62        if ([fDefaults integerForKey: @"UploadLimit"] < 0)
63        {
64            [fDefaults setInteger: 20 forKey: @"UploadLimit"];
65            [fDefaults setBool: NO forKey: @"CheckUpload"];
66        }
67        if ([fDefaults integerForKey: @"DownloadLimit"] < 0)
68        {
69            [fDefaults setInteger: 20 forKey: @"DownloadLimit"];
70            [fDefaults setBool: NO forKey: @"CheckDownload"];
71        }
72       
73        //check for old version download location
74        NSString * choice;
75        if ((choice = [fDefaults stringForKey: @"DownloadChoice"]))
76        {
77            [fDefaults setBool: [choice isEqualToString: @"Constant"] forKey: @"DownloadLocationConstant"];
78            [fDefaults setBool: YES forKey: @"DownloadAsk"];
79           
80            [fDefaults removeObjectForKey: @"DownloadChoice"];
81        }
82       
83        //set check for update to right value
84        [self setCheckForUpdate: nil];
85       
86        //set auto import
87        NSString * autoPath;
88        if ([fDefaults boolForKey: @"AutoImport"] && (autoPath = [fDefaults stringForKey: @"AutoImportDirectory"]))
89            [[UKKQueue sharedFileWatcher] addPath: [autoPath stringByExpandingTildeInPath]];
90       
91        //set encryption
92        [self setEncryptionMode: nil];
93       
94        //actually set bandwidth limits
95        [self applySpeedSettings: nil];
96    }
97   
98    return self;
99}
100
101- (void) dealloc
102{
103    if (fPortStatusTimer)
104        [fPortStatusTimer invalidate];
105    if (fPortChecker)
106    {
107        [fPortChecker cancelProbe];
108        [fPortChecker release];
109    }
110   
111    [super dealloc];
112}
113
114- (void) awakeFromNib
115{
116    fHasLoaded = YES;
117   
118    NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
119    [toolbar setDelegate: self];
120    [toolbar setAllowsUserCustomization: NO];
121    [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
122    [toolbar setSizeMode: NSToolbarSizeModeRegular];
123    [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
124    [[self window] setToolbar: toolbar];
125    [toolbar release];
126   
127    [self setPrefView: nil];
128   
129    //set download folder
130    [fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
131   
132    //set stop ratio
133    [self updateRatioStopField];
134   
135    //set limits
136    [self updateLimitFields];
137   
138    //set speed limit
139    [fSpeedLimitUploadField setIntValue: [fDefaults integerForKey: @"SpeedLimitUploadLimit"]];
140    [fSpeedLimitDownloadField setIntValue: [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]];
141   
142    //set port
143    [fPortField setIntValue: [fDefaults integerForKey: @"BindPort"]];
144    fNatStatus = -1;
145   
146    [self updatePortStatus];
147    fPortStatusTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
148                        selector: @selector(updatePortStatus) userInfo: nil repeats: YES];
149   
150    //set peer connections
151    [fPeersGlobalField setIntValue: [fDefaults integerForKey: @"PeersTotal"]];
152    [fPeersTorrentField setIntValue: [fDefaults integerForKey: @"PeersTorrent"]];
153   
154    //set queue values
155    [fQueueDownloadField setIntValue: [fDefaults integerForKey: @"QueueDownloadNumber"]];
156    [fQueueSeedField setIntValue: [fDefaults integerForKey: @"QueueSeedNumber"]];
157    [fStalledField setIntValue: [fDefaults integerForKey: @"StalledMinutes"]];
158   
159    //set blocklist
160    [self updateBlocklistFields];
161}
162
163- (void) setUpdater: (SUUpdater *) updater
164{
165    fUpdater = updater;
166}
167
168- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
169{
170    NSToolbarItem * item;
171    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
172
173    if ([ident isEqualToString: TOOLBAR_GENERAL])
174    {
175        [item setLabel: NSLocalizedString(@"General", "Preferences -> toolbar item title")];
176        [item setImage: [NSImage imageNamed: [NSApp isOnLeopardOrBetter] ? NSImageNamePreferencesGeneral : @"Preferences.png"]];
177        [item setTarget: self];
178        [item setAction: @selector(setPrefView:)];
179        [item setAutovalidates: NO];
180    }
181    else if ([ident isEqualToString: TOOLBAR_TRANSFERS])
182    {
183        [item setLabel: NSLocalizedString(@"Transfers", "Preferences -> toolbar item title")];
184        [item setImage: [NSImage imageNamed: @"Transfers.png"]];
185        [item setTarget: self];
186        [item setAction: @selector(setPrefView:)];
187        [item setAutovalidates: NO];
188    }
189    else if ([ident isEqualToString: TOOLBAR_BANDWIDTH])
190    {
191        [item setLabel: NSLocalizedString(@"Bandwidth", "Preferences -> toolbar item title")];
192        [item setImage: [NSImage imageNamed: @"Bandwidth.png"]];
193        [item setTarget: self];
194        [item setAction: @selector(setPrefView:)];
195        [item setAutovalidates: NO];
196    }
197    #warning update tiger image
198    else if ([ident isEqualToString: TOOLBAR_PEERS])
199    {
200        [item setLabel: NSLocalizedString(@"Peers", "Preferences -> toolbar item title")];
201        [item setImage: [NSImage imageNamed: [NSApp isOnLeopardOrBetter] ? NSImageNameUserGroup : @"Advanced.png"]];
202        [item setTarget: self];
203        [item setAction: @selector(setPrefView:)];
204        [item setAutovalidates: NO];
205    }
206    else if ([ident isEqualToString: TOOLBAR_NETWORK])
207    {
208        [item setLabel: NSLocalizedString(@"Network", "Preferences -> toolbar item title")];
209        [item setImage: [NSImage imageNamed: [NSApp isOnLeopardOrBetter] ? NSImageNameNetwork : @"Network.png"]];
210        [item setTarget: self];
211        [item setAction: @selector(setPrefView:)];
212        [item setAutovalidates: NO];
213    }
214    else
215    {
216        [item release];
217        return nil;
218    }
219
220    return [item autorelease];
221}
222
223- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
224{
225    return [self toolbarDefaultItemIdentifiers: toolbar];
226}
227
228- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
229{
230    return [self toolbarAllowedItemIdentifiers: toolbar];
231}
232
233- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
234{
235    return [NSArray arrayWithObjects: TOOLBAR_GENERAL, TOOLBAR_TRANSFERS, TOOLBAR_BANDWIDTH, TOOLBAR_PEERS, TOOLBAR_NETWORK, nil];
236}
237
238//used by ipc
239- (void) updatePortField
240{
241    [fPortField setIntValue: [fDefaults integerForKey: @"BindPort"]];
242}
243
244- (void) setPort: (id) sender
245{
246    int port = [sender intValue];
247    [fDefaults setInteger: port forKey: @"BindPort"];
248    tr_setBindPort(fHandle, port);
249   
250    fPublicPort = -1;
251    [self updatePortStatus];
252}
253
254- (void) setNat: (id) sender
255{
256    tr_natTraversalEnable(fHandle, [fDefaults boolForKey: @"NatTraversal"]);
257   
258    fNatStatus = -1;
259    [self updatePortStatus];
260}
261
262- (void) updatePortStatus
263{
264    const tr_handle_status * stat = tr_handleStatus(fHandle);
265    if (fNatStatus != stat->natTraversalStatus || fPublicPort != stat->publicPort)
266    {
267        fNatStatus = stat->natTraversalStatus;
268        fPublicPort = stat->publicPort;
269       
270        [fPortStatusField setStringValue: [NSLocalizedString(@"Checking port status", "Preferences -> Network -> port status")
271            stringByAppendingEllipsis]];
272        [fPortStatusImage setImage: nil];
273        [fPortStatusProgress startAnimation: self];
274       
275        if (fPortChecker)
276        {
277            [fPortChecker cancelProbe];
278            [fPortChecker release];
279        }
280        fPortChecker = [[PortChecker alloc] initForPort: fPublicPort withDelegate: self];
281    }
282}
283
284- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
285{
286    [fPortStatusProgress stopAnimation: self];
287    switch ([fPortChecker status])
288    {
289        case PORT_STATUS_OPEN:
290            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Network -> port status")];
291            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]];
292            break;
293        case PORT_STATUS_CLOSED:
294            [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Network -> port status")];
295            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
296            break;
297        case PORT_STATUS_STEALTH:
298            [fPortStatusField setStringValue: NSLocalizedString(@"Port is stealth", "Preferences -> Network -> port status")];
299            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
300            break;
301        case PORT_STATUS_ERROR:
302            [fPortStatusField setStringValue: NSLocalizedString(@"Unable to check port status",
303                                                "Preferences -> Network -> port status")];
304            [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.png"]];
305            break;
306    }
307    [fPortChecker release];
308    fPortChecker = nil;
309}
310
311- (NSArray *) sounds
312{
313    NSMutableArray * sounds = [NSMutableArray array];
314   
315    //until Apple can fix soundNamed to not crash on invalid sound files, don't use custom sounds
316    NSArray * directories = [NSArray arrayWithObjects: @"/System/Library/Sounds", @"/Library/Sounds",
317                                /*[NSHomeDirectory() stringByAppendingPathComponent: @"Library/Sounds"],*/ nil];
318    BOOL isDirectory;
319    NSEnumerator * soundEnumerator;
320    NSString * sound;
321   
322    NSString * directory;
323    NSEnumerator * enumerator = [directories objectEnumerator];
324    while ((directory = [enumerator nextObject]))
325        if ([[NSFileManager defaultManager] fileExistsAtPath: directory isDirectory: &isDirectory] && isDirectory)
326        {
327                soundEnumerator = [[[NSFileManager defaultManager] directoryContentsAtPath: directory] objectEnumerator];
328                while ((sound = [soundEnumerator nextObject]))
329                {
330                    sound = [sound stringByDeletingPathExtension];
331                    if ([NSSound soundNamed: sound])
332                        [sounds addObject: sound];
333                }
334        }
335   
336    return sounds;
337}
338
339- (void) setSound: (id) sender
340{
341    //play sound when selecting
342    NSSound * sound;
343    if ((sound = [NSSound soundNamed: [sender titleOfSelectedItem]]))
344        [sound play];
345}
346
347- (void) setPeersGlobal: (id) sender
348{
349    int count = [sender intValue];
350    [fDefaults setInteger: count forKey: @"PeersTotal"];
351    tr_setGlobalPeerLimit(fHandle, count);
352}
353
354- (void) setPeersTorrent: (id) sender
355{
356    int count = [sender intValue];
357    [fDefaults setInteger: count forKey: @"PeersTorrent"];
358}
359
360- (void) setPEX: (id) sender
361{
362    tr_setPexEnabled(fHandle, [fDefaults boolForKey: @"PEXGlobal"]);
363}
364
365- (void) setEncryptionMode: (id) sender
366{
367    tr_setEncryptionMode(fHandle, [fDefaults boolForKey: @"EncryptionPrefer"] ?
368        ([fDefaults boolForKey: @"EncryptionRequire"] ? TR_ENCRYPTION_REQUIRED : TR_ENCRYPTION_PREFERRED) : TR_PLAINTEXT_PREFERRED);
369}
370
371- (void) setBlocklistEnabled: (id) sender
372{
373    BOOL enable = [sender state] == NSOnState;
374    [fDefaults setBool: enable forKey: @"Blocklist"];
375    tr_blocklistSetEnabled(fHandle, enable);
376}
377
378- (void) updateBlocklist: (id) sender
379{
380    [BlocklistDownloader downloadWithPrefsController: self withHandle: fHandle];
381}
382
383- (void) updateBlocklistFields
384{
385    BOOL exists = tr_blocklistExists(fHandle);
386   
387    if (exists)
388    {
389        NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
390        [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
391        [numberFormatter setMaximumFractionDigits: 0];
392        NSString * countString = [numberFormatter stringFromNumber: [NSNumber numberWithInt: tr_blocklistGetRuleCount(fHandle)]];
393        [numberFormatter release];
394       
395        [fBlocklistMessageField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ IP address rules in list",
396            "Prefs -> blocklist -> message"), countString]];
397    }
398    else
399        [fBlocklistMessageField setStringValue: NSLocalizedString(@"A blocklist must first be downloaded",
400            "Prefs -> blocklist -> message")];
401   
402    [fBlocklistEnableCheck setEnabled: exists];
403    [fBlocklistEnableCheck setState: exists && [fDefaults boolForKey: @"Blocklist"]];
404}
405
406- (void) applySpeedSettings: (id) sender
407{
408    if ([fDefaults boolForKey: @"SpeedLimit"])
409    {
410        tr_setUseGlobalSpeedLimit(fHandle, TR_UP, 1);
411        tr_setGlobalSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
412       
413        tr_setUseGlobalSpeedLimit(fHandle, TR_DOWN, 1);
414        tr_setGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
415    }
416    else
417    {
418        tr_setUseGlobalSpeedLimit(fHandle, TR_UP, [fDefaults boolForKey: @"CheckUpload"]);
419        tr_setGlobalSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"UploadLimit"]);
420       
421        tr_setUseGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults boolForKey: @"CheckDownload"]);
422        tr_setGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"DownloadLimit"]);
423    }
424}
425
426- (void) applyRatioSetting: (id) sender
427{
428    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
429}
430
431- (void) updateRatioStopField
432{
433    if (!fHasLoaded)
434        return;
435   
436    [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
437   
438    [self applyRatioSetting: nil];
439}
440
441- (void) setRatioStop: (id) sender
442{
443    [fDefaults setFloat: [sender floatValue] forKey: @"RatioLimit"];
444    [self applyRatioSetting: nil];
445}
446
447- (void) updateLimitFields
448{
449    if (!fHasLoaded)
450        return;
451   
452    [fUploadField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
453    [fDownloadField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
454}
455
456- (void) setGlobalLimit: (id) sender
457{
458    [fDefaults setInteger: [sender intValue] forKey: sender == fUploadField ? @"UploadLimit" : @"DownloadLimit"];
459    [self applySpeedSettings: self];
460}
461
462- (void) setSpeedLimit: (id) sender
463{
464    [fDefaults setInteger: [sender intValue] forKey: sender == fSpeedLimitUploadField
465                                                        ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"];
466    [self applySpeedSettings: self];
467}
468
469- (void) setAutoSpeedLimit: (id) sender
470{
471    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
472}
473
474- (BOOL) control: (NSControl *) control textShouldBeginEditing: (NSText *) fieldEditor
475{
476    [fInitialString release];
477    fInitialString = [[control stringValue] retain];
478   
479    return YES;
480}
481
482- (BOOL) control: (NSControl *) control didFailToFormatString: (NSString *) string errorDescription: (NSString *) error
483{
484    NSBeep();
485    if (fInitialString)
486    {
487        [control setStringValue: fInitialString];
488        [fInitialString release];
489        fInitialString = nil;
490    }
491    return NO;
492}
493
494- (void) setBadge: (id) sender
495{
496    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
497}
498
499- (void) resetWarnings: (id) sender
500{
501    [fDefaults setBool: YES forKey: @"WarningDuplicate"];
502    [fDefaults setBool: YES forKey: @"WarningRemainingSpace"];
503    [fDefaults setBool: YES forKey: @"WarningFolderDataSameName"];
504}
505
506- (void) setCheckForUpdate: (id) sender
507{
508    NSTimeInterval seconds = [fDefaults boolForKey: @"CheckForUpdates"] ? UPDATE_SECONDS : 0;
509    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
510    if (fUpdater)
511        [fUpdater scheduleCheckWithInterval: seconds];
512}
513
514- (void) setQueue: (id) sender
515{
516    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
517}
518
519- (void) setQueueNumber: (id) sender
520{
521    [fDefaults setInteger: [sender intValue] forKey: sender == fQueueDownloadField ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
522    [self setQueue: nil];
523}
524
525- (void) setStalled: (id) sender
526{
527    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
528}
529
530- (void) setStalledMinutes: (id) sender
531{
532    [fDefaults setInteger: [sender intValue] forKey: @"StalledMinutes"];
533    [self setStalled: nil];
534}
535
536- (void) setDownloadLocation: (id) sender
537{
538    [fDefaults setBool: [fFolderPopUp indexOfSelectedItem] ==  DOWNLOAD_FOLDER forKey: @"DownloadLocationConstant"];
539}
540
541- (void) folderSheetShow: (id) sender
542{
543    NSOpenPanel * panel = [NSOpenPanel openPanel];
544
545    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
546    [panel setAllowsMultipleSelection: NO];
547    [panel setCanChooseFiles: NO];
548    [panel setCanChooseDirectories: YES];
549    [panel setCanCreateDirectories: YES];
550
551    [panel beginSheetForDirectory: nil file: nil types: nil
552        modalForWindow: [self window] modalDelegate: self didEndSelector:
553        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
554}
555
556- (void) incompleteFolderSheetShow: (id) sender
557{
558    NSOpenPanel * panel = [NSOpenPanel openPanel];
559
560    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
561    [panel setAllowsMultipleSelection: NO];
562    [panel setCanChooseFiles: NO];
563    [panel setCanChooseDirectories: YES];
564    [panel setCanCreateDirectories: YES];
565
566    [panel beginSheetForDirectory: nil file: nil types: nil
567        modalForWindow: [self window] modalDelegate: self didEndSelector:
568        @selector(incompleteFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
569}
570
571- (void) setAutoImport: (id) sender
572{
573    NSString * path;
574    if ((path = [fDefaults stringForKey: @"AutoImportDirectory"]))
575    {
576        path = [path stringByExpandingTildeInPath];
577        if ([fDefaults boolForKey: @"AutoImport"])
578            [[UKKQueue sharedFileWatcher] addPath: path];
579        else
580            [[UKKQueue sharedFileWatcher] removePathFromQueue: path];
581       
582        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
583    }
584    else
585        [self importFolderSheetShow: nil];
586}
587
588- (void) importFolderSheetShow: (id) sender
589{
590    NSOpenPanel * panel = [NSOpenPanel openPanel];
591
592    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
593    [panel setAllowsMultipleSelection: NO];
594    [panel setCanChooseFiles: NO];
595    [panel setCanChooseDirectories: YES];
596    [panel setCanCreateDirectories: YES];
597
598    [panel beginSheetForDirectory: nil file: nil types: nil
599        modalForWindow: [self window] modalDelegate: self didEndSelector:
600        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
601}
602
603- (void) setAutoSize: (id) sender
604{
605    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
606}
607
608- (void) helpForPeers: (id) sender
609{
610    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PeersPrefs"
611        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
612}
613
614- (void) helpForNetwork: (id) sender
615{
616    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"NetworkPrefs"
617        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
618}
619
620@end
621
622@implementation PrefsController (Private)
623
624- (void) setPrefView: (id) sender
625{
626    NSView * view = fGeneralView;
627    if (sender)
628    {
629        NSString * identifier = [sender itemIdentifier];
630        if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
631            view = fTransfersView;
632        else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
633            view = fBandwidthView;
634        else if ([identifier isEqualToString: TOOLBAR_PEERS])
635            view = fPeersView;
636        else if ([identifier isEqualToString: TOOLBAR_NETWORK])
637            view = fNetworkView;
638        else; //general view already selected
639    }
640   
641    NSWindow * window = [self window];
642    if ([window contentView] == view)
643        return;
644   
645    NSRect windowRect = [window frame];
646    float difference = ([view frame].size.height - [[window contentView] frame].size.height) * [window userSpaceScaleFactor];
647    windowRect.origin.y -= difference;
648    windowRect.size.height += difference;
649   
650    [view setHidden: YES];
651    [window setContentView: view];
652    [window setFrame: windowRect display: YES animate: YES];
653    [view setHidden: NO];
654   
655    //set title label
656    if (sender)
657        [window setTitle: [sender label]];
658    else
659    {
660        NSToolbar * toolbar = [window toolbar];
661        NSString * itemIdentifier = [toolbar selectedItemIdentifier];
662        NSEnumerator * enumerator = [[toolbar items] objectEnumerator];
663        NSToolbarItem * item;
664        while ((item = [enumerator nextObject]))
665            if ([[item itemIdentifier] isEqualToString: itemIdentifier])
666            {
667                [window setTitle: [item label]];
668                break;
669            }
670    }
671   
672    //for network view make sure progress indicator hides itself
673    if (view == fNetworkView && [fPortStatusImage image])
674        [fPortStatusProgress setDisplayedWhenStopped: NO];
675}
676
677- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
678{
679    if (code == NSOKButton)
680    {
681        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
682        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"DownloadFolder"];
683        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
684    }
685    else
686    {
687        //reset if cancelled
688        [fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
689    }
690}
691
692- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
693{
694    if (code == NSOKButton)
695        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"IncompleteDownloadFolder"];
696    [fIncompleteFolderPopUp selectItemAtIndex: 0];
697}
698
699- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
700{
701    NSString * path = [fDefaults stringForKey: @"AutoImportDirectory"];
702    if (code == NSOKButton)
703    {
704        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
705        if (path)
706            [sharedQueue removePathFromQueue: [path stringByExpandingTildeInPath]];
707       
708        path = [[openPanel filenames] objectAtIndex: 0];
709        [fDefaults setObject: path forKey: @"AutoImportDirectory"];
710        [sharedQueue addPath: [path stringByExpandingTildeInPath]];
711       
712        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
713    }
714    else if (!path)
715        [fDefaults setBool: NO forKey: @"AutoImport"];
716   
717    [fImportFolderPopUp selectItemAtIndex: 0];
718}
719
720@end
Note: See TracBrowser for help on using the repository browser.