Add warning about missing Nepomuk in the KMail composer.
[kdepim.git] / kleopatra / mainwindow_mobile.cpp
blob99172016a2d4db54617da37fac39ed8f77a66e7c
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 mainwindow_mobile.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2010 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 "mainwindow_mobile.h"
37 #include "aboutdata.h"
39 #include "models/keylistmodel.h"
40 #include "models/keylistsortfilterproxymodel.h"
42 #include "view/searchbar.h"
43 #if 0
44 #include "view/tabwidget.h"
45 #endif
46 #include "view/keytreeview.h"
47 #include "view/keylistcontroller.h"
49 #include "commands/selftestcommand.h"
50 #include "commands/importcrlcommand.h"
51 #include "commands/importcertificatefromfilecommand.h"
52 #include "commands/decryptverifyfilescommand.h"
53 #include "commands/signencryptfilescommand.h"
55 #include "utils/detail_p.h"
56 #include "utils/gnupg-helper.h"
57 #include "utils/action_data.h"
58 #include "utils/classify.h"
59 #include "utils/filedialog.h"
61 // from libkdepim
62 #include "statusbarprogresswidget.h"
63 #include "progressdialog.h"
65 // from mobileui
66 #include "declarativewidgetbase.h"
68 #include <KActionCollection>
69 #include <KLocale>
70 #include <KTabWidget>
71 #include <KStatusBar>
72 #include <KStandardAction>
73 #include <KAction>
74 #include <KAboutData>
75 #include <KMessageBox>
76 #include <KStandardGuiItem>
77 #include <KStandardDirs>
78 #include <KLineEdit>
79 #if 0
80 #include <KShortcutsDialog>
81 #include <KEditToolBar>
82 #endif
83 #include <KAboutApplicationDialog>
84 #include <kdebug.h>
86 #include <QTreeView>
87 #include <QFile>
88 #include <QToolBar>
89 #include <QWidgetAction>
90 #include <QApplication>
91 #include <QCloseEvent>
92 #include <QMenu>
93 #include <QTimer>
94 #include <QProcess>
95 #include <QPointer>
96 #include <QDeclarativeItem>
97 #include <QDeclarativeEngine>
98 #include <QDeclarativeContext>
99 #include <QVariant>
100 #include <QHeaderView>
102 #include <kleo/stl_util.h>
103 #include <kleo/cryptobackendfactory.h>
104 #include <ui/cryptoconfigdialog.h>
105 #include <kleo/cryptoconfig.h>
107 #include <boost/bind.hpp>
108 #include <boost/shared_ptr.hpp>
110 #include <vector>
112 #ifdef Q_OS_WIN32
113 static const bool OS_WIN = true;
114 #else
115 static const bool OS_WIN = false;
116 #endif
118 using namespace Kleo;
119 using namespace Kleo::Commands;
120 using namespace boost;
121 using namespace GpgME;
123 namespace {
125 static const KAboutData * aboutGpg4WinData() {
126 static const AboutGpg4WinData data;
127 return &data;
132 class MainWindow::KeyTreeViewItem : public DeclarativeWidgetBase<KeyTreeView,MainWindow,&MainWindow::registerKeyTreeView> {
133 Q_OBJECT
134 public:
135 explicit KeyTreeViewItem( QGraphicsItem * parent=0 )
136 : DeclarativeWidgetBase<KeyTreeView,MainWindow,&MainWindow::registerKeyTreeView>( parent ) {}
137 ~KeyTreeViewItem() {}
140 class MainWindow::SearchBarItem : public DeclarativeWidgetBase<SearchBar,MainWindow,&MainWindow::registerSearchBar> {
141 Q_OBJECT
142 public:
143 explicit SearchBarItem( QGraphicsItem * parent=0 )
144 : DeclarativeWidgetBase<SearchBar,MainWindow,&MainWindow::registerSearchBar>( parent ) {}
145 ~SearchBarItem() {}
148 static KGuiItem KStandardGuiItem_quit() {
149 static const QString app = KGlobal::mainComponent().aboutData()->programName();
150 KGuiItem item = KStandardGuiItem::quit();
151 item.setText( i18nc( "Quit [ApplicationName]", "&Quit %1", app ) );
152 return item;
155 static KGuiItem KStandardGuiItem_close() {
156 KGuiItem item = KStandardGuiItem::close();
157 item.setText( i18n("Only &Close Window" ) );
158 return item;
161 class MainWindow::Private {
162 friend class ::MainWindow;
163 MainWindow * const q;
165 public:
166 explicit Private( MainWindow * qq );
167 ~Private();
169 void start( Command * c ) {
170 c->setParentWidget( q );
171 c->start();
174 template <typename T>
175 void createAndStart() {
176 this->start( new T( this->currentView(), &this->controller ) );
178 template <typename T>
179 void createAndStart( QAbstractItemView * view ) {
180 start( new T( view, &this->controller ) );
182 template <typename T>
183 void createAndStart( const QStringList & a ) {
184 start( new T( a, this->currentView(), &this->controller ) );
186 template <typename T>
187 void createAndStart( const QStringList & a, QAbstractItemView * view ) {
188 start( new T( a, view, &this->controller ) );
191 void closeAndQuit() {
192 qApp->quit();
195 void selfTest() {
196 createAndStart<SelfTestCommand>();
198 void configureBackend();
199 void showHandbook();
201 void gnupgLogViewer() {
202 if( !QProcess::startDetached("kwatchgnupg" ) )
203 KMessageBox::error( q, i18n( "Could not start the GnuPG Log Viewer (kwatchgnupg). "
204 "Please check your installation." ),
205 i18n( "Error Starting KWatchGnuPG" ) );
208 void gnupgAdministrativeConsole() {
209 if( !QProcess::startDetached("kgpgconf" ) )
210 KMessageBox::error( q, i18n( "Could not start the GnuPG Administrative Console (kgpgconf). "
211 "Please check your installation." ),
212 i18n( "Error Starting KGpgConf" ) );
215 void slotConfigCommitted();
216 void slotSearchBarTextChanged( const QString & );
218 void aboutGpg4Win() {
219 ( new KAboutApplicationDialog( aboutGpg4WinData(), KAboutApplicationDialog::HideKdeVersion|KAboutApplicationDialog::HideTranslators, q ) )->show();
222 private:
223 void setupActions();
224 void tryToConnectSearchBarToKeyTreeView() {
225 if ( searchBar && keyTreeView )
226 keyTreeView->connectSearchBar( searchBar );
229 QAbstractItemView * currentView() const {
230 return controller.currentView();
233 private:
234 QPointer<SearchBar> searchBar;
235 QPointer<KeyTreeView> keyTreeView;
236 Kleo::KeyListController controller;
237 bool firstShow : 1;
240 MainWindow::Private::Private( MainWindow * qq )
241 : q( qq ),
242 searchBar(),
243 keyTreeView(),
244 controller( q ),
245 firstShow( true )
247 KDAB_SET_OBJECT_NAME( controller );
249 AbstractKeyListModel * flatModel = AbstractKeyListModel::createFlatKeyListModel( q );
250 AbstractKeyListModel * hierarchicalModel = AbstractKeyListModel::createHierarchicalKeyListModel( q );
252 KDAB_SET_OBJECT_NAME( flatModel );
253 KDAB_SET_OBJECT_NAME( hierarchicalModel );
256 controller.setFlatModel( flatModel );
257 controller.setHierarchicalModel( hierarchicalModel );
258 controller.setParentWidget( q );
262 MainWindow::Private::~Private() {}
264 MainWindow::MainWindow( QWidget * parent )
265 : KDeclarativeFullScreenView( QLatin1String("kleopatra-mobile"), parent ), d( new Private( this ) )
269 MainWindow::~MainWindow() {}
272 void MainWindow::Private::setupActions() {
274 KActionCollection * const coll = q->actionCollection();
276 const action_data action_data[] = {
277 // Settings menu
278 { "settings_self_test", i18n("Perform Self-Test"), QString(),
279 0, q, SLOT(selfTest()), QString(), false, true },
282 make_actions_from_data( action_data, coll );
284 KStandardAction::close( q, SLOT(close()), coll );
285 KStandardAction::quit( q, SLOT(closeAndQuit()), coll );
286 KStandardAction::preferences( qApp, SLOT(openOrRaiseConfigDialog()), coll );
288 controller.createActions( coll );
291 void MainWindow::doDelayedInit() {
292 qmlRegisterType<KeyTreeViewItem>( "org.kde.kleopatra", 2, 1, "KeyTreeView" );
293 qmlRegisterType<SearchBarItem> ( "org.kde.kleopatra", 2, 1, "SearchBar" );
294 d->setupActions();
295 engine()->rootContext()->setContextProperty( "application", QVariant::fromValue( static_cast<QObject*>( this ) ) );
298 void MainWindow::registerKeyTreeView( KeyTreeView * view ) {
299 if ( !view )
300 return;
301 view->setFlatModel( d->controller.flatModel() );
302 view->setHierarchicalModel( d->controller.hierarchicalModel() );
303 QTreeView * const v = view->view();
304 v->setItemsExpandable( false );
305 v->header()->setResizeMode( QHeaderView::Stretch );
306 v->header()->hide();
307 d->controller.addView( v );
308 d->controller.setCurrentView( v );
309 d->keyTreeView = view;
310 d->tryToConnectSearchBarToKeyTreeView();
312 connect( v->model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
313 SIGNAL(certificatesAvailabilityChanged()) );
314 connect( v->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
315 SIGNAL(certificatesAvailabilityChanged()) );
316 connect( v->model(), SIGNAL(modelReset()),
317 SIGNAL(certificatesAvailabilityChanged()) );
319 emit certificatesAvailabilityChanged();
322 void MainWindow::registerSearchBar( SearchBar * bar ) {
323 if ( !bar )
324 return;
325 d->searchBar = bar;
326 bar->setFixedHeight( 0 );
327 connect( bar, SIGNAL(stringFilterChanged(QString)),
328 this, SLOT(slotSearchBarTextChanged(QString)) );
329 d->tryToConnectSearchBarToKeyTreeView();
332 void MainWindow::Private::slotConfigCommitted() {
333 controller.updateConfig();
336 void MainWindow::closeEvent( QCloseEvent * e ) {
338 d->closeAndQuit();
340 // KMainWindow::closeEvent() insists on quitting the application,
341 // so do not let it touch the event...
342 kDebug();
343 if ( d->controller.hasRunningCommands() ) {
344 if ( d->controller.shutdownWarningRequired() ) {
345 const int ret = KMessageBox::warningContinueCancel( this, i18n("There are still some background operations ongoing. "
346 "These will be terminated when closing the window. "
347 "Proceed?"),
348 i18n("Ongoing Background Tasks") );
349 if ( ret != KMessageBox::Continue ) {
350 e->ignore();
351 return;
354 d->controller.cancelCommands();
355 if ( d->controller.hasRunningCommands() ) {
356 // wait for them to be finished:
357 setEnabled( false );
358 QEventLoop ev;
359 QTimer::singleShot( 100, &ev, SLOT(quit()) );
360 connect( &d->controller, SIGNAL(commandsExecuting(bool)), &ev, SLOT(quit()) );
361 ev.exec();
362 kWarning( d->controller.hasRunningCommands() )
363 << "controller still has commands running, this may crash now...";
364 setEnabled( true );
367 e->accept();
370 void MainWindow::keyPressEvent( QKeyEvent * e ) {
371 static bool isSendingEvent = false;
373 if ( !isSendingEvent && d->searchBar && !e->text().isEmpty() ) {
374 const struct guard { guard() { isSendingEvent = true; } ~guard() { isSendingEvent = false; } } guard;
375 QCoreApplication::sendEvent( d->searchBar->lineEdit(), e );
376 } else {
377 KDeclarativeFullScreenView::keyPressEvent( e );
381 void MainWindow::importCertificatesFromFile( const QStringList & files ) {
382 if ( !files.empty() )
383 d->createAndStart<ImportCertificateFromFileCommand>( files );
386 void MainWindow::Private::slotSearchBarTextChanged( const QString & text ) {
387 if ( text.isEmpty() && searchBar && searchBar->isVisible() ) {
388 searchBar->setFixedHeight( 0 );
389 searchBar->hide();
390 } else if ( !text.isEmpty() && searchBar && !searchBar->isVisible() ) {
391 searchBar->setFixedHeight( searchBar->minimumSizeHint().height() );
392 searchBar->show();
393 searchBar->setFocus();
397 bool MainWindow::certificatesAvailable() const {
398 return (d->keyTreeView && d->keyTreeView->view()->model()->rowCount());
401 #include "moc_mainwindow_mobile.cpp"
402 #include "mainwindow_mobile.moc"