4 Utils::Utils(QmlApplicationViewer *viewer, QSettings *settings, QObject *parent) :
10 _nam = new QNetworkAccessManager(this);
14 void Utils::minimizeWindow()
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);
23 _viewer->setWindowState(Qt::WindowMinimized);
27 void Utils::setOrientation(const QString &orientation)
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);
37 bool Utils::isAuthorized()
39 QString ck = customerKey();
40 QString cs = customerSecret();
42 QString ts = tokenSecret();
44 if(ck!="" && cs!="" && t!="" && ts!="")
50 void Utils::resetAuthorization()
53 setCustomerSecret("");
61 return _settings->value("name").toString();
64 void Utils::setName(const QString &name)
66 _settings->setValue("name",name);
69 QString Utils::locale()
71 return _settings->value("locale").toString();
74 void Utils::setLocale(const QString &locale)
76 _settings->setValue("locale",locale);
79 QString Utils::customerKey()
81 return _settings->value("customer_key").toString();
84 void Utils::setCustomerKey(const QString &ckey)
86 _settings->setValue("customer_key",ckey);
89 QString Utils::customerSecret()
91 return _settings->value("customer_secret").toString();
94 void Utils::setCustomerSecret(const QString &csecret)
96 _settings->setValue("customer_secret",csecret);
99 QString Utils::token()
101 return _settings->value("token").toString();
104 void Utils::setToken(const QString &token)
106 _settings->setValue("token",token);
109 QString Utils::tokenSecret()
111 return _settings->value("token_secret").toString();
114 /*QString Utils::backgroundColor()
116 return _settings->value("background_color").toString();
119 void Utils::setTokenSecret(const QString &tsecret)
121 _settings->setValue("token_secret",tsecret);
124 QString Utils::lastFolder()
126 return _settings->value("last_folder").toString();
129 void Utils::setLastFolder(const QString &folder)
131 _settings->setValue("last_folder",folder);
134 /*void Utils::setBackgroundColor(const QString &color)
136 _settings->setValue("background_color",color);
140 void Utils::downloadFile(const QString &folder, const QString &filename,
141 const QString &url, int size, const QString &auth)
144 data.isDownload = true;
145 data.folder = folder;
146 data.filename = filename;
152 emit downloadAdded(filename);
162 //qDebug() << "quee.isEmpty";
166 RequestData data = quee.takeFirst();
169 QNetworkRequest req(url);
170 req.setRawHeader("Authorization", data.auth.toAscii());
174 QNetworkReply* reply = _nam->get(req);
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)));
181 cur_folder = data.folder;
182 cur_filename = data.filename;
183 cur_size = data.size;
187 emit downloadStarted(data.filename);
189 //qDebug() << "startDownload, filename: " << cur_filename;
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);
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)));
204 cur_folder = data.folder;
205 cur_filename = data.filename;
206 cur_size = cur_file->size();
210 emit uploadStarted(data.filename);
212 //qDebug() << "size:" << cur_file->size();
215 qDebug() << "error: file not open!";
221 void Utils::downloadFinished()
223 //qDebug() << "downloadFinished";
225 if (cur_reply->error() == QNetworkReply::NoError)
227 int httpStatus = cur_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
228 //qDebug() << "status: " << httpStatus;
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);
239 emit fileDownloaded(cur_filename);
241 qDebug() << "download error";
242 emit downloadError(cur_filename);
247 qDebug() << "download error";
248 emit downloadError(cur_filename);
252 cur_reply->deleteLater();
257 void Utils::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
259 //qDebug() << "progres: " << 100*bytesReceived/cur_size << "%";
261 float progress = (float)bytesReceived/cur_size;
263 emit fileDownloadProgress(cur_filename,progress);
266 void Utils::error(QNetworkReply::NetworkError code)
268 qDebug() << "error: " << code;
271 void Utils::uploadFile(const QString &folder, const QString &filename,
272 const QString &url, const QString &auth)
274 //qDebug() << "uploadFile";
277 data.isDownload = false;
278 data.folder = folder;
279 data.filename = filename;
284 emit uploadAdded(filename);
291 void Utils::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
293 //qDebug() << "progres: " << 100*bytesSent/cur_size << "%";
294 //qDebug() << "progres: " << 100*bytesSent/bytesTotal << "%";
296 float progress = (float)bytesSent/cur_size;
297 emit fileUploadProgress(cur_filename,progress);
300 void Utils::uploadFinished()
302 //qDebug() << "uploadFinished";
304 if (cur_reply->error() == QNetworkReply::NoError)
306 emit fileUploaded(cur_filename);
310 int httpStatus = cur_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
311 //QString httpStatusMessage = cur_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
312 //QByteArray bytes = cur_reply->readAll();
314 //qDebug() << "status: " << httpStatus << " " << httpStatusMessage;
315 //qDebug() << "bytes: " << bytes;
319 emit fileUploaded(cur_filename);
321 qDebug() << "upload error";
322 emit uploadError(cur_filename);
330 cur_reply->deleteLater();
335 bool Utils::emptyQuee()
337 return isFinished && quee.length()==0;
343 msgBox.setText("Test!");
347 void Utils::deleteFile(const QString &url, const QString &auth)
349 //qDebug() << "Utils::deleteFile";
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)));
360 void Utils::deleteFinished()
362 //qDebug() << "Utils::deleteFinished";
364 int httpStatus = temp_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
365 QString httpStatusMessage = temp_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
366 QByteArray bytes = temp_reply->readAll();
368 //qDebug() << "status: " << httpStatus << " " << httpStatusMessage;
369 //qDebug() << "bytes: " << bytes;
371 if (temp_reply->error() == QNetworkReply::NoError)
377 emit operationError(httpStatus);
381 temp_reply->deleteLater();