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