1
0
Fork 0
mirror of https://gitlab.com/niansa/qcommsy.git synced 2025-03-06 20:53:33 +01:00

Minor code structure and UI improvements

This commit is contained in:
niansa 2020-08-23 12:20:49 +02:00
parent b6062d0bb4
commit c3a8920fa5
2 changed files with 76 additions and 64 deletions

138
main.cpp
View file

@ -31,7 +31,11 @@
#define QCommsy_NAME "QCommsy" #define QCommsy_NAME "QCommsy"
#define QCommsy_VERSION "0.3-beta" #define QCommsy_VERSION "0.3-beta"
#define UPDATE_WINDOW(a) a->processEvents();
static QSettings *settings; static QSettings *settings;
static QApplication *a;
static QMainWindow *w;
class _UILoader { class _UILoader {
@ -40,12 +44,12 @@ class _UILoader {
bool cacheInvalidate = true; bool cacheInvalidate = true;
public: public:
void failureWindow(QMainWindow *w); void failureWindow();
void overviewWindow(QMainWindow *w, bool catchInvalidRoomError = true); void overviewWindow(bool catchInvalidRoomError = true);
void loginWindow(QMainWindow *w, const QString& failure = ""); void loginWindow(const QString& failure = "");
void infoWindow(QMainWindow *w); void infoWindow();
void postViewWindow(QMainWindow *w, commsyPost *thispost, const QString& description); void postViewWindow(commsyPost *thispost, const QString& description);
void offlineWindow(QMainWindow *w); void offlineWindow();
_UILoader () { _UILoader () {
auther = new libCommsyAuth(); auther = new libCommsyAuth();
@ -69,14 +73,14 @@ public:
settings->remove("server_sid"); settings->remove("server_sid");
} }
void downloadAndOpenFile(QMainWindow *w, QNetworkRequest mNetReq, const QString& filename) { void downloadAndOpenFile(QNetworkRequest mNetReq, const QString& filename) {
auto mNetMan = new QNetworkAccessManager(w); auto mNetMan = new QNetworkAccessManager();
// Set SID in request so we're authenticated TODO: think about a cleaner way // Set SID in request so we're authenticated TODO: think about a cleaner way
mNetReq.setRawHeader("Cookie", "SID=" + settings->value("server_sid").toString().toUtf8()); mNetReq.setRawHeader("Cookie", "SID=" + settings->value("server_sid").toString().toUtf8());
// Connect to downloading // 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 // Save and open downloaded file
{ {
// Initialise // Initialise
@ -85,7 +89,7 @@ public:
// Write downloaded data to file // Write downloaded data to file
if (!destFile.open(QIODevice::WriteOnly)) { if (!destFile.open(QIODevice::WriteOnly)) {
return failureWindow(w); return failureWindow();
} }
destFile.write(mNetReply->readAll()); destFile.write(mNetReply->readAll());
destFile.close(); destFile.close();
@ -105,7 +109,7 @@ public:
}; };
void _UILoader::infoWindow(QMainWindow *w) { void _UILoader::infoWindow() {
Ui::infoWindow *thisui = new Ui::infoWindow; Ui::infoWindow *thisui = new Ui::infoWindow;
// Show initial UI // Show initial UI
@ -124,10 +128,10 @@ void _UILoader::infoWindow(QMainWindow *w) {
); );
// Add button handlers // 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; Ui::failureWindow *thisui = new Ui::failureWindow;
QString reason; QString reason;
@ -143,26 +147,26 @@ void _UILoader::failureWindow(QMainWindow *w) {
thisui->descriptionText->setText("<b>Verbindung fehlgeschlagen</b><br />" + reason); thisui->descriptionText->setText("<b>Verbindung fehlgeschlagen</b><br />" + reason);
// Add button handlers // Add button handlers
w->connect(thisui->retryButton, &QPushButton::pressed, [this, w] () { loginWindow(w); }); w->connect(thisui->retryButton, &QPushButton::pressed, [this] () { loginWindow(); });
w->connect(thisui->logoutButton, &QPushButton::pressed, [this, w] () { logout(); loginWindow(w); }); w->connect(thisui->logoutButton, &QPushButton::pressed, [this] () { logout(); loginWindow(); });
} }
void _UILoader::offlineWindow(QMainWindow *w) { void _UILoader::offlineWindow() {
Ui::offlineWindow *thisui = new Ui::offlineWindow; Ui::offlineWindow *thisui = new Ui::offlineWindow;
// Show initial UI // Show initial UI
thisui->setupUi(w); thisui->setupUi(w);
w->show(); w->show();
// Add button handlers // 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; Ui::loginWindow *thisui = new Ui::loginWindow;
// Check if logged in already // Check if logged in already
if (settings->contains("server_sid")) { if (settings->contains("server_sid")) {
return overviewWindow(w); return overviewWindow();
} }
// Show initial UI // 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 w->connect(auther, &libCommsyAuth::authDone, [thisui] (QNetworkReply *) { // Finished authentication; reenable login button
thisui->loginButton->setDisabled(false); 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 // Login
try { try {
settings->setValue("server_sid", auther->getSID(netReply)); settings->setValue("server_sid", auther->getSID(netReply));
auther->deleteLater(); auther->deleteLater();
cacheInvalidate = true; cacheInvalidate = true;
return overviewWindow(w, false); return overviewWindow(false);
} catch (authFailureError&) { } catch (authFailureError&) {
thisui->failureText->setText("Die eingegebenen Anmeldedaten sind ungültig"); thisui->failureText->setText("Die eingegebenen Anmeldedaten sind ungültig");
} catch (invalidRoomError&) { } 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; Ui::postViewWindow *thisui = new Ui::postViewWindow;
// Show initial UI // Show initial UI
@ -252,64 +256,72 @@ void _UILoader::postViewWindow(QMainWindow *w, commsyPost *thispost, const QStri
} }
// Add button handlers // 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 // Get full file URL
commsyFile *thisfile = &thispost->files[static_cast<unsigned long>(index.row())]; commsyFile *thisfile = &thispost->files[static_cast<unsigned long>(index.row())];
QUrl fullFileUrl(settings->value("server_url").toString() + QString::fromStdString(thisfile->url)); QUrl fullFileUrl(settings->value("server_url").toString() + QString::fromStdString(thisfile->url));
// Start download // Start download
downloadAndOpenFile(w, QNetworkRequest(fullFileUrl), QString::fromStdString(thisfile->name)); downloadAndOpenFile(QNetworkRequest(fullFileUrl), QString::fromStdString(thisfile->name));
// Show download status für 5 seconds // Show download status für 5 seconds
thisui->statusText->setVisible(true); thisui->statusText->setVisible(true);
QTimer::singleShot(5 * 1000, thisui->statusText, &QLabel::hide); 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; 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 // Show initial UI
thisui->setupUi(w); thisui->setupUi(w);
w->show(); 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 // 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<unsigned long>(index.row()); auto postID = static_cast<unsigned long>(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->logoutButton, &QPushButton::pressed, [this] () { logout(); loginWindow(); });
w->connect(thisui->infoButton, &QPushButton::pressed, [this, w] () { infoWindow(w); }); 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(); settings = new QSettings();
//Initialise variables //Initialise variables
QApplication a(argc, argv); a = new QApplication(argc, argv);
QMainWindow w; w = new QMainWindow;
_UILoader UILoader; _UILoader UILoader;
// Set theme // Set theme
a.setStyle(QStyleFactory::create("android")); a->setStyle(QStyleFactory::create("android"));
// Load initial window // Load initial window
UILoader.loginWindow(&w); UILoader.loginWindow();
// Run forever // Run forever
return a.exec(); return a->exec();
} }

View file

@ -27,7 +27,7 @@
<item> <item>
<widget class="QLabel" name="postCounter"> <widget class="QLabel" name="postCounter">
<property name="text"> <property name="text">
<string>QCommsy</string> <string>Lade...</string>
</property> </property>
</widget> </widget>
</item> </item>