Browse Source

streamlined fb integration, single authentication

gwillz 5 years ago
parent
commit
3da4d73f10
6 changed files with 160 additions and 153 deletions
  1. 29 33
      dialogs.cpp
  2. 2 9
      dialogs.h
  3. 65 47
      fbApi.cpp
  4. 33 25
      fbApi.h
  5. 30 37
      slidewindow.cpp
  6. 1 2
      slidewindow.h

+ 29 - 33
dialogs.cpp

@@ -7,7 +7,7 @@
 #include "dialogs.h"
 #include "common.h"
 
-LoginDialog::LoginDialog(QWidget *parent) 
+LoginDialog::LoginDialog(FbApi *fb, QWidget *parent) 
         : QDialog(parent) {
     
     //housekeeping
@@ -17,6 +17,9 @@ LoginDialog::LoginDialog(QWidget *parent)
     QFormLayout *layout = new QFormLayout(this);
     setLayout(layout);
     
+    //load FB api
+    this->fb = fb;
+    
     //create form elements
     userEdit = new QLineEdit(this);
     passEdit = new QLineEdit(this);
@@ -31,14 +34,12 @@ LoginDialog::LoginDialog(QWidget *parent)
     
     //load settings
     st = new QSettings(company_str, app_str);
-    userEdit->setText(st->value("user").toString());
-    passEdit->setText(st->value("pass").toString());
+    userEdit->setText(fb->user());
+    hdEdit->setChecked(fb->highRes);
+    cacheEdit->setChecked(fb->caching);
     timeEdit->setValue(st->value("timeout").toLongLong()/1000);
-    hdEdit->setChecked(st->value("hd").toBool());
-    cacheEdit->setChecked(st->value("cache").toBool());
     
-    //load FB api
-    fb = new FbApi(APP_ID);
+    //permissions url
     accessButton->setText(tr("<a href='%1'>Grant Access</a>").arg(fb->getLoginUrl().toString()));
     accessButton->setOpenExternalLinks(true);
     accessButton->setAlignment(Qt::AlignRight);
@@ -70,20 +71,17 @@ LoginDialog::LoginDialog(QWidget *parent)
 }
 
 void LoginDialog::okClicked() {
-    st->setValue("user", userEdit->text());
-    st->setValue("pass", passEdit->text());
-    st->setValue("timeout", timeEdit->value()*1000);
-    st->setValue("hd", hdEdit->isChecked());
-    st->setValue("cache", cacheEdit->isChecked());
-    st->sync();
-    
     okButton->setEnabled(false);
     userEdit->setEnabled(false);
     passEdit->setEnabled(false);
     timeEdit->setEnabled(false);
     hdEdit->setEnabled(false);
     cacheEdit->setEnabled(false);
-    fb->authenticate();
+    
+    st->setValue("timeout", timeEdit->value()*1000);
+    fb->caching = cacheEdit->isChecked();
+    fb->highRes = hdEdit->isChecked();
+    fb->authenticate(userEdit->text(), passEdit->text());
 }
 
 void LoginDialog::loggedIn(bool success) {
@@ -102,7 +100,7 @@ void LoginDialog::loggedIn(bool success) {
     }
 }
 
-AlbumDialog::AlbumDialog(QWidget *parent) 
+AlbumDialog::AlbumDialog(FbApi *fb, QWidget *parent) 
         : QDialog(parent) {
     
     //housekeeping
@@ -113,21 +111,16 @@ AlbumDialog::AlbumDialog(QWidget *parent)
     setLayout(layout);
     
     st = new QSettings(company_str, app_str);
+    this->fb = fb;
     
-    fb = new FbApi(APP_ID, this);
-    fb->saveCredentials(st->value("user").toString(), st->value("pass").toString());
-    fb->authenticate();
-    
-    connect(fb, SIGNAL(loginResult(bool)), this, SLOT(loggedIn()));
-    connect(fb, SIGNAL(albumsLoaded()), this, SLOT(loadAlbums()));
-    
-    
+    //widgets
     list = new QListWidget(this);
     QPushButton *okButton = new QPushButton("Select", this);
     okButton->setMaximumWidth(70);
     QPushButton *cancelButton = new QPushButton("Cancel", this);
     cancelButton->setMaximumWidth(70);
     
+    //mixing
     QHBoxLayout *buttons = new QHBoxLayout(this);
     buttons->addStretch();
     buttons->addWidget(okButton);
@@ -138,7 +131,15 @@ AlbumDialog::AlbumDialog(QWidget *parent)
     
     connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
-    //connect(list, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(okClicked()));
+    connect(fb, SIGNAL(albumsLoaded()), this, SLOT(loadAlbums()));
+    
+    if (fb->loggedIn()) {
+        fb->loadAlbums();
+    } else {
+        QMessageBox::critical(this,
+                              "Login Error",
+                              "Not logged in!");
+    }
 }
 
 void AlbumDialog::okClicked() {
@@ -154,18 +155,13 @@ void AlbumDialog::okClicked() {
         }
     }
     
