| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "TrayIcon.h" |
| 21 | |
| 22 | TrayIcon::TrayIcon(const QSharedPointer<IConfig> &config, const QSharedPointer<IIconLoader> &iconLoader, QObject *parent) : |
| 23 | QSystemTrayIcon(parent), |
| 24 | mConfig(config), |
| 25 | mOpenAction(nullptr), |
| 26 | mSaveAction(nullptr), |
| 27 | mPasteAction(nullptr), |
| 28 | mCopyAction(nullptr), |
| 29 | mUploadAction(nullptr), |
| 30 | mShowEditorAction(nullptr), |
| 31 | mQuitAction(nullptr), |
| 32 | mActionsMenu(nullptr) |
| 33 | { |
| 34 | auto icon = iconLoader->loadForTheme(QLatin1String("ksnip")); |
| 35 | setIcon(icon); |
| 36 | |
| 37 | mShowEditorAction = new QAction(tr("Show Editor"), this); |
| 38 | mShowEditorAction->setIcon(icon); |
| 39 | connect(mShowEditorAction, &QAction::triggered, this, &TrayIcon::showEditorTriggered); |
| 40 | connect(this, &QSystemTrayIcon::activated, this, &TrayIcon::activatedDefaultAction); |
| 41 | connect(this, &QSystemTrayIcon::messageClicked, this, &TrayIcon::openContentUrl); |
| 42 | } |
| 43 | |
| 44 | void TrayIcon::setupMenu() |
| 45 | { |
| 46 | mMenu.addAction(mShowEditorAction); |
| 47 | mMenu.addSeparator(); |
| 48 | for(auto captureAction : mCaptureActions) { |
| 49 | mMenu.addAction(captureAction); |
| 50 | } |
| 51 | mMenu.addSeparator(); |
| 52 | mMenu.addMenu(mActionsMenu); |
| 53 | mMenu.addSeparator(); |
| 54 | mMenu.addAction(mOpenAction); |
| 55 | mMenu.addAction(mSaveAction); |
| 56 | mMenu.addAction(mPasteAction); |
| 57 | mMenu.addAction(mCopyAction); |
| 58 | mMenu.addAction(mUploadAction); |
| 59 | mMenu.addSeparator(); |
| 60 | mMenu.addAction(mQuitAction); |
| 61 | setContextMenu(&mMenu); |
| 62 | } |
| 63 | |
| 64 | TrayIcon::~TrayIcon() |
| 65 | { |
| 66 | delete mShowEditorAction; |
| 67 | } |
| 68 | |
| 69 | void TrayIcon::setCaptureActions(const QList<QAction*> &captureActions) |
| 70 | { |
| 71 | mCaptureActions = captureActions; |
| 72 | } |
| 73 | |
| 74 | void TrayIcon::setOpenAction(QAction *action) |
| 75 | { |
| 76 | mOpenAction = action; |
| 77 | } |
| 78 | |
| 79 | void TrayIcon::setSaveAction(QAction *action) |
| 80 | { |
| 81 | mSaveAction = action; |
| 82 | } |
| 83 | |
| 84 | void TrayIcon::setPasteAction(QAction *action) |
| 85 | { |
| 86 | mPasteAction = action; |
| 87 | } |
| 88 | |
| 89 | void TrayIcon::setCopyAction(QAction *action) |
| 90 | { |
| 91 | mCopyAction = action; |
| 92 | } |
| 93 | |
| 94 | void TrayIcon::setUploadAction(QAction *action) |
| 95 | { |
| 96 | mUploadAction = action; |
| 97 | } |
| 98 | |
| 99 | void TrayIcon::setQuitAction(QAction *action) |
| 100 | { |
| 101 | mQuitAction = action; |
| 102 | } |
| 103 | |
| 104 | void TrayIcon::setActionsMenu(QMenu *actionsMenu) |
| 105 | { |
| 106 | mActionsMenu = actionsMenu; |
| 107 | } |
| 108 | |
| 109 | void TrayIcon::setEnabled(bool enabled) |
| 110 | { |
| 111 | if(enabled) { |
| 112 | setupMenu(); |
| 113 | show(); |
| 114 | } else { |
| 115 | hide(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void TrayIcon::showInfo(const QString &title, const QString &message, const QString &contentUrl) |
| 120 | { |
| 121 | showMessage(title, message, contentUrl, QSystemTrayIcon::Information); |
| 122 | } |
| 123 | |
| 124 | void TrayIcon::showWarning(const QString &title, const QString &message, const QString &contentUrl) |
| 125 | { |
| 126 | showMessage(title, message, contentUrl, QSystemTrayIcon::Warning); |
| 127 | } |
| 128 | |
| 129 | void TrayIcon::showCritical(const QString &title, const QString &message, const QString &contentUrl) |
| 130 | { |
| 131 | showMessage(title, message, contentUrl, QSystemTrayIcon::Critical); |
| 132 | } |
| 133 | |
| 134 | void TrayIcon::showMessage(const QString &title, const QString &message, const QString &contentUrl, QSystemTrayIcon::MessageIcon messageIcon) |
| 135 | { |
| 136 | mToastContentUrl = PathHelper::extractParentDirectory(contentUrl); |
| 137 | QSystemTrayIcon::showMessage(title, message, messageIcon); |
| 138 | } |
| 139 | |
| 140 | void TrayIcon::activatedDefaultAction(ActivationReason reason) const |
| 141 | { |
| 142 | if(reason != ActivationReason::Context) { |
| 143 | if (mConfig->defaultTrayIconActionMode() == TrayIconDefaultActionMode::ShowEditor) { |
| 144 | mShowEditorAction->trigger(); |
| 145 | } else { |
| 146 | triggerDefaultCaptureMode(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void TrayIcon::triggerDefaultCaptureMode() const |
| 152 | { |
| 153 | auto captureMode = mConfig->defaultTrayIconCaptureMode(); |
| 154 | for(auto action : mCaptureActions) { |
| 155 | if (action->data().value<CaptureModes>() == captureMode) { |
| 156 | action->trigger(); |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void TrayIcon::openContentUrl() |
| 163 | { |
| 164 | if(!mToastContentUrl.isEmpty() && !mToastContentUrl.isNull()) { |
| 165 | QDesktopServices::openUrl(mToastContentUrl); |
| 166 | } |
| 167 | } |