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

kleopatra

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

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