source: trunk/macosx/PrefsController.m @ 5564

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

on Tiger, show an appropriate image for the Peers pref tab

  • Property svn:keywords set to Date Rev Author Id
File size: 24.5 KB
Line 
1/******************************************************************************
2 * $Id: PrefsController.m 5564 2008-04-07 21:13:10Z 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    else if ([ident isEqualToString: TOOLBAR_PEERS])
198    {
199        [item setLabel: NSLocalizedString(@"Peers", "Preferences -> toolbar item title")];
200        [item setImage: [NSImage imageNamed: [NSApp isOnLeopardOrBetter] ? NSImageNameUserGroup : @"Peers.png"]];
201        [item setTarget: self];
202        [item setAction: @selector(setPrefView:)];
203        [item setAutovalidates: NO];
204    }
205    else if ([ident isEqualToString: TOOLBAR_NETWORK])
206    {
207        [item setLabel: NSLocalizedString(@"Network", "Preferences -> toolbar item title")];
208        [item setImage: [NSImage imageNamed: [NSApp isOnLeopardOrBetter] ? NSImageNameNetwork : @"Network.png"]];
209        [item setTarget: self];
210        [item setAction: @selector(setPrefView:)];
211        [item setAutovalidates: NO];
212    }
213    else
214    {
215        [item release];
216        return nil;
217    }
218
219    return [item autorelease];
220}
221
222- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
223{
224    return [self toolbarDefaultItemIdentifiers: toolbar];
225}
226
227- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
228{
229    return [self toolbarAllowedItemIdentifiers: toolbar];
230}
231
232- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
233{
234    return [NSArray arrayWithObjects: TOOLBAR_GENERAL, TOOLBAR_TRANSFERS, TOOLBAR_BANDWIDTH, TOOLBAR_PEERS, TOOLBAR_NETWORK, nil];
235}
236
237//used by ipc
238- (void) updatePortField
239{
240    [fPortField setIntValue: [fDefaults integerForKey: @"BindPort"]];
241}
242
243- (void) setPort: (id) sender
244{
245    int port = [sender intValue];
246    [fDefaults setInteger: port forKey: @"BindPort"];
247    tr_setBindPort(fHandle, port);
248   
249    fPublicPort = -1;
250    [self updatePortStatus];
251}
252
253- (void) setNat: (id) sender
254{
255    tr_natTraversalEnable(fHandle, [fDefaults boolForKey: @"NatTraversal"]);
256   
257    fNatStatus = -1;
258    [self updatePortStatus];
259}
260
261- (void) updatePortStatus
262{
263    const tr_handle_status * stat = tr_handleStatus(fHandle);
264    if (fNatStatus != stat->natTraversalStatus || fPublicPort != stat->publicPort)
265    {
266        fNatStatus = stat->natTraversalStatus;
267        fPublicPort = stat->publicPort;
268       
269        [fPortStatusField setStringValue: [NSLocalizedString(@"Checking port status", "Preferences -> Network -> port status")
270            stringByAppendingEllipsis]];
271        [fPortStatusImage setImage: nil];
272        [fPortStatusProgress startAnimation: self];
273       
274        if (fPortChecker)
275        {
276            [fPortChecker cancelProbe];
277            [fPortChecker release];
278        }
279        fPortChecker = [[PortChecker alloc] initForPort: fPublicPort withDelegate: self];
280    }
281}
282
283- (void) portCheckerDidFinishProbing: (PortChecker *) portChecker
284{
285    [fPortStatusProgress stopAnimation: self];
286    switch ([fPortChecker status])
287    {
288        case PORT_STATUS_OPEN:
289            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Network -> port status")];
290            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]];
291            break;
292        case PORT_STATUS_CLOSED:
293            [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Network -> port status")];
294            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
295            break;
296        case PORT_STATUS_STEALTH:
297            [fPortStatusField setStringValue: NSLocalizedString(@"Port is stealth", "Preferences -> Network -> port status")];
298            [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
299            break;
300        case PORT_STATUS_ERROR:
301            [fPortStatusField setStringValue: NSLocalizedString(@"Unable to check port status",
302                                                "Preferences -> Network -> port status")];
303            [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.png"]];
304            break;
305    }
306    [fPortChecker release];
307    fPortChecker = nil;
308}
309
310- (NSArray *) sounds
311{
312    NSMutableArray * sounds = [NSMutableArray array];
313   
314    //until Apple can fix soundNamed to not crash on invalid sound files, don't use custom sounds
315    NSArray * directories = [NSArray arrayWithObjects: @"/System/Library/Sounds", @"/Library/Sounds",
316                                /*[NSHomeDirectory() stringByAppendingPathComponent: @"Library/Sounds"],*/ nil];
317    BOOL isDirectory;
318    NSEnumerator * soundEnumerator;
319    NSString * sound;
320   
321    NSString * directory;
322    NSEnumerator * enumerator = [directories objectEnumerator];
323    while ((directory = [enumerator nextObject]))
324        if ([[NSFileManager defaultManager] fileExistsAtPath: directory isDirectory: &isDirectory] && isDirectory)
325        {
326                soundEnumerator = [[[NSFileManager defaultManager] directoryContentsAtPath: directory] objectEnumerator];
327                while ((sound = [soundEnumerator nextObject]))
328                {
329                    sound = [sound stringByDeletingPathExtension];
330                    if ([NSSound soundNamed: sound])
331                        [sounds addObject: sound];
332                }
333        }
334   
335    return sounds;
336}
337
338- (void) setSound: (id) sender
339{
340    //play sound when selecting
341    NSSound * sound;
342    if ((sound = [NSSound soundNamed: [sender titleOfSelectedItem]]))
343        [sound play];
344}
345
346- (void) setPeersGlobal: (id) sender
347{
348    int count = [sender intValue];
349    [fDefaults setInteger: count forKey: @"PeersTotal"];
350    tr_setGlobalPeerLimit(fHandle, count);
351}
352
353- (void) setPeersTorrent: (id) sender
354{
355    int count = [sender intValue];
356    [fDefaults setInteger: count forKey: @"PeersTorrent"];
357}
358
359- (void) setPEX: (id) sender
360{
361    tr_setPexEnabled(fHandle, [fDefaults boolForKey: @"PEXGlobal"]);
362}
363
364- (void) setEncryptionMode: (id) sender
365{
366    tr_setEncryptionMode(fHandle, [fDefaults boolForKey: @"EncryptionPrefer"] ?
367        ([fDefaults boolForKey: @"EncryptionRequire"] ? TR_ENCRYPTION_REQUIRED : TR_ENCRYPTION_PREFERRED) : TR_PLAINTEXT_PREFERRED);
368}
369
370- (void) setBlocklistEnabled: (id) sender
371{
372    BOOL enable = [sender state] == NSOnState;
373    [fDefaults setBool: enable forKey: @"Blocklist"];
374    tr_blocklistSetEnabled(fHandle, enable);
375}
376
377- (void) updateBlocklist: (id) sender
378{
379    [BlocklistDownloader downloadWithPrefsController: self withHandle: fHandle];
380}
381
382- (void) updateBlocklistFields
383{
384    BOOL exists = tr_blocklistExists(fHandle);
385   
386    if (exists)
387    {
388        NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
389        [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
390        [numberFormatter setMaximumFractionDigits: 0];
391        NSString * countString = [numberFormatter stringFromNumber: [NSNumber numberWithInt: tr_blocklistGetRuleCount(fHandle)]];
392        [numberFormatter release];
393       
394        [fBlocklistMessageField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ IP address rules in list",
395            "Prefs -> blocklist -> message"), countString]];
396    }
397    else
398        [fBlocklistMessageField setStringValue: NSLocalizedString(@"A blocklist must first be downloaded",
399            "Prefs -> blocklist -> message")];
400   
401    [fBlocklistEnableCheck setEnabled: exists];
402    [fBlocklistEnableCheck setState: exists && [fDefaults boolForKey: @"Blocklist"]];
403}
404
405- (void) applySpeedSettings: (id) sender
406{
407    if ([fDefaults boolForKey: @"SpeedLimit"])
408    {
409        tr_setUseGlobalSpeedLimit(fHandle, TR_UP, 1);
410        tr_setGlobalSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]);
411       
412        tr_setUseGlobalSpeedLimit(fHandle, TR_DOWN, 1);
413        tr_setGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]);
414    }
415    else
416    {
417        tr_setUseGlobalSpeedLimit(fHandle, TR_UP, [fDefaults boolForKey: @"CheckUpload"]);
418        tr_setGlobalSpeedLimit(fHandle, TR_UP, [fDefaults integerForKey: @"UploadLimit"]);
419       
420        tr_setUseGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults boolForKey: @"CheckDownload"]);
421        tr_setGlobalSpeedLimit(fHandle, TR_DOWN, [fDefaults integerForKey: @"DownloadLimit"]);
422    }
423}
424
425- (void) applyRatioSetting: (id) sender
426{
427    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
428}
429
430- (void) updateRatioStopField
431{
432    if (!fHasLoaded)
433        return;
434   
435    [fRatioStopField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]];
436   
437    [self applyRatioSetting: nil];
438}
439
440- (void) setRatioStop: (id) sender
441{
442    [fDefaults setFloat: [sender floatValue] forKey: @"RatioLimit"];
443    [self applyRatioSetting: nil];
444}
445
446- (void) updateLimitFields
447{
448    if (!fHasLoaded)
449        return;
450   
451    [fUploadField setIntValue: [fDefaults integerForKey: @"UploadLimit"]];
452    [fDownloadField setIntValue: [fDefaults integerForKey: @"DownloadLimit"]];
453}
454
455- (void) setGlobalLimit: (id) sender
456{
457    [fDefaults setInteger: [sender intValue] forKey: sender == fUploadField ? @"UploadLimit" : @"DownloadLimit"];
458    [self applySpeedSettings: self];
459}
460
461- (void) setSpeedLimit: (id) sender
462{
463    [fDefaults setInteger: [sender intValue] forKey: sender == fSpeedLimitUploadField
464                                                        ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"];
465    [self applySpeedSettings: self];
466}
467
468- (void) setAutoSpeedLimit: (id) sender
469{
470    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self];
471}
472
473- (BOOL) control: (NSControl *) control textShouldBeginEditing: (NSText *) fieldEditor
474{
475    [fInitialString release];
476    fInitialString = [[control stringValue] retain];
477   
478    return YES;
479}
480
481- (BOOL) control: (NSControl *) control didFailToFormatString: (NSString *) string errorDescription: (NSString *) error
482{
483    NSBeep();
484    if (fInitialString)
485    {
486        [control setStringValue: fInitialString];
487        [fInitialString release];
488        fInitialString = nil;
489    }
490    return NO;
491}
492
493- (void) setBadge: (id) sender
494{
495    [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self];
496}
497
498- (void) resetWarnings: (id) sender
499{
500    [fDefaults setBool: YES forKey: @"WarningDuplicate"];
501    [fDefaults setBool: YES forKey: @"WarningRemainingSpace"];
502    [fDefaults setBool: YES forKey: @"WarningFolderDataSameName"];
503}
504
505- (void) setCheckForUpdate: (id) sender
506{
507    NSTimeInterval seconds = [fDefaults boolForKey: @"CheckForUpdates"] ? UPDATE_SECONDS : 0;
508    [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"];
509    if (fUpdater)
510        [fUpdater scheduleCheckWithInterval: seconds];
511}
512
513- (void) setQueue: (id) sender
514{
515    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
516}
517
518- (void) setQueueNumber: (id) sender
519{
520    [fDefaults setInteger: [sender intValue] forKey: sender == fQueueDownloadField ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
521    [self setQueue: nil];
522}
523
524- (void) setStalled: (id) sender
525{
526    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self];
527}
528
529- (void) setStalledMinutes: (id) sender
530{
531    [fDefaults setInteger: [sender intValue] forKey: @"StalledMinutes"];
532    [self setStalled: nil];
533}
534
535- (void) setDownloadLocation: (id) sender
536{
537    [fDefaults setBool: [fFolderPopUp indexOfSelectedItem] ==  DOWNLOAD_FOLDER forKey: @"DownloadLocationConstant"];
538}
539
540- (void) folderSheetShow: (id) sender
541{
542    NSOpenPanel * panel = [NSOpenPanel openPanel];
543
544    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
545    [panel setAllowsMultipleSelection: NO];
546    [panel setCanChooseFiles: NO];
547    [panel setCanChooseDirectories: YES];
548    [panel setCanCreateDirectories: YES];
549
550    [panel beginSheetForDirectory: nil file: nil types: nil
551        modalForWindow: [self window] modalDelegate: self didEndSelector:
552        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
553}
554
555- (void) incompleteFolderSheetShow: (id) sender
556{
557    NSOpenPanel * panel = [NSOpenPanel openPanel];
558
559    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
560    [panel setAllowsMultipleSelection: NO];
561    [panel setCanChooseFiles: NO];
562    [panel setCanChooseDirectories: YES];
563    [panel setCanCreateDirectories: YES];
564
565    [panel beginSheetForDirectory: nil file: nil types: nil
566        modalForWindow: [self window] modalDelegate: self didEndSelector:
567        @selector(incompleteFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
568}
569
570- (void) setAutoImport: (id) sender
571{
572    NSString * path;
573    if ((path = [fDefaults stringForKey: @"AutoImportDirectory"]))
574    {
575        path = [path stringByExpandingTildeInPath];
576        if ([fDefaults boolForKey: @"AutoImport"])
577            [[UKKQueue sharedFileWatcher] addPath: path];
578        else
579            [[UKKQueue sharedFileWatcher] removePathFromQueue: path];
580       
581        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
582    }
583    else
584        [self importFolderSheetShow: nil];
585}
586
587- (void) importFolderSheetShow: (id) sender
588{
589    NSOpenPanel * panel = [NSOpenPanel openPanel];
590
591    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
592    [panel setAllowsMultipleSelection: NO];
593    [panel setCanChooseFiles: NO];
594    [panel setCanChooseDirectories: YES];
595    [panel setCanCreateDirectories: YES];
596
597    [panel beginSheetForDirectory: nil file: nil types: nil
598        modalForWindow: [self window] modalDelegate: self didEndSelector:
599        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
600}
601
602- (void) setAutoSize: (id) sender
603{
604    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self];
605}
606
607- (void) helpForPeers: (id) sender
608{
609    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PeersPrefs"
610        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
611}
612
613- (void) helpForNetwork: (id) sender
614{
615    [[NSHelpManager sharedHelpManager] openHelpAnchor: @"NetworkPrefs"
616        inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]];
617}
618
619@end
620
621@implementation PrefsController (Private)
622
623- (void) setPrefView: (id) sender
624{
625    NSView * view = fGeneralView;
626    if (sender)
627    {
628        NSString * identifier = [sender itemIdentifier];
629        if ([identifier isEqualToString: TOOLBAR_TRANSFERS])
630            view = fTransfersView;
631        else if ([identifier isEqualToString: TOOLBAR_BANDWIDTH])
632            view = fBandwidthView;
633        else if ([identifier isEqualToString: TOOLBAR_PEERS])
634            view = fPeersView;
635        else if ([identifier isEqualToString: TOOLBAR_NETWORK])
636            view = fNetworkView;
637        else; //general view already selected
638    }
639   
640    NSWindow * window = [self window];
641    if ([window contentView] == view)
642        return;
643   
644    NSRect windowRect = [window frame];
645    float difference = ([view frame].size.height - [[window contentView] frame].size.height) * [window userSpaceScaleFactor];
646    windowRect.origin.y -= difference;
647    windowRect.size.height += difference;
648   
649    [view setHidden: YES];
650    [window setContentView: view];
651    [window setFrame: windowRect display: YES animate: YES];
652    [view setHidden: NO];
653   
654    //set title label
655    if (sender)
656        [window setTitle: [sender label]];
657    else
658    {
659        NSToolbar * toolbar = [window toolbar];
660        NSString * itemIdentifier = [toolbar selectedItemIdentifier];
661        NSEnumerator * enumerator = [[toolbar items] objectEnumerator];
662        NSToolbarItem * item;
663        while ((item = [enumerator nextObject]))
664            if ([[item itemIdentifier] isEqualToString: itemIdentifier])
665            {
666                [window setTitle: [item label]];
667                break;
668            }
669    }
670   
671    //for network view make sure progress indicator hides itself
672    if (view == fNetworkView && [fPortStatusImage image])
673        [fPortStatusProgress setDisplayedWhenStopped: NO];
674}
675
676- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
677{
678    if (code == NSOKButton)
679    {
680        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
681        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"DownloadFolder"];
682        [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
683    }
684    else
685    {
686        //reset if cancelled
687        [fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
688    }
689}
690
691- (void) incompleteFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
692{
693    if (code == NSOKButton)
694        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"IncompleteDownloadFolder"];
695    [fIncompleteFolderPopUp selectItemAtIndex: 0];
696}
697
698- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
699{
700    NSString * path = [fDefaults stringForKey: @"AutoImportDirectory"];
701    if (code == NSOKButton)
702    {
703        UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher];
704        if (path)
705            [sharedQueue removePathFromQueue: [path stringByExpandingTildeInPath]];
706       
707        path = [[openPanel filenames] objectAtIndex: 0];
708        [fDefaults setObject: path forKey: @"AutoImportDirectory"];
709        [sharedQueue addPath: [path stringByExpandingTildeInPath]];
710       
711        [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
712    }
713    else if (!path)
714        [fDefaults setBool: NO forKey: @"AutoImport"];
715   
716    [fImportFolderPopUp selectItemAtIndex: 0];
717}
718
719@end
Note: See TracBrowser for help on using the repository browser.