1 /* -*- mode: c++; c-basic-offset:4 -*-
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
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"
44 #include "view/tabwidget.h"
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"
62 #include "statusbarprogresswidget.h"
63 #include "progressdialog.h"
66 #include "declarativewidgetbase.h"
68 #include <KActionCollection>
72 #include <KStandardAction>
75 #include <KMessageBox>
76 #include <KStandardGuiItem>
77 #include <KStandardDirs>
80 #include <KShortcutsDialog>
81 #include <KEditToolBar>
83 #include <KAboutApplicationDialog>
89 #include <QWidgetAction>
90 #include <QApplication>
91 #include <QCloseEvent>
96 #include <QDeclarativeItem>
97 #include <QDeclarativeEngine>
98 #include <QDeclarativeContext>
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>
113 static const bool OS_WIN
= true;
115 static const bool OS_WIN
= false;
118 using namespace Kleo
;
119 using namespace Kleo::Commands
;
120 using namespace boost
;
121 using namespace GpgME
;
125 static const KAboutData
* aboutGpg4WinData() {
126 static const AboutGpg4WinData data
;
132 class MainWindow::KeyTreeViewItem
: public DeclarativeWidgetBase
<KeyTreeView
,MainWindow
,&MainWindow::registerKeyTreeView
> {
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
> {
143 explicit SearchBarItem( QGraphicsItem
* parent
=0 )
144 : DeclarativeWidgetBase
<SearchBar
,MainWindow
,&MainWindow::registerSearchBar
>( parent
) {}
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
) );
155 static KGuiItem
KStandardGuiItem_close() {
156 KGuiItem item
= KStandardGuiItem::close();
157 item
.setText( i18n("Only &Close Window" ) );
161 class MainWindow::Private
{
162 friend class ::MainWindow
;
163 MainWindow
* const q
;
166 explicit Private( MainWindow
* qq
);
169 void start( Command
* c
) {
170 c
->setParentWidget( q
);
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() {
196 createAndStart
<SelfTestCommand
>();
198 void configureBackend();
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();
224 void tryToConnectSearchBarToKeyTreeView() {
225 if ( searchBar
&& keyTreeView
)
226 keyTreeView
->connectSearchBar( searchBar
);
229 QAbstractItemView
* currentView() const {
230 return controller
.currentView();
234 QPointer
<SearchBar
> searchBar
;
235 QPointer
<KeyTreeView
> keyTreeView
;
236 Kleo::KeyListController controller
;
240 MainWindow::Private::Private( MainWindow
* qq
)
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
[] = {
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" );
295 engine()->rootContext()->setContextProperty( "application", QVariant::fromValue( static_cast<QObject
*>( this ) ) );
298 void MainWindow::registerKeyTreeView( KeyTreeView
* view
) {
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
);
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
) {
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
) {
340 // KMainWindow::closeEvent() insists on quitting the application,
341 // so do not let it touch the event...
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. "
348 i18n("Ongoing Background Tasks") );
349 if ( ret
!= KMessageBox::Continue
) {
354 d
->controller
.cancelCommands();
355 if ( d
->controller
.hasRunningCommands() ) {
356 // wait for them to be finished:
359 QTimer::singleShot( 100, &ev
, SLOT(quit()) );
360 connect( &d
->controller
, SIGNAL(commandsExecuting(bool)), &ev
, SLOT(quit()) );
362 kWarning( d
->controller
.hasRunningCommands() )
363 << "controller still has commands running, this may crash now...";
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
);
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 );
390 } else if ( !text
.isEmpty() && searchBar
&& !searchBar
->isVisible() ) {
391 searchBar
->setFixedHeight( searchBar
->minimumSizeHint().height() );
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"