• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
main.cpp
Go to the documentation of this file.
1 /*
2  main.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2001,2002,2004,2008 Klar�vdalens Datakonsult AB
6 
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.
11 
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.
16 
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
20 
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.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "aboutdata.h"
36 #include "kleopatraapplication.h"
37 #include "mainwindow.h"
38 
39 #include <commands/reloadkeyscommand.h>
40 #include <commands/selftestcommand.h>
41 
42 #include <utils/gnupg-helper.h>
43 #include <utils/archivedefinition.h>
44 
45 #ifdef HAVE_USABLE_ASSUAN
46 # include <uiserver/uiserver.h>
47 # include <uiserver/assuancommand.h>
48 # include <uiserver/echocommand.h>
49 # include <uiserver/decryptcommand.h>
50 # include <uiserver/verifycommand.h>
51 # include <uiserver/decryptverifyfilescommand.h>
52 # include <uiserver/decryptfilescommand.h>
53 # include <uiserver/verifyfilescommand.h>
54 # include <uiserver/prepencryptcommand.h>
55 # include <uiserver/prepsigncommand.h>
56 # include <uiserver/encryptcommand.h>
57 # include <uiserver/signcommand.h>
58 # include <uiserver/signencryptfilescommand.h>
59 # include <uiserver/selectcertificatecommand.h>
60 # include <uiserver/importfilescommand.h>
61 # include <uiserver/createchecksumscommand.h>
62 # include <uiserver/verifychecksumscommand.h>
63 #else
64 namespace Kleo {
65  class UiServer;
66 }
67 #endif
68 
69 #include <kleo/checksumdefinition.h>
70 
71 #ifdef KDEPIM_MOBILE_UI
72 # include <kdeclarativeapplication.h>
73 #endif
74 
75 #include <KDebug>
76 #include <kcmdlineargs.h>
77 #include <klocale.h>
78 #include <kiconloader.h>
79 #include <ksplashscreen.h>
80 #include <kmessagebox.h>
81 
82 #include <QTextDocument> // for Qt::escape
83 #include <QMessageBox>
84 #include <QTimer>
85 #include <QTime>
86 #include <QEventLoop>
87 #include <QThreadPool>
88 
89 #include <gpgme++/global.h>
90 #include <gpgme++/error.h>
91 
92 #ifndef Q_MOC_RUN
93 #include <boost/shared_ptr.hpp>
94 #endif
95 
96 #include <cassert>
97 
98 using namespace boost;
99 
100 static const int SPLASHSCREEN_TIMEOUT = 5000; // 5s
101 
102 namespace {
103  template <typename T>
104  boost::shared_ptr<T> make_shared_ptr( T * t ) {
105  return t ? boost::shared_ptr<T>( t ) : boost::shared_ptr<T>() ;
106  }
107 }
108 
109 static QPixmap UserIcon_nocached( const char * name ) {
110  // KIconLoader insists on caching all pixmaps. Since the splash
111  // screen is a particularly large 'icon' and used only once,
112  // caching is unneccesary and just hurts startup performance.
113  KIconLoader * const il = KIconLoader::global();
114  assert( il );
115  const QString iconPath = il->iconPath( QLatin1String( name ), KIconLoader::User );
116  return iconPath.isEmpty() ? il->unknown() : QPixmap( iconPath ) ;
117 }
118 
119 #ifndef QT_NO_SPLASHSCREEN
120 class SplashScreen : public KSplashScreen {
121  QBasicTimer m_timer;
122 public:
123  SplashScreen()
124  : KSplashScreen( UserIcon_nocached( "kleopatra_splashscreen" ), Qt::WindowStaysOnTopHint ),
125  m_timer()
126  {
127  m_timer.start( SPLASHSCREEN_TIMEOUT, this );
128  }
129 
130 protected:
131  void timerEvent( QTimerEvent * ev ) {
132  if ( ev->timerId() == m_timer.timerId() ) {
133  m_timer.stop();
134  hide();
135  } else {
136  KSplashScreen::timerEvent( ev );
137  }
138  }
139 
140 };
141 #else
142 class SplashScreen {};
143 #endif // QT_NO_SPLASHSCREEN
144 
145 static bool selfCheck( SplashScreen & splash ) {
146 #ifndef QT_NO_SPLASHSCREEN
147  splash.showMessage( i18n("Performing Self-Check...") );
148 #endif
149  Kleo::Commands::SelfTestCommand cmd( 0 );
150  cmd.setAutoDelete( false );
151  cmd.setAutomaticMode( true );
152 #ifndef QT_NO_SPLASHSCREEN
153  cmd.setSplashScreen( &splash );
154 #endif
155  QEventLoop loop;
156  QObject::connect( &cmd, SIGNAL(finished()), &loop, SLOT(quit()) );
157 #ifndef QT_NO_SPLASHSCREEN
158  QObject::connect( &cmd, SIGNAL(info(QString)), &splash, SLOT(showMessage(QString)) );
159 #endif
160  QTimer::singleShot( 0, &cmd, SLOT(start()) ); // start() may emit finished()...
161  loop.exec();
162  if ( cmd.isCanceled() ) {
163 #ifndef QT_NO_SPLASHSCREEN
164  splash.showMessage( i18nc("did not pass", "Self-Check Failed") );
165 #endif
166  return false;
167  } else {
168 #ifndef QT_NO_SPLASHSCREEN
169  splash.showMessage( i18n("Self-Check Passed") );
170 #endif
171  return true;
172  }
173 }
174 
175 static void fillKeyCache( SplashScreen * splash, Kleo::UiServer * server ) {
176 
177  QEventLoop loop;
178  Kleo::ReloadKeysCommand * cmd = new Kleo::ReloadKeysCommand( 0 );
179  QObject::connect( cmd, SIGNAL(finished()), &loop, SLOT(quit()) );
180 #ifdef HAVE_USABLE_ASSUAN
181  QObject::connect( cmd, SIGNAL(finished()), server, SLOT(enableCryptoCommands()) );
182 #else
183  Q_UNUSED( server );
184 #endif
185 #ifndef QT_NO_SPLASHSCREEN
186  splash->showMessage( i18n("Loading certificate cache...") );
187 #else
188  Q_UNUSED( splash );
189 #endif
190  cmd->start();
191  loop.exec();
192 #ifndef QT_NO_SPLASHSCREEN
193  splash->showMessage( i18n("Certificate cache loaded.") );
194 #endif
195 }
196 
197 int main( int argc, char** argv )
198 {
199  QTime timer;
200  timer.start();
201 
202  const GpgME::Error gpgmeInitError = GpgME::initializeLibrary(0);
203 
204  {
205  const unsigned int threads = QThreadPool::globalInstance()->maxThreadCount();
206  QThreadPool::globalInstance()->setMaxThreadCount( qMax( 2U, threads ) );
207  }
208 
209  AboutData aboutData;
210 
211  KCmdLineArgs::init(argc, argv, &aboutData);
212 
213 #ifdef KDEPIM_MOBILE_UI
214  KDeclarativeApplicationBase::preApplicationSetup( KleopatraApplication::commandLineOptions() );
215 #else
216  KCmdLineArgs::addCmdLineOptions( KleopatraApplication::commandLineOptions() );
217 #endif
218 
219  kDebug() << "Statup timing:" << timer.elapsed() << "ms elapsed: Command line args created";
220 
221  KleopatraApplication app;
222 #ifdef KDEPIM_MOBILE_UI
223  KDeclarativeApplicationBase::postApplicationSetup();
224 #endif
225 
226  kDebug() << "Startup timing:" << timer.elapsed() << "ms elapsed: Application created";
227 
228  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
229 
230  if ( gpgmeInitError ) {
231  KMessageBox::sorry( 0, i18nc("@info",
232  "<para>The version of the <application>GpgME</application> library you are running against "
233  "is older than the one that the <application>GpgME++</application> library was built against.</para>"
234  "<para><application>Kleopatra</application> will not function in this setting.</para>"
235  "<para>Please ask your administrator for help in resolving this issue.</para>"),
236  i18nc("@title", "GpgME Too Old") );
237  return EXIT_FAILURE;
238  }
239 
240  SplashScreen splash;
241 
242  const QString installPath = Kleo::gpg4winInstallPath();
243  Kleo::ChecksumDefinition::setInstallPath( installPath );
244  Kleo::ArchiveDefinition::setInstallPath( installPath );
245 
246  int rc;
247 #ifdef HAVE_USABLE_ASSUAN
248  try {
249  Kleo::UiServer server( args->getOption("uiserver-socket") );
250 
251  kDebug() << "Startup timing:" << timer.elapsed() << "ms elapsed: UiServer created";
252 
253  QObject::connect( &server, SIGNAL(startKeyManagerRequested()),
254  &app, SLOT(openOrRaiseMainWindow()) );
255 
256  QObject::connect( &server, SIGNAL(startConfigDialogRequested()),
257  &app, SLOT(openOrRaiseConfigDialog()) );
258 
259 #define REGISTER( Command ) server.registerCommandFactory( boost::shared_ptr<Kleo::AssuanCommandFactory>( new Kleo::GenericAssuanCommandFactory<Kleo::Command> ) )
260  REGISTER( CreateChecksumsCommand );
261  REGISTER( DecryptCommand );
262  REGISTER( DecryptFilesCommand );
263  REGISTER( DecryptVerifyFilesCommand );
264  REGISTER( EchoCommand );
265  REGISTER( EncryptCommand );
266  REGISTER( EncryptFilesCommand );
267  REGISTER( EncryptSignFilesCommand );
268  REGISTER( ImportFilesCommand );
269  REGISTER( PrepEncryptCommand );
270  REGISTER( PrepSignCommand );
271  REGISTER( SelectCertificateCommand );
272  REGISTER( SignCommand );
273  REGISTER( SignEncryptFilesCommand );
274  REGISTER( SignFilesCommand );
275 #ifndef QT_NO_DIRMODEL
276  REGISTER( VerifyChecksumsCommand );
277 #endif // QT_NO_DIRMODEL
278  REGISTER( VerifyCommand );
279  REGISTER( VerifyFilesCommand );
280 #undef REGISTER
281 
282  server.start();
283  kDebug() << "Startup timing:" << timer.elapsed() << "ms elapsed: UiServer started";
284 #endif
285 
286  const bool daemon = args->isSet("daemon");
287  if ( !daemon && app.isSessionRestored() ) {
288  app.restoreMainWindow();
289  }
290 
291 #ifndef QT_NO_SPLASHSCREEN
292  // Don't show splash screen if daemon or session restore
293  if ( !( daemon || app.isSessionRestored() ) )
294  splash.show();
295 #endif
296  if ( !selfCheck( splash ) )
297  return 1;
298  kDebug() << "Startup timing:" << timer.elapsed() << "ms elapsed: SelfCheck completed";
299 
300 #ifdef HAVE_USABLE_ASSUAN
301  fillKeyCache( &splash, &server );
302 #else
303  fillKeyCache( &splash, 0 );
304 #endif
305  kDebug() << "Startup timing:" << timer.elapsed() << "ms elapsed: KeyCache loaded";
306 
307 #ifndef QT_NO_SYSTEMTRAYICON
308  app.startMonitoringSmartCard();
309 #endif
310 
311  app.setIgnoreNewInstance( false );
312 
313  if ( !daemon ) {
314  app.newInstance();
315  app.setFirstNewInstance( false );
316  kDebug() << "Startup timing:" << timer.elapsed() << "ms elapsed: new instance created";
317 #ifndef QT_NO_SPLASHSCREEN
318  splash.finish( app.mainWindow() );
319 #endif // QT_NO_SPLASHSCREEN
320  }
321 
322  rc = app.exec();
323 
324 #ifdef HAVE_USABLE_ASSUAN
325  app.setIgnoreNewInstance( true );
326  QObject::disconnect( &server, SIGNAL(startKeyManagerRequested()),
327  &app, SLOT(openOrRaiseMainWindow()) );
328  QObject::disconnect( &server, SIGNAL(startConfigDialogRequested()),
329  &app, SLOT(openOrRaiseConfigDialog()) );
330 
331  server.stop();
332  server.waitForStopped();
333  } catch ( const std::exception & e ) {
334  QMessageBox::information( 0, i18n("GPG UI Server Error"),
335  i18n("<qt>The Kleopatra GPG UI Server Module could not be initialized.<br/>"
336  "The error given was: <b>%1</b><br/>"
337  "You can use Kleopatra as a certificate manager, but cryptographic plugins that "
338  "rely on a GPG UI Server being present might not work correctly, or at all.</qt>",
339  Qt::escape( QString::fromUtf8( e.what() ) ) ));
340 #ifndef QT_NO_SYSTEMTRAYICON
341  app.startMonitoringSmartCard();
342 #endif
343  app.setIgnoreNewInstance( false );
344  rc = app.exec();
345  app.setIgnoreNewInstance( true );
346  }
347 #endif
348 
349  return rc;
350 }
signcommand.h
QEventLoop
QBasicTimer
createchecksumscommand.h
signencryptfilescommand.h
UserIcon_nocached
static QPixmap UserIcon_nocached(const char *name)
Definition: main.cpp:109
main
int main(int argc, char **argv)
Definition: main.cpp:197
Kleo::Commands::SelfTestCommand::isCanceled
bool isCanceled() const
Definition: selftestcommand.cpp:251
QThreadPool::globalInstance
QThreadPool * globalInstance()
Kleo::Command::start
void start()
Definition: commands/command.cpp:182
KleopatraApplication::restoreMainWindow
void restoreMainWindow()
Definition: kleopatraapplication.cpp:425
selectcertificatecommand.h
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QTime
QMessageBox::information
StandardButton information(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
Kleo::Command::setAutoDelete
void setAutoDelete(bool on)
Definition: commands/command.cpp:120
KleopatraApplication
Definition: kleopatraapplication.h:46
Kleo::Commands::SelfTestCommand::setAutomaticMode
void setAutomaticMode(bool automatic)
Definition: selftestcommand.cpp:237
Kleo::ReloadKeysCommand
Definition: reloadkeyscommand.h:41
prepsigncommand.h
KleopatraApplication::setIgnoreNewInstance
void setIgnoreNewInstance(bool on)
Definition: kleopatraapplication.cpp:527
QTimerEvent
AboutData
Definition: aboutdata.h:38
boost::shared_ptr
Definition: encryptemailcontroller.h:51
QTime::elapsed
int elapsed() const
QString::fromUtf8
QString fromUtf8(const char *str, int size)
Kleo::UiServer
Definition: uiserver.h:52
QThreadPool::maxThreadCount
maxThreadCount
fillKeyCache
static void fillKeyCache(SplashScreen *splash, Kleo::UiServer *server)
Definition: main.cpp:175
decryptfilescommand.h
QEventLoop::exec
int exec(QFlags< QEventLoop::ProcessEventsFlag > flags)
SPLASHSCREEN_TIMEOUT
static const int SPLASHSCREEN_TIMEOUT
Definition: main.cpp:100
KleopatraApplication::mainWindow
const MainWindow * mainWindow() const
Definition: kleopatraapplication.cpp:383
verifycommand.h
Kleo::Commands::SelfTestCommand
Definition: selftestcommand.h:43
QString::isEmpty
bool isEmpty() const
mainwindow.h
aboutdata.h
Kleo::Commands::SelfTestCommand::setSplashScreen
void setSplashScreen(KSplashScreen *splash)
Definition: selftestcommand.cpp:243
reloadkeyscommand.h
verifyfilescommand.h
QString
selftestcommand.h
gnupg-helper.h
KleopatraApplication::commandLineOptions
static KCmdLineOptions commandLineOptions()
Definition: kleopatraapplication.cpp:129
QPixmap
assuancommand.h
archivedefinition.h
prepencryptcommand.h
KleopatraApplication::startMonitoringSmartCard
void startMonitoringSmartCard()
Definition: kleopatraapplication.cpp:474
decryptcommand.h
KleopatraApplication::newInstance
int newInstance()
Definition: kleopatraapplication.cpp:266
QLatin1String
encryptcommand.h
Qt::escape
QString escape(const QString &plain)
KleopatraApplication::setFirstNewInstance
void setFirstNewInstance(bool on)
Definition: kleopatraapplication.cpp:531
verifychecksumscommand.h
decryptverifyfilescommand.h
QTimerEvent::timerId
int timerId() const
name
const char * name
Definition: uiserver/selectcertificatecommand.cpp:113
selfCheck
static bool selfCheck(SplashScreen &splash)
Definition: main.cpp:145
echocommand.h
QTime::start
void start()
importfilescommand.h
Kleo::gpg4winInstallPath
QString gpg4winInstallPath()
Definition: gnupg-helper.cpp:104
kleopatraapplication.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Kleo::ArchiveDefinition::setInstallPath
static void setInstallPath(const QString &ip)
Definition: archivedefinition.cpp:78
uiserver.h
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal