1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/***************************************************************************
gpgcfg.cpp - description
-------------------
begin : Sun Mar 17 2002
copyright : (C) 2002 by Vladimir Shutoff
email : vovan@shutoff.ru
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "misc.h"
#include "gpg.h"
#include "gpgcfg.h"
#include "ballonmsg.h"
#include "editfile.h"
#include "linklabel.h"
#ifdef WIN32
#include "gpgfind.h"
#endif
#include "gpgadv.h"
#include "gpggen.h"
#include <qtabwidget.h>
#include <qcombobox.h>
#include <qtimer.h>
#include <qprocess.h>
using namespace SIM;
GpgCfg::GpgCfg(QWidget *parent, GpgPlugin *plugin)
: GpgCfgBase(parent)
{
m_plugin = plugin;
m_process= NULL;
m_bNew = false;
#ifdef WIN32
edtGPG->setText(m_plugin->GPG());
edtGPG->setFilter(i18n("GPG(gpg.exe)"));
m_find = NULL;
#else
lblGPG->hide();
edtGPG->hide();
#endif
edtHome->setText(m_plugin->getHomeDir());
edtHome->setDirMode(true);
edtHome->setShowHidden(true);
edtHome->setTitle(i18n("Select home directory"));
lnkGPG->setUrl("http://www.gnupg.org/(en)/download/index.html");
lnkGPG->setText(i18n("Download GPG"));
connect(btnFind, SIGNAL(clicked()), this, SLOT(find()));
connect(edtGPG, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)));
textChanged(edtGPG->text());
for (QObject *p = parent; p != NULL; p = p->parent()){
if (!p->inherits("QTabWidget"))
continue;
QTabWidget *tab = static_cast<QTabWidget*>(p);
m_adv = new GpgAdvanced(tab, plugin);
tab->addTab(m_adv, i18n("&Advanced"));
tab->adjustSize();
break;
}
connect(btnRefresh, SIGNAL(clicked()), this, SLOT(refresh()));
connect(cmbKey, SIGNAL(activated(int)), this, SLOT(selectKey(int)));
fillSecret();
refresh();
}
GpgCfg::~GpgCfg()
{
#ifdef WIN32
delete m_find;
#endif
delete m_adv;
}
void GpgCfg::apply()
{
QString key;
int nKey = cmbKey->currentItem();
if (nKey && (nKey < cmbKey->count() - 1)){
QString k = cmbKey->currentText();
key = getToken(k, ' ');
}
m_plugin->setKey(key);
#ifdef WIN32
m_plugin->setGPG(edtGPG->text());
#endif
m_plugin->setHome(edtHome->text());
m_adv->apply();
m_plugin->reset();
}
#ifdef WIN32
void GpgCfg::textChanged(const QString &str)
{
if (str.isEmpty()){
lnkGPG->show();
btnFind->show();
}else{
lnkGPG->hide();
btnFind->hide();
}
}
#else
void GpgCfg::textChanged(const QString&)
{
lnkGPG->hide();
btnFind->hide();
}
#endif
void GpgCfg::find()
{
#ifdef WIN32
if (m_find == NULL){
m_find = new GpgFind(edtGPG);
connect(m_find, SIGNAL(finished()), this, SLOT(findFinished()));
}
raiseWindow(m_find);
#endif
}
void GpgCfg::findFinished()
{
#ifdef WIN32
m_find = NULL;
#endif
}
void GpgCfg::fillSecret(const QByteArray &ba)
{
int cur = 0;
int n = 1;
cmbKey->clear();
cmbKey->insertItem(i18n("None"));
if (!ba.isEmpty()){
QCString all(ba);
for (;;){
QCString line = getToken(all, '\n');
if(line.isEmpty())
break;
QCString type = getToken(line, ':');
if (type == "sec"){
getToken(line, ':');
getToken(line, ':');
getToken(line, ':');
QString sign = QString::fromLocal8Bit(getToken(line, ':'));
if (sign == m_plugin->getKey())
cur = n;
getToken(line, ':');
getToken(line, ':');
getToken(line, ':');
getToken(line, ':');
QCString name = getToken(line, ':');
cmbKey->insertItem(QString::fromLocal8Bit(sign) + QString(" - ") +
QString::fromLocal8Bit(name));
n++;
}
}
}
cmbKey->insertItem(i18n("New"));
if (m_bNew){
cur = cmbKey->count() - 2;
m_bNew = false;
}
cmbKey->setCurrentItem(cur);
}
void GpgCfg::refresh()
{
#ifdef WIN32
QString gpg = edtGPG->text();
#else
QString gpg = m_plugin->GPG();
#endif
QString home = edtHome->text();
if (gpg.isEmpty() || home.isEmpty()){
fillSecret();
return;
}
if (m_process)
return;
QStringList sl;
sl += gpg;
sl += "--no-tty";
sl += "--homedir";
sl += home;
sl += QStringList::split(' ', GpgPlugin::plugin->getSecretList());
m_process = new QProcess(sl, this);
connect(m_process, SIGNAL(processExited()), this, SLOT(secretReady()));
if (!m_process->start()) {
BalloonMsg::message(i18n("Get secret list failed"), btnRefresh);
delete m_process;
m_process = 0;
}
}
void GpgCfg::secretReady()
{
if (m_process->normalExit() && m_process->exitStatus()==0) {
fillSecret(m_process->readStdout());
} else {
QByteArray ba1, ba2;
ba1 = m_process->readStderr();
ba2 = m_process->readStdout();
QString s(" (");
if (!ba1.isEmpty())
s += QString::fromLocal8Bit(ba1.data(), ba1.size());
if (!ba2.isEmpty()) {
if(!s.isEmpty())
s += ' ';
s += QString::fromLocal8Bit(ba2.data(), ba2.size());
}
s += ')';
if(s == " ()")
s = QString::null;
BalloonMsg::message(i18n("Get secret list failed") + s, btnRefresh);
}
delete m_process;
m_process = 0;
}
void GpgCfg::selectKey(int n)
{
if (n == cmbKey->count() - 1){
if(edtHome->text().isEmpty())
edtHome->setText(m_plugin->getHomeDir());
GpgGen gen(this);
if (gen.exec()){
m_bNew = true;
QTimer::singleShot(0, this, SLOT(refresh()));
}
}
}
#ifndef NO_MOC_INCLUDES
#include "gpgcfg.moc"
#endif
|