4 Utils::Utils(QmlApplicationViewer *viewer, QSettings *settings, QObject *parent) :
9 _clipboard = QApplication::clipboard();
11 _nam = new QNetworkAccessManager(this);
15 void Utils::minimizeWindow()
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);
24 _viewer->setWindowState(Qt::WindowMinimized);
28 void Utils::setOrientation(const QString &orientation)
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);
38 bool Utils::isAuthorized()
40 QString ck = customerKey();
41 QString cs = customerSecret();
43 QString ts = tokenSecret();
45 if(ck!="" && cs!="" && t!="" && ts!="")
51 void Utils::resetAuthorization()
54 setCustomerSecret("");
62 return _settings->value("name").toString();
65 void Utils::setName(const QString &name)
67 _settings->setValue("name",name);
70 QString Utils::locale()
72 return _settings->value("locale").toString();
75 void Utils::setLocale(const QString &locale)
77 _settings->setValue("locale",locale);
80 QString Utils::customerKey()
82 return _settings->value("customer_key").toString();
85 void Utils::setCustomerKey(const QString &ckey)
87 _settings->setValue("customer_key",ckey);
90 QString Utils::customerSecret()
92 return _settings->value("customer_secret").toString();
95 void Utils::setCustomerSecret(const QString &csecret)
97 _settings->setValue("customer_secret",csecret);
100 QString Utils::token()
102 return _settings->value("token").toString();
105 void Utils::setToken(const QString &token)
107 _settings->setValue("token",token);
110 QString Utils::tokenSecret()
112 return _settings->value("token_secret").toString();
115 /*QString Utils::backgroundColor()
117 return _settings->value("background_color").toString();
120 void Utils::setTokenSecret(const QString &tsecret)
122 _settings->setValue("token_secret",tsecret);
125 QString Utils::lastFolder()
127 return _settings->value("last_folder").toString();
130 void Utils::setLastFolder(const QString &folder)
132 _settings->setValue("last_folder",folder);
135 /*void Utils::setBackgroundColor(const QString &color)
137 _settings->setValue("background_color",color);
141 void Utils::downloadFile(const QString &folder, const QString &filename,
142 const QString &url, int size, const QString &auth)
145 data.isDownload = true;
146 data.folder = folder;
147 data.filename = filename;
153 emit downloadAdded(filename);
163 //qDebug() << "quee.isEmpty";
167 RequestData data = quee.takeFirst();
170 QNetworkRequest req(url);
171 //qDebug() << "Authorization: " << data.auth;
172 //qDebug() << "Url: " << url.toEncoded();
173 req.setRawHeader("Authorization", data.auth.toAscii());
177 QNetworkReply* reply = _nam->get(req);
179 connect(reply,SIGNAL(finished()),this,SLOT(downloadFinished()));
180 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadProgress(qint64,qint64)));
181 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
184 cur_folder = data.folder;
185 cur_filename = data.filename;
186 cur_size = data.size;
190 emit downloadStarted(data.filename);
192 //qDebug() << "startDownload, filename: " << cur_filename;
196 QString filepath = data.folder + "/" + data.filename;
197 //qDebug() << "filapath: " << filepath;
198 cur_file = new QFile(filepath);
199 if(cur_file->open(QIODevice::ReadOnly)) {
200 QNetworkReply* reply = _nam->put(req,cur_file);
202 connect(reply,SIGNAL(finished()),this,SLOT(uploadFinished()));
203 connect(reply,SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
204 connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
207 cur_folder = data.folder;
208 cur_filename = data.filename;
209 cur_size = cur_file->size();
213 emit uploadStarted(data.filename);
215 //qDebug() << "size:" << cur_file->size();
218 qDebug() << "error: file not open!";
224 void Utils::downloadFinished()
226 //qDebug() << "downloadFinished";
228 if (cur_reply->error() == QNetworkReply::NoError)
230 int httpStatus = cur_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
231 //qDebug() << "status: " << httpStatus;
234 QByteArray bytes = cur_reply->readAll();
235 QString filepath = cur_folder + "/" + cur_filename;
236 QFile file(filepath);
237 file.open(QIODevice::WriteOnly);
238 int len = bytes.length();
239 QDataStream out(&file);
240 out.writeRawData(bytes.constData(),len);
242 emit fileDownloaded(cur_filename);
244 qDebug() << "download error";
245 emit downloadError(cur_filename);
248 else if (cur_reply->error() == QNetworkReply::OperationCanceledError)
250 emit operationCanceled(cur_filename);
254 //qDebug() << "download error";
255 emit downloadError(cur_filename);
259 cur_reply->deleteLater();
264 void Utils::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
266 //qDebug() << "progres: " << 100*bytesReceived/cur_size << "%";
268 float progress = (float)bytesReceived/cur_size;
270 emit fileDownloadProgress(cur_filename,progress);
273 void Utils::error(QNetworkReply::NetworkError code)
275 qDebug() << "error: " << code;
278 void Utils::uploadFile(const QString &folder, const QString &filename,
279 const QString &url, const QString &auth)
281 //qDebug() << "uploadFile";
284 data.isDownload = false;
285 data.folder = folder;
286 data.filename = filename;
291 emit uploadAdded(filename);
293 //qDebug() << "utils.cpp:uploadFile url=" << url;
300 void Utils::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
302 //qDebug() << "progres: " << 100*bytesSent/cur_size << "%";
303 //qDebug() << "progres: " << 100*bytesSent/bytesTotal << "%";
305 float progress = (float)bytesSent/cur_size;
306 emit fileUploadProgress(cur_filename,progress);
309 void Utils::uploadFinished()
311 //qDebug() << "uploadFinished";
313 if (cur_reply->error() == QNetworkReply::NoError)
315 emit fileUploaded(cur_filename);
317 else if (cur_reply->error() == QNetworkReply::OperationCanceledError)
319 emit operationCanceled(cur_filename);
323 int httpStatus = cur_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
325 //QString httpStatusMessage = cur_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
326 //QByteArray bytes = cur_reply->readAll();
327 //qDebug() << "status: " << httpStatus << " " << httpStatusMessage;
328 //qDebug() << "bytes: " << bytes;
332 emit fileUploaded(cur_filename);
334 //qDebug() << "upload error: " << httpStatus << " " << cur_reply->error() << " " << cur_reply->errorString();
335 emit uploadError(cur_filename);
342 cur_reply->deleteLater();
347 bool Utils::emptyQuee()
349 return isFinished && quee.length()==0;
355 msgBox.setText("Test!");
359 void Utils::deleteFile(const QString &url, const QString &auth)
361 //qDebug() << "Utils::deleteFile";
364 QNetworkRequest req(_url);
366 /*qDebug() << "url1=" << url;
367 qDebug() << "url2=" << req.url().toString();
368 qDebug() << "url3=" << req.url().path();*/
370 //qDebug() << "auth: " << auth;
371 req.setRawHeader("Authorization", auth.toAscii());
373 //qDebug() << "utils.cpp:uploadFile _nam=" << _nam->;
375 /*QList<QByteArray> l = req.rawHeaderList();
376 QList<QByteArray>::iterator i;
377 for(i=l.begin(); i!=l.end(); ++i)
378 qDebug() << "header=" << *i;
381 temp_reply = _nam->deleteResource(req);
382 connect(temp_reply,SIGNAL(finished()),this,SLOT(deleteFinished()));
383 connect(temp_reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
386 void Utils::deleteFinished()
388 //qDebug() << "Utils::deleteFinished";
390 int httpStatus = temp_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
391 QString httpStatusMessage = temp_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
392 QByteArray bytes = temp_reply->readAll();
394 //qDebug() << "status: " << httpStatus << " " << httpStatusMessage;
395 //qDebug() << "bytes: " << bytes;
397 if (temp_reply->error() == QNetworkReply::NoError)
403 emit operationError(httpStatus);
407 temp_reply->deleteLater();
410 void Utils::setClipboardText(const QString &text)
412 _clipboard->setText(text, QClipboard::Clipboard);
413 _clipboard->setText(text, QClipboard::Selection);
416 bool Utils::isMaemo()
418 #if defined(Q_WS_MAEMO_5)
424 void Utils::cancelFile(const QString & filename)
426 //qDebug() << "Utils::cancelFile";
427 if(!isFinished && cur_filename==filename)
434 for(int i=0;i<l;++i) {
435 RequestData data = quee.at(i);
436 //qDebug() << data.filename;
437 if(data.filename==filename) {
439 emit fileRemovedFromQuee(filename);