diff --git a/src/traymanager.cpp b/src/traymanager.cpp index 7312196..ecd764d 100644 --- a/src/traymanager.cpp +++ b/src/traymanager.cpp @@ -56,25 +56,10 @@ TrayManager::TrayManager(DatabaseConnection* db) { screenshotTool = new Screenshot(); hotkeyManager = new HotkeyManager(); hotkeyManager->updateHotkeys(); - - settingsWindow = new Settings(hotkeyManager, this); - evidenceManagerWindow = new EvidenceManager(db, this); - creditsWindow = new Credits(this); - - createActions(); - createTrayMenu(); - QIcon icon = QIcon(ICON); - // TODO: figure out if any other environments support masking -#ifdef Q_OS_MACOS - icon.setIsMask(true); -#endif - trayIcon->setIcon(icon); - - setActiveOperationLabel(); - trayIcon->show(); updateCheckTimer = new QTimer(this); updateCheckTimer->start(24*60*60*1000); // every day + buildUi(); wireUi(); // delayed so that windows can listen for get all ops signal @@ -109,6 +94,98 @@ TrayManager::~TrayManager() { delete creditsWindow; } +void TrayManager::buildUi() { + // create subwindows + settingsWindow = new Settings(hotkeyManager, this); + evidenceManagerWindow = new EvidenceManager(db, this); + creditsWindow = new Credits(this); + + trayIconMenu = new QMenu(this); + chooseOpSubmenu = new QMenu(tr("Select Operation")); + + // small helper to create an action and assign it to the tray + auto addToTray = [this](QString text, QAction** act){ + *act = new QAction(text, this); + trayIconMenu->addAction(*act); + }; + + // Tray Ordering + addToTray(tr("Add Codeblock from Clipboard"), &addCodeblockAction); + addToTray(tr("Capture Screen Area"), &captureScreenAreaAction); + addToTray(tr("Capture Window"), &captureWindowAction); + addToTray(tr("View Accumulated Evidence"), &showEvidenceManagerAction); + addToTray(tr("Settings"), &showSettingsAction); + trayIconMenu->addSeparator(); + addToTray(tr(""), ¤tOperationMenuAction); + trayIconMenu->addMenu(chooseOpSubmenu); + trayIconMenu->addSeparator(); + addToTray(tr("About"), &showCreditsAction); + addToTray(tr("Quit"), &quitAction); + + // finish action config + currentOperationMenuAction->setEnabled(false); + chooseOpStatusAction = new QAction("Loading operations...", chooseOpSubmenu); + chooseOpStatusAction->setEnabled(false); + chooseOpSubmenu->addAction(chooseOpStatusAction); + chooseOpSubmenu->addSeparator(); + + setActiveOperationLabel(); + + QIcon icon = QIcon(ICON); + // TODO: figure out if any other environments support masking +#ifdef Q_OS_MACOS + icon.setIsMask(true); +#endif + + trayIcon = new QSystemTrayIcon(this); + trayIcon->setContextMenu(trayIconMenu); + trayIcon->setIcon(icon); + trayIcon->show(); +} + +void TrayManager::wireUi() { + auto toTop = [](QDialog* window) { + window->show(); // display the window + window->raise(); // bring to the top (mac) + window->activateWindow(); // alternate bring to the top (windows) + }; + auto actTriggered = &QAction::triggered; + // connect actions + connect(quitAction, actTriggered, qApp, &QCoreApplication::quit); + connect(showSettingsAction, actTriggered, [this, toTop](){toTop(settingsWindow);}); + connect(captureScreenAreaAction, actTriggered, this, &TrayManager::captureAreaActionTriggered); + connect(captureWindowAction, actTriggered, this, &TrayManager::captureWindowActionTriggered); + connect(showEvidenceManagerAction, actTriggered, [this, toTop](){toTop(evidenceManagerWindow);}); + connect(showCreditsAction, actTriggered, [this, toTop](){toTop(creditsWindow);}); + connect(addCodeblockAction, actTriggered, this, &TrayManager::captureCodeblockActionTriggered); + + connect(screenshotTool, &Screenshot::onScreenshotCaptured, this, + &TrayManager::onScreenshotCaptured); + + // connect to hotkey signals + connect(hotkeyManager, &HotkeyManager::codeblockHotkeyPressed, this, + &TrayManager::captureCodeblockActionTriggered); + connect(hotkeyManager, &HotkeyManager::captureAreaHotkeyPressed, this, + &TrayManager::captureAreaActionTriggered); + connect(hotkeyManager, &HotkeyManager::captureWindowHotkeyPressed, this, + &TrayManager::captureWindowActionTriggered); + + // connect to network signals + connect(&NetMan::getInstance(), &NetMan::operationListUpdated, this, + &TrayManager::onOperationListUpdated); + connect(&NetMan::getInstance(), &NetMan::releasesChecked, this, &TrayManager::onReleaseCheck); + connect(&AppSettings::getInstance(), &AppSettings::onOperationUpdated, this, + &TrayManager::setActiveOperationLabel); + + connect(trayIcon, &QSystemTrayIcon::messageClicked, [](){QDesktopServices::openUrl(Constants::releasePageUrl());}); + connect(trayIcon, &QSystemTrayIcon::activated, [this] { + chooseOpStatusAction->setText("Loading operations..."); + NetMan::getInstance().refreshOperationsList(); + }); + + connect(updateCheckTimer, &QTimer::timeout, this, &TrayManager::checkForUpdate); +} + void TrayManager::cleanChooseOpSubmenu() { // delete all of the existing events for (QAction* act : allOperationActions) { @@ -119,26 +196,6 @@ void TrayManager::cleanChooseOpSubmenu() { selectedAction = nullptr; // clear the selected action to ensure no funny business } -void TrayManager::wireUi() { - connect(screenshotTool, &Screenshot::onScreenshotCaptured, this, - &TrayManager::onScreenshotCaptured); - - connect(hotkeyManager, &HotkeyManager::codeblockHotkeyPressed, this, - &TrayManager::captureCodeblockActionTriggered); - connect(hotkeyManager, &HotkeyManager::captureAreaHotkeyPressed, this, - &TrayManager::captureAreaActionTriggered); - connect(hotkeyManager, &HotkeyManager::captureWindowHotkeyPressed, this, - &TrayManager::captureWindowActionTriggered); - - connect(&NetMan::getInstance(), &NetMan::operationListUpdated, this, - &TrayManager::onOperationListUpdated); - connect(&NetMan::getInstance(), &NetMan::releasesChecked, this, &TrayManager::onReleaseCheck); - connect(&AppSettings::getInstance(), &AppSettings::onOperationUpdated, this, - &TrayManager::setActiveOperationLabel); - connect(trayIcon, &QSystemTrayIcon::messageClicked, [](){QDesktopServices::openUrl(Constants::releasePageUrl());}); - connect(updateCheckTimer, &QTimer::timeout, this, &TrayManager::checkForUpdate); -} - void TrayManager::closeEvent(QCloseEvent* event) { #ifdef Q_OS_MACOS if (!event->spontaneous() || !isVisible()) { @@ -151,45 +208,6 @@ void TrayManager::closeEvent(QCloseEvent* event) { } } -void TrayManager::createActions() { - auto toTop = [](QDialog* window) { - window->show(); // display the window - window->raise(); // bring to the top (mac) - window->activateWindow(); // alternate bring to the top (windows) - }; - - quitAction = new QAction(tr("Quit"), this); - connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); - - showSettingsAction = new QAction(tr("Settings"), this); - connect(showSettingsAction, &QAction::triggered, [this, toTop](){toTop(settingsWindow);}); - - currentOperationMenuAction = new QAction(this); - currentOperationMenuAction->setEnabled(false); - - captureScreenAreaAction = new QAction(tr("Capture Screen Area"), this); - connect(captureScreenAreaAction, &QAction::triggered, this, &TrayManager::captureAreaActionTriggered); - - captureWindowAction = new QAction(tr("Capture Window"), this); - connect(captureWindowAction, &QAction::triggered, this, &TrayManager::captureWindowActionTriggered); - - showEvidenceManagerAction = new QAction(tr("View Accumulated Evidence"), this); - connect(showEvidenceManagerAction, &QAction::triggered, [this, toTop](){toTop(evidenceManagerWindow);}); - - showCreditsAction = new QAction(tr("About"), this); - connect(showCreditsAction, &QAction::triggered, [this, toTop](){toTop(creditsWindow);}); - - addCodeblockAction = new QAction(tr("Add Codeblock from Clipboard"), this); - connect(addCodeblockAction, &QAction::triggered, this, &TrayManager::captureCodeblockActionTriggered); - - chooseOpSubmenu = new QMenu(tr("Select Operation")); - chooseOpStatusAction = new QAction("Loading operations...", chooseOpSubmenu); - chooseOpStatusAction->setEnabled(false); - - chooseOpSubmenu->addAction(chooseOpStatusAction); - chooseOpSubmenu->addSeparator(); -} - void TrayManager::spawnGetInfoWindow(qint64 evidenceID) { auto getInfoWindow = new GetInfo(db, evidenceID, this); connect(getInfoWindow, &GetInfo::evidenceSubmitted, [](model::Evidence evi){ @@ -247,29 +265,6 @@ void TrayManager::onCodeblockCapture() { } } -void TrayManager::createTrayMenu() { - trayIconMenu = new QMenu(this); - - trayIconMenu->addAction(this->addCodeblockAction); - trayIconMenu->addAction(this->captureScreenAreaAction); - trayIconMenu->addAction(this->captureWindowAction); - trayIconMenu->addAction(this->showEvidenceManagerAction); - trayIconMenu->addAction(this->showSettingsAction); - trayIconMenu->addSeparator(); - trayIconMenu->addAction(this->currentOperationMenuAction); - trayIconMenu->addMenu(chooseOpSubmenu); - trayIconMenu->addSeparator(); - trayIconMenu->addAction(this->showCreditsAction); - trayIconMenu->addAction(this->quitAction); - - trayIcon = new QSystemTrayIcon(this); - trayIcon->setContextMenu(trayIconMenu); - connect(trayIcon, &QSystemTrayIcon::activated, [this]{ - chooseOpStatusAction->setText("Loading operations..."); - NetMan::getInstance().refreshOperationsList(); - }); -} - void TrayManager::onScreenshotCaptured(const QString& path) { try { auto evidenceID = createNewEvidence(path, "image"); diff --git a/src/traymanager.h b/src/traymanager.h index b6a10c5..b661e17 100644 --- a/src/traymanager.h +++ b/src/traymanager.h @@ -41,8 +41,18 @@ class TrayManager : public QDialog { TrayManager(DatabaseConnection *); ~TrayManager(); - protected: - void closeEvent(QCloseEvent *event) override; + private: + void buildUi(); + void wireUi(); + qint64 createNewEvidence(QString filepath, QString evidenceType); + void spawnGetInfoWindow(qint64 evidenceID); + void showNoOperationSetTrayMessage(); + void checkForUpdate(); + void cleanChooseOpSubmenu(); + + private slots: + void onOperationListUpdated(bool success, const std::vector &operations); + void onReleaseCheck(bool success, std::vector releases); public slots: void onScreenshotCaptured(const QString &filepath); @@ -52,47 +62,37 @@ class TrayManager : public QDialog { void captureWindowActionTriggered(); void captureCodeblockActionTriggered(); - private slots: - void onOperationListUpdated(bool success, const std::vector &operations); - void onReleaseCheck(bool success, std::vector releases); + protected: + void closeEvent(QCloseEvent *event) override; private: - void createActions(); - void createTrayMenu(); - void wireUi(); - qint64 createNewEvidence(QString filepath, QString evidenceType); - void spawnGetInfoWindow(qint64 evidenceID); - void showNoOperationSetTrayMessage(); - void checkForUpdate(); + DatabaseConnection *db = nullptr; + HotkeyManager *hotkeyManager = nullptr; + Screenshot *screenshotTool = nullptr; + QTimer *updateCheckTimer = nullptr; - private: - QAction *quitAction; - QAction *showSettingsAction; - QAction *currentOperationMenuAction; - QAction *captureScreenAreaAction; - QAction *captureWindowAction; - QAction *showEvidenceManagerAction; - QAction *showCreditsAction; - QAction *addCodeblockAction; + // Subwindows + Settings *settingsWindow = nullptr; + EvidenceManager *evidenceManagerWindow = nullptr; + Credits *creditsWindow = nullptr; - void cleanChooseOpSubmenu(); - QMenu *chooseOpSubmenu; - QAction *chooseOpStatusAction; + // UI Elements + QSystemTrayIcon *trayIcon = nullptr; + QMenu *trayIconMenu = nullptr; + + QAction *quitAction = nullptr; + QAction *showSettingsAction = nullptr; + QAction *currentOperationMenuAction = nullptr; + QAction *captureScreenAreaAction = nullptr; + QAction *captureWindowAction = nullptr; + QAction *showEvidenceManagerAction = nullptr; + QAction *showCreditsAction = nullptr; + QAction *addCodeblockAction = nullptr; + + QMenu *chooseOpSubmenu = nullptr; + QAction *chooseOpStatusAction = nullptr; QAction *selectedAction = nullptr; // note: do not delete; for reference only std::vector allOperationActions; - - QSystemTrayIcon *trayIcon; - QMenu *trayIconMenu; - - Settings *settingsWindow; - EvidenceManager *evidenceManagerWindow; - Credits *creditsWindow; - - Screenshot *screenshotTool; - HotkeyManager *hotkeyManager; - - DatabaseConnection *db; - QTimer *updateCheckTimer = nullptr; }; #endif // QT_NO_SYSTEMTRAYICON