From c3a8920fa5ede81dd540768807fd32dd76a9435d Mon Sep 17 00:00:00 2001 From: niansa Date: Sun, 23 Aug 2020 12:20:49 +0200 Subject: [PATCH] Minor code structure and UI improvements --- main.cpp | 138 ++++++++++++++++++++++++++++------------------------ overview.ui | 2 +- 2 files changed, 76 insertions(+), 64 deletions(-) diff --git a/main.cpp b/main.cpp index 65ccbaa..e855fc1 100644 --- a/main.cpp +++ b/main.cpp @@ -31,7 +31,11 @@ #define QCommsy_NAME "QCommsy" #define QCommsy_VERSION "0.3-beta" +#define UPDATE_WINDOW(a) a->processEvents(); + static QSettings *settings; +static QApplication *a; +static QMainWindow *w; class _UILoader { @@ -40,12 +44,12 @@ class _UILoader { bool cacheInvalidate = true; public: - void failureWindow(QMainWindow *w); - void overviewWindow(QMainWindow *w, bool catchInvalidRoomError = true); - void loginWindow(QMainWindow *w, const QString& failure = ""); - void infoWindow(QMainWindow *w); - void postViewWindow(QMainWindow *w, commsyPost *thispost, const QString& description); - void offlineWindow(QMainWindow *w); + void failureWindow(); + void overviewWindow(bool catchInvalidRoomError = true); + void loginWindow(const QString& failure = ""); + void infoWindow(); + void postViewWindow(commsyPost *thispost, const QString& description); + void offlineWindow(); _UILoader () { auther = new libCommsyAuth(); @@ -69,14 +73,14 @@ public: settings->remove("server_sid"); } - void downloadAndOpenFile(QMainWindow *w, QNetworkRequest mNetReq, const QString& filename) { - auto mNetMan = new QNetworkAccessManager(w); + void downloadAndOpenFile(QNetworkRequest mNetReq, const QString& filename) { + auto mNetMan = new QNetworkAccessManager(); // Set SID in request so we're authenticated TODO: think about a cleaner way mNetReq.setRawHeader("Cookie", "SID=" + settings->value("server_sid").toString().toUtf8()); // Connect to downloading - w->connect(mNetMan, &QNetworkAccessManager::finished, [this, w, mNetMan, filename] (QNetworkReply *mNetReply) { + w->connect(mNetMan, &QNetworkAccessManager::finished, [this, mNetMan, filename] (QNetworkReply *mNetReply) { // Save and open downloaded file { // Initialise @@ -85,7 +89,7 @@ public: // Write downloaded data to file if (!destFile.open(QIODevice::WriteOnly)) { - return failureWindow(w); + return failureWindow(); } destFile.write(mNetReply->readAll()); destFile.close(); @@ -105,7 +109,7 @@ public: }; -void _UILoader::infoWindow(QMainWindow *w) { +void _UILoader::infoWindow() { Ui::infoWindow *thisui = new Ui::infoWindow; // Show initial UI @@ -124,10 +128,10 @@ void _UILoader::infoWindow(QMainWindow *w) { ); // Add button handlers - w->connect(thisui->backButton, &QPushButton::pressed, [this, w] () { loginWindow(w); }); + w->connect(thisui->backButton, &QPushButton::pressed, [this] () { loginWindow(); }); } -void _UILoader::failureWindow(QMainWindow *w) { +void _UILoader::failureWindow() { Ui::failureWindow *thisui = new Ui::failureWindow; QString reason; @@ -143,26 +147,26 @@ void _UILoader::failureWindow(QMainWindow *w) { thisui->descriptionText->setText("Verbindung fehlgeschlagen
" + reason); // Add button handlers - w->connect(thisui->retryButton, &QPushButton::pressed, [this, w] () { loginWindow(w); }); - w->connect(thisui->logoutButton, &QPushButton::pressed, [this, w] () { logout(); loginWindow(w); }); + w->connect(thisui->retryButton, &QPushButton::pressed, [this] () { loginWindow(); }); + w->connect(thisui->logoutButton, &QPushButton::pressed, [this] () { logout(); loginWindow(); }); } -void _UILoader::offlineWindow(QMainWindow *w) { +void _UILoader::offlineWindow() { Ui::offlineWindow *thisui = new Ui::offlineWindow; // Show initial UI thisui->setupUi(w); w->show(); // Add button handlers - w->connect(thisui->logoutButton, &QPushButton::pressed, [this, w] () { logout(); loginWindow(w); }); + w->connect(thisui->logoutButton, &QPushButton::pressed, [this] () { logout(); loginWindow(); }); } -void _UILoader::loginWindow(QMainWindow *w, const QString& failure) { +void _UILoader::loginWindow(const QString& failure) { Ui::loginWindow *thisui = new Ui::loginWindow; // Check if logged in already if (settings->contains("server_sid")) { - return overviewWindow(w); + return overviewWindow(); } // Show initial UI @@ -186,13 +190,13 @@ void _UILoader::loginWindow(QMainWindow *w, const QString& failure) { w->connect(auther, &libCommsyAuth::authDone, [thisui] (QNetworkReply *) { // Finished authentication; reenable login button thisui->loginButton->setDisabled(false); }); - w->connect(auther, &libCommsyAuth::authDone, [this, thisui, w] (QNetworkReply *netReply) { // Finished authentication; try to login + w->connect(auther, &libCommsyAuth::authDone, [this, thisui] (QNetworkReply *netReply) { // Finished authentication; try to login // Login try { settings->setValue("server_sid", auther->getSID(netReply)); auther->deleteLater(); cacheInvalidate = true; - return overviewWindow(w, false); + return overviewWindow(false); } catch (authFailureError&) { thisui->failureText->setText("Die eingegebenen Anmeldedaten sind ungültig"); } catch (invalidRoomError&) { @@ -233,7 +237,7 @@ void _UILoader::loginWindow(QMainWindow *w, const QString& failure) { }); } -void _UILoader::postViewWindow(QMainWindow *w, commsyPost *thispost, const QString& description) { +void _UILoader::postViewWindow(commsyPost *thispost, const QString& description) { Ui::postViewWindow *thisui = new Ui::postViewWindow; // Show initial UI @@ -252,64 +256,72 @@ void _UILoader::postViewWindow(QMainWindow *w, commsyPost *thispost, const QStri } // Add button handlers - w->connect(thisui->fileList, &QListWidget::activated, [this, w, thisui, thispost] (QModelIndex index) { + w->connect(thisui->fileList, &QListWidget::activated, [this, thisui, thispost] (QModelIndex index) { // Get full file URL commsyFile *thisfile = &thispost->files[static_cast(index.row())]; QUrl fullFileUrl(settings->value("server_url").toString() + QString::fromStdString(thisfile->url)); // Start download - downloadAndOpenFile(w, QNetworkRequest(fullFileUrl), QString::fromStdString(thisfile->name)); + downloadAndOpenFile(QNetworkRequest(fullFileUrl), QString::fromStdString(thisfile->name)); // Show download status für 5 seconds thisui->statusText->setVisible(true); QTimer::singleShot(5 * 1000, thisui->statusText, &QLabel::hide); }); - w->connect(thisui->backButton, &QPushButton::pressed, [this, w] () { loginWindow(w); }); + w->connect(thisui->backButton, &QPushButton::pressed, [this] () { loginWindow(); }); } -void _UILoader::overviewWindow(QMainWindow *w, bool catchInvalidRoomError) { +void _UILoader::overviewWindow(bool catchInvalidRoomError) { Ui::overviewWindow *thisui = new Ui::overviewWindow; - // Connect if required - try { - reconnect(); - } catch (invalidSIDError&) { - logout(); - return loginWindow(w, "Die Sitzung ist abgelaufen - bitte erneut anmelden"); - } catch (invalidRoomError& exception) { - - if (catchInvalidRoomError) { - logout(); - return loginWindow(w, "Der Raum existiert nicht mehr"); - } else { - throw exception; - } - - } catch (connectionFailError&) { - return offlineWindow(w); - } catch (std::exception&) { - return failureWindow(w); - } - // Show initial UI thisui->setupUi(w); w->show(); - // Refresh view - thisui->postCounter->setText(QString::number(connector->numposts) + " Einträge"); - thisui->postList->clear(); - for (const auto& thispost : connector->posts) { - thisui->postList->addItem(QString::fromStdString(thispost.name)); - } - // Add button handlers - w->connect(thisui->postList, &QListWidget::activated, [this, w] (QModelIndex index) { + w->connect(thisui->postList, &QListWidget::activated, [this, thisui] (QModelIndex index) { auto postID = static_cast(index.row()); - postViewWindow(w, connector->getPost(postID), QString::fromStdString(*connector->getDescription(postID))); + thisui->centralwidget->setDisabled(true); + + // Run blocking code + UPDATE_WINDOW(a) { + postViewWindow(connector->getPost(postID), QString::fromStdString(*connector->getDescription(postID))); + } }); - w->connect(thisui->logoutButton, &QPushButton::pressed, [this, w] () { logout(); loginWindow(w); }); - w->connect(thisui->infoButton, &QPushButton::pressed, [this, w] () { infoWindow(w); }); + w->connect(thisui->logoutButton, &QPushButton::pressed, [this] () { logout(); loginWindow(); }); + w->connect(thisui->infoButton, &QPushButton::pressed, [this] () { infoWindow(); }); + + // Run blocking code + UPDATE_WINDOW(a) { + // Connect if required + try { + reconnect(); + } catch (invalidSIDError&) { + logout(); + return loginWindow("Die Sitzung ist abgelaufen - bitte erneut anmelden"); + } catch (invalidRoomError& exception) { + + if (catchInvalidRoomError) { + logout(); + return loginWindow("Der Raum existiert nicht mehr"); + } else { + throw exception; + } + + } catch (connectionFailError&) { + return offlineWindow(); + } catch (std::exception&) { + return failureWindow(); + } + + // Refresh view + thisui->postCounter->setText(QString::number(connector->numposts) + " Einträge"); + thisui->postList->clear(); + for (const auto& thispost : connector->posts) { + thisui->postList->addItem(QString::fromStdString(thispost.name)); + } + } } @@ -323,16 +335,16 @@ int main(int argc, char *argv[]) { settings = new QSettings(); //Initialise variables - QApplication a(argc, argv); - QMainWindow w; + a = new QApplication(argc, argv); + w = new QMainWindow; _UILoader UILoader; // Set theme - a.setStyle(QStyleFactory::create("android")); + a->setStyle(QStyleFactory::create("android")); // Load initial window - UILoader.loginWindow(&w); + UILoader.loginWindow(); // Run forever - return a.exec(); + return a->exec(); } diff --git a/overview.ui b/overview.ui index 80e8b49..281e6f4 100644 --- a/overview.ui +++ b/overview.ui @@ -27,7 +27,7 @@ - QCommsy + Lade...