Update WWW
[ubi] / utils.cpp
1 #include "utils.h"
2 #include <QDebug>
3
4 Utils::Utils(QmlApplicationViewer *viewer, QSettings *settings, QObject *parent) :
5     QObject(parent)
6 {
7     _viewer = viewer;
8     _settings = settings;
9     _clipboard = QApplication::clipboard();
10
11     _nam = new QNetworkAccessManager(this);
12     isFinished = true;
13 }
14
15 void Utils::minimizeWindow()
16 {
17     #if defined(Q_WS_MAEMO_5)
18         // This is needed for Maemo5 to recognize minimization
19         QDBusConnection connection = QDBusConnection::sessionBus();
20         QDBusMessage message = QDBusMessage::createSignal(
21                     "/","com.nokia.hildon_desktop","exit_app_view");
22         connection.send(message);
23     #else
24         _viewer->setWindowState(Qt::WindowMinimized);
25     #endif
26 }
27
28 void Utils::setOrientation(const QString &orientation)
29 {
30     if(orientation=="auto")
31         _viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
32     if(orientation=="landscape")
33         _viewer->setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
34     if(orientation=="portrait")
35         _viewer->setOrientation(QmlApplicationViewer::ScreenOrientationLockPortrait);
36 }
37
38 bool Utils::isAuthorized()
39 {
40     QString ck = customerKey();
41     QString cs = customerSecret();
42     QString t = token();
43     QString ts = tokenSecret();
44
45     if(ck!="" && cs!="" && t!="" && ts!="")
46         return true;
47     else
48         return false;
49 }
50
51 void Utils::resetAuthorization()
52 {
53     setCustomerKey("");
54     setCustomerSecret("");
55     setToken("");
56     setTokenSecret("");
57     setName("");
58 }
59
60 QString Utils::name()
61 {
62     return _settings->value("name").toString();
63 }
64
65 void Utils::setName(const QString &name)
66 {
67     _settings->setValue("name",name);
68 }
69
70 QString Utils::locale()
71 {
72     return _settings->value("locale").toString();
73 }
74
75 void Utils::setLocale(const QString &locale)
76 {
77     _settings->setValue("locale",locale);
78 }
79
80 QString Utils::customerKey()
81 {
82     return _settings->value("customer_key").toString();
83 }
84
85 void Utils::setCustomerKey(const QString &ckey)
86 {
87     _settings->setValue("customer_key",ckey);
88 }
89
90 QString Utils::customerSecret()
91 {
92     return _settings->value("customer_secret").toString();
93 }
94
95 void Utils::setCustomerSecret(const QString &csecret)
96 {
97     _settings->setValue("customer_secret",csecret);
98 }
99
100 QString Utils::token()
101 {
102     return _settings->value("token").toString();
103 }
104
105 void Utils::setToken(const QString &token)
106 {
107     _settings->setValue("token",token);
108 }
109
110 QString Utils::tokenSecret()
111 {
112     return _settings->value("token_secret").toString();
113 }
114
115 /*QString Utils::backgroundColor()
116 {
117     return _settings->value("background_color").toString();
118 }*/
119
120 void Utils::setTokenSecret(const QString &tsecret)
121 {
122     _settings->setValue("token_secret",tsecret);
123 }
124
125 QString Utils::lastFolder()
126 {
127     return _settings->value("last_folder").toString();
128 }
129
130 void Utils::setLastFolder(const QString &folder)
131 {
132     _settings->setValue("last_folder",folder);
133 }
134
135 /*void Utils::setBackgroundColor(const QString &color)
136 {
137     _settings->setValue("background_color",color);
138 }*/
139
140
141 void Utils::downloadFile(const QString &folder, const QString &filename,
142                          const QString &url, int size, const QString &auth)
143 {
144     RequestData data;
145     data.isDownload = true;
146     data.folder = folder;
147     data.filename = filename;
148     data.url = url;
149     data.auth = auth;
150     data.size = size;
151     quee.append(data);
152
153     emit downloadAdded(filename);
154
155     if(isFinished){
156         start();
157     }
158 }
159
160 void Utils::start()
161 {
162     if(quee.isEmpty()) {
163         //qDebug() << "quee.isEmpty";
164         return;
165     }
166
167     RequestData data = quee.takeFirst();
168
169     QUrl url(data.url);
170     QNetworkRequest req(url);
171     req.setRawHeader("Authorization", data.auth.toAscii());
172
173     if(data.isDownload)
174     {
175         QNetworkReply* reply = _nam->get(req);
176
177         connect(reply,SIGNAL(finished()),this,SLOT(downloadFinished()));
178         connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadProgress(qint64,qint64)));
179         connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
180
181         cur_reply = reply;
182         cur_folder = data.folder;
183         cur_filename = data.filename;
184         cur_size = data.size;
185
186         isFinished = false;
187
188         emit downloadStarted(data.filename);
189
190         //qDebug() << "startDownload, filename: " << cur_filename;
191     }
192     else
193     {
194         QString filepath = data.folder + "/" + data.filename;
195         //qDebug() << "filapath: " << filepath;
196         cur_file = new QFile(filepath);
197         if(cur_file->open(QIODevice::ReadOnly)) {
198             QNetworkReply* reply = _nam->put(req,cur_file);
199
200             connect(reply,SIGNAL(finished()),this,SLOT(uploadFinished()));
201             connect(reply,SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
202             connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
203
204             cur_reply = reply;
205             cur_folder = data.folder;
206             cur_filename = data.filename;
207             cur_size = cur_file->size();
208
209             isFinished = false;
210
211             emit uploadStarted(data.filename);
212
213             //qDebug() << "size:" << cur_file->size();
214
215         } else {
216             qDebug() << "error: file not open!";
217         }
218
219     }
220 }
221
222 void Utils::downloadFinished()
223 {
224     //qDebug() << "downloadFinished";
225     isFinished = true;
226     if (cur_reply->error() == QNetworkReply::NoError)
227     {
228         int httpStatus = cur_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
229         //qDebug() << "status: " << httpStatus;
230
231         if(httpStatus<300) {
232             QByteArray bytes = cur_reply->readAll();
233             QString filepath = cur_folder + "/" + cur_filename;
234             QFile file(filepath);
235             file.open(QIODevice::WriteOnly);
236             int len = bytes.length();
237             QDataStream out(&file);
238             out.writeRawData(bytes.constData(),len);
239             file.close();
240             emit fileDownloaded(cur_filename);
241         } else {
242             qDebug() << "download error";
243             emit downloadError(cur_filename);
244         }
245     }
246     else
247     {
248         qDebug() << "download error";
249         emit downloadError(cur_filename);
250     }
251
252     cur_reply->close();
253     cur_reply->deleteLater();
254
255     start();
256 }
257
258 void Utils::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
259 {
260     //qDebug() << "progres: " << 100*bytesReceived/cur_size << "%";
261
262     float progress = (float)bytesReceived/cur_size;
263
264     emit fileDownloadProgress(cur_filename,progress);
265 }
266
267 void Utils::error(QNetworkReply::NetworkError code)
268 {
269     qDebug() << "error: " << code;
270 }
271
272 void Utils::uploadFile(const QString &folder, const QString &filename,
273                        const QString &url, const QString &auth)
274 {
275     //qDebug() << "uploadFile";
276
277     RequestData data;
278     data.isDownload = false;
279     data.folder = folder;
280     data.filename = filename;
281     data.url = url;
282     data.auth = auth;
283     quee.append(data);
284
285     emit uploadAdded(filename);
286
287     if(isFinished){
288         start();
289     }
290 }
291
292 void Utils::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
293 {
294     //qDebug() << "progres: " << 100*bytesSent/cur_size << "%";
295     //qDebug() << "progres: " << 100*bytesSent/bytesTotal << "%";
296
297     float progress = (float)bytesSent/cur_size;
298     emit fileUploadProgress(cur_filename,progress);
299 }
300
301 void Utils::uploadFinished()
302 {
303     //qDebug() << "uploadFinished";
304     isFinished = true;
305     if (cur_reply->error() == QNetworkReply::NoError)
306     {
307         emit fileUploaded(cur_filename);
308     }
309     else
310     {
311         int httpStatus = cur_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
312         //QString httpStatusMessage = cur_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
313         //QByteArray bytes = cur_reply->readAll();
314
315         //qDebug() << "status: " << httpStatus << " " << httpStatusMessage;
316         //qDebug() << "bytes: " << bytes;
317
318         if(httpStatus==503)
319         {
320             emit fileUploaded(cur_filename);
321         } else {
322             qDebug() << "upload error";
323             emit uploadError(cur_filename);
324         }
325
326     }
327
328     cur_file->close();
329     delete cur_file;
330     cur_reply->close();
331     cur_reply->deleteLater();
332
333     start();
334 }
335
336 bool Utils::emptyQuee()
337 {
338     return isFinished && quee.length()==0;
339 }
340
341 void Utils::test()
342 {
343     QMessageBox msgBox;
344     msgBox.setText("Test!");
345     msgBox.exec();
346 }
347
348 void Utils::deleteFile(const QString &url, const QString &auth)
349 {
350     //qDebug() << "Utils::deleteFile";
351
352     QUrl _url(url);
353     QNetworkRequest req(_url);
354     //qDebug() << "auth: " << auth;
355     req.setRawHeader("Authorization", auth.toAscii());
356     temp_reply = _nam->deleteResource(req);
357     connect(temp_reply,SIGNAL(finished()),this,SLOT(deleteFinished()));
358     connect(temp_reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
359 }
360
361 void Utils::deleteFinished()
362 {
363     //qDebug() << "Utils::deleteFinished";
364
365     int httpStatus = temp_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
366     QString httpStatusMessage = temp_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
367     QByteArray bytes = temp_reply->readAll();
368
369     //qDebug() << "status: " << httpStatus << " " << httpStatusMessage;
370     //qDebug() << "bytes: " << bytes;
371
372     if (temp_reply->error() == QNetworkReply::NoError)
373     {
374         emit fileDeleted();
375     }
376     else
377     {
378         emit operationError(httpStatus);
379     }
380
381     temp_reply->close();
382     temp_reply->deleteLater();
383 }
384
385 void Utils::setClipboardText(const QString &text)
386 {
387     _clipboard->setText(text, QClipboard::Clipboard);
388     _clipboard->setText(text, QClipboard::Selection);
389 }