Files
QtZeiterfassung/main.cpp
T

279 lines
10 KiB
C++
Raw Permalink Normal View History

2017-12-02 10:50:30 +01:00
#include <QApplication>
2017-12-05 20:43:56 +01:00
#include <QTranslator>
2017-12-10 14:42:40 +01:00
#include <QEventLoop>
2017-12-05 20:43:56 +01:00
#include <QMessageBox>
2017-12-02 10:50:30 +01:00
#include <QSplashScreen>
#include <QPixmap>
2017-12-12 21:19:17 +01:00
#include <QLocale>
#include <QTranslator>
2017-12-05 20:43:56 +01:00
#include <QDir>
2017-12-12 21:19:17 +01:00
#include <QFile>
#include <QTextStream>
#include <QFileInfo>
2017-12-02 10:50:30 +01:00
#include <QInputDialog>
2017-12-05 21:16:46 +01:00
#include <QStringBuilder>
2017-12-05 20:39:44 +01:00
#include <QDebug>
2017-12-03 22:22:07 +01:00
#include "zeiterfassungsettings.h"
2017-12-05 20:39:44 +01:00
#include "dialogs/languageselectiondialog.h"
2017-12-10 11:56:00 +01:00
#include "zeiterfassungapi.h"
2017-12-02 10:50:30 +01:00
#include "dialogs/authenticationdialog.h"
#include "mainwindow.h"
2017-12-10 14:42:40 +01:00
#include "replies/loginpagereply.h"
#include "replies/loginreply.h"
#include "replies/userinforeply.h"
2017-12-10 23:29:12 +01:00
#include "stripfactory.h"
2017-12-02 10:50:30 +01:00
2017-12-05 20:43:56 +01:00
bool loadAndInstallTranslator(QTranslator &translator,
const QLocale &locale,
const QString &filename,
const QString &prefix = QString(),
const QString &directory = QString(),
const QString &suffix = QString())
{
if(!translator.load(locale, filename, prefix, directory, suffix))
{
2017-12-10 23:23:00 +01:00
qWarning() << "could not load translation" << filename;
2017-12-05 20:43:56 +01:00
return false;
}
if(!QCoreApplication::installTranslator(&translator))
{
2017-12-10 23:23:00 +01:00
qWarning() << "could not install translation" << filename;
2017-12-05 20:43:56 +01:00
return false;
}
return true;
}
2017-12-02 10:50:30 +01:00
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
2017-12-02 10:50:30 +01:00
QApplication app(argc, argv);
2017-12-02 21:37:26 +01:00
qSetMessagePattern(QStringLiteral("%{time dd.MM.yyyy HH:mm:ss.zzz} "
"["
"%{if-debug}D%{endif}"
"%{if-info}I%{endif}"
"%{if-warning}W%{endif}"
"%{if-critical}C%{endif}"
"%{if-fatal}F%{endif}"
"] "
"%{function}(): "
"%{message}"));
2017-12-02 21:37:26 +01:00
2017-12-02 10:50:30 +01:00
QCoreApplication::setOrganizationDomain(QStringLiteral("brunner.ninja"));
QCoreApplication::setOrganizationName(QStringLiteral("db-software"));
QCoreApplication::setApplicationName(QStringLiteral("zeiterfassung"));
2017-12-14 00:41:23 +01:00
QCoreApplication::setApplicationVersion(QStringLiteral("1.3.1"));
2017-12-02 10:50:30 +01:00
QSplashScreen splashScreen(QPixmap(QStringLiteral(":/zeiterfassung/images/splash.png")));
2017-12-05 21:16:46 +01:00
splashScreen.showMessage(QCoreApplication::translate("main", "Loading settings..."));
2017-12-02 10:50:30 +01:00
splashScreen.show();
2017-12-03 22:22:07 +01:00
ZeiterfassungSettings settings(&app);
2017-12-02 10:50:30 +01:00
2017-12-12 21:19:17 +01:00
splashScreen.showMessage(QCoreApplication::translate("main", "Loading translations..."));
2017-12-05 20:39:44 +01:00
if(settings.language() == QLocale::AnyLanguage)
{
LanguageSelectionDialog dialog(&splashScreen);
again0:
if(dialog.exec() != QDialog::Accepted)
return -1;
if(dialog.language() == QLocale::AnyLanguage)
{
2017-12-05 21:16:46 +01:00
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Invalid language selection!"),
QCoreApplication::translate("main", "Invalid language selection!") % "\n\n" %
QCoreApplication::translate("main", "You did not select a valid language!"));
2017-12-05 20:39:44 +01:00
goto again0;
}
settings.setLanguage(dialog.language());
}
2017-12-05 20:49:35 +01:00
QLocale locale(settings.language(), QLocale::Austria);
QLocale::setDefault(locale);
2017-12-05 20:39:44 +01:00
2017-12-05 20:43:56 +01:00
QTranslator qtTranslator(&app);
QTranslator zeiterfassungTranslator(&app);
{
auto translationsDir = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(QStringLiteral("translations"));
loadAndInstallTranslator(qtTranslator, locale, QStringLiteral("qt"), QStringLiteral("_"), translationsDir);
loadAndInstallTranslator(zeiterfassungTranslator, locale, QStringLiteral("zeiterfassung"), QStringLiteral("_"), translationsDir);
}
2017-12-05 20:43:56 +01:00
2017-12-12 21:19:17 +01:00
if(!settings.theme().isEmpty())
{
splashScreen.showMessage(QCoreApplication::translate("main", "Loading theme..."));
auto themePath = QDir(QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(QStringLiteral("themes"))).absoluteFilePath(settings.theme());
QFile file(themePath % ".qss");
if(!file.exists())
{
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not load theme!"),
QCoreApplication::translate("main", "Could not load theme!") % "\n\n" %
QCoreApplication::translate("main", "Theme file does not exist!"));
goto after;
}
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not load theme!"),
QCoreApplication::translate("main", "Could not load theme!") % "\n\n" %
file.errorString());
goto after;
}
QTextStream textStream(&file);
app.setStyleSheet(textStream.readAll().replace(QStringLiteral("@THEME_RESOURCES@"), themePath));
}
after:
2017-12-05 21:16:46 +01:00
splashScreen.showMessage(QCoreApplication::translate("main", "Loading login page..."));
2017-12-02 10:50:30 +01:00
2017-12-10 11:56:00 +01:00
ZeiterfassungApi erfassung(settings.url(), &app);
2017-12-02 10:50:30 +01:00
{
again1:
2017-12-10 14:42:40 +01:00
auto reply = erfassung.doLoginPage();
2017-12-02 10:50:30 +01:00
2017-12-10 14:42:40 +01:00
{
QEventLoop eventLoop;
QObject::connect(reply.get(), &ZeiterfassungReply::finished, &eventLoop, &QEventLoop::quit);
2017-12-10 14:42:40 +01:00
eventLoop.exec();
}
if(!reply->success())
2017-12-02 10:50:30 +01:00
{
bool ok;
2017-12-05 21:16:46 +01:00
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not access Zeiterfassung!"),
2017-12-10 14:42:40 +01:00
QCoreApplication::translate("main", "Could not access Zeiterfassung!") % "\n\n" % reply->message());
2017-12-02 10:50:30 +01:00
2017-12-05 21:16:46 +01:00
auto url = QInputDialog::getText(&splashScreen, QCoreApplication::translate("main", "Base url"),
QCoreApplication::translate("main", "Please enter the base url to the Zeiterfassung:"),
2017-12-03 22:22:07 +01:00
QLineEdit::Normal, settings.url(), &ok);
2017-12-02 10:50:30 +01:00
if(!ok)
return -1;
2017-12-03 22:22:07 +01:00
settings.setUrl(url);
2017-12-02 10:50:30 +01:00
erfassung.setUrl(url);
2017-12-10 14:42:40 +01:00
2017-12-02 10:50:30 +01:00
goto again1;
}
}
2017-12-05 21:16:46 +01:00
splashScreen.showMessage(QCoreApplication::translate("main", "Authenticating..."));
2017-12-02 10:50:30 +01:00
2017-12-03 22:22:07 +01:00
if(settings.username().isNull() || settings.password().isNull())
2017-12-02 10:50:30 +01:00
{
AuthenticationDialog dialog(&splashScreen);
if(dialog.exec() != QDialog::Accepted)
return -1;
2017-12-03 22:22:07 +01:00
settings.setUsername(dialog.username());
settings.setPassword(dialog.password());
2017-12-02 10:50:30 +01:00
}
{
again2:
2017-12-10 14:42:40 +01:00
auto reply = erfassung.doLogin(settings.username(), settings.password());
2017-12-02 10:50:30 +01:00
2017-12-10 14:42:40 +01:00
{
QEventLoop eventLoop;
QObject::connect(reply.get(), &ZeiterfassungReply::finished, &eventLoop, &QEventLoop::quit);
2017-12-10 14:42:40 +01:00
eventLoop.exec();
}
if(!reply->success())
2017-12-02 10:50:30 +01:00
{
2017-12-05 21:16:46 +01:00
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not authenticate with Zeiterfassung!"),
2017-12-10 14:42:40 +01:00
QCoreApplication::translate("main", "Could not authenticate with Zeiterfassung!") % "\n\n" % reply->message());
2017-12-02 10:50:30 +01:00
AuthenticationDialog dialog(&splashScreen);
2017-12-03 22:22:07 +01:00
dialog.setUsername(settings.username());
dialog.setPassword(settings.password());
2017-12-02 10:50:30 +01:00
if(dialog.exec() != QDialog::Accepted)
return -1;
2017-12-03 22:22:07 +01:00
settings.setUsername(dialog.username());
settings.setPassword(dialog.password());
2017-12-02 10:50:30 +01:00
goto again2;
}
}
2017-12-05 21:16:46 +01:00
splashScreen.showMessage(QCoreApplication::translate("main", "Getting user information..."));
2017-12-02 10:50:30 +01:00
2017-12-10 11:56:00 +01:00
ZeiterfassungApi::UserInfo userInfo;
2017-12-02 10:50:30 +01:00
{
2017-12-10 14:42:40 +01:00
auto reply = erfassung.doUserInfo();
2017-12-02 10:50:30 +01:00
2017-12-10 14:42:40 +01:00
{
QEventLoop eventLoop;
QObject::connect(reply.get(), &ZeiterfassungReply::finished, &eventLoop, &QEventLoop::quit);
2017-12-10 14:42:40 +01:00
eventLoop.exec();
}
2017-12-02 10:50:30 +01:00
2017-12-10 14:42:40 +01:00
if(!reply->success())
2017-12-02 10:50:30 +01:00
{
2017-12-05 21:16:46 +01:00
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not get user information!"),
2017-12-10 14:42:40 +01:00
QCoreApplication::translate("main", "Could not get user information!") % "\n\n" % reply->message());
2017-12-02 10:50:30 +01:00
return -1;
}
2017-12-10 14:42:40 +01:00
userInfo = reply->userInfo();
2017-12-02 10:50:30 +01:00
}
splashScreen.showMessage(QCoreApplication::translate("main", "Loading strip layouts..."));
2017-12-10 23:29:12 +01:00
StripFactory stripFactory(&app);
if(!stripFactory.load(QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(QStringLiteral("strips"))))
2017-12-10 23:29:12 +01:00
{
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not load strips!"),
QCoreApplication::translate("main", "Could not load strips!") % "\n\n" % stripFactory.errorString());
return -1;
}
{
auto widget = stripFactory.createBookingStartStrip();
if(!widget)
{
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not load strips!"),
QCoreApplication::translate("main", "Could not load strips!") % "\n\n" % stripFactory.errorString());
return -1;
}
}
{
auto widget = stripFactory.createBookingEndStrip();
if(!widget)
{
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not load strips!"),
QCoreApplication::translate("main", "Could not load strips!") % "\n\n" % stripFactory.errorString());
return -1;
}
}
{
auto widget = stripFactory.createTimeAssignmentStrip();
if(!widget)
{
QMessageBox::warning(&splashScreen, QCoreApplication::translate("main", "Could not load strips!"),
QCoreApplication::translate("main", "Could not load strips!") % "\n\n" % stripFactory.errorString());
return -1;
}
}
2017-12-10 23:29:12 +01:00
MainWindow mainWindow(settings, erfassung, userInfo, stripFactory);
2017-12-02 10:50:30 +01:00
mainWindow.show();
splashScreen.finish(&mainWindow);
return app.exec();
}