mirror of
https://gitlab.com/niansa/qcommsy.git
synced 2025-03-06 20:53:33 +01:00
70 lines
2.3 KiB
C++
70 lines
2.3 KiB
C++
class _UILoader {
|
|
libCommsy *connector = nullptr;
|
|
libCommsyAuth *auther = nullptr;
|
|
bool cacheInvalidate = true;
|
|
|
|
public:
|
|
void failureWindow();
|
|
void overviewWindow(bool catchInvalidRoomError = true);
|
|
void loginWindow(const QString& failure = "");
|
|
void infoWindow();
|
|
void postViewWindow(commsyPost *thispost, QString description);
|
|
void offlineWindow();
|
|
|
|
_UILoader () {
|
|
auther = new libCommsyAuth();
|
|
}
|
|
|
|
void reconnect() {
|
|
// Destroy old connector
|
|
if (connector != nullptr) {
|
|
if (not cacheInvalidate) {
|
|
return;
|
|
}
|
|
delete connector;
|
|
connector = nullptr;
|
|
}
|
|
cacheInvalidate = false;
|
|
// Connect to commsy
|
|
connector = new libCommsy(settings->value("server_url").toString().toStdString(), settings->value("server_sid").toString().toStdString(), settings->value("server_room").toString().toStdString());
|
|
}
|
|
|
|
void logout() {
|
|
settings->remove("server_sid");
|
|
}
|
|
|
|
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, mNetMan, filename] (QNetworkReply *mNetReply) {
|
|
// Save and open downloaded file
|
|
{
|
|
// Initialise
|
|
QString destPath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + '/' + filename;
|
|
QFile destFile(destPath);
|
|
|
|
// Write downloaded data to file
|
|
if (!destFile.open(QIODevice::WriteOnly)) {
|
|
return failureWindow();
|
|
}
|
|
destFile.write(mNetReply->readAll());
|
|
destFile.close();
|
|
|
|
// Open file in some application
|
|
//QDesktopServices::openUrl(destPath);
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(destPath));
|
|
}
|
|
|
|
// Clean up
|
|
mNetMan->deleteLater();
|
|
});
|
|
|
|
// Start download
|
|
mNetMan->get(mNetReq);
|
|
}
|
|
|
|
};
|