Cleanup main (#196)

Co-authored-by: Chris Rizzitello <crizzitello@ics.com>
main
crizzitello 2022-07-16 03:50:45 -04:00 committed by GitHub
parent 46ceb4d1d8
commit 45ee47b145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 65 deletions

View File

@ -1,93 +1,54 @@
// Copyright 2020, Verizon Media // Copyright 2020, Verizon Media
// Licensed under the terms of MIT. See LICENSE file in project root for terms. // Licensed under the terms of MIT. See LICENSE file in project root for terms.
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
void handleCLI(std::vector<std::string> args);
#ifndef QT_NO_SYSTEMTRAYICON
#include <QApplication> #include <QApplication>
#include <QMessageBox> #include <QMessageBox>
#include <QMetaType> #include <QMetaType>
#include "db/databaseconnection.h" #include "db/databaseconnection.h"
#include "exceptions/fileerror.h"
#include "traymanager.h" #include "traymanager.h"
QIcon getWindowIcon(); QIcon getWindowIcon() { return QIcon(QStringLiteral(":icons/windowIcon.png")); }
int main(int argc, char* argv[]) { void showMsgBox(const QString &errorText = QString())
{
QMessageBox::critical(nullptr, QT_TRANSLATE_NOOP("main", "ASHIRT Error"), errorText);
}
int main(int argc, char* argv[])
{
Q_INIT_RESOURCE(res_icons); Q_INIT_RESOURCE(res_icons);
Q_INIT_RESOURCE(res_migrations); Q_INIT_RESOURCE(res_migrations);
QCoreApplication::setApplicationName("ashirt"); QCoreApplication::setApplicationName(QStringLiteral("ashirt"));
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QCoreApplication::setOrganizationName("ashirt"); QCoreApplication::setOrganizationName(QCoreApplication::applicationName());
#endif #endif
DatabaseConnection* conn = new DatabaseConnection(Constants::dbLocation, Constants::defaultDbName); QApplication app(argc, argv);
if (!conn->connect()) { app.setWindowIcon(getWindowIcon());
QMessageBox::critical(nullptr, QStringLiteral("ASHIRT Error"), QStringLiteral("Unable to connect to database"));
if(!QSystemTrayIcon::isSystemTrayAvailable()) {
showMsgBox(QT_TRANSLATE_NOOP("main", "A System tray is required to interact with the application"));
return -1; return -1;
} }
int rtn; auto conn = new DatabaseConnection(Constants::dbLocation, Constants::defaultDbName);
try { if(!conn->connect()) {
QApplication app(argc, argv); showMsgBox(QT_TRANSLATE_NOOP("main", "Unable to connect to database"));
qRegisterMetaType<model::Tag>(); return -1;
app.setWindowIcon(getWindowIcon());
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
handleCLI(std::vector<std::string>(argv, argv + argc));
} }
QApplication::setQuitOnLastWindowClosed(false);
app.setQuitOnLastWindowClosed(false);
qRegisterMetaType<model::Tag>();
auto window = new TrayManager(nullptr, conn);
QObject::connect(&app, &QApplication::aboutToQuit, [conn] { QObject::connect(&app, &QApplication::aboutToQuit, [conn] {
conn->close(); conn->close();
delete conn; delete conn;
}); });
auto window = new TrayManager(nullptr, conn); int rtn = app.exec();
rtn = app.exec();
window->deleteLater(); window->deleteLater();
}
catch (std::exception const& ex) {
qWarning() << "Exception while running: " << ex.what();
}
catch (...) {
qWarning() << "Unhandled exception while running";
}
return rtn; return rtn;
} }
QIcon getWindowIcon() {
return QIcon(QStringLiteral(":icons/windowIcon.png"));
}
#else
#include <QDebug>
#include <QLabel>
int main(int argc, char *argv[]) { handleCLI(std::vector<string>(argv, argv + argc)); }
#endif
void handleCLI(std::vector<std::string> args) {
size_t trueCount = args.size() - 1;
qInfo() << "You provided " << trueCount << " arguments";
if (trueCount == 0) {
qInfo() << "Next time try suppling some arguments.";
return;
}
qInfo() << "All arguments:";
for (size_t i = 1; i < args.size(); i++) {
qInfo() << "\t" << QString::fromStdString(args.at(i));
}
}