20 #include <KStandardDirs>
29 const QString &binary()
const;
30 void setBinary(
const QString &executable);
31 const QStringList &standardArguments()
const;
33 bool supportsDebugLevel()
const;
37 QStringList m_standardArguments;
38 unsigned int m_version;
42 GnupgBinary::GnupgBinary()
44 m_useDebugLevel(false)
48 const QString &GnupgBinary::binary()
const
67 gpg.setOutputChannelMode(KProcess::MergedChannels);
69 QStringList allArguments = arguments;
70 allArguments << QLatin1String(
"--version");
71 gpg.setProgram(executable, allArguments);
73 return (gpg.execute() == 0);
79 process << QLatin1String(
"--version" );
81 process.waitForFinished(-1);
83 if (process.exitCode() == 255) {
88 while (process.
readln(line) != -1) {
89 if (line.startsWith(QLatin1String(
"Home: "))) {
91 return line.trimmed();
99 void GnupgBinary::setBinary(
const QString &executable)
101 kDebug(2100) <<
"checking version of GnuPG executable" << executable;
103 m_binary = executable;
106 kDebug(2100) <<
"version is" << verstr << m_version;
108 m_useDebugLevel = (m_version > 0x20000);
112 m_standardArguments.clear();
113 m_standardArguments << QLatin1String(
"--no-secmem-warning" )
114 << QLatin1String(
"--no-tty" )
115 << QLatin1String(
"--no-greeting");
117 if (!gpgConfigFile.isEmpty()) {
118 m_standardArguments << QLatin1String(
"--options")
125 QFileInfo confFile(gpgConfigFile);
126 if (confFile.absolutePath() != gpgdir)
127 m_standardArguments << QLatin1String(
"--homedir")
128 << confFile.absolutePath();
131 QStringList debugLevelArguments(QLatin1String(
"--debug-level"));
132 debugLevelArguments << QLatin1String(
"none");
134 m_standardArguments << debugLevelArguments;
137 const QStringList& GnupgBinary::standardArguments()
const
139 return m_standardArguments;
147 bool GnupgBinary::supportsDebugLevel()
const
149 return m_useDebugLevel;
152 K_GLOBAL_STATIC(GnupgBinary, lastBinary)
157 resetProcess(binary);
167 GnupgBinary *bin = lastBinary;
169 if (binary.isEmpty())
174 if (bin->binary() != executable)
175 bin->setBinary(executable);
177 setProgram(executable, bin->standardArguments());
179 setOutputChannelMode(OnlyStdoutChannel);
181 disconnect(SIGNAL(
finished(
int,QProcess::ExitStatus)));
188 connect(
this, SIGNAL(
finished(
int,QProcess::ExitStatus)),
this, SLOT(
finished()), Qt::UniqueConnection);
209 line =
recode(a, colons, m_codec);
211 return line.length();
222 l = s.split(QLatin1Char(
':' ));
224 for (
int i = 0; i < l.count(); ++i)
227 while ((j = l[i].indexOf(QLatin1String(
"\\x3a" ), j, Qt::CaseInsensitive)) >= 0)
229 l[i].replace(j, 4, QLatin1Char(
':' ));
240 const char *textcodec = codec.isEmpty() ?
"utf8" : codec.constData();
243 while ((pos = a.indexOf(
"\\x", pos)) >= 0) {
244 if (pos > a.length() - 4)
247 const QByteArray pattern(a.mid(pos, 4));
248 const QByteArray hexnum(pattern.right(2));
251 n[0] = hexnum.toUShort(&ok, 16);
261 if (!colons && (n[0] ==
':' )) {
269 a.replace(npos, 4, n);
270 }
while ((npos = a.indexOf(pattern, npos)) >= 0);
273 return QTextCodec::codecForName(textcodec)->toUnicode(a);
280 if (!codecs.contains(codec))
293 QStringList values(vstr.split(QLatin1Char(
'.' )));
294 if (values.count() < 3)
297 return (0x10000 * values[0].toInt() + 0x100 * values[1].toInt() + values[2].toInt());
303 process << QLatin1String(
"--version" );
305 process.waitForFinished(-1);
307 if (process.exitCode() == 255)
311 if (process.
readln(line) != -1)
312 return line.simplified().section(QLatin1Char(
' ' ), -1);
320 process << QLatin1String(
"--version" );
322 process.waitForFinished(-1);
330 result += QString::fromUtf8(tmp);
340 QByteArray env(qgetenv(
"GNUPGHOME"));
342 if (!env.isEmpty()) {
343 gpgHome = QLatin1String( env );
344 }
else if (!binary.isEmpty()) {
350 if (gpgHome.isEmpty()) {
351 #ifdef Q_OS_WIN32 //krazy:exclude=cpp
352 gpgHome = qgetenv(
"APPDATA") + QLatin1String(
"/gnupg/" );
353 gpgHome.replace(QLatin1Char(
'\\' ), QLatin1Char(
'/' ));
355 gpgHome = QDir::homePath() + QLatin1String(
"/.gnupg/" );
359 gpgHome.replace(QLatin1String(
"//" ), QLatin1String(
"/" ));
361 if (!gpgHome.endsWith(QLatin1Char(
'/' )))
362 gpgHome.append(QLatin1Char(
'/' ));
364 if (gpgHome.startsWith(QLatin1String(
"~/")))
365 gpgHome.replace(0, 1, QDir::homePath());
367 KStandardDirs::makeDir(gpgHome, 0700);
371 #include "gpgproc.moc"
A interface to GnuPG handling UTF8 recoding correctly.
static QString gpgConfigPath()
Get The path of the gpg configuration file.
int readln(QString &line, const bool colons=false)
Reads a line of text (excluding '\n').
static QString getGpgHome(const QString &binary)
find users GnuPG directory
void processExited()
Emitted when the process has finished.
static QString getGpgProcessHome(const QString &binary)
void start()
Starts the process.
static bool checkGnupgArguments(const QString &executable, const QStringList &arguments)
check if GnuPG returns an error for this arguments
bool setCodec(const QByteArray &codec)
sets the codec used to translate the incoming data
static QString getGpgStartupError(const QString &binary)
run GnuPG and check if it complains about anything
void readReady()
Emitted when the process is ready for reading.
static QString gpgBinaryPath()
Get The path of the gpg binary used by KGpg.
static int gpgVersion(const QString &vstr)
parse GnuPG version string and return version as number
Read output of a process split into lines.
bool readLineStandardOutput(QByteArray *line)
Reads a line of text (excluding '\n') from stdout.
void lineReadyStandardOutput()
Emitted when there is a line of data available from stdout when there was previously none...
static QString recode(QByteArray a, const bool colons=true, const QByteArray &codec=QByteArray())
Recode a line from GnuPG encoding to UTF8.
void resetProcess(const QString &binary=QString())
Reset the class to the state it had right after creation.
static QString gpgVersionString(const QString &binary)
get the GnuPG version string of the given binary
bool readLineStandardError(QByteArray *line)
Reads a line of text (excluding '\n') from stderr.
bool hasLineStandardError() const
Checks if a line is ready on stdout.
static const char version[]