-
Notifications
You must be signed in to change notification settings - Fork 5
/
RTorrentController.m
393 lines (349 loc) · 10.7 KB
/
RTorrentController.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
/******************************************************************************
* 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 "RTorrentController.h"
#import "RTConnection.h"
#import "RTSCGIOperation.h"
#import "RTListCommand.h"
#import "NSStringRTorrentAdditions.h"
static NSString * ConnectedContext = @"ConnectedContext";
static NSString * ConnectingContext = @"ConnectingContext";
@interface RTorrentController(Private)
-(void)_runOperation:(id<RTorrentCommand>) operation;
-(void)_runCommand:(NSString*) command arguments:(NSArray*)arguments handler:(void(^)(id data, NSString* error)) h;
-(void(^)(id data, NSString* error))_voidHandler:(VoidResponseBlock) handler;
@end
@implementation RTorrentController
@dynamic groupField;
@synthesize connection, connected, connecting;
- (id) init
{
if ((self = [super init]) != nil)
{
_queue = [[NSOperationQueue alloc] init];
[_queue setMaxConcurrentOperationCount:1];
[_queue setSuspended:YES];
}
return self;
}
- (id)initWithConnection:(RTConnection*) conn;
{
self = [super init];
if (self == nil)
return nil;
[self setConnection:conn];
_queue = [[NSOperationQueue alloc] init];
[_queue setMaxConcurrentOperationCount:1];
[_queue setSuspended:YES];
return self;
}
-(void)dealloc;
{
[_queue release];
[self setConnection:nil];
[_getGroupCommand release];
[_setGroupCommand release];
[self setConnection:nil];
[super dealloc];
}
- (void) list:(ArrayResponseBlock) response;
{
RTListCommand* command = [[RTListCommand alloc] initWithArrayResponse:response];
[command setGroupCommand:_getGroupCommand];
[self _runOperation: command];
[command release];
}
- (void) start:(Torrent *)torrent handler:(VoidResponseBlock) handler
{
__block RTorrentController *blockSelf = self;
void(^r)(id data, NSString* error);
r = [self _voidHandler:handler];
[self _runCommand:@"d.open"
arguments:[NSArray arrayWithObjects:
torrent.thash,
nil]
handler:^(id data, NSString* error){
if (error != nil)
{
r(data, error);
return;
}
[blockSelf _runCommand:@"d.start"
arguments:[NSArray arrayWithObjects:
torrent.thash,
nil]
handler:r];
}];
[r release];
}
- (void) stop:(Torrent *)torrent handler:(VoidResponseBlock) handler
{
__block RTorrentController *blockSelf = self;
void(^r)(id data, NSString* error);
r = [self _voidHandler:handler];
[self _runCommand:@"d.stop"
arguments:[NSArray arrayWithObjects:
torrent.thash,
nil]
handler:^(id data, NSString* error){
if (error != nil)
{
r(data, error);
return;
}
[blockSelf _runCommand:@"d.close"
arguments:[NSArray arrayWithObjects:
torrent.thash,
nil]
handler:r];
}];
[r release];
}
- (void) add:(NSData *)rawTorrent start:(BOOL) start group:(NSString*) group folder:(NSString*) folder response:(VoidResponseBlock) response
{
NSString* command = start ? @"load_raw_start" : @"load_raw";
NSMutableArray* args = [NSMutableArray arrayWithCapacity:3];
[args addObject:rawTorrent];
if (group != nil)
[args addObject:[NSString stringWithFormat:@"%@=%@",_setGroupCommand,[group urlEncode]]];
if (folder != nil)
[args addObject:[NSString stringWithFormat:@"%@=%@",@"d.set_directory",folder]];
id r = [self _voidHandler:response];
[self _runCommand:command
arguments:args
handler:r];
[r release];
}
- (void) erase:(NSString *)hash response:(VoidResponseBlock) response;
{
id r = [self _voidHandler:response];
[self _runCommand:@"d.erase"
arguments:[NSArray arrayWithObjects:
hash,
nil]
handler:r];
[r release];
}
- (void) setGlobalDownloadSpeedLimit:(int) speed response:(VoidResponseBlock) response;
{
id r = [self _voidHandler:response];
[self _runCommand:@"set_download_rate"
arguments:[NSArray arrayWithObjects:
[NSNumber numberWithInt:speed],
nil]
handler:r];
[r release];
}
- (void) setGlobalUploadSpeedLimit:(int) speed response:(VoidResponseBlock) response;
{
id r = [self _voidHandler:response];
[self _runCommand:@"set_upload_rate"
arguments:[NSArray arrayWithObjects:
[NSNumber numberWithInt:speed],
nil]
handler:r];
[r release];
}
- (void) getGlobalDownloadSpeedLimit:(NumberResponseBlock) response
{
[self _runCommand:@"get_download_rate"
arguments:nil
handler:^(id data, NSString* error){
if (response)
response(data, error);
}];
}
- (void) getGlobalUploadSpeedLimit:(NumberResponseBlock) response
{
[self _runCommand:@"get_upload_rate"
arguments:nil
handler:^(id data, NSString* error){
if (response)
response(data, error);
}];
}
- (void) setPriority:(Torrent *)torrent priority:(TorrentPriority)priority response:(VoidResponseBlock) response
{
NSInteger pr;
switch (priority) {
case NITorrentPriorityLow:
pr = 1;
break;
case NITorrentPriorityNormal:
pr = 2;
break;
case NITorrentPriorityHigh:
pr = 3;
break;
default:
NSAssert1(NO, @"Unknown priority: %d", priority);
}
id r = [self _voidHandler:response];
[self _runCommand:@"d.set_priority"
arguments:[NSArray arrayWithObjects:
[torrent thash],
[NSNumber numberWithInteger:pr],
nil]
handler:r];
[r release];
}
- (void) setGroup:(Torrent *)torrent group:(NSString *) group response:(VoidResponseBlock) response
{
id r = [self _voidHandler:response];
NSString *gn = [group urlEncode];
[self _runCommand:_setGroupCommand
arguments:[NSArray arrayWithObjects:
torrent.thash,
gn==nil?@"":gn,
nil]
handler:r];
[r release];
}
- (void) check:(Torrent *)torrent response:(VoidResponseBlock) response
{
id r = [self _voidHandler:response];
[self _runCommand:@"d.check_hash"
arguments:[NSArray arrayWithObjects:
[torrent thash],
nil]
handler:r];
[r release];
}
- (void) pause:(Torrent *) torrent handler:(VoidResponseBlock) handler
{
id r = [self _voidHandler:handler];
[self _runCommand:@"d.stop"
arguments:[NSArray arrayWithObjects:
torrent.thash,
nil]
handler:r];
[r release];
}
- (void) moveData:(Torrent *) torrent location:(NSString *) location handler:(VoidResponseBlock) handler
{
id r = [self _voidHandler:handler];
[self _runCommand:@"d.set_directory"
arguments:[NSArray arrayWithObjects:
torrent.thash,
location,
nil]
handler:r];
[r release];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (context == &ConnectedContext)
{
[_queue setSuspended:!connection.connected];
[self setConnected:connection.connected];
}
else if (context == &ConnectingContext)
{
[self setConnecting:connection.connecting];
}
else
{
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
-(void) openConnection:(VoidResponseBlock) response;
{
[connection addObserver:self
forKeyPath:@"connected"
options:0
context:&ConnectedContext];
[connection addObserver:self
forKeyPath:@"connecting"
options:0
context:&ConnectingContext];
connected = NO;
connecting = NO;
[connection openConnection:^(RTConnection *sender){
if (response != nil)
response([sender error]);
}];
}
-(void) closeConnection
{
@try {
[connection removeObserver:self forKeyPath:@"connected"];
}
@catch (NSException *exception) {
//ignore objserver removal exception
}
@try {
[connection removeObserver:self forKeyPath:@"connecting"];
}
@catch (NSException *exception) {
//ignore objserver removal exception
}
[connection closeConnection];
[self willChangeValueForKey:@"connected"];
[self willChangeValueForKey:@"connecting"];
connected = NO;
connecting = NO;
[self didChangeValueForKey:@"connecting"];
[self didChangeValueForKey:@"connected"];
}
-(NSUInteger) groupField;
{
return _groupField;
}
-(void) setGroupField:(NSUInteger) value
{
[_getGroupCommand release];
_getGroupCommand = [NSString stringWithFormat:@"d.get_custom%@",[NSString stringWithFormat:@"%d", value]];
[_getGroupCommand retain];
[_setGroupCommand release];
_setGroupCommand = [NSString stringWithFormat:@"d.set_custom%@",[NSString stringWithFormat:@"%d", value]];
[_setGroupCommand retain];
_groupField = value;
}
- (NSString *) lastError
{
return [connection error];
}
@end
@implementation RTorrentController(Private)
-(void)_runOperation:(id<RTorrentCommand>) operation
{
RTSCGIOperation* scgiOperation = [[RTSCGIOperation alloc] initWithOperation:self operation:operation];
[_queue addOperation:scgiOperation];
[scgiOperation release];
}
-(void)_runCommand:(NSString*) command arguments:(NSArray*)arguments handler:(void(^)(id data, NSString* error)) h
{
RTSCGIOperation* scgiOperation = [[RTSCGIOperation alloc] initWithCommand:self command:command arguments:arguments handler:h];
[_queue addOperation:scgiOperation];
[scgiOperation release];
}
-(void(^)(id data, NSString* error))_voidHandler:(VoidResponseBlock) handler;
{
return [^(id data, NSString* error){
if (handler)
handler(error);
}copy];
}
@end