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