• 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
  • 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  return gpgConfListDir( "bindir" );
106 }
107 
108 QString Kleo::gpgConfListDir( const char * which ) {
109  if ( !which || !*which )
110  return QString();
111  const QString gpgConfPath = Kleo::gpgConfPath();
112  if ( gpgConfPath.isEmpty() )
113  return QString();
114  QProcess gpgConf;
115  kDebug() << "gpgConfListDir: starting " << qPrintable( gpgConfPath ) << " --list-dirs";
116  gpgConf.start( gpgConfPath, QStringList() << QLatin1String( "--list-dirs" ) );
117  if ( !gpgConf.waitForFinished() ) {
118  kDebug() << "gpgConfListDir(): failed to execute gpgconf: " << qPrintable( gpgConf.errorString() );
119  kDebug() << "output was:" << endl << gpgConf.readAllStandardError().constData();
120  return QString();
121  }
122  const QList<QByteArray> lines = gpgConf.readAllStandardOutput().split( '\n' );
123  Q_FOREACH( const QByteArray & line, lines )
124  if ( line.startsWith( which ) && line[qstrlen(which)] == ':' ) {
125  const int begin = qstrlen(which) + 1;
126  int end = line.size();
127  while ( end && ( line[end-1] == '\n' || line[end-1] == '\r' ) )
128  --end;
129  const QString result = QDir::fromNativeSeparators( QFile::decodeName( hexdecode( line.mid( begin, end - begin ) ) ) );
130  kDebug() << "gpgConfListDir: found " << qPrintable( result )
131  << " for '" << which << "'entry";
132  return result;
133  }
134  kDebug() << "gpgConfListDir(): didn't find '" << which << "'"
135  << "entry in output:" << endl << gpgConf.readAllStandardError().constData();
136  return QString();
137 }
Kleo::gpgPath
QString gpgPath()
Definition: gnupg-helper.cpp:86
QByteArray::split
QList< QByteArray > split(char sep) const
QDir::fromNativeSeparators
QString fromNativeSeparators(const QString &pathName)
QByteArray
Kleo::gnupgHomeDirectory
QString gnupgHomeDirectory()
Definition: gnupg-helper.cpp:56
QIODevice::errorString
QString errorString() const
QByteArray::isEmpty
bool isEmpty() const
QByteArray::startsWith
bool startsWith(const QByteArray &ba) const
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:108
Kleo::gnupgFileBlacklist
QStringList gnupgFileBlacklist()
Definition: gnupg-helper.cpp:90
QDir::homePath
QString homePath()
gpgConfPath
static QString gpgConfPath()
Definition: configreader.cpp:294
QProcess
QString::isEmpty
bool isEmpty() const
hex.h
Kleo::gpgSmPath
QString gpgSmPath()
Definition: gnupg-helper.cpp:82
QByteArray::constData
const char * constData() const
QString
QList
Definition: commands/command.h:46
QByteArray::mid
QByteArray mid(int pos, int len) const
QStringList
gnupg-helper.h
Kleo::makeGnuPGError
int makeGnuPGError(int code)
Definition: gnupg-helper.cpp:69
QLatin1String
Kleo::gpgConfPath
QString gpgConfPath()
Definition: gnupg-helper.cpp:78
Kleo::gpg4winInstallPath
QString gpg4winInstallPath()
Definition: gnupg-helper.cpp:104
QByteArray::size
int size() const
QProcess::readAllStandardOutput
QByteArray readAllStandardOutput()
QProcess::start
void start(const QString &program, const QStringList &arguments, QFlags< QIODevice::OpenModeFlag > mode)
QFile::decodeName
QString decodeName(const QByteArray &localFileName)
QProcess::readAllStandardError
QByteArray readAllStandardError()
Kleo::hexdecode
std::string hexdecode(const char *s)
Definition: hex.cpp:117
QProcess::waitForFinished
bool waitForFinished(int msecs)
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