-    st->setValue("albums", checked);
-    st->sync();
-    
+    fb->selectAlbums(checked);
     accept();
 }
 
-void AlbumDialog::loggedIn() {
-    fb->getAlbumsId();
-}
-
 void AlbumDialog::loadAlbums() {
-    foreach (QVariantMap album, fb->albumList) {
+    qDebug() << "album list loaded";
+    foreach (QVariantMap album, fb->allAlbums()) {
         QListWidgetItem *item = new QListWidgetItem(album.value("name").toString());
         item->setData(Qt::UserRole, album.value("id").toString());
         item->setCheckState(Qt::Unchecked);

+ 2 - 9
dialogs.h

@@ -30,7 +30,7 @@ class LoginDialog
     QPushButton *exitButton;
     
     public:
-        explicit LoginDialog(QWidget *parent = 0);
+        explicit LoginDialog(FbApi *fb, QWidget *parent = 0);
         
     public slots:
         void okClicked();
@@ -58,13 +58,11 @@ class AlbumDialog
     QString aid;
     
     public:
-        explicit AlbumDialog(QWidget *parent = 0);
+        explicit AlbumDialog(FbApi *fb, QWidget *parent = 0);
         
     public slots:
         void okClicked();
-        
         void loadAlbums();
-        void loggedIn();
 };
 
 #endif // ALBUMDIALOG_H
@@ -101,8 +99,3 @@ class ScreenDialog
 };
 
 #endif // SCREENDIALOG_H
-
-#ifndef ERRORDIALOG_H
-#define ERRORDIALOG_H
-
-#endif // ERRORDIALOG_H

+ 65 - 47
fbApi.cpp

@@ -31,9 +31,10 @@ FbApi::FbApi(char* id, QObject *parent)
     page = new QWebPage(this);
 
     //defaults
-    this->id = QString(id);
+    appID = QString(id);
     token = "";
     imgCount = 0;
+    loginStatus = false;
     tmpPath = QDir::tempPath().append("/%1").arg(app_str);
     
     if (!QDir().exists(tmpPath)) {
@@ -44,54 +45,80 @@ FbApi::FbApi(char* id, QObject *parent)
     
     //get settings
     st = new QSettings(company_str, app_str);
-    syncSettings();
+    userName = new QString(st->value("user").toString());
+    highRes = st->value("hd").toBool();
+    caching = st->value("cache").toBool();
     
     //events
     connect(page, SIGNAL(loadFinished(bool)), this, SLOT(doAuthentication(bool)));
 }
 
-void FbApi::syncSettings() {
-    picList = new QStringList(st->value("images").toStringList());
-    aid = st->value("albums").toStringList();
-    user = new QString(st->value("user").toString());
-    pass = new QString(st->value("pass").toString());
-    highRes = st->value("hd").toBool();
-    caching = st->value("cache").toBool();
+FbApi::~FbApi() {
+    st->setValue("hd", highRes);
+    st->setValue("cache", caching);
+}
+
+QString FbApi::user() {
+    if (!userName->isEmpty())
+        return *userName;
+    else
+        return "";
 }
 
-void FbApi::authenticate() {
+bool FbApi::loggedIn() {
+    return loginStatus;
+}
+
+void FbApi::selectAlbums(QStringList ids) {
+    albumIDs = ids;
+}
+
+QList<QVariantMap> FbApi::allAlbums() {
+    return albumList;
+}
+
+QStringList FbApi::pictures() {
+    return picList;
+}
+
+void FbApi::authenticate(QString userName, QString password) {
     qDebug() << "authenticating";
-    syncSettings();
+    
+    this->userName = new QString(userName);
+    this->password = new QString(password);
+    st->setValue("user", userName);
+    
     page->mainFrame()->load(QUrl(getLoginUrl()));
 }
 
 void FbApi::doAuthentication(bool stat) {
     if (stat) {
-        qDebug() << page->mainFrame()->url().toString();
+//        qDebug() << page->mainFrame()->url().toString(); //SENSITIVE_DEBUG
         
         QUrl url = QUrl(page->mainFrame()->url().toString().replace("#access_token", "?access_token"));
         
         if (url.hasQueryItem("access_token")){
             qDebug() << "success";
-            //qDebug() << url.queryItemValue("access_token");
+            //qDebug() << url.queryItemValue("access_token"); //SENSITIVE_DEBUG
             
             token = url.queryItemValue("access_token");
-            //getPicsId();
             
+            loginStatus = true;
             emit loginResult(true);
         }
         else if (url.hasQueryItem("login_attempt")) {
             qDebug() << "Wrong user/password";
             
+            loginStatus = false;
             emit loginResult(false);
         }
         
         else {
             qDebug() << "sending credentials";
-            //qDebug() << url.toString();
+            //qDebug() << url.toString(); //SENSITIVE_DEBUG
             
-            page->mainFrame()->findFirstElement("input#email").setAttribute("value", *user);
-            page->mainFrame()->findFirstElement("input#pass").setAttribute("value", *pass);
+            page->mainFrame()->findFirstElement("input#email").setAttribute("value", *userName);
+            page->mainFrame()->findFirstElement("input#pass").setAttribute("value", *password);
             page->mainFrame()->evaluateJavaScript("document.getElementById(\"login_form\").submit()");
         }
     }
@@ -99,18 +126,18 @@ void FbApi::doAuthentication(bool stat) {
 
 QUrl FbApi::getLoginUrl() {
     QString *url = new QString(fbUrl);
-    return QUrl(url->replace("APP_ID", id, Qt::CaseSensitive));
+    return QUrl(url->replace("APP_ID", appID, Qt::CaseSensitive));
 }
 
-void FbApi::getPicsId() {
+void FbApi::loadPictures() {
     qDebug() << "fetching picture ids";
-    qDebug() << "Album: " << aid;
+    qDebug() << "Album: " << albumIDs;
     
     QString url = QString(getAlbumPics);
     url.replace("ACCESS_TOKEN", token, Qt::CaseSensitive);
     
-    QString albumString = QString("%1").arg(aid.takeFirst());
-    foreach (QString a, aid) {
+    QString albumString = QString("%1").arg(albumIDs.takeFirst());
+    foreach (QString a, albumIDs) {
         albumString += QString(",%1").arg(a);
     }
     
@@ -119,44 +146,32 @@ void FbApi::getPicsId() {
     connect(resp, SIGNAL(finished()), this, SLOT(loadPicList()));
 }
 
-void FbApi::saveCredentials(QString myUser, QString myPass) {
-    qDebug() << "registering login details";
-    
-    user = new QString(myUser);
-    pass = new QString(myPass);
-    
-    st->setValue("user", *user);
-    st->setValue("pass", *pass);
-}
-
 void FbApi::loadPicList() {
     qDebug() << "processing response";
     
     bool ok;
-    
     QVariant result = parser.parse(resp->readAll(), &ok);
-    qDebug() << resp->url();
+//    qDebug() << resp->url().toString();  //SENSITIVE_DEBUG
     resp->close();
     
     if (ok) {
         qDebug() << "loading photos";
         
-        delete picList;
-        picList = new QStringList();
+        picList.clear();
         
         foreach (QVariant alb, result.toMap().values()) {
             foreach (QVariant pic, alb.toMap()["data"].toList()) {
                 if (highRes) {
-                    picList->append(pic.toMap()["images"].toList()[0].toMap()["source"].toString());
+                    picList.append(pic.toMap()["images"].toList()[0].toMap()["source"].toString());
                 } else {
-                    picList->append(pic.toMap()["source"].toString());
+                    picList.append(pic.toMap()["source"].toString());
                 }
             }
         }
-        qDebug() << picList->count() << "photo(s)";
+        qDebug() << picList.size() << "photo(s)";
         
-        if (picList->count() > 0) {
-            st->setValue("images", *picList);
+        if (picList.size() > 0) {
+            st->setValue("images", picList); //REMOVE?
             emit picsLoaded();
         }
         else {
@@ -165,7 +180,7 @@ void FbApi::loadPicList() {
     }
 }
 
-void FbApi::getAlbumsId() {
+void FbApi::loadAlbums() {
     qDebug() << "fetching album ids";
     
     QString url = QString(getAlbums);
@@ -192,20 +207,23 @@ void FbApi::loadAlbumList() {
             albumList.append(album.toMap());
         }
         
-        qDebug() << albumList.count() << "album(s)";
+        qDebug() << albumList.size() << "album(s)";
         
-        emit albumsLoaded();
+        if (albumList.size() > 0)
+            emit albumsLoaded();
+        else
+            emit error("No albums loaded. Please check the logifle.");
     } else {
         emit error("No albums loaded. Please check the logifle.");
     }
 }
 
 void FbApi::nextPicture() {
-    if (imgCount >= picList->size()) {
+    if (imgCount >= picList.size()) {
         imgCount = 0; //loop
     }
     
-    QString picUrl = picList->at(imgCount);
+    QString picUrl = picList.at(imgCount);
     QString path = QString(tmpPath).append("/%1").arg(picUrl.split("/").last());
     
     if (caching) {
@@ -231,7 +249,7 @@ void FbApi::nextPicture() {
 void FbApi::setPicture() {
     if (picResp->error() == QNetworkReply::NoError && picResp->size() > 1) {
         
-        QString picUrl = picList->at(imgCount);
+        QString picUrl = picList.at(imgCount);
         QString path = QString(tmpPath).append("/%1").arg(picUrl.split("/").last().split("?").first());
         
         //write picResp to file

+ 33 - 25
fbApi.h

@@ -20,38 +20,43 @@
 #include "common.h"
 
 class FbApi 
-        : public QObject{
+        : public QObject {
     Q_OBJECT
     
+    QSettings *st;
+    QString token;
+    QString appID;
+    QStringList picList;
+    
+    QStringList albumIDs;
+    QList<QVariantMap> albumList;
+    
+    bool loginStatus;
+    int imgCount;
+    QString tmpPath;
+    
     public:
-        FbApi(char* id, QObject *parent = 0);
-        
-        QSettings *st;
-        
-        QUrl getLoginUrl();
-        QString token;
+        FbApi(char* appID, QObject *parent = 0);
+        ~FbApi();
         
         bool highRes;
         bool caching;
-        QString id;
-        QStringList *picList;
-        void getPicsId();
         
-        QStringList aid;
-        QList<QVariantMap> albumList;
-        void getAlbumsId();
+        QUrl getLoginUrl();
+        void loadAlbums();
+        void loadPictures();
+        void authenticate(QString userName, QString password);
         
-        void nextPicture();
-        void authenticate();
-        QString *user;
-        QString *pass;
-        void saveCredentials(QString myUser, QString myPass);
-        void syncSettings();
+        //access methods
+        QString user();
+        bool loggedIn();
+        void selectAlbums(QStringList ids);
+        QList<QVariantMap> allAlbums();
+        QStringList pictures();
         
     private:
-        int imgCount;
-        QString tmpPath;
-        
+        QString *userName;
+        QString *password;
         QNetworkAccessManager *net;
         QNetworkReply *resp;
         QNetworkReply *picResp;
@@ -60,13 +65,16 @@ class FbApi
         QWebPage *page;
 
     signals:
-        void picsLoaded();
-        void picLoaded(QString pic);
         void loginResult(bool res);
         void albumsLoaded();
+        void picsLoaded();
+        void picLoaded(QString pic);
         void error(QString msg);
-
+    
     public slots:
+        void nextPicture();
+        
+    private slots:
         void loadPicList();
         void loadAlbumList();
         void setPicture();

+ 30 - 37
slidewindow.cpp

@@ -25,6 +25,25 @@ void SlideWindow::setupScreens() {
     diag->show();
 }
 
+void SlideWindow::setupLogin() {
+    //load fbApi
+    fb = new FbApi(APP_ID, this);
+    
+    //open login dialog
+    LoginDialog *loginDiag = new LoginDialog(fb, this);
+    loginDiag->show();
+    
+    connect(loginDiag, SIGNAL(accepted()), this, SLOT(setupAlbums()));
+    connect(loginDiag, SIGNAL(rejected()), qApp, SLOT(quit()));
+}
+
+void SlideWindow::setupAlbums() {
+    AlbumDialog *albumDiag = new AlbumDialog(fb, this);
+    albumDiag->show();
+    connect(albumDiag, SIGNAL(accepted()), this, SLOT(setupUi()));
+    connect(albumDiag, SIGNAL(rejected()), qApp, SLOT(quit()));
+}
+
 void SlideWindow::setupUi() {
     //housekeeping
     setWindowTitle("PhotoBunny");
@@ -76,36 +95,20 @@ void SlideWindow::setupUi() {
     connect(blankAction, SIGNAL(triggered()), this, SLOT(blank()));
     
     show();
-    setupFB();
+    setupFinal();
 }
 
-void SlideWindow::setupLogin() {
-    
-    //open login dialog
-    LoginDialog *loginDiag = new LoginDialog(this);
-    loginDiag->show();
+void SlideWindow::setupFinal() {
+    if (!fb->loggedIn()) {
+        QMessageBox::critical(this, 
+                              "Login Error", 
+                              "Not logged in!");
+        return;
+    }
     
-    connect(loginDiag, SIGNAL(accepted()), this, SLOT(setupAlbums()));
-    connect(loginDiag, SIGNAL(rejected()), qApp, SLOT(quit()));
-}
-
-void SlideWindow::setupAlbums() {
-    AlbumDialog *albumDiag = new AlbumDialog(this);
-    albumDiag->show();
-    connect(albumDiag, SIGNAL(accepted()), this, SLOT(setupUi()));
-    connect(albumDiag, SIGNAL(rejected()), qApp, SLOT(quit()));
-}
-
-void SlideWindow::setupFB() {
-    //fb stuff
+    //final stuff
     qApp->setOverrideCursor(Qt::BlankCursor);
-    
     timer = new QTimer();
-    fb = new FbApi(APP_ID, this);
-    
-    fb->syncSettings();
-    fb->saveCredentials(st->value("user").toString(), st->value("pass").toString());
-    fb->authenticate();
     
     qDebug() << "-- slideshow started with a" << timeout << "ms timeout --";
     if (fb->caching) {
@@ -115,8 +118,9 @@ void SlideWindow::setupFB() {
     connect(timer, SIGNAL(timeout()), this, SLOT(photoChange()));
     connect(fb, SIGNAL(picsLoaded()), this, SLOT(photoChange()));
     connect(fb, SIGNAL(picLoaded(QString)), this, SLOT(setImage(QString)));
-    connect(fb, SIGNAL(loginResult(bool)), this, SLOT(loggedIn(bool)));
     connect(fb, SIGNAL(error(QString)), this, SLOT(handleError(QString)));
+    
+    fb->loadPictures();
 }
 
 void SlideWindow::about() {
@@ -168,14 +172,3 @@ void SlideWindow::setImage(QString path) {
     img->setPixmap(pix);
 }
 
-void SlideWindow::loggedIn(bool success) {
-    if (success) {
-        fb->getPicsId();
-    } else {
-        QMessageBox::information(this,
-                                 "Bad Login",
-                                 "Incorrect credentials or access hasn't been granted.");
-        setupLogin();
-    }
-}
-

+ 1 - 2
slidewindow.h

@@ -32,7 +32,6 @@ class SlideWindow
     public slots:
         void photoChange();
         void setImage(QString path);
-        void loggedIn(bool success);
         void pause();
         void blank();
         void about();
@@ -40,7 +39,7 @@ class SlideWindow
         void setupUi();
         void setupLogin();
         void setupAlbums();
-        void setupFB();
+        void setupFinal();
         void handleError(QString msg);
 };