Browse Source

massive tweaks, integrated permission accepting

gwillz 5 years ago
parent
commit
a7d00d8fca
7 changed files with 132 additions and 50 deletions
  1. 5 8
      common.h
  2. 68 17
      dialogs.cpp
  3. 21 1
      dialogs.h
  4. 19 12
      fbApi.cpp
  5. 11 3
      fbApi.h
  6. 3 4
      main.cpp
  7. 5 5
      version.h

+ 5 - 8
common.h

@@ -3,18 +3,15 @@
 #define app_str "fbslideshow"
 
 #define box_scale 20
+#define SENSITIVE_DEBUG true
 
 //facebook application id
 #define APP_ID "1402768076653253" //photoBunny
 
 //fbApi urls
-#define fbUrl "https://www.facebook.com/dialog/oauth?client_id=APP_ID&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=user_photos&response_type=token"
-#define getAlbums "https://graph.facebook.com/me/albums?fields=name,id&limit=150&access_token=ACCESS_TOKEN"
-
-//API v2.0 (expires in April 2015)
-//#define getAlbumPics "https://graph.facebook.com/fql?q=SELECT+src_big+FROM+photo+WHERE+ALBUM_IDS&access_token=ACCESS_TOKEN"
-//#define getAlbumPics "https://graph.facebook.com/fql?q=SELECT+src_big,+images.source+FROM+photo+WHERE+ALBUM_IDS&access_token=ACCESS_TOKEN"
+#define fbUrl "https://www.facebook.com/v2.2/dialog/oauth?client_id=APP_ID&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=user_photos&response_type=token"
+#define getAlbums "https://graph.facebook.com/v2.2/me/albums?fields=name,id&limit=150&access_token=ACCESS_TOKEN"
 
 //API v2.2
-//#define getAlbumPics "https://graph.facebook.com/ALBUM_ID/photos?fields=images&access_token=ACCESS_TOKEN" //single pull
-#define getAlbumPics "https://graph.facebook.com/photos?ids=ALBUM_IDS&fields=images&access_token=ACCESS_TOKEN" //multi pull
+//#define getAlbumPics "https://graph.facebook.com/v2.2/ALBUM_ID/photos?fields=images&access_token=ACCESS_TOKEN" //single pull
+#define getAlbumPics "https://graph.facebook.com/v2.2/photos?ids=ALBUM_IDS&fields=images&access_token=ACCESS_TOKEN" //multi pull

+ 68 - 17
dialogs.cpp

@@ -5,8 +5,10 @@
 
 #include <QtGui>
 #include <QtWidgets>
+#include <QWebFrame>
 #include "dialogs.h"
 #include "common.h"
