1 | /****************************************************************************** |
---|
2 | * $Id: PrefsController.m 850 2006-09-05 21:17:59Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "PrefsController.h" |
---|
26 | #import "StringAdditions.h" |
---|
27 | #import "UKKQueue.h" |
---|
28 | |
---|
29 | #define MIN_PORT 1 |
---|
30 | #define MAX_PORT 65535 |
---|
31 | |
---|
32 | #define DOWNLOAD_FOLDER 0 |
---|
33 | #define DOWNLOAD_TORRENT 2 |
---|
34 | #define DOWNLOAD_ASK 3 |
---|
35 | |
---|
36 | #define START_YES_CHECK_TAG 0 |
---|
37 | #define START_WAIT_CHECK_TAG 1 |
---|
38 | #define START_NO_CHECK_TAG 2 |
---|
39 | |
---|
40 | #define UPDATE_DAILY 0 |
---|
41 | #define UPDATE_WEEKLY 1 |
---|
42 | #define UPDATE_NEVER 2 |
---|
43 | |
---|
44 | #define TOOLBAR_GENERAL @"General" |
---|
45 | #define TOOLBAR_TRANSFERS @"Transfers" |
---|
46 | #define TOOLBAR_BANDWIDTH @"Bandwidth" |
---|
47 | #define TOOLBAR_NETWORK @"Network" |
---|
48 | |
---|
49 | @interface PrefsController (Private) |
---|
50 | |
---|
51 | - (void) showGeneralPref: (id) sender; |
---|
52 | - (void) showTransfersPref: (id) sender; |
---|
53 | - (void) showBandwidthPref: (id) sender; |
---|
54 | - (void) showNetworkPref: (id) sender; |
---|
55 | |
---|
56 | - (void) setPrefView: (NSView *) view; |
---|
57 | |
---|
58 | - (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info; |
---|
59 | - (void) updatePopUp; |
---|
60 | |
---|
61 | - (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info; |
---|
62 | - (void) updateImportPopUp; |
---|
63 | |
---|
64 | @end |
---|
65 | |
---|
66 | @implementation PrefsController |
---|
67 | |
---|
68 | - (id) initWithWindowNibName: (NSString *) name handle: (tr_handle_t *) handle |
---|
69 | { |
---|
70 | if ((self = [self initWithWindowNibName: name])) |
---|
71 | { |
---|
72 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
73 | fHandle = handle; |
---|
74 | |
---|
75 | [[self window] update]; //make sure nib is loaded right away |
---|
76 | } |
---|
77 | return self; |
---|
78 | } |
---|
79 | |
---|
80 | - (void) dealloc |
---|
81 | { |
---|
82 | [fDownloadFolder release]; |
---|
83 | [fImportFolder release]; |
---|
84 | [super dealloc]; |
---|
85 | } |
---|
86 | |
---|
87 | - (void) awakeFromNib |
---|
88 | { |
---|
89 | fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"]; |
---|
90 | [fToolbar setDelegate: self]; |
---|
91 | [fToolbar setAllowsUserCustomization: NO]; |
---|
92 | [[self window] setToolbar: fToolbar]; |
---|
93 | [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel]; |
---|
94 | [fToolbar setSizeMode: NSToolbarSizeModeRegular]; |
---|
95 | |
---|
96 | [fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL]; |
---|
97 | [self showGeneralPref: nil]; |
---|
98 | |
---|
99 | //set download folder |
---|
100 | NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"]; |
---|
101 | fDownloadFolder = [[[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath] retain]; |
---|
102 | if ([downloadChoice isEqualToString: @"Constant"]) |
---|
103 | [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER]; |
---|
104 | else if ([downloadChoice isEqualToString: @"Torrent"]) |
---|
105 | [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT]; |
---|
106 | else |
---|
107 | [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK]; |
---|
108 | [self updatePopUp]; |
---|
109 | |
---|
110 | //set auto import |
---|
111 | fImportFolder = [[[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath] retain]; |
---|
112 | [self updateImportPopUp]; |
---|
113 | |
---|
114 | BOOL autoImport = [fDefaults boolForKey: @"AutoImport"]; |
---|
115 | [fAutoImportCheck setState: autoImport]; |
---|
116 | [fImportFolderPopUp setEnabled: autoImport]; |
---|
117 | |
---|
118 | if (autoImport) |
---|
119 | [[UKKQueue sharedFileWatcher] addPath: fImportFolder]; |
---|
120 | |
---|
121 | //set auto size |
---|
122 | [fAutoSizeCheck setState: [fDefaults boolForKey: @"AutoSize"]]; |
---|
123 | |
---|
124 | //set bind port |
---|
125 | int bindPort = [fDefaults integerForKey: @"BindPort"]; |
---|
126 | [fPortField setIntValue: bindPort]; |
---|
127 | tr_setBindPort(fHandle, bindPort); |
---|
128 | |
---|
129 | //checks for old version upload speed of -1 |
---|
130 | if ([fDefaults integerForKey: @"UploadLimit"] < 0) |
---|
131 | { |
---|
132 | [fDefaults setInteger: 20 forKey: @"UploadLimit"]; |
---|
133 | [fDefaults setBool: NO forKey: @"CheckUpload"]; |
---|
134 | } |
---|
135 | |
---|
136 | //set upload limit |
---|
137 | BOOL checkUpload = [fDefaults boolForKey: @"CheckUpload"]; |
---|
138 | int uploadLimit = [fDefaults integerForKey: @"UploadLimit"]; |
---|
139 | |
---|
140 | [fUploadCheck setState: checkUpload]; |
---|
141 | [fUploadField setIntValue: uploadLimit]; |
---|
142 | [fUploadField setEnabled: checkUpload]; |
---|
143 | |
---|
144 | //set download limit |
---|
145 | BOOL checkDownload = [fDefaults boolForKey: @"CheckDownload"]; |
---|
146 | int downloadLimit = [fDefaults integerForKey: @"DownloadLimit"]; |
---|
147 | |
---|
148 | [fDownloadCheck setState: checkDownload]; |
---|
149 | [fDownloadField setIntValue: downloadLimit]; |
---|
150 | [fDownloadField setEnabled: checkDownload]; |
---|
151 | |
---|
152 | //set speed limit |
---|
153 | int speedLimitUploadLimit = [fDefaults integerForKey: @"SpeedLimitUploadLimit"]; |
---|
154 | [fSpeedLimitUploadField setIntValue: speedLimitUploadLimit]; |
---|
155 | |
---|
156 | int speedLimitDownloadLimit = [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]; |
---|
157 | [fSpeedLimitDownloadField setIntValue: speedLimitDownloadLimit]; |
---|
158 | |
---|
159 | //actually set bandwidth limits |
---|
160 | if ([fDefaults boolForKey: @"SpeedLimit"]) |
---|
161 | { |
---|
162 | tr_setUploadLimit(fHandle, speedLimitUploadLimit); |
---|
163 | tr_setDownloadLimit(fHandle, speedLimitDownloadLimit); |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | tr_setUploadLimit(fHandle, checkUpload ? uploadLimit : -1); |
---|
168 | tr_setDownloadLimit(fHandle, checkDownload ? downloadLimit : -1); |
---|
169 | } |
---|
170 | |
---|
171 | //set auto speed limit |
---|
172 | BOOL speedLimitAuto = [fDefaults boolForKey: @"SpeedLimitAuto"]; |
---|
173 | [fSpeedLimitAutoCheck setState: speedLimitAuto]; |
---|
174 | |
---|
175 | int speedLimitAutoOnHour = [fDefaults integerForKey: @"SpeedLimitAutoOnHour"]; |
---|
176 | [fSpeedLimitAutoOnField setStringValue: [NSString stringWithFormat: @"%02d", speedLimitAutoOnHour]]; |
---|
177 | [fSpeedLimitAutoOnField setEnabled: speedLimitAuto]; |
---|
178 | |
---|
179 | int speedLimitAutoOffHour = [fDefaults integerForKey: @"SpeedLimitAutoOffHour"]; |
---|
180 | [fSpeedLimitAutoOffField setStringValue: [NSString stringWithFormat: @"%02d", speedLimitAutoOffHour]]; |
---|
181 | [fSpeedLimitAutoOffField setEnabled: speedLimitAuto]; |
---|
182 | |
---|
183 | //set ratio limit |
---|
184 | BOOL ratioCheck = [fDefaults boolForKey: @"RatioCheck"]; |
---|
185 | [fRatioCheck setState: ratioCheck]; |
---|
186 | [fRatioField setEnabled: ratioCheck]; |
---|
187 | [fRatioField setFloatValue: [fDefaults floatForKey: @"RatioLimit"]]; |
---|
188 | |
---|
189 | //set remove and quit prompts |
---|
190 | BOOL isQuitCheck = [fDefaults boolForKey: @"CheckQuit"], |
---|
191 | isRemoveCheck = [fDefaults boolForKey: @"CheckRemove"]; |
---|
192 | |
---|
193 | [fQuitCheck setState: isQuitCheck]; |
---|
194 | [fRemoveCheck setState: isRemoveCheck]; |
---|
195 | |
---|
196 | [fQuitDownloadingCheck setState: [fDefaults boolForKey: @"CheckQuitDownloading"]]; |
---|
197 | [fQuitDownloadingCheck setEnabled: isQuitCheck]; |
---|
198 | [fRemoveDownloadingCheck setState: [fDefaults boolForKey: @"CheckRemoveDownloading"]]; |
---|
199 | [fRemoveDownloadingCheck setEnabled: isRemoveCheck]; |
---|
200 | |
---|
201 | //set dock badging |
---|
202 | [fBadgeDownloadRateCheck setState: [fDefaults boolForKey: @"BadgeDownloadRate"]]; |
---|
203 | [fBadgeUploadRateCheck setState: [fDefaults boolForKey: @"BadgeUploadRate"]]; |
---|
204 | |
---|
205 | //set play sound |
---|
206 | NSMutableArray * sounds = [NSMutableArray array]; |
---|
207 | NSEnumerator * soundEnumerator, |
---|
208 | * soundDirectoriesEnumerator = [[NSArray arrayWithObjects: @"System/Library/Sounds", |
---|
209 | [NSHomeDirectory() stringByAppendingPathComponent: @"Library/Sounds"], nil] objectEnumerator]; |
---|
210 | NSString * soundPath, * sound; |
---|
211 | |
---|
212 | //get list of all sounds and sort alphabetically |
---|
213 | while ((soundPath = [soundDirectoriesEnumerator nextObject])) |
---|
214 | if (soundEnumerator = [[NSFileManager defaultManager] enumeratorAtPath: soundPath]) |
---|
215 | while ((sound = [soundEnumerator nextObject])) |
---|
216 | { |
---|
217 | sound = [sound stringByDeletingPathExtension]; |
---|
218 | if ([NSSound soundNamed: sound]) |
---|
219 | [sounds addObject: sound]; |
---|
220 | } |
---|
221 | |
---|
222 | [sounds sortUsingSelector: @selector(caseInsensitiveCompare:)]; |
---|
223 | |
---|
224 | //set download sound |
---|
225 | [fDownloadSoundPopUp removeAllItems]; |
---|
226 | [fDownloadSoundPopUp addItemsWithTitles: sounds]; |
---|
227 | |
---|
228 | BOOL playDownloadSound = [fDefaults boolForKey: @"PlayDownloadSound"]; |
---|
229 | [fPlayDownloadSoundCheck setState: playDownloadSound]; |
---|
230 | [fDownloadSoundPopUp setEnabled: playDownloadSound]; |
---|
231 | |
---|
232 | int downloadSoundIndex = [fDownloadSoundPopUp indexOfItemWithTitle: [fDefaults stringForKey: @"DownloadSound"]]; |
---|
233 | if (downloadSoundIndex >= 0) |
---|
234 | [fDownloadSoundPopUp selectItemAtIndex: downloadSoundIndex]; |
---|
235 | else |
---|
236 | [fDefaults setObject: [fDownloadSoundPopUp titleOfSelectedItem] forKey: @"DownloadSound"]; |
---|
237 | |
---|
238 | //set seeding sound |
---|
239 | [fSeedingSoundPopUp removeAllItems]; |
---|
240 | [fSeedingSoundPopUp addItemsWithTitles: sounds]; |
---|
241 | |
---|
242 | BOOL playSeedingSound = [fDefaults boolForKey: @"PlaySeedingSound"]; |
---|
243 | [fPlaySeedingSoundCheck setState: playSeedingSound]; |
---|
244 | [fSeedingSoundPopUp setEnabled: playSeedingSound]; |
---|
245 | |
---|
246 | int seedingSoundIndex = [fDownloadSoundPopUp indexOfItemWithTitle: [fDefaults stringForKey: @"SeedingSound"]]; |
---|
247 | if (seedingSoundIndex >= 0) |
---|
248 | [fSeedingSoundPopUp selectItemAtIndex: seedingSoundIndex]; |
---|
249 | else |
---|
250 | [fDefaults setObject: [fSeedingSoundPopUp titleOfSelectedItem] forKey: @"SeedingSound"]; |
---|
251 | |
---|
252 | //set start setting |
---|
253 | NSString * startSetting = [fDefaults stringForKey: @"StartSetting"]; |
---|
254 | int tag; |
---|
255 | if ([startSetting isEqualToString: @"Start"]) |
---|
256 | tag = START_YES_CHECK_TAG; |
---|
257 | else if ([startSetting isEqualToString: @"Wait"]) |
---|
258 | tag = START_WAIT_CHECK_TAG; |
---|
259 | else |
---|
260 | tag = START_NO_CHECK_TAG; |
---|
261 | |
---|
262 | [fStartMatrix selectCellWithTag: tag]; |
---|
263 | [fStartNumberField setEnabled: tag == START_WAIT_CHECK_TAG]; |
---|
264 | [fStartNumberField setIntValue: [fDefaults integerForKey: @"WaitToStartNumber"]]; |
---|
265 | |
---|
266 | //set private torrents |
---|
267 | BOOL copyTorrents = [fDefaults boolForKey: @"SavePrivateTorrent"]; |
---|
268 | [fCopyTorrentCheck setState: copyTorrents]; |
---|
269 | |
---|
270 | [fDeleteOriginalTorrentCheck setEnabled: copyTorrents]; |
---|
271 | [fDeleteOriginalTorrentCheck setState: [fDefaults boolForKey: @"DeleteOriginalTorrent"]]; |
---|
272 | |
---|
273 | //set update check |
---|
274 | NSString * updateCheck = [fDefaults stringForKey: @"UpdateCheck"]; |
---|
275 | if ([updateCheck isEqualToString: @"Weekly"]) |
---|
276 | [fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY]; |
---|
277 | else if ([updateCheck isEqualToString: @"Never"]) |
---|
278 | [fUpdatePopUp selectItemAtIndex: UPDATE_NEVER]; |
---|
279 | else |
---|
280 | [fUpdatePopUp selectItemAtIndex: UPDATE_DAILY]; |
---|
281 | } |
---|
282 | |
---|
283 | - (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier: |
---|
284 | (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag |
---|
285 | { |
---|
286 | NSToolbarItem * item; |
---|
287 | item = [[NSToolbarItem alloc] initWithItemIdentifier: ident]; |
---|
288 | |
---|
289 | if ([ident isEqualToString: TOOLBAR_GENERAL]) |
---|
290 | { |
---|
291 | [item setLabel: TOOLBAR_GENERAL]; |
---|
292 | [item setImage: [NSImage imageNamed: @"Preferences.png"]]; |
---|
293 | [item setTarget: self]; |
---|
294 | [item setAction: @selector(showGeneralPref:)]; |
---|
295 | } |
---|
296 | else if ([ident isEqualToString: TOOLBAR_TRANSFERS]) |
---|
297 | { |
---|
298 | [item setLabel: TOOLBAR_TRANSFERS]; |
---|
299 | [item setImage: [NSImage imageNamed: @"Transfers.png"]]; |
---|
300 | [item setTarget: self]; |
---|
301 | [item setAction: @selector(showTransfersPref:)]; |
---|
302 | } |
---|
303 | else if ([ident isEqualToString: TOOLBAR_BANDWIDTH]) |
---|
304 | { |
---|
305 | [item setLabel: TOOLBAR_BANDWIDTH]; |
---|
306 | [item setImage: [NSImage imageNamed: @"Bandwidth.png"]]; |
---|
307 | [item setTarget: self]; |
---|
308 | [item setAction: @selector(showBandwidthPref:)]; |
---|
309 | } |
---|
310 | else if ([ident isEqualToString: TOOLBAR_NETWORK]) |
---|
311 | { |
---|
312 | [item setLabel: TOOLBAR_NETWORK]; |
---|
313 | [item setImage: [NSImage imageNamed: @"Network.png"]]; |
---|
314 | [item setTarget: self]; |
---|
315 | [item setAction: @selector(showNetworkPref:)]; |
---|
316 | } |
---|
317 | else |
---|
318 | { |
---|
319 | [item release]; |
---|
320 | return nil; |
---|
321 | } |
---|
322 | |
---|
323 | return item; |
---|
324 | } |
---|
325 | |
---|
326 | - (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar |
---|
327 | { |
---|
328 | return [self toolbarDefaultItemIdentifiers: toolbar]; |
---|
329 | } |
---|
330 | |
---|
331 | - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar |
---|
332 | { |
---|
333 | return [self toolbarAllowedItemIdentifiers: toolbar]; |
---|
334 | } |
---|
335 | |
---|
336 | - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar |
---|
337 | { |
---|
338 | return [NSArray arrayWithObjects: |
---|
339 | TOOLBAR_GENERAL, TOOLBAR_TRANSFERS, |
---|
340 | TOOLBAR_BANDWIDTH, TOOLBAR_NETWORK, nil]; |
---|
341 | } |
---|
342 | |
---|
343 | - (void) setPort: (id) sender |
---|
344 | { |
---|
345 | int bindPort = [sender intValue]; |
---|
346 | if (![[NSString stringWithInt: bindPort] isEqualToString: [sender stringValue]] |
---|
347 | || bindPort < MIN_PORT || bindPort > MAX_PORT) |
---|
348 | { |
---|
349 | NSBeep(); |
---|
350 | bindPort = [fDefaults integerForKey: @"BindPort"]; |
---|
351 | [sender setIntValue: bindPort]; |
---|
352 | } |
---|
353 | else |
---|
354 | { |
---|
355 | tr_setBindPort(fHandle, bindPort); |
---|
356 | [fDefaults setInteger: bindPort forKey: @"BindPort"]; |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | - (void) setLimit: (id) sender |
---|
361 | { |
---|
362 | NSString * key; |
---|
363 | NSButton * check; |
---|
364 | NSString * type; |
---|
365 | if (sender == fUploadField) |
---|
366 | { |
---|
367 | key = @"UploadLimit"; |
---|
368 | check = fUploadCheck; |
---|
369 | type = @"Upload"; |
---|
370 | } |
---|
371 | else |
---|
372 | { |
---|
373 | key = @"DownloadLimit"; |
---|
374 | check = fDownloadCheck; |
---|
375 | type = @"Download"; |
---|
376 | } |
---|
377 | |
---|
378 | int limit = [sender intValue]; |
---|
379 | if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0) |
---|
380 | { |
---|
381 | NSBeep(); |
---|
382 | limit = [fDefaults integerForKey: key]; |
---|
383 | [sender setIntValue: limit]; |
---|
384 | } |
---|
385 | else |
---|
386 | { |
---|
387 | if (![fDefaults boolForKey: @"SpeedLimit"]) |
---|
388 | { |
---|
389 | int realLimit = [check state] ? limit : -1; |
---|
390 | if (sender == fUploadField) |
---|
391 | tr_setUploadLimit(fHandle, realLimit); |
---|
392 | else |
---|
393 | tr_setDownloadLimit(fHandle, realLimit); |
---|
394 | } |
---|
395 | |
---|
396 | [fDefaults setInteger: limit forKey: key]; |
---|
397 | } |
---|
398 | |
---|
399 | NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
400 | [NSNumber numberWithBool: [check state]], @"Enable", |
---|
401 | [NSNumber numberWithInt: limit], @"Limit", |
---|
402 | type, @"Type", nil]; |
---|
403 | [[NSNotificationCenter defaultCenter] postNotificationName: @"LimitGlobalChange" object: dict]; |
---|
404 | } |
---|
405 | |
---|
406 | - (void) setLimitCheck: (id) sender |
---|
407 | { |
---|
408 | NSString * key; |
---|
409 | NSTextField * field; |
---|
410 | if (sender == fUploadCheck) |
---|
411 | { |
---|
412 | key = @"CheckUpload"; |
---|
413 | field = fUploadField; |
---|
414 | } |
---|
415 | else |
---|
416 | { |
---|
417 | key = @"CheckDownload"; |
---|
418 | field = fDownloadField; |
---|
419 | } |
---|
420 | |
---|
421 | BOOL check = [sender state] == NSOnState; |
---|
422 | [self setLimit: field]; |
---|
423 | [field setEnabled: check]; |
---|
424 | |
---|
425 | [fDefaults setBool: check forKey: key]; |
---|
426 | } |
---|
427 | |
---|
428 | - (void) setLimitEnabled: (BOOL) enable type: (NSString *) type |
---|
429 | { |
---|
430 | NSButton * check = [type isEqualToString: @"Upload"] ? fUploadCheck : fDownloadCheck; |
---|
431 | [check setState: enable ? NSOnState : NSOffState]; |
---|
432 | [self setLimitCheck: check]; |
---|
433 | } |
---|
434 | |
---|
435 | - (void) setQuickLimit: (int) limit type: (NSString *) type |
---|
436 | { |
---|
437 | NSButton * check; |
---|
438 | if ([type isEqualToString: @"Upload"]) |
---|
439 | { |
---|
440 | [fUploadField setIntValue: limit]; |
---|
441 | check = fUploadCheck; |
---|
442 | } |
---|
443 | else |
---|
444 | { |
---|
445 | [fDownloadField setIntValue: limit]; |
---|
446 | check = fDownloadCheck; |
---|
447 | } |
---|
448 | [check setState: NSOnState]; |
---|
449 | [self setLimitCheck: check]; |
---|
450 | } |
---|
451 | |
---|
452 | - (void) enableSpeedLimit: (BOOL) enable |
---|
453 | { |
---|
454 | if ([fDefaults boolForKey: @"SpeedLimit"] != enable) |
---|
455 | { |
---|
456 | [fDefaults setBool: enable forKey: @"SpeedLimit"]; |
---|
457 | |
---|
458 | if (enable) |
---|
459 | { |
---|
460 | tr_setUploadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitUploadLimit"]); |
---|
461 | tr_setDownloadLimit(fHandle, [fDefaults integerForKey: @"SpeedLimitDownloadLimit"]); |
---|
462 | } |
---|
463 | else |
---|
464 | { |
---|
465 | tr_setUploadLimit(fHandle, [fUploadCheck state] ? [fDefaults integerForKey: @"UploadLimit"] : -1); |
---|
466 | tr_setDownloadLimit(fHandle, [fDownloadCheck state] ? [fDefaults integerForKey: @"DownloadLimit"] : -1); |
---|
467 | } |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | - (void) setSpeedLimit: (id) sender |
---|
472 | { |
---|
473 | NSString * key = sender == fSpeedLimitUploadField ? @"SpeedLimitUploadLimit" : @"SpeedLimitDownloadLimit"; |
---|
474 | |
---|
475 | int limit = [sender intValue]; |
---|
476 | if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", limit]] || limit < 0) |
---|
477 | { |
---|
478 | NSBeep(); |
---|
479 | limit = [fDefaults integerForKey: key]; |
---|
480 | [sender setIntValue: limit]; |
---|
481 | } |
---|
482 | else |
---|
483 | { |
---|
484 | if ([fDefaults boolForKey: @"SpeedLimit"]) |
---|
485 | { |
---|
486 | if (sender == fSpeedLimitUploadField) |
---|
487 | tr_setUploadLimit(fHandle, limit); |
---|
488 | else |
---|
489 | tr_setDownloadLimit(fHandle, limit); |
---|
490 | } |
---|
491 | |
---|
492 | [fDefaults setInteger: limit forKey: key]; |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | - (void) setAutoSpeedLimitCheck: (id) sender |
---|
497 | { |
---|
498 | BOOL check = [sender state] == NSOnState; |
---|
499 | |
---|
500 | [fDefaults setBool: check forKey: @"SpeedLimitAuto"]; |
---|
501 | |
---|
502 | [self setAutoSpeedLimitHour: fSpeedLimitAutoOnField]; |
---|
503 | [fSpeedLimitAutoOnField setEnabled: check]; |
---|
504 | |
---|
505 | [self setAutoSpeedLimitHour: fSpeedLimitAutoOffField]; |
---|
506 | [fSpeedLimitAutoOffField setEnabled: check]; |
---|
507 | } |
---|
508 | |
---|
509 | - (void) setAutoSpeedLimitHour: (id) sender |
---|
510 | { |
---|
511 | NSString * key = (sender == fSpeedLimitAutoOnField) ? @"SpeedLimitAutoOnHour" : @"SpeedLimitAutoOffHour"; |
---|
512 | |
---|
513 | int hour = [sender intValue]; |
---|
514 | |
---|
515 | //allow numbers under ten in the format 0x |
---|
516 | if (!([[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%d", hour]] |
---|
517 | || [[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%02d", hour]]) || hour < 0 || hour > 23 |
---|
518 | || [fSpeedLimitAutoOnField intValue] == [fSpeedLimitAutoOffField intValue]) |
---|
519 | { |
---|
520 | NSBeep(); |
---|
521 | hour = [fDefaults integerForKey: key]; |
---|
522 | [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]]; |
---|
523 | } |
---|
524 | else |
---|
525 | [fDefaults setInteger: hour forKey: key]; |
---|
526 | |
---|
527 | [sender setStringValue: [NSString stringWithFormat: @"%02d", hour]]; //ensure number has 2 digits |
---|
528 | |
---|
529 | [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSpeedLimitChange" object: self]; |
---|
530 | } |
---|
531 | |
---|
532 | - (void) setRatio: (id) sender |
---|
533 | { |
---|
534 | float ratioLimit = [sender floatValue]; |
---|
535 | if (![[sender stringValue] isEqualToString: [NSString stringWithFormat: @"%.2f", ratioLimit]] |
---|
536 | || ratioLimit < 0) |
---|
537 | { |
---|
538 | NSBeep(); |
---|
539 | ratioLimit = [fDefaults floatForKey: @"RatioLimit"]; |
---|
540 | [sender setFloatValue: ratioLimit]; |
---|
541 | } |
---|
542 | else |
---|
543 | [fDefaults setFloat: ratioLimit forKey: @"RatioLimit"]; |
---|
544 | |
---|
545 | NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
546 | [NSNumber numberWithBool: [fRatioCheck state]], @"Enable", |
---|
547 | [NSNumber numberWithFloat: ratioLimit], @"Ratio", nil]; |
---|
548 | [[NSNotificationCenter defaultCenter] postNotificationName: @"RatioGlobalChange" object: dict]; |
---|
549 | } |
---|
550 | |
---|
551 | - (void) setRatioCheck: (id) sender |
---|
552 | { |
---|
553 | BOOL check = [sender state] == NSOnState; |
---|
554 | [self setRatio: fRatioField]; |
---|
555 | [fRatioField setEnabled: check]; |
---|
556 | |
---|
557 | [fDefaults setBool: check forKey: @"RatioCheck"]; |
---|
558 | } |
---|
559 | |
---|
560 | - (void) setRatioEnabled: (BOOL) enable |
---|
561 | { |
---|
562 | int state = enable ? NSOnState : NSOffState; |
---|
563 | |
---|
564 | [fRatioCheck setState: state]; |
---|
565 | [self setRatioCheck: fRatioCheck]; |
---|
566 | } |
---|
567 | |
---|
568 | - (void) setQuickRatio: (float) ratioLimit |
---|
569 | { |
---|
570 | [fRatioField setFloatValue: ratioLimit]; |
---|
571 | |
---|
572 | [fRatioCheck setState: NSOnState]; |
---|
573 | [self setRatioCheck: fRatioCheck]; |
---|
574 | } |
---|
575 | |
---|
576 | - (void) setShowMessage: (id) sender |
---|
577 | { |
---|
578 | BOOL state = [sender state]; |
---|
579 | |
---|
580 | if (sender == fQuitCheck) |
---|
581 | { |
---|
582 | [fDefaults setBool: state forKey: @"CheckQuit"]; |
---|
583 | [fQuitDownloadingCheck setEnabled: state]; |
---|
584 | } |
---|
585 | else if (sender == fRemoveCheck) |
---|
586 | { |
---|
587 | [fDefaults setBool: state forKey: @"CheckRemove"]; |
---|
588 | [fRemoveDownloadingCheck setEnabled: state]; |
---|
589 | } |
---|
590 | if (sender == fQuitDownloadingCheck) |
---|
591 | [fDefaults setBool: state forKey: @"CheckQuitDownloading"]; |
---|
592 | else if (sender == fRemoveDownloadingCheck) |
---|
593 | [fDefaults setBool: state forKey: @"CheckRemoveDownloading"]; |
---|
594 | else; |
---|
595 | } |
---|
596 | |
---|
597 | - (void) setBadge: (id) sender |
---|
598 | { |
---|
599 | if (sender == fBadgeDownloadRateCheck) |
---|
600 | [fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"]; |
---|
601 | else if (sender == fBadgeUploadRateCheck) |
---|
602 | [fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"]; |
---|
603 | else; |
---|
604 | |
---|
605 | [[NSNotificationCenter defaultCenter] postNotificationName: @"DockBadgeChange" object: self]; |
---|
606 | } |
---|
607 | |
---|
608 | - (void) setPlaySound: (id) sender |
---|
609 | { |
---|
610 | BOOL state = [sender state]; |
---|
611 | |
---|
612 | if (sender == fPlayDownloadSoundCheck) |
---|
613 | { |
---|
614 | [fDownloadSoundPopUp setEnabled: state]; |
---|
615 | [fDefaults setBool: state forKey: @"PlayDownloadSound"]; |
---|
616 | } |
---|
617 | else if (sender == fPlaySeedingSoundCheck) |
---|
618 | { |
---|
619 | [fSeedingSoundPopUp setEnabled: state]; |
---|
620 | [fDefaults setBool: state forKey: @"PlaySeedingSound"]; |
---|
621 | } |
---|
622 | else; |
---|
623 | } |
---|
624 | |
---|
625 | - (void) setSound: (id) sender |
---|
626 | { |
---|
627 | //play sound when selecting |
---|
628 | NSString * soundName = [sender titleOfSelectedItem]; |
---|
629 | NSSound * sound; |
---|
630 | if ((sound = [NSSound soundNamed: soundName])) |
---|
631 | [sound play]; |
---|
632 | |
---|
633 | if (sender == fDownloadSoundPopUp) |
---|
634 | [fDefaults setObject: soundName forKey: @"DownloadSound"]; |
---|
635 | else if (sender == fSeedingSoundPopUp) |
---|
636 | [fDefaults setObject: soundName forKey: @"SeedingSound"]; |
---|
637 | else; |
---|
638 | } |
---|
639 | |
---|
640 | - (void) setUpdate: (id) sender |
---|
641 | { |
---|
642 | int index = [fUpdatePopUp indexOfSelectedItem]; |
---|
643 | NSTimeInterval seconds; |
---|
644 | if (index == UPDATE_DAILY) |
---|
645 | { |
---|
646 | [fDefaults setObject: @"Daily" forKey: @"UpdateCheck"]; |
---|
647 | seconds = 86400; |
---|
648 | } |
---|
649 | else if (index == UPDATE_WEEKLY) |
---|
650 | { |
---|
651 | [fDefaults setObject: @"Weekly" forKey: @"UpdateCheck"]; |
---|
652 | seconds = 604800; |
---|
653 | } |
---|
654 | else |
---|
655 | { |
---|
656 | [fDefaults setObject: @"Never" forKey: @"UpdateCheck"]; |
---|
657 | seconds = 0; |
---|
658 | } |
---|
659 | |
---|
660 | [fDefaults setInteger: seconds forKey: @"SUScheduledCheckInterval"]; |
---|
661 | [fUpdater scheduleCheckWithInterval: seconds]; |
---|
662 | } |
---|
663 | |
---|
664 | - (void) setStartSetting: (id) sender |
---|
665 | { |
---|
666 | NSString * startSetting; |
---|
667 | |
---|
668 | int tag = [[fStartMatrix selectedCell] tag]; |
---|
669 | if (tag == START_YES_CHECK_TAG) |
---|
670 | startSetting = @"Start"; |
---|
671 | else if (tag == START_WAIT_CHECK_TAG) |
---|
672 | startSetting = @"Wait"; |
---|
673 | else |
---|
674 | startSetting = @"Manual"; |
---|
675 | |
---|
676 | [fDefaults setObject: startSetting forKey: @"StartSetting"]; |
---|
677 | |
---|
678 | [self setStartNumber: fStartNumberField]; |
---|
679 | [fStartNumberField setEnabled: tag == START_WAIT_CHECK_TAG]; |
---|
680 | } |
---|
681 | |
---|
682 | - (void) setStartNumber: (id) sender |
---|
683 | { |
---|
684 | int waitNumber = [sender intValue]; |
---|
685 | if (![[sender stringValue] isEqualToString: [NSString stringWithInt: waitNumber]] || waitNumber < 1) |
---|
686 | { |
---|
687 | NSBeep(); |
---|
688 | waitNumber = [fDefaults floatForKey: @"WaitToStartNumber"]; |
---|
689 | [sender setIntValue: waitNumber]; |
---|
690 | } |
---|
691 | else |
---|
692 | [fDefaults setInteger: waitNumber forKey: @"WaitToStartNumber"]; |
---|
693 | |
---|
694 | [[NSNotificationCenter defaultCenter] postNotificationName: @"GlobalStartSettingChange" object: self]; |
---|
695 | } |
---|
696 | |
---|
697 | - (void) setMoveTorrent: (id) sender |
---|
698 | { |
---|
699 | int state = [sender state]; |
---|
700 | if (sender == fCopyTorrentCheck) |
---|
701 | { |
---|
702 | [fDefaults setBool: state forKey: @"SavePrivateTorrent"]; |
---|
703 | |
---|
704 | [fDeleteOriginalTorrentCheck setEnabled: state]; |
---|
705 | if (state == NSOffState) |
---|
706 | { |
---|
707 | [fDeleteOriginalTorrentCheck setState: NSOffState]; |
---|
708 | [fDefaults setBool: NO forKey: @"DeleteOriginalTorrent"]; |
---|
709 | } |
---|
710 | } |
---|
711 | else |
---|
712 | [fDefaults setBool: state forKey: @"DeleteOriginalTorrent"]; |
---|
713 | } |
---|
714 | |
---|
715 | - (void) setDownloadLocation: (id) sender |
---|
716 | { |
---|
717 | //Download folder |
---|
718 | switch ([fFolderPopUp indexOfSelectedItem]) |
---|
719 | { |
---|
720 | case DOWNLOAD_FOLDER: |
---|
721 | [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"]; |
---|
722 | break; |
---|
723 | case DOWNLOAD_TORRENT: |
---|
724 | [fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"]; |
---|
725 | break; |
---|
726 | case DOWNLOAD_ASK: |
---|
727 | [fDefaults setObject: @"Ask" forKey: @"DownloadChoice"]; |
---|
728 | break; |
---|
729 | } |
---|
730 | } |
---|
731 | |
---|
732 | - (void) checkUpdate |
---|
733 | { |
---|
734 | [fUpdater checkForUpdates: nil]; |
---|
735 | } |
---|
736 | |
---|
737 | - (void) folderSheetShow: (id) sender |
---|
738 | { |
---|
739 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
740 | |
---|
741 | [panel setPrompt: @"Select"]; |
---|
742 | [panel setAllowsMultipleSelection: NO]; |
---|
743 | [panel setCanChooseFiles: NO]; |
---|
744 | [panel setCanChooseDirectories: YES]; |
---|
745 | [panel setCanCreateDirectories: YES]; |
---|
746 | |
---|
747 | [panel beginSheetForDirectory: nil file: nil types: nil |
---|
748 | modalForWindow: [self window] modalDelegate: self didEndSelector: |
---|
749 | @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil]; |
---|
750 | } |
---|
751 | |
---|
752 | - (void) setAutoImport: (id) sender |
---|
753 | { |
---|
754 | int state = [fAutoImportCheck state]; |
---|
755 | [fDefaults setBool: state forKey: @"AutoImport"]; |
---|
756 | [fImportFolderPopUp setEnabled: state]; |
---|
757 | |
---|
758 | if (state == NSOnState) |
---|
759 | [[UKKQueue sharedFileWatcher] addPath: fImportFolder]; |
---|
760 | else |
---|
761 | [[UKKQueue sharedFileWatcher] removePathFromQueue: fImportFolder]; |
---|
762 | |
---|
763 | [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self]; |
---|
764 | } |
---|
765 | |
---|
766 | - (void) importFolderSheetShow: (id) sender |
---|
767 | { |
---|
768 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
769 | |
---|
770 | [panel setPrompt: @"Select"]; |
---|
771 | [panel setAllowsMultipleSelection: NO]; |
---|
772 | [panel setCanChooseFiles: NO]; |
---|
773 | [panel setCanChooseDirectories: YES]; |
---|
774 | [panel setCanCreateDirectories: YES]; |
---|
775 | |
---|
776 | [panel beginSheetForDirectory: nil file: nil types: nil |
---|
777 | modalForWindow: [self window] modalDelegate: self didEndSelector: |
---|
778 | @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil]; |
---|
779 | } |
---|
780 | |
---|
781 | - (void) setAutoSize: (id) sender |
---|
782 | { |
---|
783 | [fDefaults setBool: [sender state] forKey: @"AutoSize"]; |
---|
784 | |
---|
785 | [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoSizeSettingChange" object: self]; |
---|
786 | } |
---|
787 | |
---|
788 | - (void) helpForNetwork: (id) sender |
---|
789 | { |
---|
790 | [[NSHelpManager sharedHelpManager] openHelpAnchor: @"PortForwarding" |
---|
791 | inBook: [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleHelpBookName"]]; |
---|
792 | } |
---|
793 | |
---|
794 | @end |
---|
795 | |
---|
796 | @implementation PrefsController (Private) |
---|
797 | |
---|
798 | - (void) showGeneralPref: (id) sender |
---|
799 | { |
---|
800 | [self setPrefView: fGeneralView]; |
---|
801 | } |
---|
802 | |
---|
803 | - (void) showTransfersPref: (id) sender |
---|
804 | { |
---|
805 | [self setPrefView: fTransfersView]; |
---|
806 | } |
---|
807 | |
---|
808 | - (void) showBandwidthPref: (id) sender |
---|
809 | { |
---|
810 | [self setPrefView: fBandwidthView]; |
---|
811 | } |
---|
812 | |
---|
813 | - (void) showNetworkPref: (id) sender |
---|
814 | { |
---|
815 | [self setPrefView: fNetworkView]; |
---|
816 | } |
---|
817 | |
---|
818 | - (void) setPrefView: (NSView *) view |
---|
819 | { |
---|
820 | NSWindow * window = [self window]; |
---|
821 | |
---|
822 | if ([window contentView] == view) |
---|
823 | return; |
---|
824 | |
---|
825 | NSRect windowRect = [window frame]; |
---|
826 | int difference = [view frame].size.height - [[window contentView] frame].size.height; |
---|
827 | windowRect.origin.y -= difference; |
---|
828 | windowRect.size.height += difference; |
---|
829 | |
---|
830 | [window setTitle: [fToolbar selectedItemIdentifier]]; |
---|
831 | |
---|
832 | [view setHidden: YES]; |
---|
833 | [window setContentView: view]; |
---|
834 | [window setFrame: windowRect display: YES animate: YES]; |
---|
835 | [view setHidden: NO]; |
---|
836 | } |
---|
837 | |
---|
838 | - (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info |
---|
839 | { |
---|
840 | if (code == NSOKButton) |
---|
841 | { |
---|
842 | [fDownloadFolder release]; |
---|
843 | fDownloadFolder = [[[openPanel filenames] objectAtIndex: 0] retain]; |
---|
844 | |
---|
845 | [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER]; |
---|
846 | [fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"]; |
---|
847 | [fDefaults setObject: @"Constant" forKey: @"DownloadChoice"]; |
---|
848 | |
---|
849 | [self updatePopUp]; |
---|
850 | } |
---|
851 | else |
---|
852 | { |
---|
853 | //reset if cancelled |
---|
854 | NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"]; |
---|
855 | if ([downloadChoice isEqualToString: @"Constant"]) |
---|
856 | [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER]; |
---|
857 | else if ([downloadChoice isEqualToString: @"Torrent"]) |
---|
858 | [fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT]; |
---|
859 | else |
---|
860 | [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK]; |
---|
861 | } |
---|
862 | } |
---|
863 | |
---|
864 | - (void) updatePopUp |
---|
865 | { |
---|
866 | //get and resize the icon |
---|
867 | NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fDownloadFolder]; |
---|
868 | [icon setScalesWhenResized: YES]; |
---|
869 | [icon setSize: NSMakeSize(16.0, 16.0)]; |
---|
870 | |
---|
871 | //update menu item |
---|
872 | NSMenuItem * menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0]; |
---|
873 | [menuItem setTitle: [fDownloadFolder lastPathComponent]]; |
---|
874 | [menuItem setImage: icon]; |
---|
875 | } |
---|
876 | |
---|
877 | - (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info |
---|
878 | { |
---|
879 | if (code == NSOKButton) |
---|
880 | { |
---|
881 | UKKQueue * sharedQueue = [UKKQueue sharedFileWatcher]; |
---|
882 | [sharedQueue removePathFromQueue: fImportFolder]; |
---|
883 | |
---|
884 | [fImportFolder release]; |
---|
885 | fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain]; |
---|
886 | |
---|
887 | [fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"]; |
---|
888 | |
---|
889 | [self updateImportPopUp]; |
---|
890 | |
---|
891 | [sharedQueue addPath: fImportFolder]; |
---|
892 | |
---|
893 | [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self]; |
---|
894 | } |
---|
895 | [fImportFolderPopUp selectItemAtIndex: 0]; |
---|
896 | } |
---|
897 | |
---|
898 | - (void) updateImportPopUp |
---|
899 | { |
---|
900 | //get and resize the icon |
---|
901 | NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fImportFolder]; |
---|
902 | [icon setScalesWhenResized: YES]; |
---|
903 | [icon setSize: NSMakeSize(16.0, 16.0)]; |
---|
904 | |
---|
905 | //update menu item |
---|
906 | NSMenuItem * menuItem = (NSMenuItem *) [fImportFolderPopUp itemAtIndex: 0]; |
---|
907 | [menuItem setTitle: [fImportFolder lastPathComponent]]; |
---|
908 | [menuItem setImage: icon]; |
---|
909 | } |
---|
910 | |
---|
911 | @end |
---|