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 req.setRawHeader("Authorization", data.auth.toAscii());
175 QNetworkReply* reply = _nam->get(req);
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)));
182 cur_folder = data.folder;
183 cur_filename = data.filename;
184 cur_size = data.size;
188 emit downloadStarted(data.filename);
190 //qDebug() << "startDownload, filename: " << cur_filename;
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);
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)));
205 cur_folder = data.folder;
206 cur_filename = data.filename;
207 cur_size = cur_file->size();
211 emit uploadStarted(data.filename);
213 //qDebug() << "size:" << cur_file->size();
216 qDebug() << "error: file not open!";
222 void Utils::downloadFinished()
224 //qDebug() << "downloadFinished";
226 if (cur_reply->error() == QNetworkReply::NoError)
228 int httpStatus = cur_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
229 //qDebug() << "status: " << httpStatus;
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);
240 emit fileDownloaded(cur_filename);
242 qDebug() << "download error";
243 emit downloadError(cur_filename);
248 qDebug() << "download error";
249 emit downloadError(cur_filename);
253 cur_reply->deleteLater();
258 void Utils::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
260 //qDebug() << "progres: " << 100*bytesReceived/cur_size << "%";
262 float progress = (float)bytesReceived/cur_size;
264 emit fileDownloadProgress(cur_filename,progress);
267 void Utils::error(QNetworkReply::NetworkError code)
269 qDebug() << "error: " << code;
272 void Utils::uploadFile(const QString &folder, const QString &filename,
273 const QString &url, const QString &auth)
275 //qDebug() << "uploadFile";
278 data.isDownload = false;
279 data.folder = folder;
280 data.filename = filename;
285 emit uploadAdded(filename);
292 void Utils::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
294 //qDebug() << "progres: " << 100*bytesSent/cur_size << "%";
295 //qDebug() << "progres: " << 100*bytesSent/bytesTotal << "%";
297 float progress = (float)bytesSent/cur_size;
298 emit fileUploadProgress(cur_filename,progress);
301 void Utils::uploadFinished()
303 //qDebug() << "uploadFinished";
305 if (cur_reply->error() == QNetworkReply::NoError)
307 emit fileUploaded(cur_filename);
311 int httpStatus = cur_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
312 //QString httpStatusMessage = cur_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
313 //QByteArray bytes = cur_reply->readAll();
315 //qDebug() << "status: " << httpStatus << " " << httpStatusMessage;
316 //qDebug() << "bytes: " << bytes;
320 emit fileUploaded(cur_filename);
322 qDebug() << "upload error";
323 emit uploadError(cur_filename);
331 cur_reply->deleteLater();
336 bool Utils::emptyQuee()
338 return isFinished && quee.length()==0;
344 msgBox.setText("Test!");
348 void Utils::deleteFile(const QString &url, const QString &auth)
350 //qDebug() << "Utils::deleteFile";
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)));
361 void Utils::deleteFinished()
363 //qDebug() << "Utils::deleteFinished";
365 int httpStatus = temp_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
366 QString httpStatusMessage = temp_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
367 QByteArray bytes = temp_reply->readAll();
369 //qDebug() << "status: " << httpStatus << " " << httpStatusMessage;
370 //qDebug() << "bytes: " << bytes;
372 if (temp_reply->error() == QNetworkReply::NoError)
378 emit operationError(httpStatus);
382 temp_reply->deleteLater();
385 void Utils::setClipboardText(const QString &text)
387 _clipboard->setText(text, QClipboard::Clipboard);
388 _clipboard->setText(text, QClipboard::Selection);