kgpg
gpgproc.cpp
Go to the documentation of this file.00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "gpgproc.h"
00015
00016 #include <ctype.h>
00017
00018 #include <QTextCodec>
00019 #include <QFile>
00020
00021
00022
00023 #include "kgpgsettings.h"
00024
00025 class GPGProcPrivate
00026 {
00027 public:
00028 GPGProcPrivate() : codec(QTextCodec::codecForName("utf8"))
00029 {
00030 }
00031
00032 QTextCodec *codec;
00033 QByteArray recvbuffer;
00034 };
00035
00036 GPGProc::GPGProc(QObject *parent)
00037 : KProcess(parent), d(new GPGProcPrivate())
00038 {
00039 QStringList args;
00040 args << "--no-secmem-warning" << "--no-tty" << "--with-colons";
00041 setProgram(KGpgSettings::gpgBinaryPath(), args);
00042 setOutputChannelMode(OnlyStdoutChannel);
00043 }
00044
00045 GPGProc::~GPGProc()
00046 {
00047 delete d;
00048 }
00049
00050 void GPGProc::start()
00051 {
00052 connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished()));
00053 connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(received()));
00054 KProcess::start();
00055 }
00056
00057 void GPGProc::received()
00058 {
00059 d->recvbuffer.append(readAllStandardOutput());
00060 if (d->recvbuffer.indexOf('\n') >= 0)
00061 emit readReady(this);
00062 }
00063
00064 void GPGProc::finished()
00065 {
00066 emit processExited(this);
00067 }
00068
00069 int GPGProc::readln(QString &line)
00070 {
00071 int len = d->recvbuffer.indexOf('\n');
00072 if (len < 0)
00073 return -1;
00074
00075
00076 QByteArray a = d->recvbuffer.mid(0, len);
00077 d->recvbuffer.remove(0, len + 1);
00078
00079 int pos = 0;
00080
00081 while ((pos = a.indexOf("\\x", pos)) >= 0)
00082 {
00083 if (pos > a.length() - 4)
00084 break;
00085
00086 char c1, c2;
00087 c1 = a[pos + 2];
00088 c2 = a[pos + 3];
00089
00090
00091
00092 if ((c1 == '3') && ((c2 == 'a') || (c2 == 'A')))
00093 {
00094 pos += 3;
00095 continue;
00096 }
00097
00098 if (!isxdigit(c1) || !isxdigit(c2))
00099 continue;
00100
00101 char n[2] = { 0, 0 };
00102
00103 if (isdigit(c1))
00104 n[0] = c1 - '0';
00105 else
00106 n[0] = tolower(c1) - 'a' + 10;
00107 n[0] *= 16;
00108
00109 if (isdigit(c2))
00110 n[0] += c2 - '0';
00111 else
00112 n[0] += tolower(c2) - 'a' + 10;
00113
00114
00115 int npos = pos;
00116 QByteArray pattern = a.mid(pos, 4);
00117 do
00118 {
00119 a.replace(npos, 4, n);
00120 } while ((npos = a.indexOf(pattern, npos)) >= 0);
00121 }
00122
00123 line = d->codec->toUnicode(a);
00124
00125 return line.length();
00126 }
00127
00128 int GPGProc::readln(QStringList &l)
00129 {
00130 QString s;
00131
00132 int len = readln(s);
00133 if (len < 0)
00134 return len;
00135
00136 l = s.split(':');
00137
00138 for (int i = 0; i < l.count(); ++i)
00139 {
00140 int j = 0;
00141 while ((j = l[i].indexOf("\\x3a", j, Qt::CaseInsensitive)) >= 0)
00142 {
00143 l[i].replace(j, 4, ':');
00144 j++;
00145 }
00146 }
00147
00148 return l.count();
00149 }
00150
00151 #include "gpgproc.moc"