1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2013 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 | #ifdef Q_OS_WIN
|
---|
51 | qDebug("OSClient::search: hash: %s, file_size: %I64d", hash.toUtf8().constData(), file_size);
|
---|
52 | #else
|
---|
53 | qDebug("OSClient::search: hash: %s, file_size: %lld", hash.toUtf8().constData(), file_size);
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | search_hash = hash;
|
---|
57 | search_size = file_size;
|
---|
58 |
|
---|
59 | #if 0
|
---|
60 | if (logged_in) {
|
---|
61 | doSearch();
|
---|
62 | } else {
|
---|
63 | connect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
|
---|
64 | login();
|
---|
65 | }
|
---|
66 | #else
|
---|
67 | connect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
|
---|
68 | login();
|
---|
69 | #endif
|
---|
70 | }
|
---|
71 |
|
---|
72 | void OSClient::doSearch() {
|
---|
73 | qDebug("OSClient::doSearch");
|
---|
74 |
|
---|
75 | QVariantMap m;
|
---|
76 | m["sublanguageid"] = "all";
|
---|
77 | m["moviehash"] = search_hash;
|
---|
78 | m["moviebytesize"] = QString::number(search_size);
|
---|
79 |
|
---|
80 | // For some reason it seems opensubtitles fails
|
---|
81 | // sometimes if there's only one item in the list.
|
---|
82 | // So as workaround, the item is appended twice.
|
---|
83 |
|
---|
84 | // Update: and the opposite, sometimes it doesn't return any
|
---|
85 | // result with 2 items but it does with 1.
|
---|
86 | // Workaround: use 3 items... Seems to work in all cases.
|
---|
87 | QVariantList list;
|
---|
88 | list.append(m);
|
---|
89 | list.append(m);
|
---|
90 | list.append(m);
|
---|
91 |
|
---|
92 | QVariantList args;
|
---|
93 | args << token << QVariant(list);
|
---|
94 |
|
---|
95 | /*
|
---|
96 | for (int n=0; n < args.count(); n++) {
|
---|
97 | qDebug("%d = %d (%s)", n, args[n].type(), args[n].typeName());
|
---|
98 | }
|
---|
99 | */
|
---|
100 |
|
---|
101 | rpc->call("SearchSubtitles", args,
|
---|
102 | this, SLOT(responseSearch(QVariant &)),
|
---|
103 | this, SLOT(gotFault(int, const QString &)));
|
---|
104 | }
|
---|
105 |
|
---|
106 | void OSClient::responseLogin(QVariant &arg) {
|
---|
107 | qDebug("OSClient::responseLogin");
|
---|
108 |
|
---|
109 | QVariantMap m = arg.toMap();
|
---|
110 | QString status = m["status"].toString();
|
---|
111 | QString t = m["token"].toString();
|
---|
112 |
|
---|
113 | qDebug("OSClient::responseLogin: status: %s", status.toLatin1().constData());
|
---|
114 | qDebug("OSClient::responseLogin: token: %s", t.toLatin1().constData());
|
---|
115 |
|
---|
116 | if (status == "200 OK") {
|
---|
117 | token = t;
|
---|
118 | logged_in = true;
|
---|
119 | emit loggedIn();
|
---|
120 | } else {
|
---|
121 | emit loginFailed();
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | void OSClient::responseSearch(QVariant &arg) {
|
---|
126 | qDebug("OSClient::responseSearch");
|
---|
127 |
|
---|
128 | QVariantMap m = arg.toMap();
|
---|
129 | QString status = m["status"].toString();
|
---|
130 |
|
---|
131 | qDebug("OSClient::responseSearch: status: %s", status.toLatin1().constData());
|
---|
132 | //qDebug("count: %d", m.count());
|
---|
133 |
|
---|
134 | /*
|
---|
135 | QMapIterator<QString, QVariant> i(m);
|
---|
136 | while (i.hasNext()) {
|
---|
137 | i.next();
|
---|
138 | qDebug("key: %s", i.key().toLatin1().constData());
|
---|
139 | }
|
---|
140 | */
|
---|
141 |
|
---|
142 | if (status != "200 OK") {
|
---|
143 | emit searchFailed();
|
---|
144 | return;
|
---|
145 | }
|
---|
146 |
|
---|
147 | s_list.clear();
|
---|
148 |
|
---|
149 | QVariantList data = m["data"].toList();
|
---|
150 | qDebug("OSClient::responseSearch: data count: %d", data.count());
|
---|
151 |
|
---|
152 | for (int n = 0; n < data.count(); n++) {
|
---|
153 | OSSubtitle sub;
|
---|
154 |
|
---|
155 | //qDebug("%d: type: %d (%s)", n, data[n].type(), data[n].typeName());
|
---|
156 | QVariantMap m = data[n].toMap();
|
---|
157 |
|
---|
158 | sub.releasename = m["MovieReleaseName"].toString();
|
---|
159 | sub.movie = m["MovieName"].toString();
|
---|
160 | #ifdef USE_QUAZIP
|
---|
161 | sub.link = m["ZipDownloadLink"].toString();
|
---|
162 | #else
|
---|
163 | sub.link = m["SubDownloadLink"].toString();
|
---|
164 | #endif
|
---|
165 | sub.date = m["SubAddDate"].toString();
|
---|
166 | sub.iso639 = m["ISO639"].toString();
|
---|
167 | sub.rating = m["SubRating"].toString();
|
---|
168 | sub.comments = m["SubAuthorComment"].toString();
|
---|
169 | sub.format = m["SubFormat"].toString();
|
---|
170 | sub.language = m["LanguageName"].toString();
|
---|
171 | sub.user = m["UserNickName"].toString();
|
---|
172 | sub.files = "1";
|
---|
173 |
|
---|
174 | s_list.append(sub);
|
---|
175 |
|
---|
176 | /*
|
---|
177 | qDebug("MovieName: %s", sub.movie.toLatin1().constData());
|
---|
178 | qDebug("MovieReleaseName: %s", sub.releasename.toLatin1().constData());
|
---|
179 | //qDebug("SubFileName: %s", m["SubFileName"].toString().toLatin1().constData());
|
---|
180 | //qDebug("SubDownloadLink: %s", m["SubDownloadLink"].toString().toLatin1().constData());
|
---|
181 | qDebug("ZipDownloadLink: %s", sub.link.toLatin1().constData());
|
---|
182 | qDebug("SubAddDate: %s", sub.date.toLatin1().constData());
|
---|
183 | qDebug("ISO639: %s", sub.iso639.toLatin1().constData());
|
---|
184 | qDebug("SubRating: %s", sub.rating.toLatin1().constData());
|
---|
185 | qDebug("SubAuthorComment: %s", sub.comments.toLatin1().constData());
|
---|
186 | qDebug("SubFormat: %s", sub.format.toLatin1().constData());
|
---|
187 | qDebug("LanguageName: %s", sub.language.toLatin1().constData());
|
---|
188 | qDebug("UserNickName: %s", sub.user.toLatin1().constData());
|
---|
189 | qDebug("=======");
|
---|
190 | */
|
---|
191 | }
|
---|
192 |
|
---|
193 | emit searchFinished();
|
---|
194 | }
|
---|
195 |
|
---|
196 | void OSClient::gotFault(int error, const QString &message) {
|
---|
197 | qDebug("OSClient::gotFault: error: %d, message: %s", error, message.toUtf8().constData());
|
---|
198 | emit errorFound(error, message);
|
---|
199 | }
|
---|
200 |
|
---|
201 | #include "moc_osclient.cpp"
|
---|