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
// 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 <QMessageBox>
#include <QMetaType>
#include "db/databaseconnection.h"
#include "exceptions/fileerror.h"
#include "traymanager.h"
QIcon getWindowIcon();
QIcon getWindowIcon() { return QIcon(QStringLiteral(":icons/windowIcon.png")); }
int main(int argc, char* argv[]) {
Q_INIT_RESOURCE(res_icons);
Q_INIT_RESOURCE(res_migrations);
void showMsgBox(const QString &errorText = QString())
{
QMessageBox::critical(nullptr, QT_TRANSLATE_NOOP("main", "ASHIRT Error"), errorText);
}
QCoreApplication::setApplicationName("ashirt");
int main(int argc, char* argv[])
{
Q_INIT_RESOURCE(res_icons);
Q_INIT_RESOURCE(res_migrations);
QCoreApplication::setApplicationName(QStringLiteral("ashirt"));
#ifdef Q_OS_WIN
QCoreApplication::setOrganizationName("ashirt");
QCoreApplication::setOrganizationName(QCoreApplication::applicationName());
#endif
DatabaseConnection* conn = new DatabaseConnection(Constants::dbLocation, Constants::defaultDbName);
if (!conn->connect()) {
QMessageBox::critical(nullptr, QStringLiteral("ASHIRT Error"), QStringLiteral("Unable to connect to database"));
return -1;
}
int rtn;
try {
QApplication app(argc, argv);
qRegisterMetaType<model::Tag>();
app.setWindowIcon(getWindowIcon());
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
handleCLI(std::vector<std::string>(argv, argv + argc));
if(!QSystemTrayIcon::isSystemTrayAvailable()) {
showMsgBox(QT_TRANSLATE_NOOP("main", "A System tray is required to interact with the application"));
return -1;
}
QApplication::setQuitOnLastWindowClosed(false);
auto conn = new DatabaseConnection(Constants::dbLocation, Constants::defaultDbName);
if(!conn->connect()) {
showMsgBox(QT_TRANSLATE_NOOP("main", "Unable to connect to database"));
return -1;
}
app.setQuitOnLastWindowClosed(false);
qRegisterMetaType<model::Tag>();
auto window = new TrayManager(nullptr, conn);
QObject::connect(&app, &QApplication::aboutToQuit, [conn] {
conn->close();
delete conn;
});
auto window = new TrayManager(nullptr, conn);
rtn = app.exec();
int rtn = app.exec();
window->deleteLater();
}
catch (std::exception const& ex) {
qWarning() << "Exception while running: " << ex.what();
}
catch (...) {
qWarning() << "Unhandled exception while running";
}
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));
}
return rtn;
}