-
Notifications
You must be signed in to change notification settings - Fork 5
/
RTListCommand.m
184 lines (161 loc) · 5.22 KB
/
RTListCommand.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
/******************************************************************************
* 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 "RTListCommand.h"
#import "Torrent.h"
#import "NSStringRTorrentAdditions.h"
@interface RTListCommand(Private)
- (TorrentState) defineTorrentState:(NSNumber*) state checking:(NSNumber*)checking opened:(NSNumber*) opened complete:(NSNumber *) complete active:(NSNumber *) active;
- (TorrentPriority) defineTorrentPriority:(NSNumber*) priority;
@end
@implementation RTListCommand
@synthesize response;
@synthesize groupCommand = _groupCommand;
- (id)initWithArrayResponse:(ArrayResponseBlock) resp;
{
self = [super init];
if (self == nil)
return nil;
[self setResponse:resp];
return self;
}
- (void) processResponse:(id) data error:(NSString *) error;
{
NSMutableArray* result = nil;
if (error == nil)
{
result = [[NSMutableArray alloc] init];
for (NSArray* row in data)
{
Torrent* r = [[Torrent alloc] init];
r.thash = [row objectAtIndex:0];
r.name = [row objectAtIndex:1];
NSNumber* size = [row objectAtIndex:2];
NSNumber* completed = [row objectAtIndex:3];
r.size = [size integerValue];
r.downloadRate = [completed integerValue];
NSNumber* state = [row objectAtIndex:4];
NSNumber* opened = [row objectAtIndex:5];
NSNumber* speedDownload = [row objectAtIndex:6];
r.speedDownload = [speedDownload floatValue];
NSNumber* speedUpload = [row objectAtIndex:7];
r.speedUpload = [speedUpload floatValue];
NSNumber* uploadRate = [row objectAtIndex:8];
r.uploadRate = [uploadRate integerValue];
r.dataLocation = [row objectAtIndex:9];
NSNumber *conn = [row objectAtIndex:10];
NSNumber *notConn = [row objectAtIndex:11];
NSNumber *compl = [row objectAtIndex:12];
r.totalPeersLeech = [conn integerValue] - [compl integerValue];
r.totalPeersSeed = [compl integerValue];
r.totalPeersDisconnected = [notConn integerValue];
r.priority = [self defineTorrentPriority:[row objectAtIndex:13]];
r.isFolder = [[row objectAtIndex:14] boolValue];
NSString* errorMessage = [row objectAtIndex:15];
r.error = [errorMessage isEqualToString:@""]?nil:errorMessage;
NSString *groupName = [row objectAtIndex:16];
NSString *decodedGroupName = [groupName isEqualToString:@""]?nil:[groupName urlDecode];
r.groupName = decodedGroupName;
NSNumber* checking = [row objectAtIndex:17];
NSNumber* complete = [row objectAtIndex:18];
NSNumber* active = [row objectAtIndex:19];
r.state = [self defineTorrentState:state checking:checking opened:opened complete:complete active:active];
[result addObject:r];
[r release];
}
[result autorelease];
}
if (response)
response(result, error);
}
- (NSString *) command;
{
return @"d.multicall";
}
- (NSArray *) arguments;
{
return [NSArray arrayWithObjects:
@"main",
@"d.get_hash=",
@"d.get_name=",
@"d.get_size_bytes=",
@"d.get_completed_bytes=",
@"d.get_state=",
@"d.is_open=",
@"d.get_down_rate=",
@"d.get_up_rate=",
@"d.get_up_total=",
@"d.get_base_path=",
@"d.get_peers_connected=",
@"d.get_peers_not_connected=",
@"d.get_peers_complete=",
@"d.get_priority=",
@"d.is_multi_file=",
@"d.get_message=",
[_groupCommand stringByAppendingString:@"="],
@"d.is_hash_checking=",
@"d.get_complete=",
@"d.is_active=",
nil];
}
- (void)dealloc
{
[self setResponse:nil];
[self setGroupCommand:nil];
[super dealloc];
}
@end
@implementation RTListCommand(Private)
- (TorrentState) defineTorrentState:(NSNumber*) state checking:(NSNumber*)checking opened:(NSNumber*) opened complete:(NSNumber *) complete active:(NSNumber *) active
{
if ([checking boolValue])
return NITorrentStateChecking;
if (![active boolValue])
return [opened boolValue]?NITorrentStatePaused:NITorrentStateStopped;
switch ([state intValue]) {
case 1: //started
if ([opened intValue]==0)
return NITorrentStateStopped;
else
{
if ([complete boolValue])
return NITorrentStateSeeding;
else
return NITorrentStateLeeching;
}
case 0: //stopped
return NITorrentStateStopped;
}
return NITorrentStateUnknown;
}
- (TorrentPriority) defineTorrentPriority:(NSNumber*) priority
{
switch ([priority integerValue]) {
case 0:
return NITorrentPriorityOff;
case 1:
return NITorrentPriorityLow;
case 2:
return NITorrentPriorityNormal;
case 3:
return NITorrentPriorityHigh;
}
return NITorrentPriorityNormal;
}
@end