• 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
  • utils
gnupg-helper.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  utils/gnupg-helper.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 Klarälvdalens 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 "gnupg-helper.h"
36 
37 #include "utils/hex.h"
38 
39 #include <gpgme++/engineinfo.h>
40 
41 #include <KDebug>
42 #include <KStandardDirs>
43 
44 #include <QDir>
45 #include <QFile>
46 #include <QString>
47 #include <QProcess>
48 #include <QByteArray>
49 
50 #include <gpg-error.h>
51 
52 #ifdef Q_OS_WIN
53 #include "gnupg-registry.h"
54 #endif // Q_OS_WIN
55 
56 QString Kleo::gnupgHomeDirectory()
57 {
58 #ifdef Q_OS_WIN
59  return QFile::decodeName( default_homedir() );
60 #else
61  const QByteArray gnupgHome = qgetenv( "GNUPGHOME" );
62  if ( !gnupgHome.isEmpty() )
63  return QFile::decodeName( gnupgHome );
64  else
65  return QDir::homePath() + QLatin1String("/.gnupg");
66 #endif
67 }
68 
69 int Kleo::makeGnuPGError( int code ) {
70  return gpg_error( static_cast<gpg_err_code_t>( code ) );
71 }
72 
73 static QString findGpgExe( GpgME::Engine engine, const QString & exe ) {
74  const GpgME::EngineInfo info = GpgME::engineInfo( engine );
75  return info.fileName() ? QFile::decodeName( info.fileName() ) : KStandardDirs::findExe( exe ) ;
76 }
77 
78 QString Kleo::gpgConfPath() {
79  return findGpgExe( GpgME::GpgConfEngine, QLatin1String("gpgconf") );
80 }
81 
82 QString Kleo::gpgSmPath() {
83  return findGpgExe( GpgME::GpgSMEngine, QLatin1String("gpgsm") );
84 }
85 
86 QString Kleo::gpgPath() {
87  return findGpgExe( GpgME::GpgEngine, QLatin1String("gpg") );
88 }
89 
90 QStringList Kleo::gnupgFileBlacklist() {
91  return QStringList()
92  << QLatin1String("dirmngr-cache.d")
93  << QLatin1String("S.uiserver")
94  << QLatin1String("S.gpg-agent")
95  << QLatin1String("random_seed")
96  << QLatin1String("*~")
97  << QLatin1String("*.bak")
98  << QLatin1String("*.lock")
99  << QLatin1String("*.tmp")
100  << QLatin1String("reader_*.status")
101  ;
102 }
103 
104 QString Kleo::gpg4winInstallPath() {
105 #ifndef _WIN32_WCE
106  return gpgConfListDir( "bindir" );
107 #else
108  HKEY hKey;
109  TCHAR *lszValue;
110  DWORD dwType=REG_SZ;
111  DWORD dwSize;
112 
113  if ( ERROR_SUCCESS!=RegOpenKeyExW ( HKEY_LOCAL_MACHINE, L"Software\\GNU\\GnuPG", 0, KEY_READ, &hKey ) ){
114  return QString();
115  }
116 
117  if ( ERROR_SUCCESS!=RegQueryValueExW ( hKey, L"Install Directory", NULL, NULL, NULL, &dwSize ) ){
118  return QString();
119  }
120 
121  lszValue = new TCHAR[dwSize];
122 
123  if ( ERROR_SUCCESS!=RegQueryValueExW ( hKey,L"Install Directory", NULL, &dwType, ( LPBYTE ) lszValue, &dwSize ) ) {
124  delete [] lszValue;
125  return QString();
126  }
127  RegCloseKey ( hKey );
128 
129  QString res = QString::fromUtf16 ( ( const ushort* ) lszValue );
130  delete [] lszValue;
131 
132  return res;
133 #endif
134 }
135 
136 QString Kleo::gpgConfListDir( const char * which ) {
137  if ( !which || !*which )
138  return QString();
139  const QString gpgConfPath = Kleo::gpgConfPath();
140  if ( gpgConfPath.isEmpty() )
141  return QString();
142  QProcess gpgConf;
143  kDebug() << "gpgConfListDir: starting " << qPrintable( gpgConfPath ) << " --list-dirs";
144  gpgConf.start( gpgConfPath, QStringList() << QLatin1String( "--list-dirs" ) );
145  if ( !gpgConf.waitForFinished() ) {
146  kDebug() << "gpgConfListDir(): failed to execute gpgconf: " << qPrintable( gpgConf.errorString() );
147  kDebug() << "output was:" << endl << gpgConf.readAllStandardError().constData();
148  return QString();
149  }
150  const QList<QByteArray> lines = gpgConf.readAllStandardOutput().split( '\n' );
151  Q_FOREACH( const QByteArray & line, lines )
152  if ( line.startsWith( which ) && line[qstrlen(which)] == ':' ) {
153  const int begin = qstrlen(which) + 1;
154  int end = line.size();
155  while ( end && ( line[end-1] == '\n' || line[end-1] == '\r' ) )
156  --end;
157  const QString result = QDir::fromNativeSeparators( QFile::decodeName( hexdecode( line.mid( begin, end - begin ) ) ) );
158  kDebug() << "gpgConfListDir: found " << qPrintable( result )
159  << " for '" << which << "'entry";
160  return result;
161  }
162  kDebug() << "gpgConfListDir(): didn't find '" << which << "'"
163  << "entry in output:" << endl << gpgConf.readAllStandardError().constData();
164  return QString();
165 }
Kleo::gpgPath
QString gpgPath()
Definition: gnupg-helper.cpp:86
Kleo::gnupgHomeDirectory
QString gnupgHomeDirectory()
Definition: gnupg-helper.cpp:56
findGpgExe
static QString findGpgExe(GpgME::Engine engine, const QString &exe)
Definition: gnupg-helper.cpp:73
default_homedir
char * default_homedir(void)
gnupg-registry.h
Kleo::gpgConfListDir
QString gpgConfListDir(const char *which)
Definition: gnupg-helper.cpp:136
Kleo::gnupgFileBlacklist
QStringList gnupgFileBlacklist()
Definition: gnupg-helper.cpp:90
gpgConfPath
static QString gpgConfPath()
Definition: configreader.cpp:294
hex.h
Kleo::gpgSmPath
QString gpgSmPath()
Definition: gnupg-helper.cpp:82
gnupg-helper.h
Kleo::makeGnuPGError
int makeGnuPGError(int code)
Definition: gnupg-helper.cpp:69
Kleo::gpgConfPath
QString gpgConfPath()
Definition: gnupg-helper.cpp:78
Kleo::gpg4winInstallPath
QString gpg4winInstallPath()
Definition: gnupg-helper.cpp:104
Kleo::hexdecode
std::string hexdecode(const char *s)
Definition: hex.cpp:117
QList
Definition: commands/command.h:46
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