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(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(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 +#include #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; };