+#include "fbApi.h"
 
 LoginDialog::LoginDialog(FbApi *fb, QWidget *parent) 
         : QDialog(parent) {
@@ -24,7 +26,6 @@ LoginDialog::LoginDialog(FbApi *fb, QWidget *parent)
     timeEdit = new QSpinBox(this);
     hdEdit = new QCheckBox(this);
     cacheEdit = new QCheckBox(this);
-    QLabel *accessButton = new QLabel(this);
     
     passEdit->setEchoMode(QLineEdit::Password);
     timeEdit->setMaximum(300);
@@ -34,19 +35,18 @@ LoginDialog::LoginDialog(FbApi *fb, QWidget *parent)
     st = new QSettings(company_str, app_str);
     this->fb = fb;
     
+    //permission dialog
+    perms = new PermissionDialog(this);
+    perms->show();
+    
     userEdit->setText(fb->user());
     hdEdit->setChecked(fb->highRes);
     cacheEdit->setChecked(fb->caching);
     timeEdit->setValue(st->value("timeout").toLongLong()/1000);
     
-    //permissions url
-    accessButton->setText(tr("<a href='%1'>Grant Access</a>").arg(fb->getLoginUrl().toString()));
-    accessButton->setOpenExternalLinks(true);
-    accessButton->setAlignment(Qt::AlignRight);
-    
     //create buttons
     clearButton = new QPushButton("Clear Cache", this);
-    clearButton->setMaximumWidth(70);
+    clearButton->setMaximumWidth(90);
     clearButton->setFocusPolicy(Qt::NoFocus);
     okButton = new QPushButton("Login", this);
     exitButton = new QPushButton("Cancel", this);
@@ -66,13 +66,14 @@ LoginDialog::LoginDialog(FbApi *fb, QWidget *parent)
     layout->addRow("High Res:", hdEdit);
     layout->addRow("Caching:", cacheEdit);
     layout->addWidget(clearButton);
-    layout->addWidget(accessButton);
     layout->addRow(buttons);
     
     connect(clearButton, SIGNAL(clicked()), this, SLOT(clearCache()));
     connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
-    connect(fb, SIGNAL(loginResult(bool)), this, SLOT(loggedIn(bool)));
+    connect(fb, SIGNAL(loginResult(LoginStatus)), this, SLOT(loggedIn(LoginStatus)));
     connect(exitButton, SIGNAL(clicked()), this, SLOT(reject()));
+    connect(perms, SIGNAL(accepted()), fb, SLOT(acceptPermissions()));
+    connect(perms, SIGNAL(rejected()), this, SLOT(showDeclineBox()));
 }
 
 void LoginDialog::clearCache() {
@@ -91,14 +92,14 @@ void LoginDialog::clearCache() {
     } else if (size <= 1024*1024*1024) {
         sizeStr = QString("%1 MiB").arg(size/1024/1024);
     } else {
-        sizeStr = QString("%1 MiB").arg(size/1024/1024/1024);
+        sizeStr = QString("%1 GiB").arg(size/1024/1024/1024);
     }
     
     int res = QMessageBox::question(this,
                                     "Confirm",
-                                    QString("There are currently %1 images in the cache\n").arg(count) +
-                                    "totalling " + sizeStr + " in disk space.\n"
-                                    "Are you sure you want to delete the cached images?");
+                                    QString("There are currently %1 images in the cache\n"
+                                    "totalling %2 in disk space.\n"
+                                    "Are you sure you want to delete the cached images?").arg(count).arg(sizeStr));
     
     if (res == QMessageBox::Yes) {
         qDebug() << "deleting cache files";
@@ -122,22 +123,42 @@ void LoginDialog::okClicked() {
     fb->authenticate(userEdit->text(), passEdit->text());
 }
 
-void LoginDialog::loggedIn(bool success) {
-    if (success) {
+void LoginDialog::loggedIn(LoginStatus status) {
+    switch(status) {
+    case SUCCESS:
         accept();
-    } else {
+        break;
+    case BAD_PASS:
         QMessageBox::information(this,
                                  "Bad Login",
-                                 "Credentials were incorrent or permission hasn't been granted.");
+                                 "Password or email is incorrect.");
         okButton->setEnabled(true);
         userEdit->setEnabled(true);
         passEdit->setEnabled(true);
         timeEdit->setEnabled(true);
         hdEdit->setEnabled(true);
         cacheEdit->setEnabled(true);
+        break;
+    case BAD_PERM:
+        perms->show();
+        break;
+    default:
+        qDebug() << "Impossible switch case!";
     }
 }
 
+void LoginDialog::showDeclineBox() {
+    QMessageBox::information(this, 
+                             "No Permissions", 
+                             "This app requires Facebook permissions, please try again.");
+    okButton->setEnabled(true);
+    userEdit->setEnabled(true);
+    passEdit->setEnabled(true);
+    timeEdit->setEnabled(true);
+    hdEdit->setEnabled(true);
+    cacheEdit->setEnabled(true);
+}
+
 AlbumDialog::AlbumDialog(FbApi *fb, QWidget *parent) 
         : QDialog(parent) {
     
@@ -295,6 +316,7 @@ void ScreenDialog::identify() {
 void ScreenDialog::selectScreen(int screen) {
     selectedScreen = screen;
     okButton->setEnabled(true);
+    okButton->setFocus();
     screenLabel->setText(QString::number(screen));
     
 }
@@ -305,3 +327,32 @@ void ScreenDialog::clearHighlights() {
     }
 }
 
+PermissionDialog::PermissionDialog(QWidget *parent)
+        : QDialog(parent) {
+    //housekeeping
+    setWindowTitle("Permissions | PhotoBunny");
+    QVBoxLayout *layout = new QVBoxLayout(this);
+    setFixedHeight(250);
+    setFixedWidth(150);
+    layout->setSizeConstraint(QLayout::SetFixedSize);
+    setLayout(layout);
+    
+    QPushButton* okButton = new QPushButton("Accept", this);
+    QPushButton* cancelButton = new QPushButton("Decline", this);
+    
+    QLabel* msgLabel = new QLabel("PhotoBunny will receive the following info: your public profile and Photos.<br>"
+                                  "<i>Your public profile includes name, profile picture, age rangle, gender, language, country and other public info.<br>"
+                                  "(This does not let the app post to Facebook.)</i>");
+    
+    QHBoxLayout *buttons = new QHBoxLayout(this);
+    buttons->addStretch();
+    buttons->addWidget(okButton);
+    buttons->addWidget(cancelButton);
+    
+    layout->addWidget(msgLabel);
+    layout->addLayout(buttons);
+    
+    connect(okButton, SIGNAL(pressed()), this, SLOT(accept()));
+    connect(cancelButton, SIGNAL(pressed()), this, SLOT(reject()));
+}
+

+ 21 - 1
dialogs.h

@@ -13,6 +13,7 @@
 class QLineEdit;
 class QSpinBox;
 class QCheckBox;
+class PermissionDialog;
 
 class LoginDialog 
         : public QDialog {
@@ -30,13 +31,16 @@ class LoginDialog
     QPushButton *exitButton;
     QPushButton *clearButton;
     
+    PermissionDialog* perms;
+    
     public:
         explicit LoginDialog(FbApi *fb, QWidget *parent = 0);
         
     public slots:
         void okClicked();
-        void loggedIn(bool success);
+        void loggedIn(LoginStatus status);
         void clearCache();
+        void showDeclineBox();
 };
 #endif // LOGINDIALOG_H
 
@@ -101,3 +105,19 @@ class ScreenDialog
 };
 
 #endif // SCREENDIALOG_H
+
+
+#ifndef PERMISSIONDIALOG_H
+#define PERMISSIONDIALOG_H
+#include "common.h"
+
+class PermissionDialog
+        : public QDialog {
+    Q_OBJECT
+    
+    public:
+        explicit PermissionDialog(QWidget *parent = 0);
+        
+};
+
+#endif // WEBDIALOG_H

+ 19 - 12
fbApi.cpp

@@ -18,7 +18,6 @@
 #include <QWebFrame>
 #include <QWebElement>
 #include <QJsonDocument>
-
 #include <QSettings>
 
 FbApi::FbApi(const char* id, QObject *parent) 
@@ -27,7 +26,7 @@ FbApi::FbApi(const char* id, QObject *parent)
     //housekeeping
     net = new QNetworkAccessManager(this);
     page = new QWebPage(this);
-
+    
     //defaults
     appID = QString(id);
     token = "";
@@ -93,30 +92,34 @@ void FbApi::authenticate(QString userName, QString password) {
 
 void FbApi::doAuthentication(bool stat) {
     if (stat) {
-//        qDebug() << page->mainFrame()->url().toString(); //SENSITIVE_DEBUG
+        if (SENSITIVE_DEBUG) qDebug() << page->mainFrame()->url().toString(); //SENSITIVE_DEBUG
         
         QUrl url = QUrl(page->mainFrame()->url().toString().replace("#access_token", "?access_token"));
         QUrlQuery query(url);
         
         if (query.hasQueryItem("access_token")) {
-            qDebug() << "success";
-            //qDebug() <<query.queryItemValue("access_token"); //SENSITIVE_DEBUG
-            
+            qDebug() << "login success";
             token = query.queryItemValue("access_token");
             
+            if (SENSITIVE_DEBUG) qDebug() << token; //SENSITIVE_DEBUG
+            
             loginStatus = true;
-            emit loginResult(true);
+            emit loginResult(SUCCESS);
         }
         else if (query.hasQueryItem("login_attempt")) {
             qDebug() << "Wrong user/password";
             
             loginStatus = false;
-            emit loginResult(false);
+            emit loginResult(BAD_PASS);
+        }
+        else if (query.hasQueryItem("ret") && query.queryItemValue("ret") == "login") {
+            qDebug() << "Missing permissions";
+            loginStatus = false;
+            emit loginResult(BAD_PERM);
         }
-        
         else {
             qDebug() << "sending credentials";
-            //qDebug() << url.toString(); //SENSITIVE_DEBUG
+            if (SENSITIVE_DEBUG) qDebug() << url.toString(); //SENSITIVE_DEBUG
             
             page->mainFrame()->findFirstElement("input#email").setAttribute("value", *userName);
             page->mainFrame()->findFirstElement("input#pass").setAttribute("value", *password);
@@ -150,7 +153,7 @@ void FbApi::loadPictures() {
 void FbApi::loadPicList() {
     qDebug() << "processing response";
 
-//    qDebug() << resp->url().toString();  //SENSITIVE_DEBUG
+    if (SENSITIVE_DEBUG) qDebug() << resp->url().toString();  //SENSITIVE_DEBUG
     QJsonDocument doc = QJsonDocument::fromJson(resp->readAll());
     QVariant result = doc.toVariant();
     resp->close();
@@ -190,7 +193,7 @@ void FbApi::loadAlbums() {
 void FbApi::loadAlbumList() {
     qDebug() << "processing response";
     
-//    qDebug() << resp->url().toString();  //SENSITIVE_DEBUG
+    if (SENSITIVE_DEBUG) qDebug() << resp->url().toString();  //SENSITIVE_DEBUG
     QJsonDocument doc = QJsonDocument::fromJson(resp->readAll());
     QVariant result = doc.toVariant();
     resp->close();
@@ -254,3 +257,7 @@ void FbApi::setPicture() {
         imgCount++;
     }
 }
+
+void FbApi::acceptPermissions() {
+    page->mainFrame()->evaluateJavaScript("document.getElementsByName(\"__CONFIRM__\")[0].click()");
+}

+ 11 - 3
fbApi.h

@@ -19,6 +19,12 @@
 #include <QWebPage>
 #include "common.h"
 
+enum LoginStatus {
+    BAD_PERM,
+    BAD_PASS,
+    SUCCESS
+};
+
 class FbApi 
         : public QObject {
     Q_OBJECT
@@ -42,6 +48,8 @@ class FbApi
         bool highRes;
         bool caching;
         
+        QWebPage *page;
+
         QUrl getLoginUrl();
         void loadAlbums();
         void loadPictures();
@@ -54,7 +62,7 @@ class FbApi
         QList<QVariantMap> allAlbums();
         QStringList pictures();
         QString getTempPath();
-        
+
     private:
         QString *userName;
         QString *password;
@@ -63,10 +71,9 @@ class FbApi
         QNetworkReply *picResp;
         //QJson::Parser parser;
         QFile *pic;
-        QWebPage *page;
 
     signals:
-        void loginResult(bool res);
+        void loginResult(LoginStatus status);
         void albumsLoaded();
         void picsLoaded();
         void picLoaded(QString pic);
@@ -80,6 +87,7 @@ class FbApi
         void loadAlbumList();
         void setPicture();
         void doAuthentication(bool stat);
+        void acceptPermissions();
 };
 
 

+ 3 - 4
main.cpp

@@ -7,9 +7,8 @@
 #include <QApplication>
 #include <QtCore>
 
-void customMessageHandler(QtMsgType type, const char *msg) {
-    //Q_UNUSED(context);
-    
+void customMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
+    Q_UNUSED(context);
     
     QString dt = QDateTime::currentDateTime().toString("mm:ss");
     QString txt = QString("[%1] ").arg(dt);
@@ -50,7 +49,7 @@ int main(int argc, char *argv[]) {
     outFile.close();
     
     //replace qDebug handler
-    //qInstallMsgHandler(customMessageHandler);
+//    qInstallMessageHandler(customMessageHandler);
     
     QApplication a(argc, argv);
     SlideWindow w;

+ 5 - 5
version.h

@@ -1,11 +1,11 @@
 #ifndef VERSION_H
 #define VERSION_H
 
-#define VER_FILEVERSION             1,4,0,4
-#define VER_FILEVERSION_STR         "1.4.0.4\0"
+#define VER_FILEVERSION             1,5,1,4
+#define VER_FILEVERSION_STR         "1.5.1.4\0"
 
-#define VER_PRODUCTVERSION          1,4,0,4
-#define VER_PRODUCTVERSION_STR      "1.4\0"
+#define VER_PRODUCTVERSION          1,5,1,4
+#define VER_PRODUCTVERSION_STR      "1.5\0"
 
 #define VER_COMPANYNAME_STR         "GwillzCorp"
 #define VER_FILEDESCRIPTION_STR     "PhotoBunny"
@@ -16,6 +16,6 @@
 #define VER_ORIGINALFILENAME_STR    "photobunny.exe"
 #define VER_PRODUCTNAME_STR         "PhotoBunny"
 
-#define VER_COMPANYDOMAIN_STR       "gwillz.com"
+#define VER_COMPANYDOMAIN_STR       "gwillz.com.au"
 
 #endif // VERSION_H