Initial Commit.
[onlineservices] / 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     QUrl *login = new QUrl(QString("http://www.slideshare.net/login"));
115     QByteArray data;
116     data.append(QString("user_login=").append(username).toUtf8());
117     data.append(QString("&user_password=").append(password).toUtf8());
118
119     QNetworkRequest request;
120     request.setUrl(*login);
121
122     reply = manager.post(request, data);
123     connect(reply,SIGNAL(finished()), this, SLOT(afterLogin()));
124 }
125
126
127 void SlideShare::afterLogin()
128 {
129     if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302)
130     {
131         if(QString("http://www.slideshare.net/").append(username) == reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString())
132             emit loginDone(true);
133     }
134     else
135         emit loginDone(false);
136 }
137
138 void SlideShare::download(QString *durl)
139 {
140     QUrl *dload = new QUrl(*durl) ;
141     QNetworkRequest request;
142     request.setUrl(*dload);
143
144     QList<QNetworkCookie> list = cookieJar.cookiesForUrl(QUrl("http://www.slideshare.net/"));
145
146     foreach(QNetworkCookie c, list)
147     {
148         if(c.name() == "_cookie_id")
149         {
150             request.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(list));
151         }
152     }
153
154     reply = manager.get(request);
155     connect(reply,SIGNAL(finished()),this,SLOT(saveFile()));
156
157 }
158 void SlideShare::saveFile()
159 {
160     if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302)
161     {
162         reply = manager.get(QNetworkRequest(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl()));
163         connect(reply,SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(downloadProgress(qint64,qint64)));
164         connect(reply,SIGNAL(finished()), this, SLOT(saveFile()));
165         return;
166     }
167
168     QFile file(*saveFileName);
169     file.open(QFile::WriteOnly);
170
171     file.write(reply->readAll());
172     file.close();
173
174
175     emit downloadDone();
176 }
177
178 void SlideShare::upload()
179 {
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     QDateTime *time = new QDateTime(QDateTime::currentDateTime());
232     QString ts;
233     ts.setNum(time->toTime_t());
234
235     QByteArray data = this->secretKey->toAscii();
236     data += ts;
237
238     QList<QPair<QString, QString> > params;
239     params.append(qMakePair(QString("api_key"),*this->apiKey));
240     params.append(qMakePair(QString("ts"),ts));
241     params.append(qMakePair(QString("hash"),QString(QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex())));
242     params.append(qMakePair(QString("username"),*this->username));
243     params.append(qMakePair(QString("password"),*this->password));
244     params.append(qMakePair(QString("username_for"),*this->username));
245
246
247     QUrl url("http://www.slideshare.net/api/2/get_slideshows_by_user");
248     url.setQueryItems(params);
249
250
251     QNetworkRequest request;
252     request.setUrl(url);
253
254     reply = manager.get(request);
255     connect(reply,SIGNAL(finished()), this, SLOT(parseList()));
256 }
257 void SlideShare::parseList()
258 {
259     QDomDocument doc;
260     QByteArray data = reply->readAll();
261     doc.setContent(data);
262
263     QDomElement root = doc.documentElement();
264     QDomElement child = root.firstChildElement("Slideshow");
265
266     textDocList.clear();
267     presentationList.clear();
268     spreadsheetList.clear();
269
270     while(!child.isNull())
271     {
272         SlideShareDocument *p = new SlideShareDocument;
273         p->title = child.firstChildElement("Title").text();
274         p->url = child.firstChildElement("URL").text();
275         p->format = child.firstChildElement("Format").text();
276         p->slideID = child.firstChildElement("ID").text();
277         p->thumbnailBig = child.firstChildElement("ThumbnailUrl").text();
278         p->thumbnailSmall = child.firstChildElement("ThumbnailSmallURL").text();
279         p->downloadUrl = p->url.append("/download");
280         QString download = child.firstChildElement("Download").text();
281
282         if(download == "1")
283         {
284             if (p->format == "odt" || p->format == "doc")
285                 this->textDocList.append(*p);
286             if (p->format == "odp" || p->format == "ppt")
287                 this->presentationList.append(*p);
288             if (p->format == "ods" || p->format == "xls")
289                 this->spreadsheetList.append(*p);
290         }
291
292         child = child.nextSiblingElement("Slideshow");
293     }
294     emit listDone();
295 }
296