1 | /****************************************************************************** |
---|
2 | * Copyright (c) 2005 Eric Petit |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
5 | * copy of this software and associated documentation files (the "Software"), |
---|
6 | * to deal in the Software without restriction, including without limitation |
---|
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
8 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
9 | * Software is furnished to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in |
---|
12 | * all copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
20 | * DEALINGS IN THE SOFTWARE. |
---|
21 | *****************************************************************************/ |
---|
22 | |
---|
23 | #include <IOKit/IOMessage.h> |
---|
24 | |
---|
25 | #include "NameCell.h" |
---|
26 | #include "ProgressCell.h" |
---|
27 | #include "Utils.h" |
---|
28 | #include "TorrentTableView.h" |
---|
29 | |
---|
30 | #define TOOLBAR_OPEN @"Toolbar Open" |
---|
31 | #define TOOLBAR_REMOVE @"Toolbar Remove" |
---|
32 | #define TOOLBAR_PREFS @"Toolbar Preferences" |
---|
33 | #define TOOLBAR_INFO @"Toolbar Info" |
---|
34 | |
---|
35 | #define CONTEXT_PAUSE 1 |
---|
36 | #define CONTEXT_REMOVE 2 |
---|
37 | #define CONTEXT_REMOVE_TORRENT 3 |
---|
38 | #define CONTEXT_REMOVE_DATA 4 |
---|
39 | #define CONTEXT_REMOVE_BOTH 5 |
---|
40 | #define CONTEXT_INFO 6 |
---|
41 | |
---|
42 | static void sleepCallBack( void * controller, io_service_t y, |
---|
43 | natural_t messageType, void * messageArgument ) |
---|
44 | { |
---|
45 | Controller * c = controller; |
---|
46 | [c sleepCallBack: messageType argument: messageArgument]; |
---|
47 | } |
---|
48 | |
---|
49 | @implementation Controller |
---|
50 | |
---|
51 | - (void) updateBars |
---|
52 | { |
---|
53 | NSArray * items; |
---|
54 | NSToolbarItem * item; |
---|
55 | BOOL enable; |
---|
56 | int row; |
---|
57 | unsigned i; |
---|
58 | |
---|
59 | row = [fTableView selectedRow]; |
---|
60 | |
---|
61 | /* Can we remove it ? */ |
---|
62 | enable = ( row >= 0 ) && ( fStat[row].status & |
---|
63 | ( TR_STATUS_STOPPING | TR_STATUS_PAUSE ) ); |
---|
64 | items = [fToolbar items]; |
---|
65 | for( i = 0; i < [items count]; i++ ) |
---|
66 | { |
---|
67 | item = [items objectAtIndex: i]; |
---|
68 | if( [[item itemIdentifier] isEqualToString: TOOLBAR_REMOVE] ) |
---|
69 | { |
---|
70 | [item setAction: enable ? @selector( removeTorrent: ) : NULL]; |
---|
71 | } |
---|
72 | } |
---|
73 | [fRemoveItem setAction: enable ? @selector( removeTorrent: ) : NULL]; |
---|
74 | |
---|
75 | /* Can we pause or resume it ? */ |
---|
76 | [fPauseResumeItem setTitle: @"Pause"]; |
---|
77 | [fPauseResumeItem setAction: NULL]; |
---|
78 | if( row < 0 ) |
---|
79 | { |
---|
80 | [fRevealItem setAction: NULL]; |
---|
81 | return; |
---|
82 | } |
---|
83 | |
---|
84 | [fRevealItem setAction: @selector( revealFromMenu: )]; |
---|
85 | |
---|
86 | if( fStat[row].status & TR_STATUS_PAUSE ) |
---|
87 | { |
---|
88 | [fPauseResumeItem setTitle: @"Resume"]; |
---|
89 | [fPauseResumeItem setAction: @selector( resumeTorrent: )]; |
---|
90 | } |
---|
91 | else if( fStat[row].status & ( TR_STATUS_CHECK | |
---|
92 | TR_STATUS_DOWNLOAD | TR_STATUS_SEED ) ) |
---|
93 | { |
---|
94 | [fPauseResumeItem setAction: @selector( stopTorrent: )]; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | - (void) awakeFromNib |
---|
99 | { |
---|
100 | fHandle = tr_init(); |
---|
101 | |
---|
102 | [fPrefsController setHandle: fHandle]; |
---|
103 | |
---|
104 | [fWindow setContentMinSize: NSMakeSize( 400, 120 )]; |
---|
105 | |
---|
106 | /* Check or uncheck menu item in respect to current preferences */ |
---|
107 | [fAdvancedBarItem setState: [[NSUserDefaults standardUserDefaults] |
---|
108 | boolForKey:@"UseAdvancedBar"] ? NSOnState : NSOffState]; |
---|
109 | |
---|
110 | fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Transmission Toolbar"]; |
---|
111 | [fToolbar setDelegate: self]; |
---|
112 | [fToolbar setAllowsUserCustomization: YES]; |
---|
113 | [fToolbar setAutosavesConfiguration: YES]; |
---|
114 | [fWindow setToolbar: fToolbar]; |
---|
115 | [fWindow setDelegate: self]; |
---|
116 | |
---|
117 | NSTableColumn * tableColumn; |
---|
118 | NameCell * nameCell; |
---|
119 | ProgressCell * progressCell; |
---|
120 | |
---|
121 | nameCell = [[NameCell alloc] init]; |
---|
122 | progressCell = [[ProgressCell alloc] init]; |
---|
123 | tableColumn = [fTableView tableColumnWithIdentifier: @"Name"]; |
---|
124 | [tableColumn setDataCell: nameCell]; |
---|
125 | [tableColumn setMinWidth: 10.0]; |
---|
126 | [tableColumn setMaxWidth: 3000.0]; |
---|
127 | |
---|
128 | tableColumn = [fTableView tableColumnWithIdentifier: @"Progress"]; |
---|
129 | [tableColumn setDataCell: progressCell]; |
---|
130 | [tableColumn setMinWidth: 134.0]; |
---|
131 | [tableColumn setMaxWidth: 134.0]; |
---|
132 | |
---|
133 | [fTableView setAutosaveTableColumns: YES]; |
---|
134 | [fTableView sizeToFit]; |
---|
135 | |
---|
136 | [fTableView registerForDraggedTypes: [NSArray arrayWithObjects: |
---|
137 | NSFilenamesPboardType, NULL]]; |
---|
138 | |
---|
139 | IONotificationPortRef notify; |
---|
140 | io_object_t anIterator; |
---|
141 | |
---|
142 | /* Register for sleep notifications */ |
---|
143 | fRootPort = IORegisterForSystemPower( self, ¬ify, sleepCallBack, |
---|
144 | &anIterator); |
---|
145 | if( !fRootPort ) |
---|
146 | { |
---|
147 | printf( "Could not IORegisterForSystemPower\n" ); |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | CFRunLoopAddSource( CFRunLoopGetCurrent(), |
---|
152 | IONotificationPortGetRunLoopSource( notify ), |
---|
153 | kCFRunLoopCommonModes ); |
---|
154 | } |
---|
155 | |
---|
156 | NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; |
---|
157 | |
---|
158 | NSArray * history = [defaults arrayForKey: @"History"]; |
---|
159 | if( history ) |
---|
160 | { |
---|
161 | unsigned i; |
---|
162 | NSDictionary * dic; |
---|
163 | NSString * torrentPath, * downloadFolder, * paused; |
---|
164 | |
---|
165 | for( i = 0; i < [history count]; i++ ) |
---|
166 | { |
---|
167 | dic = [history objectAtIndex: i]; |
---|
168 | |
---|
169 | torrentPath = [dic objectForKey: @"TorrentPath"]; |
---|
170 | downloadFolder = [dic objectForKey: @"DownloadFolder"]; |
---|
171 | paused = [dic objectForKey: @"Paused"]; |
---|
172 | |
---|
173 | if( !torrentPath || !downloadFolder || !paused ) |
---|
174 | { |
---|
175 | continue; |
---|
176 | } |
---|
177 | |
---|
178 | if( tr_torrentInit( fHandle, [torrentPath UTF8String] ) ) |
---|
179 | { |
---|
180 | continue; |
---|
181 | } |
---|
182 | |
---|
183 | tr_torrentSetFolder( fHandle, tr_torrentCount( fHandle ) - 1, |
---|
184 | [downloadFolder UTF8String] ); |
---|
185 | |
---|
186 | if( [paused isEqualToString: @"NO"] ) |
---|
187 | { |
---|
188 | tr_torrentStart( fHandle, tr_torrentCount( fHandle ) - 1 ); |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /* Register with the growl system */ |
---|
194 | [self growlRegister: self]; |
---|
195 | |
---|
196 | /* Update the interface every 500 ms */ |
---|
197 | fCount = 0; |
---|
198 | fStat = NULL; |
---|
199 | fTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5 target: self |
---|
200 | selector: @selector( updateUI: ) userInfo: NULL repeats: YES]; |
---|
201 | [[NSRunLoop currentRunLoop] addTimer: fTimer |
---|
202 | forMode: NSEventTrackingRunLoopMode]; |
---|
203 | } |
---|
204 | |
---|
205 | - (void) windowDidResize: (NSNotification *) n |
---|
206 | { |
---|
207 | [fTableView sizeToFit]; |
---|
208 | } |
---|
209 | |
---|
210 | - (BOOL) windowShouldClose: (id) sender |
---|
211 | { |
---|
212 | [fWindow orderOut: NULL]; |
---|
213 | return NO; |
---|
214 | } |
---|
215 | |
---|
216 | - (BOOL) applicationShouldHandleReopen: (NSApplication *) app |
---|
217 | hasVisibleWindows: (BOOL) flag |
---|
218 | { |
---|
219 | [self showMainWindow: NULL]; |
---|
220 | return NO; |
---|
221 | } |
---|
222 | |
---|
223 | - (NSApplicationTerminateReply) applicationShouldTerminate: |
---|
224 | (NSApplication *) app |
---|
225 | { |
---|
226 | NSMutableArray * history = [NSMutableArray |
---|
227 | arrayWithCapacity: TR_MAX_TORRENT_COUNT]; |
---|
228 | int i; |
---|
229 | |
---|
230 | /* Stop updating the interface */ |
---|
231 | [fTimer invalidate]; |
---|
232 | |
---|
233 | /* Save history and stop running torrents */ |
---|
234 | for( i = 0; i < fCount; i++ ) |
---|
235 | { |
---|
236 | [history addObject: [NSDictionary dictionaryWithObjectsAndKeys: |
---|
237 | [NSString stringWithUTF8String: fStat[i].info.torrent], |
---|
238 | @"TorrentPath", |
---|
239 | [NSString stringWithUTF8String: tr_torrentGetFolder( fHandle, i )], |
---|
240 | @"DownloadFolder", |
---|
241 | ( fStat[i].status & ( TR_STATUS_CHECK | TR_STATUS_DOWNLOAD | |
---|
242 | TR_STATUS_SEED ) ) ? @"NO" : @"YES", |
---|
243 | @"Paused", |
---|
244 | NULL]]; |
---|
245 | |
---|
246 | if( fStat[i].status & ( TR_STATUS_CHECK | |
---|
247 | TR_STATUS_DOWNLOAD | TR_STATUS_SEED ) ) |
---|
248 | { |
---|
249 | tr_torrentStop( fHandle, i ); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | /* Wait for torrents to stop (5 seconds timeout) */ |
---|
254 | NSDate * start = [NSDate date]; |
---|
255 | while( fCount > 0 ) |
---|
256 | { |
---|
257 | while( [[NSDate date] timeIntervalSinceDate: start] < 5 ) |
---|
258 | { |
---|
259 | fCount = tr_torrentStat( fHandle, &fStat ); |
---|
260 | if( fStat[0].status & TR_STATUS_PAUSE ) |
---|
261 | { |
---|
262 | break; |
---|
263 | } |
---|
264 | usleep( 500000 ); |
---|
265 | } |
---|
266 | tr_torrentClose( fHandle, 0 ); |
---|
267 | fCount = tr_torrentStat( fHandle, &fStat ); |
---|
268 | } |
---|
269 | |
---|
270 | tr_close( fHandle ); |
---|
271 | |
---|
272 | [[NSUserDefaults standardUserDefaults] |
---|
273 | setObject: history forKey: @"History"]; |
---|
274 | |
---|
275 | return NSTerminateNow; |
---|
276 | } |
---|
277 | |
---|
278 | - (void) folderChoiceClosed: (NSOpenPanel *) s returnCode: (int) code |
---|
279 | contextInfo: (void *) info |
---|
280 | { |
---|
281 | if( code != NSOKButton ) |
---|
282 | { |
---|
283 | tr_torrentClose( fHandle, tr_torrentCount( fHandle ) - 1 ); |
---|
284 | [NSApp stopModal]; |
---|
285 | return; |
---|
286 | } |
---|
287 | |
---|
288 | tr_torrentSetFolder( fHandle, tr_torrentCount( fHandle ) - 1, |
---|
289 | [[[s filenames] objectAtIndex: 0] UTF8String] ); |
---|
290 | tr_torrentStart( fHandle, tr_torrentCount( fHandle ) - 1 ); |
---|
291 | [NSApp stopModal]; |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | - (void) application: (NSApplication *) sender |
---|
296 | openFiles: (NSArray *) filenames |
---|
297 | { |
---|
298 | unsigned i; |
---|
299 | NSUserDefaults * defaults; |
---|
300 | NSString * downloadChoice, * downloadFolder, * torrentPath; |
---|
301 | |
---|
302 | defaults = [NSUserDefaults standardUserDefaults]; |
---|
303 | downloadChoice = [defaults stringForKey: @"DownloadChoice"]; |
---|
304 | downloadFolder = [defaults stringForKey: @"DownloadFolder"]; |
---|
305 | |
---|
306 | for( i = 0; i < [filenames count]; i++ ) |
---|
307 | { |
---|
308 | torrentPath = [filenames objectAtIndex: i]; |
---|
309 | |
---|
310 | if( tr_torrentInit( fHandle, [torrentPath UTF8String] ) ) |
---|
311 | { |
---|
312 | continue; |
---|
313 | } |
---|
314 | |
---|
315 | if( [downloadChoice isEqualToString: @"Constant"] ) |
---|
316 | { |
---|
317 | tr_torrentSetFolder( fHandle, tr_torrentCount( fHandle ) - 1, |
---|
318 | [downloadFolder UTF8String] ); |
---|
319 | tr_torrentStart( fHandle, tr_torrentCount( fHandle ) - 1 ); |
---|
320 | continue; |
---|
321 | } |
---|
322 | |
---|
323 | if( [downloadChoice isEqualToString: @"Torrent"] ) |
---|
324 | { |
---|
325 | tr_torrentSetFolder( fHandle, tr_torrentCount( fHandle ) - 1, |
---|
326 | [[torrentPath stringByDeletingLastPathComponent] UTF8String] ); |
---|
327 | tr_torrentStart( fHandle, tr_torrentCount( fHandle ) - 1 ); |
---|
328 | continue; |
---|
329 | } |
---|
330 | |
---|
331 | NSOpenPanel * panel; |
---|
332 | NSString * message; |
---|
333 | |
---|
334 | panel = [NSOpenPanel openPanel]; |
---|
335 | message = [NSString stringWithFormat: |
---|
336 | @"Select the download folder for %@", |
---|
337 | [torrentPath lastPathComponent]]; |
---|
338 | |
---|
339 | [panel setPrompt: @"Select"]; |
---|
340 | [panel setMessage: message]; |
---|
341 | [panel setAllowsMultipleSelection: NO]; |
---|
342 | [panel setCanChooseFiles: NO]; |
---|
343 | [panel setCanChooseDirectories: YES]; |
---|
344 | |
---|
345 | [panel beginSheetForDirectory: NULL file: NULL types: NULL |
---|
346 | modalForWindow: fWindow modalDelegate: self didEndSelector: |
---|
347 | @selector( folderChoiceClosed:returnCode:contextInfo: ) |
---|
348 | contextInfo: NULL]; |
---|
349 | [NSApp runModalForWindow: panel]; |
---|
350 | } |
---|
351 | |
---|
352 | [self updateUI: NULL]; |
---|
353 | } |
---|
354 | |
---|
355 | - (void) advancedChanged: (id) sender |
---|
356 | { |
---|
357 | NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; |
---|
358 | if( [fAdvancedBarItem state] == NSOnState ) |
---|
359 | { |
---|
360 | [fAdvancedBarItem setState: NSOffState]; |
---|
361 | [defaults setObject:@"NO" forKey:@"UseAdvancedBar"]; |
---|
362 | } |
---|
363 | else |
---|
364 | { |
---|
365 | [fAdvancedBarItem setState: NSOnState]; |
---|
366 | [defaults setObject:@"YES" forKey:@"UseAdvancedBar"]; |
---|
367 | } |
---|
368 | [fTableView display]; |
---|
369 | } |
---|
370 | |
---|
371 | /* called on by applescript */ |
---|
372 | - (void) open: (NSArray *) files |
---|
373 | { |
---|
374 | fFilenames = [files retain]; |
---|
375 | [self performSelectorOnMainThread: @selector(cantFindAName:) |
---|
376 | withObject: NULL waitUntilDone: NO]; |
---|
377 | } |
---|
378 | |
---|
379 | - (void) openShowSheet: (id) sender |
---|
380 | { |
---|
381 | NSOpenPanel * panel; |
---|
382 | NSArray * fileTypes; |
---|
383 | |
---|
384 | panel = [NSOpenPanel openPanel]; |
---|
385 | fileTypes = [NSArray arrayWithObject: @"torrent"]; |
---|
386 | |
---|
387 | [panel setAllowsMultipleSelection: YES]; |
---|
388 | [panel setCanChooseFiles: YES]; |
---|
389 | [panel setCanChooseDirectories: NO]; |
---|
390 | |
---|
391 | [panel beginSheetForDirectory: NULL file: NULL types: fileTypes |
---|
392 | modalForWindow: fWindow modalDelegate: self didEndSelector: |
---|
393 | @selector( openSheetClosed:returnCode:contextInfo: ) |
---|
394 | contextInfo: NULL]; |
---|
395 | } |
---|
396 | |
---|
397 | - (void) cantFindAName: (id) sender |
---|
398 | { |
---|
399 | [self application: NSApp openFiles: fFilenames]; |
---|
400 | [fFilenames release]; |
---|
401 | } |
---|
402 | |
---|
403 | - (void) openSheetClosed: (NSOpenPanel *) s returnCode: (int) code |
---|
404 | contextInfo: (void *) info |
---|
405 | { |
---|
406 | if( code != NSOKButton ) |
---|
407 | { |
---|
408 | return; |
---|
409 | } |
---|
410 | |
---|
411 | fFilenames = [[s filenames] retain]; |
---|
412 | |
---|
413 | [self performSelectorOnMainThread: @selector(cantFindAName:) |
---|
414 | withObject: NULL waitUntilDone: NO]; |
---|
415 | } |
---|
416 | |
---|
417 | - (void) resumeTorrent: (id) sender |
---|
418 | { |
---|
419 | [self resumeTorrentWithIndex: [fTableView selectedRow]]; |
---|
420 | } |
---|
421 | |
---|
422 | - (void) resumeAllTorrents: (id) sender |
---|
423 | { |
---|
424 | int i; |
---|
425 | for ( i = 0; i < fCount; i++) |
---|
426 | { |
---|
427 | if ( fStat[i].status & ( TR_STATUS_STOPPING |
---|
428 | | TR_STATUS_PAUSE | TR_STATUS_STOPPED ) ) |
---|
429 | { |
---|
430 | [self resumeTorrentWithIndex: i]; |
---|
431 | } |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | - (void) resumeTorrentWithIndex: (int) idx |
---|
436 | { |
---|
437 | tr_torrentStart( fHandle, idx ); |
---|
438 | [self updateUI: NULL]; |
---|
439 | } |
---|
440 | |
---|
441 | - (void) stopTorrent: (id) sender |
---|
442 | { |
---|
443 | [self stopTorrentWithIndex: [fTableView selectedRow]]; |
---|
444 | } |
---|
445 | |
---|
446 | - (void) stopAllTorrents: (id) sender |
---|
447 | { |
---|
448 | int i; |
---|
449 | for ( i = 0; i < fCount; i++) |
---|
450 | { |
---|
451 | if ( fStat[i].status & ( TR_STATUS_CHECK |
---|
452 | | TR_STATUS_DOWNLOAD | TR_STATUS_SEED) ) |
---|
453 | { |
---|
454 | [self stopTorrentWithIndex: i]; |
---|
455 | } |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | - (void) stopTorrentWithIndex: (int) idx |
---|
460 | { |
---|
461 | tr_torrentStop( fHandle, idx ); |
---|
462 | [self updateUI: NULL]; |
---|
463 | } |
---|
464 | |
---|
465 | |
---|
466 | - (void) removeTorrentWithIndex: (int) idx |
---|
467 | deleteTorrent: (BOOL) deleteTorrent |
---|
468 | deleteData: (BOOL) deleteData |
---|
469 | { |
---|
470 | if( deleteData ) |
---|
471 | { |
---|
472 | [self finderTrash: [NSString stringWithFormat: @"%@/%@", |
---|
473 | [NSString stringWithUTF8String: fStat[idx].folder], |
---|
474 | [NSString stringWithUTF8String: fStat[idx].info.name]]]; |
---|
475 | } |
---|
476 | if( deleteTorrent ) |
---|
477 | { |
---|
478 | [self finderTrash: [NSString stringWithUTF8String: |
---|
479 | fStat[idx].info.torrent]]; |
---|
480 | } |
---|
481 | |
---|
482 | tr_torrentClose( fHandle, idx ); |
---|
483 | [self updateUI: NULL]; |
---|
484 | } |
---|
485 | |
---|
486 | - (void) removeTorrent: (id) sender |
---|
487 | { |
---|
488 | [self removeTorrentWithIndex: [fTableView selectedRow] deleteTorrent: NO deleteData: NO ]; |
---|
489 | } |
---|
490 | |
---|
491 | - (void) removeTorrentDeleteFile: (id) sender |
---|
492 | { |
---|
493 | [self removeTorrentWithIndex: [fTableView selectedRow] deleteTorrent: YES deleteData: NO]; |
---|
494 | } |
---|
495 | |
---|
496 | - (void) removeTorrentDeleteData: (id) sender |
---|
497 | { |
---|
498 | [self removeTorrentWithIndex: [fTableView selectedRow] deleteTorrent: NO deleteData: YES]; |
---|
499 | } |
---|
500 | |
---|
501 | - (void) removeTorrentDeleteBoth: (id) sender |
---|
502 | { |
---|
503 | [self removeTorrentWithIndex: [fTableView selectedRow] deleteTorrent: YES deleteData: YES]; |
---|
504 | } |
---|
505 | |
---|
506 | - (void) showInfo: (id) sender |
---|
507 | { |
---|
508 | if( [fInfoPanel isVisible] ) |
---|
509 | { |
---|
510 | [fInfoPanel close]; |
---|
511 | } |
---|
512 | else |
---|
513 | { |
---|
514 | [fInfoPanel orderFront: sender]; |
---|
515 | } |
---|
516 | } |
---|
517 | |
---|
518 | - (void) updateUI: (NSTimer *) t |
---|
519 | { |
---|
520 | float dl, ul; |
---|
521 | int row, i; |
---|
522 | |
---|
523 | /* Update the NSTableView */ |
---|
524 | if( fStat ) |
---|
525 | { |
---|
526 | free( fStat ); |
---|
527 | } |
---|
528 | fCount = tr_torrentStat( fHandle, &fStat ); |
---|
529 | [fTableView updateUI: fStat]; |
---|
530 | |
---|
531 | /* Update the global DL/UL rates */ |
---|
532 | tr_torrentRates( fHandle, &dl, &ul ); |
---|
533 | [fTotalDLField setStringValue: [NSString stringWithFormat: |
---|
534 | @"Total DL: %.2f KB/s", dl]]; |
---|
535 | [fTotalULField setStringValue: [NSString stringWithFormat: |
---|
536 | @"Total UL: %.2f KB/s", ul]]; |
---|
537 | |
---|
538 | /* Update DL/UL totals in the Info panel */ |
---|
539 | row = [fTableView selectedRow]; |
---|
540 | if( row > -1 ) |
---|
541 | { |
---|
542 | [fInfoDownloaded setStringValue: |
---|
543 | stringForFileSize( fStat[row].downloaded )]; |
---|
544 | [fInfoUploaded setStringValue: |
---|
545 | stringForFileSize( fStat[row].uploaded )]; |
---|
546 | } |
---|
547 | |
---|
548 | /* check if torrents have recently ended. */ |
---|
549 | for (i = 0; i < fCount; i++) |
---|
550 | { |
---|
551 | if( !tr_getFinished( fHandle, i ) ) |
---|
552 | { |
---|
553 | continue; |
---|
554 | } |
---|
555 | [self notifyGrowl: [NSString stringWithUTF8String: |
---|
556 | fStat[i].info.name]]; |
---|
557 | tr_setFinished( fHandle, i, 0 ); |
---|
558 | } |
---|
559 | |
---|
560 | /* Must we do this? Can't remember */ |
---|
561 | [self updateBars]; |
---|
562 | } |
---|
563 | |
---|
564 | |
---|
565 | - (NSMenu *) menuForIndex: (int) idx |
---|
566 | { |
---|
567 | if ( idx < 0 || idx >= fCount ) |
---|
568 | { |
---|
569 | return nil; |
---|
570 | } |
---|
571 | |
---|
572 | int status = fStat[idx].status; |
---|
573 | |
---|
574 | NSMenuItem *pauseItem = [fContextMenu itemWithTag: CONTEXT_PAUSE]; |
---|
575 | NSMenuItem *removeItem = [fContextMenu itemAtIndex: 1]; |
---|
576 | NSMenuItem *infoItem = [fContextMenu itemAtIndex: 2]; |
---|
577 | |
---|
578 | [pauseItem setTarget: self]; |
---|
579 | |
---|
580 | if ( status & TR_STATUS_CHECK || |
---|
581 | status & TR_STATUS_DOWNLOAD || |
---|
582 | status & TR_STATUS_SEED ) |
---|
583 | { |
---|
584 | /* we can stop */ |
---|
585 | [removeItem setEnabled: NO]; |
---|
586 | [pauseItem setTitle: @"Pause"]; |
---|
587 | [pauseItem setAction: @selector(stopTorrent:)]; |
---|
588 | [pauseItem setEnabled: YES]; |
---|
589 | } else { |
---|
590 | /* we are stopped */ |
---|
591 | [removeItem setEnabled: YES]; |
---|
592 | [pauseItem setTitle: @"Resume"]; |
---|
593 | [pauseItem setAction: @selector(resumeTorrent:)]; |
---|
594 | /* don't allow resuming if we aren't in PAUSE */ |
---|
595 | if ( !(status & TR_STATUS_PAUSE) ) |
---|
596 | [pauseItem setEnabled: NO]; |
---|
597 | } |
---|
598 | |
---|
599 | if( [fInfoPanel isVisible] ) |
---|
600 | { |
---|
601 | [infoItem setTitle: @"Hide Info"]; |
---|
602 | } else { |
---|
603 | [infoItem setTitle: @"Show Info"]; |
---|
604 | } |
---|
605 | |
---|
606 | return fContextMenu; |
---|
607 | } |
---|
608 | |
---|
609 | - (int) numberOfRowsInTableView: (NSTableView *) t |
---|
610 | { |
---|
611 | return fCount; |
---|
612 | } |
---|
613 | |
---|
614 | - (id) tableView: (NSTableView *) t objectValueForTableColumn: |
---|
615 | (NSTableColumn *) tableColumn row: (int) rowIndex |
---|
616 | { |
---|
617 | return NULL; |
---|
618 | } |
---|
619 | |
---|
620 | - (void) tableView: (NSTableView *) t willDisplayCell: (id) cell |
---|
621 | forTableColumn: (NSTableColumn *) tableColumn row: (int) rowIndex |
---|
622 | { |
---|
623 | if( [[tableColumn identifier] isEqualToString: @"Name"] ) |
---|
624 | { |
---|
625 | [(NameCell *) cell setStat: &fStat[rowIndex]]; |
---|
626 | } |
---|
627 | else if( [[tableColumn identifier] isEqualToString: @"Progress"] ) |
---|
628 | { |
---|
629 | [(ProgressCell *) cell setStat: &fStat[rowIndex]]; |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | - (BOOL) tableView: (NSTableView *) t acceptDrop: |
---|
634 | (id <NSDraggingInfo>) info row: (int) row dropOperation: |
---|
635 | (NSTableViewDropOperation) operation |
---|
636 | { |
---|
637 | NSPasteboard * pasteboard; |
---|
638 | |
---|
639 | pasteboard = [info draggingPasteboard]; |
---|
640 | if( ![[pasteboard types] containsObject: NSFilenamesPboardType] ) |
---|
641 | { |
---|
642 | return NO; |
---|
643 | } |
---|
644 | |
---|
645 | [self application: NSApp openFiles: |
---|
646 | [pasteboard propertyListForType: NSFilenamesPboardType]]; |
---|
647 | |
---|
648 | return YES; |
---|
649 | } |
---|
650 | |
---|
651 | - (NSDragOperation) tableView: (NSTableView *) t validateDrop: |
---|
652 | (id <NSDraggingInfo>) info proposedRow: (int) row |
---|
653 | proposedDropOperation: (NSTableViewDropOperation) operation |
---|
654 | { |
---|
655 | return NSDragOperationGeneric; |
---|
656 | } |
---|
657 | |
---|
658 | - (void) tableViewSelectionDidChange: (NSNotification *) n |
---|
659 | { |
---|
660 | int row = [fTableView selectedRow]; |
---|
661 | |
---|
662 | [self updateBars]; |
---|
663 | |
---|
664 | if( row < 0 ) |
---|
665 | { |
---|
666 | [fInfoTitle setStringValue: @"No torrent selected"]; |
---|
667 | [fInfoTracker setStringValue: @""]; |
---|
668 | [fInfoAnnounce setStringValue: @""]; |
---|
669 | [fInfoSize setStringValue: @""]; |
---|
670 | [fInfoPieces setStringValue: @""]; |
---|
671 | [fInfoPieceSize setStringValue: @""]; |
---|
672 | [fInfoFolder setStringValue: @""]; |
---|
673 | [fInfoDownloaded setStringValue: @""]; |
---|
674 | [fInfoUploaded setStringValue: @""]; |
---|
675 | [fInfoSeeders setStringValue: @""]; |
---|
676 | [fInfoLeechers setStringValue: @""]; |
---|
677 | return; |
---|
678 | } |
---|
679 | |
---|
680 | /* Update info window */ |
---|
681 | [fInfoTitle setStringValue: [NSString stringWithUTF8String: |
---|
682 | fStat[row].info.name]]; |
---|
683 | [fInfoTracker setStringValue: [NSString stringWithFormat: |
---|
684 | @"%s:%d", fStat[row].info.trackerAddress, fStat[row].info.trackerPort]]; |
---|
685 | [fInfoAnnounce setStringValue: [NSString stringWithCString: |
---|
686 | fStat[row].info.trackerAnnounce]]; |
---|
687 | [fInfoSize setStringValue: |
---|
688 | stringForFileSize( fStat[row].info.totalSize )]; |
---|
689 | [fInfoPieces setStringValue: [NSString stringWithFormat: @"%d", |
---|
690 | fStat[row].info.pieceCount]]; |
---|
691 | [fInfoPieceSize setStringValue: |
---|
692 | stringForFileSize( fStat[row].info.pieceSize )]; |
---|
693 | [fInfoFolder setStringValue: [[NSString stringWithUTF8String: |
---|
694 | tr_torrentGetFolder( fHandle, row )] lastPathComponent]]; |
---|
695 | |
---|
696 | if ( fStat[row].seeders == -1 ) { |
---|
697 | [fInfoSeeders setStringValue: [NSString stringWithUTF8String: "?"]]; |
---|
698 | } else { |
---|
699 | [fInfoSeeders setStringValue: [NSString stringWithFormat: @"%d", |
---|
700 | fStat[row].seeders]]; |
---|
701 | } |
---|
702 | if ( fStat[row].leechers == -1 ) { |
---|
703 | [fInfoLeechers setStringValue: [NSString stringWithUTF8String: "?"]]; |
---|
704 | } else { |
---|
705 | [fInfoLeechers setStringValue: [NSString stringWithFormat: @"%d", |
---|
706 | fStat[row].leechers]]; |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | - (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier: |
---|
711 | (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag |
---|
712 | { |
---|
713 | NSToolbarItem * item; |
---|
714 | item = [[NSToolbarItem alloc] initWithItemIdentifier: ident]; |
---|
715 | |
---|
716 | if( [ident isEqualToString: TOOLBAR_OPEN] ) |
---|
717 | { |
---|
718 | [item setLabel: @"Open"]; |
---|
719 | [item setToolTip: @"Open a torrent"]; |
---|
720 | [item setImage: [NSImage imageNamed: @"Open.png"]]; |
---|
721 | [item setTarget: self]; |
---|
722 | [item setAction: @selector( openShowSheet: )]; |
---|
723 | } |
---|
724 | else if( [ident isEqualToString: TOOLBAR_REMOVE] ) |
---|
725 | { |
---|
726 | [item setLabel: @"Remove"]; |
---|
727 | [item setToolTip: @"Remove torrent from list"]; |
---|
728 | [item setImage: [NSImage imageNamed: @"Remove.png"]]; |
---|
729 | [item setTarget: self]; |
---|
730 | /* We set the selector in updateBars: */ |
---|
731 | } |
---|
732 | else if( [ident isEqualToString: TOOLBAR_PREFS] ) |
---|
733 | { |
---|
734 | [item setLabel: @"Preferences"]; |
---|
735 | [item setToolTip: @"Show the Preferences panel"]; |
---|
736 | [item setImage: [NSImage imageNamed: @"Preferences.png"]]; |
---|
737 | [item setTarget: fPrefsController]; |
---|
738 | [item setAction: @selector( show: )]; |
---|
739 | } |
---|
740 | else if( [ident isEqualToString: TOOLBAR_INFO] ) |
---|
741 | { |
---|
742 | [item setLabel: @"Info"]; |
---|
743 | [item setToolTip: @"Information"]; |
---|
744 | [item setImage: [NSImage imageNamed: @"Info.png"]]; |
---|
745 | [item setTarget: self]; |
---|
746 | [item setAction: @selector( showInfo: )]; |
---|
747 | } |
---|
748 | else |
---|
749 | { |
---|
750 | [item release]; |
---|
751 | return NULL; |
---|
752 | } |
---|
753 | |
---|
754 | return item; |
---|
755 | } |
---|
756 | |
---|
757 | - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) t |
---|
758 | { |
---|
759 | return [NSArray arrayWithObjects: TOOLBAR_OPEN, TOOLBAR_REMOVE, |
---|
760 | NSToolbarFlexibleSpaceItemIdentifier, TOOLBAR_PREFS, |
---|
761 | TOOLBAR_INFO, NULL]; |
---|
762 | } |
---|
763 | |
---|
764 | - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) t |
---|
765 | { |
---|
766 | return [self toolbarAllowedItemIdentifiers: t]; |
---|
767 | } |
---|
768 | |
---|
769 | - (void) sleepCallBack: (natural_t) messageType argument: |
---|
770 | (void *) messageArgument |
---|
771 | { |
---|
772 | int i; |
---|
773 | |
---|
774 | switch( messageType ) |
---|
775 | { |
---|
776 | case kIOMessageSystemWillSleep: |
---|
777 | /* Close all connections before going to sleep and remember |
---|
778 | we should resume when we wake up */ |
---|
779 | for( i = 0; i < fCount; i++ ) |
---|
780 | { |
---|
781 | if( fStat[i].status & ( TR_STATUS_CHECK | |
---|
782 | TR_STATUS_DOWNLOAD | TR_STATUS_SEED ) ) |
---|
783 | { |
---|
784 | tr_torrentStop( fHandle, i ); |
---|
785 | fResumeOnWake[i] = 1; |
---|
786 | } |
---|
787 | else |
---|
788 | { |
---|
789 | fResumeOnWake[i] = 0; |
---|
790 | } |
---|
791 | } |
---|
792 | |
---|
793 | /* TODO: wait a few seconds to let the torrents |
---|
794 | stop properly */ |
---|
795 | |
---|
796 | IOAllowPowerChange( fRootPort, (long) messageArgument ); |
---|
797 | break; |
---|
798 | |
---|
799 | case kIOMessageCanSystemSleep: |
---|
800 | /* Do not prevent idle sleep */ |
---|
801 | /* TODO: prevent it unless there are all paused? */ |
---|
802 | IOAllowPowerChange( fRootPort, (long) messageArgument ); |
---|
803 | break; |
---|
804 | |
---|
805 | case kIOMessageSystemHasPoweredOn: |
---|
806 | /* Resume download after we wake up */ |
---|
807 | for( i = 0; i < fCount; i++ ) |
---|
808 | { |
---|
809 | if( fResumeOnWake[i] ) |
---|
810 | { |
---|
811 | tr_torrentStart( fHandle, i ); |
---|
812 | } |
---|
813 | } |
---|
814 | [self updateBars]; |
---|
815 | break; |
---|
816 | } |
---|
817 | } |
---|
818 | |
---|
819 | - (NSRect) windowWillUseStandardFrame: (NSWindow *) w |
---|
820 | defaultFrame: (NSRect) defaultFrame |
---|
821 | { |
---|
822 | NSRect rectWin, rectView; |
---|
823 | float foo; |
---|
824 | |
---|
825 | rectWin = [fWindow frame]; |
---|
826 | rectView = [[fWindow contentView] frame]; |
---|
827 | foo = 47.0 + MAX( 1, tr_torrentCount( fHandle ) ) * 62.0 - |
---|
828 | rectView.size.height; |
---|
829 | |
---|
830 | rectWin.size.height += foo; |
---|
831 | rectWin.origin.y -= foo; |
---|
832 | |
---|
833 | return rectWin; |
---|
834 | } |
---|
835 | |
---|
836 | - (void) showMainWindow: (id) sender |
---|
837 | { |
---|
838 | [fWindow makeKeyAndOrderFront: NULL]; |
---|
839 | } |
---|
840 | |
---|
841 | - (void) linkHomepage: (id) sender |
---|
842 | { |
---|
843 | [[NSWorkspace sharedWorkspace] openURL: [NSURL |
---|
844 | URLWithString:@"http://transmission.m0k.org/"]]; |
---|
845 | } |
---|
846 | |
---|
847 | - (void) linkForums: (id) sender |
---|
848 | { |
---|
849 | [[NSWorkspace sharedWorkspace] openURL: [NSURL |
---|
850 | URLWithString:@"http://transmission.m0k.org/forum/"]]; |
---|
851 | } |
---|
852 | |
---|
853 | - (BOOL) hasGrowl |
---|
854 | { |
---|
855 | NSFileManager * manager = [NSFileManager defaultManager]; |
---|
856 | NSString * helper = @"/Library/PreferencePanes/Growl.prefPane/" |
---|
857 | "Contents/Resources/GrowlHelperApp.app"; |
---|
858 | |
---|
859 | if( [manager fileExistsAtPath: helper] ) |
---|
860 | { |
---|
861 | /* Growl was installed for all users */ |
---|
862 | return YES; |
---|
863 | } |
---|
864 | if( [manager fileExistsAtPath: [[NSString stringWithFormat: @"~%@", |
---|
865 | helper] stringByExpandingTildeInPath]] ) |
---|
866 | { |
---|
867 | /* Growl was installed for this user only */ |
---|
868 | return YES; |
---|
869 | } |
---|
870 | |
---|
871 | return NO; |
---|
872 | } |
---|
873 | |
---|
874 | - (void) notifyGrowl: (NSString * ) file |
---|
875 | { |
---|
876 | NSString * growlScript; |
---|
877 | NSAppleScript * appleScript; |
---|
878 | NSDictionary * error; |
---|
879 | |
---|
880 | if( ![self hasGrowl] ) |
---|
881 | { |
---|
882 | return; |
---|
883 | } |
---|
884 | |
---|
885 | growlScript = [NSString stringWithFormat: |
---|
886 | @"tell application \"System Events\"\n" |
---|
887 | " if exists application process \"GrowlHelperApp\" then\n" |
---|
888 | " tell application \"GrowlHelperApp\"\n " |
---|
889 | " notify with name \"Download Complete\"" |
---|
890 | " title \"Download Complete\"" |
---|
891 | " description \"%@\"" |
---|
892 | " application name \"Transmission\"\n" |
---|
893 | " end tell\n" |
---|
894 | " end if\n" |
---|
895 | "end tell", file]; |
---|
896 | appleScript = [[NSAppleScript alloc] initWithSource: growlScript]; |
---|
897 | if( ![appleScript executeAndReturnError: &error] ) |
---|
898 | { |
---|
899 | printf( "Growl notify failed\n" ); |
---|
900 | } |
---|
901 | [appleScript release]; |
---|
902 | } |
---|
903 | |
---|
904 | - (void) growlRegister: (id) sender |
---|
905 | { |
---|
906 | NSString * growlScript; |
---|
907 | NSAppleScript * appleScript; |
---|
908 | NSDictionary * error; |
---|
909 | |
---|
910 | if( ![self hasGrowl] ) |
---|
911 | { |
---|
912 | return; |
---|
913 | } |
---|
914 | |
---|
915 | growlScript = [NSString stringWithFormat: |
---|
916 | @"tell application \"System Events\"\n" |
---|
917 | " if exists application process \"GrowlHelperApp\" then\n" |
---|
918 | " tell application \"GrowlHelperApp\"\n" |
---|
919 | " register as application \"Transmission\" " |
---|
920 | " all notifications {\"Download Complete\"}" |
---|
921 | " default notifications {\"Download Complete\"}" |
---|
922 | " icon of application \"Transmission\"\n" |
---|
923 | " end tell\n" |
---|
924 | " end if\n" |
---|
925 | "end tell"]; |
---|
926 | appleScript = [[NSAppleScript alloc] initWithSource: growlScript]; |
---|
927 | if( ![appleScript executeAndReturnError: &error] ) |
---|
928 | { |
---|
929 | printf( "Growl registration failed\n" ); |
---|
930 | } |
---|
931 | [appleScript release]; |
---|
932 | } |
---|
933 | |
---|
934 | |
---|
935 | - (void) revealFromMenu: (id) sender |
---|
936 | { |
---|
937 | int row; |
---|
938 | |
---|
939 | row = [fTableView selectedRow]; |
---|
940 | if( row < 0 ) |
---|
941 | { |
---|
942 | return; |
---|
943 | } |
---|
944 | [self finderReveal: [NSString stringWithFormat: @"%@/%@", |
---|
945 | [NSString stringWithUTF8String: fStat[row].folder], |
---|
946 | [NSString stringWithUTF8String: fStat[row].info.name]]]; |
---|
947 | } |
---|
948 | |
---|
949 | - (void) finderReveal: (NSString *) path |
---|
950 | { |
---|
951 | NSString * string; |
---|
952 | NSAppleScript * appleScript; |
---|
953 | NSDictionary * error; |
---|
954 | |
---|
955 | string = [NSString stringWithFormat: |
---|
956 | @"tell application \"Finder\"\n" |
---|
957 | " activate\n" |
---|
958 | " reveal (POSIX file \"%@\")\n" |
---|
959 | "end tell", path]; |
---|
960 | |
---|
961 | appleScript = [[NSAppleScript alloc] initWithSource: string]; |
---|
962 | if( ![appleScript executeAndReturnError: &error] ) |
---|
963 | { |
---|
964 | printf( "finderReveal failed\n" ); |
---|
965 | } |
---|
966 | [appleScript release]; |
---|
967 | } |
---|
968 | |
---|
969 | - (void) finderTrash: (NSString *) path |
---|
970 | { |
---|
971 | NSString * string; |
---|
972 | NSAppleScript * appleScript; |
---|
973 | NSDictionary * error; |
---|
974 | |
---|
975 | string = [NSString stringWithFormat: |
---|
976 | @"tell application \"Finder\"\n" |
---|
977 | " move (POSIX file \"%@\") to trash\n" |
---|
978 | "end tell", path]; |
---|
979 | |
---|
980 | appleScript = [[NSAppleScript alloc] initWithSource: string]; |
---|
981 | if( ![appleScript executeAndReturnError: &error] ) |
---|
982 | { |
---|
983 | printf( "finderTrash failed\n" ); |
---|
984 | } |
---|
985 | [appleScript release]; |
---|
986 | } |
---|
987 | |
---|
988 | @end |
---|