2 * Copyright (c) 2010 Kaushal M <kshlmster@gmail.com>
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.
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.
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.
19 #include <QNetworkAccessManager>
20 #include <QNetworkRequest>
21 #include <QNetworkReply>
23 #include <QCryptographicHash>
31 #include <QDomDocument>
32 #include <QNetworkProxy>
34 #include "slideshare.h"
35 #include "slideshareDocument.h"
36 #include "mimetypes.h"
38 SlideShare::SlideShare(QObject *parent):QObject(parent)
40 manager.setCookieJar(&this->cookieJar);
41 doc = new SlideShareDocument;
44 SlideShare::SlideShare(QString *username, QString *password, QObject *parent):QObject(parent)
46 this->username = username;
47 this->password = password;
50 SlideShare::SlideShare(QString *username, QString *password, QString *apiKey, QString *secretKey, QObject *parent):QObject(parent)
52 this->apiKey = apiKey;
53 this->secretKey = secretKey;
54 SlideShare(username, password);
57 SlideShare::~SlideShare()
61 void SlideShare::setUsername(QString *username)
63 this->username = username;
66 void SlideShare::setPassword(QString *password)
68 this->password = password;
71 void SlideShare::setApikey(QString *apiKey)
73 this->apiKey = apiKey;
76 void SlideShare::setsecretKey(QString *secretKey)
78 this->secretKey = secretKey;
81 void SlideShare::setSlideTitle(QString *title)
86 void SlideShare::setDescription(QString *description)
88 doc->description = *description;
91 void SlideShare::setTags(QString *tags)
96 void SlideShare::setFormat(QString *format)
98 doc->format = *format;
101 void SlideShare::setSourceFile(QString *filename)
103 this->sourceFilename = filename;
106 void SlideShare::setSaveFileName(QString *saveFileName)
108 this->saveFileName = saveFileName;
112 void SlideShare::login()
114 QUrl *login = new QUrl(QString("http://www.slideshare.net/login"));
116 data.append(QString("user_login=").append(username).toUtf8());
117 data.append(QString("&user_password=").append(password).toUtf8());
119 QNetworkRequest request;
120 request.setUrl(*login);
122 reply = manager.post(request, data);
123 connect(reply,SIGNAL(finished()), this, SLOT(afterLogin()));
127 void SlideShare::afterLogin()
129 if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302)
131 if(QString("http://www.slideshare.net/").append(username) == reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString())
132 emit loginDone(true);
135 emit loginDone(false);
138 void SlideShare::download(QString *durl)
140 QUrl *dload = new QUrl(*durl) ;
141 QNetworkRequest request;
142 request.setUrl(*dload);
144 QList<QNetworkCookie> list = cookieJar.cookiesForUrl(QUrl("http://www.slideshare.net/"));
146 foreach(QNetworkCookie c, list)
148 if(c.name() == "_cookie_id")
150 request.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(list));
154 reply = manager.get(request);
155 connect(reply,SIGNAL(finished()),this,SLOT(saveFile()));
158 void SlideShare::saveFile()
160 if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302)
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()));
168 QFile file(*saveFileName);
169 file.open(QFile::WriteOnly);
171 file.write(reply->readAll());
178 void SlideShare::upload()
180 QDateTime *time = new QDateTime();
182 ts.setNum(time->toTime_t());
184 QByteArray data = this->secretKey->toAscii();
187 QFile *file = new QFile(*this->sourceFilename,this);
188 file->open(QFile::ReadOnly);
190 QByteArray fileData ;
191 fileData = file->readAll();
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");
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 ));
214 params.append(qMakePair(QString("slideshow_tags"), doc->tags));
216 QUrl url("http://www.slideshare.net/api/2/upload_slideshow", QUrl::TolerantMode);
217 url.setQueryItems(params);
218 QNetworkRequest request;
220 request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=BOUNDARY");
221 request.setHeader(QNetworkRequest::ContentLengthHeader, postData.length());
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()));
229 void SlideShare::listDocuments()
231 QDateTime *time = new QDateTime(QDateTime::currentDateTime());
233 ts.setNum(time->toTime_t());
235 QByteArray data = this->secretKey->toAscii();
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));
247 QUrl url("http://www.slideshare.net/api/2/get_slideshows_by_user");
248 url.setQueryItems(params);
251 QNetworkRequest request;
254 reply = manager.get(request);
255 connect(reply,SIGNAL(finished()), this, SLOT(parseList()));
257 void SlideShare::parseList()
260 QByteArray data = reply->readAll();
261 doc.setContent(data);
263 QDomElement root = doc.documentElement();
264 QDomElement child = root.firstChildElement("Slideshow");
267 presentationList.clear();
268 spreadsheetList.clear();
270 while(!child.isNull())
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();
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);
292 child = child.nextSiblingElement("Slideshow");