1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "osclient.h"
|
---|
20 | #include "version.h"
|
---|
21 |
|
---|
22 | OSClient::OSClient(QObject* parent) : QObject(parent), logged_in(false), search_size(0) {
|
---|
23 | rpc = new MaiaXmlRpcClient(QUrl("http://api.opensubtitles.org/xml-rpc"), this);
|
---|
24 | }
|
---|
25 |
|
---|
26 | void OSClient::setServer(const QString & server) {
|
---|
27 | rpc->setUrl(QUrl(server));
|
---|
28 | }
|
---|
29 |
|
---|
30 | void OSClient::setProxy(const QNetworkProxy & proxy) {
|
---|
31 | rpc->setProxy(proxy);
|
---|
32 | }
|
---|
33 |
|
---|
34 | void OSClient::login() {
|
---|
35 | qDebug("OSClient::login");
|
---|
36 |
|
---|
37 | QString user_agent = "SMPlayer v" + Version::stable();
|
---|
38 | qDebug("OSClient::login: user agent: %s", user_agent.toUtf8().constData());
|
---|
39 |
|
---|
40 | QVariantList args;
|
---|
41 |
|
---|
42 | args << "" << "" << "" << user_agent;
|
---|
43 |
|
---|
44 | rpc->call("LogIn", args,
|
---|
45 | this, SLOT(responseLogin(QVariant &)),
|
---|
46 | this, SLOT(gotFault(int, const QString &)));
|
---|
47 | }
|
---|
48 |
|
---|
49 | void OSClient::search(const QString & hash, qint64 file_size) {
|
---|
50 | qDebug() << "OSClient::search: hash: " << hash << "file_size: " << file_size;
|
---|
51 |
|
---|
52 | search_hash = hash;
|
---|
53 | search_size = file_size;
|
---|
54 |
|
---|
55 | #if 0
|
---|
56 | if (logged_in) {
|
---|
57 | doSearch();
|
---|
58 | } else {
|
---|
59 | connect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
|
---|
60 | login();
|
---|
61 | }
|
---|
62 | #else
|
---|
63 | connect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
|
---|
64 | login();
|
---|
65 | #endif
|
---|
66 | }
|
---|
67 |
|
---|
68 | void OSClient::doSearch() {
|
---|
69 | qDebug("OSClient::doSearch");
|
---|
70 |
|
---|
71 | QVariantMap m;
|
---|
72 | m["sublanguageid"] = "all";
|
---|
73 | m["moviehash"] = search_hash;
|
---|
74 | m["moviebytesize"] = QString::number(search_size);
|
---|
75 |
|
---|
76 | // For some reason it seems opensubtitles fails
|
---|
77 | // sometimes if there's only one item in the list.
|
---|
78 | // So as workaround, the item is appended twice.
|
---|
79 |
|
---|
80 | // Update: and the opposite, sometimes it doesn't return any
|
---|
81 | // result with 2 items but it does with 1.
|
---|
82 | // Workaround: use 3 items... Seems to work in all cases.
|
---|
83 | QVariantList list;
|
---|
84 | list.append(m);
|
---|
85 | list.append(m);
|
---|
86 | list.append(m);
|
---|
87 | list.append(m);
|
---|
88 | //list.append(m);
|
---|
89 | list.append(m); // Adding more, sometimes it keeps failing...
|
---|
90 |
|
---|
91 | QVariantList args;
|
---|
92 | args << token << QVariant(list);
|
---|
93 |
|
---|
94 | /*
|
---|
95 | for (int n=0; n < args.count(); n++) {
|
---|
96 | qDebug("%d = %d (%s)", n, args[n].type(), args[n].typeName());
|
---|
97 | }
|
---|
98 | */
|
---|
99 |
|
---|
100 | rpc->call("SearchSubtitles", args,
|
---|
101 | this, SLOT(responseSearch(QVariant &)),
|
---|
102 | this, SLOT(gotFault(int, const QString &)));
|
---|
103 | }
|
---|
104 |
|
---|
105 | void OSClient::responseLogin(QVariant &arg) {
|
---|
106 | qDebug("OSClient::responseLogin");
|
---|
107 |
|
---|
108 | QVariantMap m = arg.toMap();
|
---|
109 | QString status = m["status"].toString();
|
---|
110 | QString t = m["token"].toString();
|
---|
111 |
|
---|
112 | qDebug("OSClient::responseLogin: status: %s", status.toLatin1().constData());
|
---|
113 | qDebug("OSClient::responseLogin: token: %s", t.toLatin1().constData());
|
---|
114 |
|
---|
115 | if (status == "200 OK") {
|
---|
116 | token = t;
|
---|
117 | logged_in = true;
|
---|
118 | emit loggedIn();
|
---|
119 | } else {
|
---|
120 | emit loginFailed();
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | void OSClient::responseSearch(QVariant &arg) {
|
---|
125 | qDebug("OSClient::responseSearch");
|
---|
126 |
|
---|
127 | QVariantMap m = arg.toMap();
|
---|
128 | QString status = m["status"].toString();
|
---|
129 |
|
---|
130 | qDebug("OSClient::responseSearch: status: %s", status.toLatin1().constData());
|
---|
131 | //qDebug("count: %d", m.count());
|
---|
132 |
|
---|
133 | /*
|
---|
134 | QMapIterator<QString, QVariant> i(m);
|
---|
135 | while (i.hasNext()) {
|
---|
136 | i.next();
|
---|
137 | qDebug("key: %s", i.key().toLatin1().constData());
|
---|
138 | }
|
---|
139 | */
|
---|
140 |
|
---|
141 | if (status != "200 OK") {
|
---|
142 | emit searchFailed();
|
---|
143 | return;
|
---|
144 | }
|
---|
145 |
|
---|
146 | s_list.clear();
|
---|
147 |
|
---|
148 | QVariantList data = m["data"].toList();
|
---|
149 | qDebug("OSClient::responseSearch: data count: %d", data.count());
|
---|
150 |
|
---|
151 | for (int n = 0; n < data.count(); n++) {
|
---|
152 | OSSubtitle sub;
|
---|
153 |
|
---|
154 | //qDebug("%d: type: %d (%s)", n, data[n].type(), data[n].typeName());
|
---|
155 | QVariantMap m = data[n].toMap();
|
---|
156 |
|
---|
157 | sub.releasename = m["MovieReleaseName"].toString();
|
---|
158 | sub.movie = m["MovieName"].toString();
|
---|
159 | #ifdef USE_QUAZIP
|
---|
160 | sub.link = m["ZipDownloadLink"].toString();
|
---|
161 | #else
|
---|
162 | sub.link = m["SubDownloadLink"].toString();
|
---|
163 | #endif
|
---|
164 | sub.date = m["SubAddDate"].toString();
|
---|
165 | sub.iso639 = m["ISO639"].toString();
|
---|
166 | sub.rating = m["SubRating"].toString();
|
---|
167 | sub.comments = m["SubAuthorComment"].toString();
|
---|
168 | sub.format = m["SubFormat"].toString();
|
---|
169 | sub.language = m["LanguageName"].toString();
|
---|
170 | sub.user = m["UserNickName"].toString();
|
---|
171 | sub.files = "1";
|
---|
172 |
|
---|
173 | s_list.append(sub);
|
---|
174 |
|
---|
175 | /*
|
---|
176 | qDebug("MovieName: %s", sub.movie.toLatin1().constData());
|
---|
177 | qDebug("MovieReleaseName: %s", sub.releasename.toLatin1().constData());
|
---|
178 | //qDebug("SubFileName: %s", m["SubFileName"].toString().toLatin1().constData());
|
---|
179 | //qDebug("SubDownloadLink: %s", m["SubDownloadLink"].toString().toLatin1().constData());
|
---|
180 | qDebug("ZipDownloadLink: %s", sub.link.toLatin1().constData());
|
---|
181 | qDebug("SubAddDate: %s", sub.date.toLatin1().constData());
|
---|
182 | qDebug("ISO639: %s", sub.iso639.toLatin1().constData());
|
---|
183 | qDebug("SubRating: %s", sub.rating.toLatin1().constData());
|
---|
184 | qDebug("SubAuthorComment: %s", sub.comments.toLatin1().constData());
|
---|
185 | qDebug("SubFormat: %s", sub.format.toLatin1().constData());
|
---|
186 | qDebug("LanguageName: %s", sub.language.toLatin1().constData());
|
---|
187 | qDebug("UserNickName: %s", sub.user.toLatin1().constData());
|
---|
188 | qDebug("=======");
|
---|
189 | */
|
---|
190 | }
|
---|
191 |
|
---|
192 | emit searchFinished();
|
---|
193 | }
|
---|
194 |
|
---|
195 | void OSClient::gotFault(int error, const QString &message) {
|
---|
196 | qDebug("OSClient::gotFault: error: %d, message: %s", error, message.toUtf8().constData());
|
---|
197 | emit errorFound(error, message);
|
---|
198 | }
|
---|
199 |
|
---|
200 | #include "moc_osclient.cpp"
|
---|