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

108
main.cpp
View file

@ -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("<b>Verbindung fehlgeschlagen</b><br />" + 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<unsigned long>(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;
// Show initial UI
thisui->setupUi(w);
w->show();
// Add button handlers
w->connect(thisui->postList, &QListWidget::activated, [this, thisui] (QModelIndex index) {
auto postID = static_cast<unsigned long>(index.row());
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] () { 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(w, "Die Sitzung ist abgelaufen - bitte erneut anmelden");
return loginWindow("Die Sitzung ist abgelaufen - bitte erneut anmelden");
} catch (invalidRoomError& exception) {
if (catchInvalidRoomError) {
logout();
return loginWindow(w, "Der Raum existiert nicht mehr");
return loginWindow("Der Raum existiert nicht mehr");
} else {
throw exception;
}
} catch (connectionFailError&) {
return offlineWindow(w);
return offlineWindow();
} catch (std::exception&) {
return failureWindow(w);
return failureWindow();
}
// 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) {
auto postID = static_cast<unsigned long>(index.row());
postViewWindow(w, 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); });
}
}
@ -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();
}

View file

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