Use qtpaths directly
[kdepim.git] / kleopatra / systrayicon.cpp
blob788642c3c6a19575e9641ead83d3cf6266fc65a2
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 systemtrayicon.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2007 Klarälvdalens Datakonsult AB
7 Kleopatra is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Kleopatra is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
33 #include <config-kleopatra.h>
35 #include "systrayicon.h"
37 #ifndef QT_NO_SYSTEMTRAYICON
39 #include "mainwindow.h"
40 #include "kleopatraapplication.h"
42 #include <smartcard/readerstatus.h>
44 #include <utils/kdsignalblocker.h>
45 #include <utils/clipboardmenu.h>
47 #include <commands/importcertificatefromclipboardcommand.h>
48 #include <commands/encryptclipboardcommand.h>
49 #include <commands/signclipboardcommand.h>
50 #include <commands/decryptverifyclipboardcommand.h>
51 #include <commands/setinitialpincommand.h>
52 #include <commands/learncardkeyscommand.h>
54 #include <QIcon>
55 #include <KLocalizedString>
56 #include <KAboutApplicationDialog>
57 #include <KAboutData>
58 #include <KActionMenu>
59 #include <QEventLoopLocker>
61 #include <QMenu>
62 #include <QAction>
63 #include <QApplication>
64 #include <QClipboard>
65 #include <QPointer>
67 #include <boost/shared_ptr.hpp>
68 #include <boost/bind.hpp>
70 #include <cassert>
72 using namespace boost;
73 using namespace Kleo;
74 using namespace Kleo::Commands;
75 using namespace Kleo::SmartCard;
77 class SysTrayIcon::Private
79 friend class ::SysTrayIcon;
80 SysTrayIcon *const q;
81 public:
82 explicit Private(SysTrayIcon *qq);
83 ~Private();
85 private:
86 void slotAbout()
88 if (!aboutDialog) {
89 aboutDialog = new KAboutApplicationDialog(KAboutData::applicationData());
90 aboutDialog->setAttribute(Qt::WA_DeleteOnClose);
93 if (aboutDialog->isVisible()) {
94 aboutDialog->raise();
95 } else {
96 aboutDialog->show();
100 void enableDisableActions()
102 //work around a Qt bug (seen with Qt 4.4.0, Windows): QClipBoard->mimeData() triggers QClipboard::changed(),
103 //triggering slotEnableDisableActions again
104 const KDSignalBlocker blocker(QApplication::clipboard());
105 openCertificateManagerAction.setEnabled(!q->mainWindow() || !q->mainWindow()->isVisible());
106 setInitialPinAction.setEnabled(anyCardHasNullPin);
107 learnCertificatesAction.setEnabled(anyCardCanLearnKeys);
109 q->setAttentionWanted((anyCardHasNullPin || anyCardCanLearnKeys) && !q->attentionWindow());
112 void slotSetInitialPin()
114 SetInitialPinCommand *cmd = new SetInitialPinCommand;
115 q->setAttentionWindow(cmd->dialog());
116 startCommand(cmd);
119 void slotLearnCertificates()
121 LearnCardKeysCommand *cmd = new LearnCardKeysCommand(GpgME::CMS);
122 q->setAttentionWindow(cmd->dialog());
123 startCommand(cmd);
125 void startCommand(Command *cmd)
127 assert(cmd);
128 cmd->setParent(q->mainWindow());
129 cmd->start();
132 private:
133 bool anyCardHasNullPin;
134 bool anyCardCanLearnKeys;
136 QMenu menu;
137 QAction openCertificateManagerAction;
138 QAction configureAction;
139 QAction aboutAction;
140 QAction quitAction;
142 ClipboardMenu clipboardMenu;
144 QMenu cardMenu;
145 QAction updateCardStatusAction;
146 QAction setInitialPinAction;
147 QAction learnCertificatesAction;
149 QPointer<KAboutApplicationDialog> aboutDialog;
150 QEventLoopLocker eventLoopLocker;
153 SysTrayIcon::Private::Private(SysTrayIcon *qq)
154 : q(qq),
155 anyCardHasNullPin(false),
156 anyCardCanLearnKeys(false),
157 menu(),
158 openCertificateManagerAction(i18n("&Open Certificate Manager..."), q),
159 configureAction(i18n("&Configure %1...", KAboutData::applicationData().componentName()), q),
160 aboutAction(i18n("&About %1...", KAboutData::applicationData().componentName()), q),
161 quitAction(i18n("&Shutdown Kleopatra"), q),
162 clipboardMenu(q),
163 cardMenu(i18n("SmartCard")),
164 updateCardStatusAction(i18n("Update Card Status"), q),
165 setInitialPinAction(i18n("Set NetKey v3 Initial PIN..."), q),
166 learnCertificatesAction(i18n("Learn NetKey v3 Card Certificates"), q),
167 aboutDialog()
169 q->setNormalIcon(QIcon::fromTheme(QStringLiteral("kleopatra")));
170 q->setAttentionIcon(QIcon::fromTheme(QStringLiteral("secure-card")));
172 KDAB_SET_OBJECT_NAME(menu);
173 KDAB_SET_OBJECT_NAME(openCertificateManagerAction);
174 KDAB_SET_OBJECT_NAME(configureAction);
175 KDAB_SET_OBJECT_NAME(aboutAction);
176 KDAB_SET_OBJECT_NAME(quitAction);
177 KDAB_SET_OBJECT_NAME(clipboardMenu);
178 KDAB_SET_OBJECT_NAME(cardMenu);
179 KDAB_SET_OBJECT_NAME(setInitialPinAction);
180 KDAB_SET_OBJECT_NAME(learnCertificatesAction);
182 connect(&openCertificateManagerAction, SIGNAL(triggered()), qApp, SLOT(openOrRaiseMainWindow()));
183 connect(&configureAction, SIGNAL(triggered()), qApp, SLOT(openOrRaiseConfigDialog()));
184 connect(&aboutAction, SIGNAL(triggered()), q, SLOT(slotAbout()));
185 connect(&quitAction, &QAction::triggered, QCoreApplication::instance(), &QCoreApplication::quit);
186 connect(&updateCardStatusAction, &QAction::triggered, ReaderStatus::instance(), &ReaderStatus::updateStatus);
187 connect(&setInitialPinAction, SIGNAL(triggered()), q, SLOT(slotSetInitialPin()));
188 connect(&learnCertificatesAction, SIGNAL(triggered()), q, SLOT(slotLearnCertificates()));
190 connect(QApplication::clipboard(), SIGNAL(changed(QClipboard::Mode)),
191 q, SLOT(slotEnableDisableActions()));
193 menu.addAction(&openCertificateManagerAction);
194 menu.addAction(&configureAction);
195 menu.addAction(&aboutAction);
196 menu.addSeparator();
197 menu.addMenu(clipboardMenu.clipboardMenu()->menu());
198 menu.addSeparator();
199 menu.addMenu(&cardMenu);
200 cardMenu.addAction(&updateCardStatusAction);
201 cardMenu.addAction(&setInitialPinAction);
202 cardMenu.addAction(&learnCertificatesAction);
203 menu.addSeparator();
204 menu.addAction(&quitAction);
206 q->setContextMenu(&menu);
207 clipboardMenu.setMainWindow(q->mainWindow());
210 SysTrayIcon::Private::~Private() {}
212 SysTrayIcon::SysTrayIcon(QObject *p)
213 : SystemTrayIcon(p), d(new Private(this))
215 slotEnableDisableActions();
218 SysTrayIcon::~SysTrayIcon()
222 MainWindow *SysTrayIcon::mainWindow() const
224 return static_cast<MainWindow *>(SystemTrayIcon::mainWindow());
227 QDialog *SysTrayIcon::attentionWindow() const
229 return static_cast<QDialog *>(SystemTrayIcon::attentionWindow());
232 void SysTrayIcon::doActivated()
234 if (const QWidget *const aw = attentionWindow())
235 if (aw->isVisible()) {
236 return; // ignore clicks while an attention window is open.
238 if (d->anyCardHasNullPin) {
239 d->slotSetInitialPin();
240 } else if (d->anyCardCanLearnKeys) {
241 d->slotLearnCertificates();
242 } else {
243 // Toggle visibility of MainWindow
244 KleopatraApplication::instance()->toggleMainWindowVisibility();
248 void SysTrayIcon::setAnyCardHasNullPin(bool on)
250 if (d->anyCardHasNullPin == on) {
251 return;
253 d->anyCardHasNullPin = on;
254 slotEnableDisableActions();
257 void SysTrayIcon::setAnyCardCanLearnKeys(bool on)
259 if (d->anyCardCanLearnKeys == on) {
260 return;
262 d->anyCardCanLearnKeys = on;
263 slotEnableDisableActions();
266 void SysTrayIcon::slotEnableDisableActions()
268 d->enableDisableActions();
271 #include "moc_systrayicon.cpp"
273 #endif // QT_NO_SYSTEMTRAYICON