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
00020 #include "kgpgsettings.h"
00021
00022 GPGProc::GPGProc(QObject *parent, const QString &binary)
00023 : KLineBufferedProcess(parent)
00024 {
00025 resetProcess(binary);
00026 }
00027
00028 GPGProc::~GPGProc()
00029 {
00030 }
00031
00032 void
00033 GPGProc::resetProcess(const QString &binary)
00034 {
00035 QStringList args;
00036 args << "--no-secmem-warning" << "--no-tty";
00037 if (binary.isEmpty())
00038 setProgram(KGpgSettings::gpgBinaryPath(), args);
00039 else
00040 setProgram(binary, args);
00041 setOutputChannelMode(OnlyStdoutChannel);
00042
00043 disconnect(SIGNAL(finished(int, QProcess::ExitStatus)));
00044 disconnect(SIGNAL(lineReadyStandardOutput()));
00045 }
00046
00047 void GPGProc::start()
00048 {
00049 connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished()));
00050 connect(this, SIGNAL(lineReadyStandardOutput()), this, SLOT(received()));
00051 KProcess::start();
00052 }
00053
00054 void GPGProc::received()
00055 {
00056 emit readReady(this);
00057 }
00058
00059 void GPGProc::finished()
00060 {
00061 emit processExited(this);
00062 }
00063
00064 int GPGProc::readln(QString &line, const bool &colons)
00065 {
00066 QByteArray a;
00067 if (!readLineStandardOutput(&a))
00068 return -1;
00069
00070 line = recode(a, colons);
00071
00072 return line.length();
00073 }
00074
00075 int GPGProc::readln(QStringList &l)
00076 {
00077 QString s;
00078
00079 int len = readln(s);
00080 if (len < 0)
00081 return len;
00082
00083 l = s.split(':');
00084
00085 for (int i = 0; i < l.count(); ++i)
00086 {
00087 int j = 0;
00088 while ((j = l[i].indexOf("\\x3a", j, Qt::CaseInsensitive)) >= 0)
00089 {
00090 l[i].replace(j, 4, ':');
00091 j++;
00092 }
00093 }
00094
00095 return l.count();
00096 }
00097
00098 QString
00099 GPGProc::recode(QByteArray a, const bool colons)
00100 {
00101 int pos = 0;
00102
00103 while ((pos = a.indexOf("\\x", pos)) >= 0) {
00104 if (pos > a.length() - 4)
00105 break;
00106
00107 char c1, c2;
00108 c1 = a[pos + 2];
00109 c2 = a[pos + 3];
00110
00111
00112
00113 if (!colons && (c1 == '3') && ((c2 == 'a') || (c2 == 'A'))) {
00114 pos += 3;
00115 continue;
00116 }
00117
00118 if (!isxdigit(c1) || !isxdigit(c2))
00119 continue;
00120
00121 char n[2] = { 0, 0 };
00122
00123 if (isdigit(c1))
00124 n[0] = c1 - '0';
00125 else
00126 n[0] = tolower(c1) - 'a' + 10;
00127 n[0] *= 16;
00128
00129 if (isdigit(c2))
00130 n[0] += c2 - '0';
00131 else
00132 n[0] += tolower(c2) - 'a' + 10;
00133
00134
00135 int npos = pos;
00136 QByteArray pattern = a.mid(pos, 4);
00137 do {
00138 a.replace(npos, 4, n);
00139 } while ((npos = a.indexOf(pattern, npos)) >= 0);
00140 }
00141
00142 return QTextCodec::codecForName("utf8")->toUnicode(a);
00143 }
00144
00145 #include "gpgproc.moc"