source: trunk/macosx/PrefsController.m @ 5820

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

mac build compiles

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