-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTransmissionClient.m
More file actions
144 lines (116 loc) · 4.64 KB
/
TransmissionClient.m
File metadata and controls
144 lines (116 loc) · 4.64 KB
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
//
// TransmissionClient.m
// TransmissionRemote
//
// Created by Tobias Rundström on 2010-03-13.
// Copyright 2010 Purple Scout. All rights reserved.
//
#import "TransmissionClient.h"
#import "JSON.h"
#import "NSDataAdditions.h"
#import "TCURLConnection.h"
#import "PreferencesWindow.h"
static TransmissionClient *_gClient = nil;
@implementation TransmissionClient
+(TransmissionClient*)client
{
if (!_gClient) {
_gClient = [TransmissionClient new];
}
return _gClient;
}
-(id)init
{
self = [super init];
if (self) {
_sessionId = @"XXXY";
}
return self;
}
-(void)addTorrentUrl:(NSURL*)url
{
NSDictionary *transmissionServer = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"transmissionServer"];
if (!transmissionServer) {
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Couldn't find configuration", nil)
defaultButton:NSLocalizedString(@"Open Preferences", nil)
alternateButton:NSLocalizedString(@"Skip", nil)
otherButton:nil
informativeTextWithFormat:NSLocalizedString(@"Couldn't find any configured Transmission Server, will not be able to add the torrent! Please configure a Transmission Server", nil)];
NSInteger res = [alert runModal];
if (res == 1) {
[[PreferencesWindow new] showWindow:nil];
return;
}
return;
}
NSString *torrentData = [[NSData dataWithContentsOfURL:url] base64EncodingWithLineLength:0];
if (!torrentData) {
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Failed to open file!", nil)
defaultButton:NSLocalizedString(@"OK", nil)
alternateButton:nil
otherButton:nil
informativeTextWithFormat:NSLocalizedString(@"Failed to open the torrent file, please try again.", nil)];
[alert runModal];
return;
}
NSDictionary *arguments = [NSDictionary dictionaryWithObjectsAndKeys:
torrentData, @"metainfo",
nil];
NSDictionary *request = [NSDictionary dictionaryWithObjectsAndKeys:
@"torrent-add", @"method",
[NSString stringWithFormat:@"%d", _requestTag++], @"tag",
arguments, @"arguments",
nil];
NSMutableURLRequest *urlRequest = [self doRequestWithDict:request];
TCURLConnection *conn = [[TCURLConnection alloc] initWithMutableRequest:urlRequest delegate:self];
[conn release];
[urlRequest release];
NSLog(@"Sending request");
}
-(NSMutableURLRequest*)doRequestWithDict:(NSDictionary*)requestDict
{
NSData *jsonData = [[requestDict JSONRepresentation] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"transmissionServer"];
NSString *host = [NSString stringWithFormat:@"http://%@:%@/transmission/rpc", [dict objectForKey:@"serverIP"], [dict objectForKey:@"serverPort"]];
NSLog(@"Connecting to %@", host);
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:host]];
urlRequest.HTTPMethod = @"POST";
[urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[urlRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue:@"TransmissionRemote/0.1" forHTTPHeaderField:@"User-Agent"];
[urlRequest setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:_sessionId forHTTPHeaderField:@"X-Transmission-Session-Id"];
[urlRequest setHTTPBody:jsonData];
return urlRequest;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
TCURLConnection *conn = (TCURLConnection*)connection;
NSLog(@"Adding data %d", [data length]);
[conn.requestData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSAlert *alert = [NSAlert alertWithError:error];
[alert runModal];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *resp = (NSHTTPURLResponse*)response;
if ([resp statusCode] == 409) {
_sessionId = [[[resp allHeaderFields] objectForKey:@"X-Transmission-Session-Id"] copy];
NSLog(@"New session id = %@", _sessionId);
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
TCURLConnection *conn = (TCURLConnection*)connection;
if (![_sessionId isEqualToString:[conn.urlRequest valueForHTTPHeaderField:@"X-Transmission-Session-Id"]]) {
NSMutableURLRequest *req = conn.urlRequest;
[req setValue:_sessionId forHTTPHeaderField:@"X-Transmission-Session-Id"];
TCURLConnection *newConn = [[TCURLConnection alloc] initWithMutableRequest:req delegate:self];
[newConn release];
NSLog(@"New connection should be sent");
}
}
@end