Added libxi-dev dependency
[froff-onlinedoc] / slideshare.cpp
1 /*
2  *  Copyright (c) 2010 Kaushal M <kshlmster@gmail.com>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <QNetworkAccessManager>
20 #include <QNetworkRequest>
21 #include <QNetworkReply>
22 #include <QDateTime>
23 #include <QCryptographicHash>
24 #include <QHash>
25 #include <QDebug>
26 #include <QByteArray>
27 #include <QList>
28 #include <QPair>
29 #include <QFile>
30 #include <QFileInfo>
31 #include <QDomDocument>
32 #include <QNetworkProxy>
33
34 #include "slideshare.h"
35 #include "slideshareDocument.h"
36 #include "mimetypes.h"
37
38 SlideShare::SlideShare(QObject *parent): QObject(parent)
39 {
40     manager.setCookieJar(&this->cookieJar);
41     doc = new SlideShareDocument;
42 }
43
44 SlideShare::SlideShare(QString *username, QString *password, QObject *parent): QObject(parent)
45 {
46     this->username = username;
47     this->password = password;
48     SlideShare();
49 }
50 SlideShare::SlideShare(QString *username, QString *password, QString *apiKey, QString *secretKey, QObject *parent): QObject(parent)
51 {
52     this->apiKey = apiKey;
53     this->secretKey = secretKey;
54     SlideShare(username, password);
55 }
56
57 SlideShare::~SlideShare()
58 {
59 }
60
61 void SlideShare::setUsername(QString *username)
62 {
63     this->username = username;
64 }
65
66 void SlideShare::setPassword(QString *password)
67 {
68     this->password = password;
69 }
70
71 void SlideShare::setApikey(QString *apiKey)
72 {
73     this->apiKey = apiKey;
74 }
75
76 void SlideShare::setsecretKey(QString *secretKey)
77 {
78     this->secretKey = secretKey;
79 }
80
81 void SlideShare::setSlideTitle(QString *title)
82 {
83     doc->title = *title;
84 }
85
86 void SlideShare::setDescription(QString *description)
87 {
88     doc->description = *description;
89 }
90
91 void SlideShare::setTags(QString *tags)
92 {
93     doc->tags = *tags;
94 }
95
96 void SlideShare::setFormat(QString *format)
97 {
98     doc->format = *format;
99 }
100
101 void SlideShare::setSourceFile(QString *filename)
102 {
103     this->sourceFilename = filename;
104 }
105
106 void SlideShare::setSaveFileName(QString *saveFileName)
107 {
108     this->saveFileName = saveFileName;
109 }
110
111
112 void SlideShare::login()
113 {
114     qDebug() << ">>>>>>>>> SlideShare::login()";
115     QUrl *login = new QUrl(QString("http://www.slideshare.net/login"));
116     QByteArray data;
117     data.append(QString("user_login=").append(username).toUtf8());
118     data.append(QString("&user_password=").append(password).toUtf8());
119
120     QNetworkRequest request;
121     request.setUrl(*login);
122
123     reply = manager.post(request, data);
124     connect(reply, SIGNAL(finished()), this, SLOT(afterLogin()));
125 }
126
127
128 void SlideShare::afterLogin()
129 {
130     qDebug() << ">>>>>>>>> SlideShare::afterLogin()";
131     if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302) {
132         if(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString().contains(*username))
133             emit loginDone(true);
134     } else
135         emit loginDone(false);
136 }
137
138 void SlideShare::download(QString *durl)
139 {
140     qDebug() << ">>>>>>>>> SlideShare::download()";
141     QUrl *dload = new QUrl(*durl) ;
142     QNetworkRequest request;
143     request.setUrl(*dload);
144
145     QList<QNetworkCookie> list = cookieJar.cookiesForUrl(QUrl("http://www.slideshare.net/"));
146
147     foreach(QNetworkCookie c, list) {
148         if(c.name() == "_cookie_id") {
149             request.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(list));
150         }
151     }
152
153     reply = manager.get(request);
154     connect(reply, SIGNAL(finished()), this, SLOT(saveFile()));
155
156 }
157 void SlideShare::saveFile()
158 {
159     qDebug() << ">>>>>>>>> SlideShare::saveFile()";
160     if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302) {
161         reply = manager.get(QNetworkRequest(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl()));
162         connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SIGNAL(downloadProgress(qint64, qint64)));
163         connect(reply, SIGNAL(finished()), this, SLOT(saveFile()));
164         return;
165     }
166
167     QFile file(*saveFileName);
168     file.open(QFile::WriteOnly);
169
170     file.write(reply->readAll());
171     file.close();
172
173
174     emit downloadDone();
175 }
176
177 void SlideShare::upload()
178 {
179     qDebug() << ">>>>>>>>> SlideShare::upload()";
180     QDateTime *time = new QDateTime();
181     QString ts;
182     ts.setNum(time->toTime_t());
183
184     QByteArray data = this->secretKey->toAscii();
185     data += ts;
186
187     QFile *file = new QFile(*this->sourceFilename, this);
188     file->open(QFile::ReadOnly);
189
190     QByteArray fileData ;
191     fileData = file->readAll();
192
193     QByteArray postData;
194     postData.append("--BOUNDARY\r\n");
195     postData.append("Content-Disposition: form-data; name=\"slideshow_srcfile\"; filename=\"");
196     postData.append(file->fileName().toUtf8());
197     postData.append("\"\r\n");
198     postData.append("Content-Type: " + MimeTypes::getMimeType(doc->format));
199     postData.append("\r\n\r\n");
200     postData.append(fileData);
201     postData.append("\r\n--BOUNDARY\r\n");
202
203
204     QList<QPair<QString, QString> > params;
205     params.append(qMakePair(QString("api_key"), *this->apiKey));
206     params.append(qMakePair(QString("ts"), ts));
207     params.append(qMakePair(QString("hash"), QString(QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex())));
208     params.append(qMakePair(QString("username"), *this->username));
209     params.append(qMakePair(QString("password"), *this->password));
210     params.append(qMakePair(QString("slideshow_title"), doc->title));
211     if(doc->description != "")
212         params.append(qMakePair(QString("slideshow_description"), doc->description));
213     if(doc->tags != "")
214         params.append(qMakePair(QString("slideshow_tags"), doc->tags));
215
216     QUrl url("http://www.slideshare.net/api/2/upload_slideshow", QUrl::TolerantMode);
217     url.setQueryItems(params);
218     QNetworkRequest request;
219     request.setUrl(url);
220     request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=BOUNDARY");
221     request.setHeader(QNetworkRequest::ContentLengthHeader, postData.length());
222
223
224     QNetworkReply *reply = manager.post(request, postData);
225     connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SIGNAL(uploadProgress(qint64, qint64)));
226     connect(reply, SIGNAL(finished()), this, SIGNAL(uploadDone()));
227 }
228
229 void SlideShare::listDocuments()
230 {
231     qDebug() << ">>>>>>>>> SlideShare::listDocuments()";
232     QDateTime *time = new QDateTime(QDateTime::currentDateTime());
233     QString ts;
234     ts.setNum(time->toTime_t());
235
236     QByteArray data = this->secretKey->toAscii();
237     data += ts;
238
239     QList<QPair<QString, QString> > params;
240     params.append(qMakePair(QString("api_key"), *this->apiKey));
241     params.append(qMakePair(QString("ts"), ts));
242     params.append(qMakePair(QString("hash"), QString(QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex())));
243     params.append(qMakePair(QString("username"), *this->username));
244     params.append(qMakePair(QString("password"), *this->password));
245     params.append(qMakePair(QString("username_for"), *this->username));
246
247
248     QUrl url("http://www.slideshare.net/api/2/get_slideshows_by_user");
249     url.setQueryItems(params);
250
251
252     QNetworkRequest request;
253     request.setUrl(url);
254
255     reply = manager.get(request);
256     connect(reply, SIGNAL(finished()), this, SLOT(parseList()));
257 }
258 void SlideShare::parseList()
259 {
260     qDebug() << ">>>>>>>>> SlideShare::parseList()" ;
261     QDomDocument doc;
262     QByteArray data = reply->readAll();
263     doc.setContent(data);
264
265     QDomElement root = doc.documentElement();
266     QDomElement child = root.firstChildElement("Slideshow");
267
268     textDocList.clear();
269     presentationList.clear();
270     spreadsheetList.clear();
271
272     while(!child.isNull()) {
273         SlideShareDocument *p = new SlideShareDocument;
274         p->title = child.firstChildElement("Title").text();
275         p->url = child.firstChildElement("URL").text();
276         p->format = child.firstChildElement("Format").text();
277         p->slideID = child.firstChildElement("ID").text();
278         p->thumbnailBig = child.firstChildElement("ThumbnailUrl").text();
279         p->thumbnailSmall = child.firstChildElement("ThumbnailSmallURL").text();
280         p->downloadUrl = p->url.append("/download");
281         QString download = child.firstChildElement("Download").text();
282         if(download == "1") {
283             if(p->format == "odt" || p->format == "doc")
284                 this->textDocList.append(*p);
285             if(p->format == "odp" || p->format == "ppt")
286                 this->presentationList.append(*p);
287             if(p->format == "ods" || p->format == "xls")
288                 this->spreadsheetList.append(*p);
289         }
290
291         child = child.nextSiblingElement("Slideshow");
292     }
293     emit listDone();
294 }
295