Settings update: Fix Facebook Widget to use the SettingsObjectWrapper

Fix facebook widget to use the SettingsObjectWrapper instead
of figthing our complementary Settings each call.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2016-08-26 15:17:49 -03:00 committed by Dirk Hohndel
parent 5651abfd75
commit 12eccda523
3 changed files with 15 additions and 69 deletions

View file

@ -11,7 +11,6 @@
#include <QUrlQuery> #include <QUrlQuery>
#include <QEventLoop> #include <QEventLoop>
#include <QHttpMultiPart> #include <QHttpMultiPart>
#include <QSettings>
#include <QFile> #include <QFile>
#include <QBuffer> #include <QBuffer>
#include <QDebug> #include <QDebug>
@ -24,6 +23,7 @@
#include "core/pref.h" #include "core/pref.h"
#include "core/helpers.h" #include "core/helpers.h"
#include "core/subsurface-qt/SettingsObjectWrapper.h"
#include "ui_socialnetworksdialog.h" #include "ui_socialnetworksdialog.h"
#include "ui_facebookconnectwidget.h" #include "ui_facebookconnectwidget.h"
@ -45,7 +45,6 @@ FacebookManager *FacebookManager::instance()
FacebookManager::FacebookManager(QObject *parent) : QObject(parent) FacebookManager::FacebookManager(QObject *parent) : QObject(parent)
{ {
sync();
} }
QUrl FacebookManager::connectUrl() { QUrl FacebookManager::connectUrl() {
@ -62,20 +61,6 @@ bool FacebookManager::loggedIn() {
return prefs.facebook.access_token != NULL; return prefs.facebook.access_token != NULL;
} }
void FacebookManager::sync()
{
#if SAVE_FB_CREDENTIALS
QSettings s;
s.beginGroup("WebApps");
s.beginGroup("Facebook");
QVariant v;
GET_TXT("ConnectToken", facebook.access_token);
GET_TXT("UserId", facebook.user_id);
GET_TXT("AlbumId", facebook.album_id);
#endif
}
void FacebookManager::tryLogin(const QUrl& loginResponse) void FacebookManager::tryLogin(const QUrl& loginResponse)
{ {
QString result = loginResponse.toString(); QString result = loginResponse.toString();
@ -90,38 +75,18 @@ void FacebookManager::tryLogin(const QUrl& loginResponse)
int to = result.indexOf("&expires_in"); int to = result.indexOf("&expires_in");
QString securityToken = result.mid(from, to-from); QString securityToken = result.mid(from, to-from);
#if SAVE_FB_CREDENTIALS auto fb = SettingsObjectWrapper::instance()->facebook;
QSettings settings; fb->setAccessToken(securityToken);
settings.beginGroup("WebApps");
settings.beginGroup("Facebook");
settings.setValue("ConnectToken", securityToken);
sync();
#else
prefs.facebook.access_token = copy_string(securityToken.toUtf8().data());
#endif
requestUserId(); requestUserId();
sync();
emit justLoggedIn(true); emit justLoggedIn(true);
} }
void FacebookManager::logout() void FacebookManager::logout()
{ {
#if SAVE_FB_CREDENTIALS auto fb = SettingsObjectWrapper::instance()->facebook;
QSettings settings; fb->setAccessToken(QString());
settings.beginGroup("WebApps"); fb->setUserId(QString());
settings.beginGroup("Facebook"); fb->setAlbumId(QString());
settings.remove("ConnectToken");
settings.remove("UserId");
settings.remove("AlbumId");
sync();
#else
free(prefs.facebook.access_token);
free(prefs.facebook.album_id);
free(prefs.facebook.user_id);
prefs.facebook.access_token = NULL;
prefs.facebook.album_id = NULL;
prefs.facebook.user_id = NULL;
#endif
emit justLoggedOut(true); emit justLoggedOut(true);
} }
@ -135,22 +100,14 @@ void FacebookManager::requestAlbumId()
connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec(); loop.exec();
#if SAVE_FB_CREDENTIALS
QSettings s;
s.beginGroup("WebApps");
s.beginGroup("Facebook");
#endif
QJsonDocument albumsDoc = QJsonDocument::fromJson(reply->readAll()); QJsonDocument albumsDoc = QJsonDocument::fromJson(reply->readAll());
QJsonArray albumObj = albumsDoc.object().value("data").toArray(); QJsonArray albumObj = albumsDoc.object().value("data").toArray();
auto fb = SettingsObjectWrapper::instance()->facebook;
foreach(const QJsonValue &v, albumObj){ foreach(const QJsonValue &v, albumObj){
QJsonObject obj = v.toObject(); QJsonObject obj = v.toObject();
if (obj.value("name").toString() == albumName) { if (obj.value("name").toString() == albumName) {
#if SAVE_FB_CREDENTIALS fb->setAlbumId(obj.value("id").toString());
s.setValue("AlbumId", obj.value("id").toString());
#else
prefs.facebook.album_id = copy_string(obj.value("id").toString().toUtf8().data());
#endif
return; return;
} }
} }
@ -169,12 +126,7 @@ void FacebookManager::requestAlbumId()
albumsDoc = QJsonDocument::fromJson(reply->readAll()); albumsDoc = QJsonDocument::fromJson(reply->readAll());
QJsonObject album = albumsDoc.object(); QJsonObject album = albumsDoc.object();
if (album.contains("id")) { if (album.contains("id")) {
#if SAVE_FB_CREDENTIALS fb->setAlbumId(album.value("id").toString());
s.setValue("AlbumId", album.value("id").toString());
#else
prefs.facebook.album_id = copy_string(album.value("id").toString().toUtf8().data());
#endif
sync();
return; return;
} }
} }
@ -192,14 +144,7 @@ void FacebookManager::requestUserId()
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll()); QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
QJsonObject obj = jsonDoc.object(); QJsonObject obj = jsonDoc.object();
if (obj.keys().contains("id")){ if (obj.keys().contains("id")){
#if SAVE_FB_CREDENTIALS SettingsObjectWrapper::instance()->facebook->setUserId(obj.value("id").toString());
QSettings s;
s.beginGroup("WebApps");
s.beginGroup("Facebook");
s.setValue("UserId", obj.value("id").toVariant());
#else
prefs.facebook.user_id = copy_string(obj.value("id").toString().toUtf8().data());
#endif
return; return;
} }
} }

