summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/plugins
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2017-09-29 12:45:28 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2017-09-29 12:45:28 -0700
commit7b8832bc0ba1d287baae247307d3352da1371b55 (patch)
tree4c324aa2014a68b878c6e2b9c5cede3ef1e2f8b7 /desktop-widgets/plugins
parentc8c9a5dc65c184f0c0acdc19411b4e7efc39fb99 (diff)
downloadsubsurface-7b8832bc0ba1d287baae247307d3352da1371b55.tar.gz
[Facebook] fewer UI freezes while accessing facebook
Use signal / slots on the networkreply instead of freezing the event loop. Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'desktop-widgets/plugins')
-rw-r--r--desktop-widgets/plugins/facebook/facebookconnectwidget.cpp34
-rw-r--r--desktop-widgets/plugins/facebook/facebookconnectwidget.h11
2 files changed, 32 insertions, 13 deletions
diff --git a/desktop-widgets/plugins/facebook/facebookconnectwidget.cpp b/desktop-widgets/plugins/facebook/facebookconnectwidget.cpp
index 6ed02832b..de0fcb682 100644
--- a/desktop-widgets/plugins/facebook/facebookconnectwidget.cpp
+++ b/desktop-widgets/plugins/facebook/facebookconnectwidget.cpp
@@ -49,6 +49,7 @@ FacebookManager *FacebookManager::instance()
FacebookManager::FacebookManager(QObject *parent) : QObject(parent)
{
+ albumListUrl = QUrl("https://graph.facebook.com/me/albums?access_token=" + QString(prefs.facebook.access_token));
}
static QString graphApi = QStringLiteral("https://graph.facebook.com/v2.10/");
@@ -98,14 +99,14 @@ void FacebookManager::logout()
void FacebookManager::requestAlbumId()
{
- QUrl albumListUrl("https://graph.facebook.com/me/albums?access_token=" + QString(prefs.facebook.access_token));
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply *reply = manager->get(QNetworkRequest(albumListUrl));
+ connect(reply, &QNetworkReply::finished, this, &FacebookManager::albumListReceived);
+}
- QEventLoop loop;
- connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
- loop.exec();
-
+void FacebookManager::albumListReceived()
+{
+ QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
QJsonDocument albumsDoc = QJsonDocument::fromJson(reply->readAll());
QJsonArray albumObj = albumsDoc.object().value("data").toArray();
auto fb = SettingsObjectWrapper::instance()->facebook;
@@ -117,7 +118,12 @@ void FacebookManager::requestAlbumId()
return;
}
}
+ // No album with the name we requested, create a new one.
+ createFacebookAlbum();
+}
+void FacebookManager::createFacebookAlbum()
+{
QUrlQuery params;
params.addQueryItem("name", albumName );
params.addQueryItem("description", "Subsurface Album");
@@ -125,13 +131,19 @@ void FacebookManager::requestAlbumId()
QNetworkRequest request(albumListUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
- reply = manager->post(request, params.query().toLocal8Bit());
- connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
- loop.exec();
- albumsDoc = QJsonDocument::fromJson(reply->readAll());
+ QNetworkAccessManager *manager = new QNetworkAccessManager();
+ QNetworkReply *reply = manager->post(request, params.query().toLocal8Bit());
+ connect(reply, &QNetworkReply::finished, this, &FacebookManager::facebookAlbumCreated);
+}
+
+void FacebookManager::facebookAlbumCreated()
+{
+ QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
+ QJsonDocument albumsDoc = QJsonDocument::fromJson(reply->readAll());
QJsonObject album = albumsDoc.object();
if (album.contains("id")) {
+ auto fb = SettingsObjectWrapper::instance()->facebook;
fb->setAlbumId(album.value("id").toString());
return;
}
@@ -195,7 +207,9 @@ void FacebookManager::sendDive()
data.append("Content-Disposition: form-data; name=\"action\"\r\n\r\n");
data.append(graphApi + "\r\n");
data.append("--" + bound + "\r\n"); //according to rfc 1867
- data.append("Content-Disposition: form-data; name=\"uploaded\"; filename=\"" + QString::number(qrand()) + ".png\"\r\n"); //name of the input is "uploaded" in my form, next one is a file name.
+
+ //name of the input is "uploaded" in my form, next one is a file name.
+ data.append("Content-Disposition: form-data; name=\"uploaded\"; filename=\"" + QString::number(qrand()) + ".png\"\r\n");
data.append("Content-Type: image/jpeg\r\n\r\n"); //data type
data.append(bytes); //let's read the file
data.append("\r\n");
diff --git a/desktop-widgets/plugins/facebook/facebookconnectwidget.h b/desktop-widgets/plugins/facebook/facebookconnectwidget.h
index 1c6e40e07..c47f4be66 100644
--- a/desktop-widgets/plugins/facebook/facebookconnectwidget.h
+++ b/desktop-widgets/plugins/facebook/facebookconnectwidget.h
@@ -3,6 +3,7 @@
#define FACEBOOKCONNECTWIDGET_H
#include <QDialog>
+#include <QUrl>
#ifdef USE_WEBENGINE
class QWebEngineView;
#else
@@ -11,8 +12,8 @@ class QWebView;
class QNetworkReply;
namespace Ui {
- class FacebookConnectWidget;
- class SocialnetworksDialog;
+ class FacebookConnectWidget;
+ class SocialnetworksDialog;
}
class FacebookManager : public QObject
@@ -33,10 +34,14 @@ public slots:
void logout();
void setDesiredAlbumName(const QString& albumName);
void sendDive();
- void uploadFinished();
+ void uploadFinished();
+ void albumListReceived();
+ void createFacebookAlbum();
+ void facebookAlbumCreated();
private:
explicit FacebookManager(QObject *parent = 0);
QString albumName;
+ QUrl albumListUrl;
};