-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmrpcsession.cpp
225 lines (196 loc) · 5.95 KB
/
transmrpcsession.cpp
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
#include "transmrpcsession.h"
#include "json/json.h"
#include "tags.h"
#include <QHttp>
#include "torrent.h"
#include <QBuffer>
#include <sstream>
#include <QDebug>
#include <QtGui>
TransmRpcSession::TransmRpcSession(QString h = "127.0.0.1", QString p = "9091", QString u = "/transmission/rpc/", QWidget *par = 0) {
parent = par;
if(h != NULL) host = h;
else host = "127.0.0.1";
if(p != NULL) port = p;
else port = "9091";
if(u != NULL) url = u;
else url = "/transmission/rpc/";
http = new QHttp();
connect(http, SIGNAL(done(bool)), this, SLOT(dataReceived(bool)));
response = new QBuffer;
requestBody = new QBuffer;
transmSessionId = "";
http->setHost(host, port.toInt());
Fields.push_back("id");
Fields.push_back("name");
Fields.push_back("totalSize");
Fields.push_back("files");
Fields.push_back("peers");
Fields.push_back("peersConnected");
Fields.push_back("peersGettingFromUs");
Fields.push_back("peersSendingToUs");
Fields.push_back("percentDone");
Fields.push_back("status");
};
void TransmRpcSession::setConnectionSettings(QString h = NULL, QString p = NULL, QString u = NULL) {
if(h != NULL) host = h;
if(p != NULL) port = p;
if(u != NULL) url = u;
http->setHost(host, port.toInt());
};
int TransmRpcSession::sendRequest(unsigned int tag, unsigned int *ids) {
if(response->isOpen()) {
response->buffer().clear();
response->close();
}
if(requestBody->isOpen()) {
requestBody->buffer().clear();
requestBody->close();
}
requestBody->setData(generateJsonRequest(tag, ids).c_str());
requestHeader.setRequest("POST", url);
requestHeader.setValue(host, port);
requestHeader.setValue("X-Transmission-Session-Id", transmSessionId);
return http->request(requestHeader, requestBody, response);
};
int TransmRpcSession::getTorrentsList(unsigned int *ids) {
return sendRequest(TORRENTSLIST, ids);
};
int TransmRpcSession::stopTorrents(unsigned int *ids) {
return sendRequest(TORRENTSSTOP, ids);
};
int TransmRpcSession::startTorrents(unsigned int *ids) {
return sendRequest(TORRENTSSTART, ids);
};
std::string TransmRpcSession::generateJsonRequest(int tag, unsigned int *ids = NULL) {
std::ostringstream out;
int i;
unsigned int j;
std::string method;
switch(tag) {
case TORRENTSLIST:
method = "torrent-get";
break;
case TORRENTSSTOP:
method = "torrent-stop";
break;
case TORRENTSSTART:
method = "torrent-start";
}
out << "{ \"arguments\" : { ";
if(ids != NULL) {
out << "\"ids\" : [ ";
for(j=0;j<(sizeof(ids)/sizeof(int))-1;j++)
out << ids[j] << ", ";
out << ids[j] <<" ] ";
}
if(tag == TORRENTSLIST) {
if(ids != NULL)
out << ", ";
out << "\"fields\" : [ ";
for(i=0; i<Fields.size()-1;i++)
out << "\"" << Fields[i].toAscii().data() << "\", ";
out << "\"" << Fields[i].toAscii().data() << "\" ]";
}
out << " }, ";
out << "\"method\" : \"" << method << "\", ";
out << "\"tag\" : " << tag;
out << " } ";
// qDebug() << out.str().c_str();
return out.str();
}
void TransmRpcSession::dataReceived(bool error) {
if(error) {
QMessageBox::warning(parent, tr("TranRem"), tr("Error recieving data!"), QMessageBox::Ok );
//qDebug() << "Error recieving data!" << http->errorString();
emit errorSignal(dataRecievingError);
}
else {
switch(http->lastResponse().statusCode()) {
case 409:
transmSessionId = http->lastResponse().value("X-Transmission-Session-Id");
requestHeader.setValue("X-Transmission-Session-Id", transmSessionId);
response->close();
response->buffer().clear();
http->request(requestHeader, requestBody, response);//check this!!!
break;
case 200:
response->seek(0);
if(parseRequestData()) {
emit requestComplete();
/* //debug
qDebug() << "Tag: " << Tag << " Result: " << Result;
int i;
for(i=0;i<Torrents.size();i++)
qDebug() << "# " << i << " Status: " << Torrents.at(i).status();
//end debug */
}
else
emit errorSignal(parsingError);
break;
default:
QMessageBox::warning(parent, tr("TranRem"), tr("Response status code: %1").arg(http->lastResponse().statusCode()), QMessageBox::Ok );
//qDebug() << "Response status code: " << http->lastResponse().statusCode();
response->close();
response->buffer().clear();
emit errorSignal(connectionError);
}
};
};
bool TransmRpcSession::parseRequestData() {
Json::Reader reader;
Json::Value root;
Json::Value torrentsValue;
if(!reader.parse(response->buffer().data(), root)) {
QMessageBox::warning(parent, tr("TranRem"), tr("Error parsong JSON data!"), QMessageBox::Ok );
//qDebug() << "Error parsong JSON data!";
return false;
}
Result = root.get("result", "none").asString().c_str();
Tag = root.get("tag", "0").asUInt();
// qDebug() << "Respose: " << response->buffer().data();
if(Tag == TORRENTSLIST) {
torrentsValue = root["arguments"]["torrents"];
if(torrentsValue.isNull()) {
QMessageBox::warning(parent, tr("TranRem"), tr("Request doesn't contains 'torretns' part!"), QMessageBox::Ok );
//qDebug() << "Request doesn't contains 'torretns' part!";
return false;
}
Torrent *torrent;
Torrents.clear();
unsigned int i;
for(i=0;i<torrentsValue.size();i++) {
torrent = new Torrent(torrentsValue[i]);
Torrents.push_back(*torrent);
delete torrent;
}
}
return true;
};
std::vector<Torrent> *TransmRpcSession::torrents(){
return &Torrents;
};
unsigned int TransmRpcSession::tag() {
return Tag;
};
QString TransmRpcSession::result() {
return Result;
};
QStringList *TransmRpcSession::fields() {
return &Fields;
};
QString TransmRpcSession::h() {
return host;
};
QString TransmRpcSession::p() {
return port;
};
QString TransmRpcSession::u() {
return url;
};
Torrent TransmRpcSession::torrent(unsigned int id) {
unsigned int i;
for(i=0; i<Torrents.size(); i++)
if(Torrents.at(i).id() == id) return Torrents.at(i);
throw "Id not found.";
};