View file

@ -15,7 +15,6 @@ public:
static FacebookManager *instance(); static FacebookManager *instance();
void requestAlbumId(); void requestAlbumId();
void requestUserId(); void requestUserId();
void sync();
QUrl connectUrl(); QUrl connectUrl();
bool loggedIn(); bool loggedIn();
signals: signals:
@ -58,4 +57,4 @@ private:
Ui::SocialnetworksDialog *ui; Ui::SocialnetworksDialog *ui;
}; };
#endif #endif

View file

@ -438,6 +438,7 @@ void SubsurfaceWebServices::buttonClicked(QAbstractButton *button)
bool qSaveUid = ui.saveUidLocal->checkState(); bool qSaveUid = ui.saveUidLocal->checkState();
SettingsObjectWrapper::instance()->cloud_storage->setSaveUserIdLocal(qSaveUid); SettingsObjectWrapper::instance()->cloud_storage->setSaveUserIdLocal(qSaveUid);
//WARN: Dirk, this seems to be wrong, I coundn't really understand the code.
if (qSaveUid) { if (qSaveUid) {
QString qSettingUid = s.value("subsurface_webservice_uid").toString(); QString qSettingUid = s.value("subsurface_webservice_uid").toString();
QString qFileUid = QString(prefs.userid); QString qFileUid = QString(prefs.userid);
@ -758,6 +759,7 @@ DivelogsDeWebServices::DivelogsDeWebServices(QWidget *parent, Qt::WindowFlags f)
multipart(NULL), multipart(NULL),
uploadMode(false) uploadMode(false)
{ {
//FIXME: DivelogDE user and pass should be on the prefs struct or something?
QSettings s; QSettings s;
ui.userID->setText(s.value("divelogde_user").toString()); ui.userID->setText(s.value("divelogde_user").toString());
ui.password->setText(s.value("divelogde_pass").toString()); ui.password->setText(s.value("divelogde_pass").toString());