forked from mus1cjunk1e/NativaPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadsController.m
607 lines (517 loc) · 21.1 KB
/
DownloadsController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/******************************************************************************
* Nativa - MacOS X UI for rtorrent
* http://www.aramzamzam.net
*
* Copyright Solomenchuk V. 2010.
* Solomenchuk Vladimir <[email protected]>
*
* Licensed under the GPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#import "DownloadsController.h"
#import "SynthesizeSingleton.h"
#import "Torrent.h"
#import "TorrentController.h"
#import "ProcessesController.h"
#import "PreferencesController.h"
#import "GroupsController.h"
#include <Growl/Growl.h>
#import "NSStringTorrentAdditions.h"
static NSString * ConnectedContext = @"ConnectedContext";
static NSString * ConnectingContext = @"ConnectingContext";
NSString* const NINotifyUpdateDownloads = @"NINotifyUpdateDownloads";
@interface DownloadsController(Private)
- (void)_updateList;
- (id<TorrentController>) _controller;
- (NSInteger) _processIndex;
- (VoidResponseBlock) _updateListResponse: (VoidResponseBlock) originalResponse errorFormat:(NSString*) errorFormat;
-(void) setError:(NSString*) fmt error:(NSString*) error;
@end
@implementation DownloadsController
SYNTHESIZE_SINGLETON_FOR_CLASS(DownloadsController);
@synthesize globalUploadSpeed = _globalUploadSpeed;
@synthesize globalDownloadSpeed = _globalDownloadSpeed;
@synthesize spaceLeft = _spaceLeft;
@synthesize globalDownloadSize = _globalDownloadSize;
@synthesize globalUploadSize = _globalUploadSize;
@synthesize globalRatio = _globalRatio;
@synthesize globalDownloadSpeedLimit = _globalDownloadSpeedLimit;
@synthesize globalUploadSpeedLimit = _globalUploadSpeedLimit;
@synthesize connected, connecting;
@dynamic lastError;
- (NSString *) lastError
{
return [[self _controller] lastError];
}
-(id)init;
{
self = [super init];
if (self == nil)
return nil;
_downloads = [[[NSMutableArray alloc] init] retain];
_defaults = [NSUserDefaults standardUserDefaults];
_queue = [[NSOperationQueue alloc] init];
[_queue retain];
_deleteSound = [NSSound soundNamed: @"drag to trash"];
[_deleteSound retain];
return self;
}
-(void)dealloc
{
[_downloads release];
[_queue release];
[_deleteSound release];
[super dealloc];
}
#pragma mark -
#pragma mark public methods
-(void) startUpdates:(VoidResponseBlock) response;
{
[_updateListTimer invalidate];
[_updateGlobalsTimer invalidate];
[(NSObject *)[self _controller] addObserver:self
forKeyPath:@"connected"
options:0
context:&ConnectedContext];
[(NSObject *)[self _controller] addObserver:self
forKeyPath:@"connecting"
options:0
context:&ConnectingContext];
__block DownloadsController *blockSelf = self;
[[ProcessesController sharedProcessesController] openProcessForIndex:[self _processIndex] handler:^(NSString* error){
if (response)
response(error);
if (error == nil)
{
[blockSelf _updateList];
blockSelf->_updateListTimer = [NSTimer scheduledTimerWithTimeInterval:
[blockSelf->_defaults integerForKey:NIRefreshRateKey]
target:self
selector:@selector(_updateList)
userInfo:nil
repeats:YES];
[blockSelf->_updateListTimer retain];
[[NSRunLoop currentRunLoop] addTimer:blockSelf->_updateListTimer forMode:NSDefaultRunLoopMode];
[blockSelf updateGlobals];
blockSelf->_updateGlobalsTimer = [NSTimer scheduledTimerWithTimeInterval:
[blockSelf->_defaults integerForKey:NIUpdateGlobalsRateKey]
target:self
selector:@selector(updateGlobals)
userInfo:nil
repeats:YES];
[blockSelf->_updateGlobalsTimer retain];
[[NSRunLoop currentRunLoop] addTimer:blockSelf->_updateGlobalsTimer forMode:NSDefaultRunLoopMode];
}
}];
}
-(void) stopUpdates;
{
[_updateListTimer invalidate];
[_updateGlobalsTimer invalidate];
@try {
[(NSObject *)[self _controller] removeObserver:self forKeyPath:@"connected"];
}
@catch (NSException *exception) {
//ignore objserver removal exception
}
@try {
[(NSObject *)[self _controller] removeObserver:self forKeyPath:@"connecting"];
}
@catch (NSException *exception) {
//ignore objserver removal exception
}
ProcessesController* pc = [ProcessesController sharedProcessesController];
for (NSInteger i=0;i<[pc count];i++)
{
NSInteger index =[pc indexForRow:i];
[pc closeProcessForIndex:index];
}
}
-(NSArray*) downloads;
{
return _downloads;
}
#pragma mark -
#pragma mark concrete torrent methods
- (void) start:(Torrent *) torrent handler:(VoidResponseBlock) handler
{
VoidResponseBlock r = [self _updateListResponse:handler errorFormat:@"Unable to start torrent: %@"];
[[self _controller] start:torrent handler:r];
[r release];
}
- (void) stop:(Torrent *) torrent force:(BOOL)force handler:(VoidResponseBlock) handler
{
VoidResponseBlock r = [self _updateListResponse:handler errorFormat:@"Unable to stop torrent: %@"];
if (force)
[[self _controller] stop:torrent handler:r];
else
[[self _controller] pause:torrent handler:r];
[r release];
}
- (void) add:(NSArray *) filesNames
{
__block DownloadsController *blockSelf = self;
[_queue addOperationWithBlock:^{
for(NSString *file in filesNames)
{
NSURL* url = [NSURL fileURLWithPath:file];
NSArray* urls = [NSArray arrayWithObjects:url, nil];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLResponse *returningResponse = nil;
NSError* connError = nil;
NSData *rawTorrent = [NSURLConnection sendSynchronousRequest:request returningResponse:&returningResponse error:&connError];
if (rawTorrent == nil)
{
[blockSelf setError:@"Unable to add torrent: %@" error:[connError localizedDescription]];
continue;
}
Torrent *constructed = [Torrent torrentWithData:rawTorrent];
NSInteger index = [[GroupsController groups] groupIndexForTorrentByRules:constructed];
NSString *groupName = [[GroupsController groups] nameForIndex:index];
NSString *folderName = [[GroupsController groups] usesCustomDownloadLocationForIndex:index]?
[[GroupsController groups] customDownloadLocationForIndex:index]:
nil;
[[blockSelf _controller] add:rawTorrent
start:[_defaults boolForKey:NIStartTransferWhenAddedKey]
group:groupName
folder:folderName
response:^(NSString* error){
#warning memory leak here (recycleURLs)
if (error)
{
[blockSelf setError:@"unable to add torrent: %@" error:error];
return;
}
if ([_defaults boolForKey:NITrashDownloadDescriptorsKey])
{
[[NSWorkspace sharedWorkspace] recycleURLs: urls
completionHandler:^(NSDictionary *newURLs, NSError *error){
if (!error)
[blockSelf->_deleteSound play];
}];
}
[blockSelf _updateList];
[blockSelf updateGlobals];
[GrowlApplicationBridge
notifyWithTitle:@"Torrent added"
description:[NSString stringWithFormat:@"Torrent \"%@\", size %@, succesfully added", constructed.name, [NSString stringForFileSize:constructed.size]]
notificationName:@"INFO"
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
}];
}
}];
}
- (void) erase:(Torrent *) torrent withData:(BOOL) removeData response:(VoidResponseBlock) response
{
__block DownloadsController *blockSelf = self;
VoidResponseBlock r = [^(NSString* error){
if (response)
response(error);
if (error)
{
[blockSelf setError:@"Unable to remove torrent:%@" error:error];
return;
}
if (removeData)
{
NSString* dataLocation = [blockSelf findLocation:torrent];
if (dataLocation)
{
NSURL* url = [NSURL fileURLWithPath:dataLocation];
NSArray* urls = [NSArray arrayWithObjects:url, nil];
[[NSWorkspace sharedWorkspace] recycleURLs: urls
completionHandler:^(NSDictionary *newURLs, NSError *error){
if (error)
{
NSLog(@"unable to trash file %@:",error);
NSError* removeError = nil;
[[NSFileManager defaultManager] removeItemAtPath:dataLocation error:&removeError];
if (removeError)
[self setError:@"Unable to delete file %@: " error:[removeError localizedDescription]];
else
[blockSelf->_deleteSound play]; //play "trash" sound
}
else
{
//play "trash" sound
[blockSelf->_deleteSound play]; }
}];
}
else
[self setError:@"Unable to delete torrent data: %@" error:@"cannot find torrent data"];
}
[blockSelf _updateList];
[blockSelf updateGlobals];
}copy];
[[self _controller] erase:[torrent thash] response:r];
[r release];
}
#pragma mark -
#pragma mark global state methods
- (void) setGlobalDownloadSpeedLimit:(NSInteger) speed response:(VoidResponseBlock) response
{
__block DownloadsController *blockSelf = self;
[[self _controller] setGlobalDownloadSpeedLimit:speed response:^(NSString* error){
if (response)
response(error);
if (error)
{
[blockSelf setError:@"Unable to set global download speed limit: %@" error:error];
return;
}
[blockSelf willChangeValueForKey:@"globalDownloadSpeedLimit"];
_globalDownloadSpeedLimit = speed;
[blockSelf didChangeValueForKey:@"globalDownloadSpeedLimit"];
}];
}
- (void) setGlobalUploadSpeedLimit:(NSInteger) speed response:(VoidResponseBlock) response
{
__block DownloadsController *blockSelf = self;
[[self _controller] setGlobalUploadSpeedLimit:speed response:^(NSString* error){
if (response)
response(error);
if (error)
{
[blockSelf setError:@"Unable to set global upload speed limit: %@" error:error];
return;
}
[blockSelf willChangeValueForKey:@"globalUploadSpeedLimit"];
_globalUploadSpeedLimit = speed;
[blockSelf didChangeValueForKey:@"globalUploadSpeedLimit"];
}];
}
- (void) reveal:(Torrent*) torrent
{
NSString* location = [self findLocation:torrent];
if (!location)
location = [[ProcessesController sharedProcessesController] localDownloadsFolderForIndex:[self _processIndex]];
if (location)
{
NSURL * file = [NSURL fileURLWithPath: location];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs: [NSArray arrayWithObject: file]];
}
}
-(void) setPriority:(Torrent *)torrent priority:(TorrentPriority)priority response:(VoidResponseBlock) response
{
VoidResponseBlock r = [self _updateListResponse:response errorFormat:@"Unable to set priority for torrent: %@"];
[[self _controller] setPriority:torrent priority:priority response:r];
[r release];
}
- (void) setGroup:(Torrent *)torrent group:(NSString *) group response:(VoidResponseBlock) response
{
VoidResponseBlock r = [self _updateListResponse:response errorFormat:@"Unable to set group for torrent: %@"];
[[self _controller] setGroup:torrent group:group response:r];
[r release];
}
-(NSString*) findLocation:(Torrent *)torrent
{
NSString * location = [[ProcessesController sharedProcessesController] localDownloadsFolderForIndex:[self _processIndex]];
if (location)
{
NSMutableString* exactLocation = [NSMutableString stringWithCapacity:[location length]];
NSArray* splittedPath = [torrent.dataLocation pathComponents];
[exactLocation setString:location];
NSFileManager* dm = [NSFileManager defaultManager];
for(int i=[splittedPath count]-1;i>-1;i--) //we do not know where is file, so lets make some guesses
{
for (int ii = i;ii<[splittedPath count];ii++)
[exactLocation appendFormat:@"/%@",[splittedPath objectAtIndex:ii]];
if ([dm fileExistsAtPath:exactLocation])
break;
else
[exactLocation setString:location];
}
if ([dm fileExistsAtPath:exactLocation] && ![exactLocation isEqualToString:location])
{
return exactLocation;
}
}
return nil;
}
- (void) check:(Torrent*) torrent response:(VoidResponseBlock) response
{
VoidResponseBlock r = [self _updateListResponse:response errorFormat:@"Unable to check hash for torrent: %@"];
[[self _controller] check:torrent response:r];
[r release];
}
- (void) updateGlobals
{
NSString *path = [[ProcessesController sharedProcessesController] localDownloadsFolderForIndex:[self _processIndex]];
if (path != nil)
{
NSError* error = nil;
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfFileSystemForPath:path error:&error];
if (error == nil)
[self setSpaceLeft:[[attr objectForKey:NSFileSystemFreeSize] doubleValue]];
}
__block DownloadsController *blockSelf = self;
[[self _controller] getGlobalDownloadSpeedLimit:^(NSNumber *number, NSString* error){
if (error != nil)
{
[blockSelf setError:@"Unable to get global download speed limit: %@" error:error];
return;
}
[blockSelf willChangeValueForKey:@"globalDownloadSpeedLimit"];
_globalDownloadSpeedLimit = [number floatValue];
[blockSelf didChangeValueForKey:@"globalDownloadSpeedLimit"];
//update global max speed limits
if ([_defaults boolForKey: NIGlobalSpeedLimitMaxAuto]
&& _globalDownloadSpeedLimit == 0
&& [_defaults integerForKey:NIGlobalSpeedLimitMaxDownload]<_globalDownloadSpeed/1024)
[_defaults setInteger:_globalDownloadSpeed/1024 forKey:NIGlobalSpeedLimitMaxDownload];
}];
[[self _controller] getGlobalUploadSpeedLimit:^(NSNumber *number, NSString* error){
if (error != nil)
{
[blockSelf setError:@"Unable to get global upload speed limit: %@" error:error];
return;
}
[blockSelf willChangeValueForKey:@"globalUploadSpeedLimit"];
_globalUploadSpeedLimit = [number floatValue];
[blockSelf didChangeValueForKey:@"globalUploadSpeedLimit"];
//update global max speed limits
if ([_defaults boolForKey: NIGlobalSpeedLimitMaxAuto]
&& _globalUploadSpeedLimit == 0
&& [_defaults integerForKey:NIGlobalSpeedLimitMaxUpload]<_globalUploadSpeed/1024)
[_defaults setInteger:_globalUploadSpeed/1024 forKey:NIGlobalSpeedLimitMaxUpload];
}];
}
- (void) moveData:(Torrent *) torrent location:(NSString *) location handler:(VoidResponseBlock) handler
{
VoidResponseBlock r = [self _updateListResponse:handler errorFormat:@"Unable to set location for torrent: %@"];
[[self _controller] moveData:torrent location:location handler:r];
[r release];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (context == &ConnectedContext)
{
[self setConnected:[self _controller].connected];
}
else if (context == &ConnectingContext)
{
[self setConnecting:[self _controller].connecting];
}
else
{
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
@end
@implementation DownloadsController(Private)
- (id<TorrentController>) _controller
{
return [[ProcessesController sharedProcessesController] processForIndex:[self _processIndex]];
}
- (NSInteger) _processIndex
{
return [[ProcessesController sharedProcessesController] indexForRow:0];
}
- (void)_updateList
{
if (![[self _controller] connected])
return;
__block DownloadsController *blockSelf = self;
ArrayResponseBlock response = [^(NSArray *array, NSString* error) {
if (error != nil)
{
NSLog(@"update download list error: %@", error);
return;
}
NSUInteger idx;
#warning multiple objects?
Torrent* stored_obj;
CGFloat globalUploadRate = 0.0;
CGFloat globalDownloadRate = 0.0;
CGFloat download = 0.0;
CGFloat upload = 0.0;
for (Torrent *obj in array)
{
idx = [blockSelf->_downloads indexOfObject:obj];
if (idx == NSNotFound)
[blockSelf->_downloads addObject:obj];
else
{
stored_obj = [blockSelf->_downloads objectAtIndex:idx];
[stored_obj update:obj];
}
if (obj.state != NITorrentStateChecking)
{
globalUploadRate += [obj speedUpload];
globalDownloadRate += [obj speedDownload];
download+=obj.downloadRate;
upload+=obj.uploadRate;
}
}
//find removed torrents
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity: [blockSelf->_downloads count]];
for (Torrent *obj in blockSelf->_downloads)
{
idx = [array indexOfObject:obj];
if (idx == NSNotFound)
[toRemove addObject:obj];
}
for (Torrent *obj in toRemove)
[blockSelf->_downloads removeObject:obj];
[[NSNotificationCenter defaultCenter] postNotificationName: NINotifyUpdateDownloads object: blockSelf];
[blockSelf willChangeValueForKey:@"globalDownloadSpeed"];
[blockSelf willChangeValueForKey:@"globalUploadSpeed"];
[blockSelf willChangeValueForKey:@"globalDownloadSize"];
[blockSelf willChangeValueForKey:@"globalUploadSize"];
[blockSelf willChangeValueForKey:@"globalRatio"];
_globalDownloadSpeed = globalDownloadRate;
_globalUploadSpeed = globalUploadRate;
_globalUploadSize = upload;
_globalDownloadSize = download;
_globalRatio = download==0?0:upload/download;
[blockSelf didChangeValueForKey:@"globalDownloadSpeed"];
[blockSelf didChangeValueForKey:@"globalUploadSpeed"];
[blockSelf didChangeValueForKey:@"globalDownloadSize"];
[blockSelf didChangeValueForKey:@"globalUploadSize"];
[blockSelf didChangeValueForKey:@"globalRatio"];
} copy];
[[self _controller] list:response];
[response release];
}
- (VoidResponseBlock) _updateListResponse: (VoidResponseBlock) originalResponse errorFormat:(NSString*) errorFormat
{
__block DownloadsController *blockSelf = self;
return [^(NSString* error){
if (originalResponse)
originalResponse(error);
if (error)
[blockSelf setError:errorFormat error:error];
[blockSelf _updateList];
}copy];
}
-(void) setError:(NSString*) fmt error:(NSString*) error;
{
[GrowlApplicationBridge
notifyWithTitle:@"Error"
description:[NSString stringWithFormat:fmt, error]
notificationName:@"ERROR"
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
NSLog(fmt, error);
}
@end