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
|
/* vi: set sw=4 ts=4:
*
* Copyright (C) 2011 Christian Hohnstaedt.
*
* All rights reserved.
*/
#include "SearchPkcs11.h"
#include "lib/base.h"
#include "lib/func.h"
#include "lib/pkcs11_lib.h"
#include <QComboBox>
#include <QLineEdit>
#include <QFileDialog>
#include <QPushButton>
#include <QMessageBox>
#include <QStringList>
#include <QFile>
SearchPkcs11::SearchPkcs11(QWidget *parent, const QString &fname)
:QDialog(parent)
{
setupUi(this);
filename->setText(nativeSeparator(fname));
setWindowTitle(XCA_TITLE);
liblist->setSelectionMode(QAbstractItemView::ExtendedSelection);
searching = NULL;
}
SearchPkcs11::~SearchPkcs11()
{
if (searching)
search->click();
}
void SearchPkcs11::on_fileBut_clicked()
{
QString s = QFileDialog::getExistingDirectory(this, QString(XCA_TITLE),
filename->text(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!s.isEmpty())
filename->setText(nativeSeparator(s));
}
void SearchPkcs11::on_search_clicked()
{
if (searching) {
return;
}
searching = new searchThread(filename->text(),
getLibExtensions(),
subdirs->isChecked());
liblist->clear();
connect(searching, SIGNAL(updateLibs(QString)),
this, SLOT(updateLibs(QString)));
connect(searching, SIGNAL(updateCurrFile(QString)),
this, SLOT(updateCurrFile(QString)));
connect(searching, SIGNAL(finished()),
this, SLOT(finishSearch()));
connect(search, SIGNAL(clicked()),
searching, SLOT(cancelSearch()));
search->setText("Cancel");
searching->start();
}
void SearchPkcs11::finishSearch()
{
search->setText("Start");
currFile->setText(tr("The following files are possible PKCS#11 libraries"));
if (!searching)
return;
searching->wait(1000);
delete searching;
searching = NULL;
}
void SearchPkcs11::buttonPress(QAbstractButton *but)
{
QList<QListWidgetItem *> libitems;
QListWidgetItem *lib;
switch (buttonBox->standardButton(but)) {
case QDialogButtonBox::Ok:
accept();
break;
default:
case QDialogButtonBox::Cancel:
reject();
break;
case QDialogButtonBox::Open:
libitems = liblist->selectedItems();
foreach(lib, libitems)
loadItem(lib);
break;
}
}
void SearchPkcs11::loadItem(QListWidgetItem *lib)
{
emit addLib(lib->whatsThis());
delete lib;
}
void SearchPkcs11::updateCurrFile(QString f)
{
f = nativeSeparator(f);
int len = f.length();
QString reduced = f;
QFontMetrics fm(currFile->font());
currFile->setToolTip(f);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
while ((currFile->width() < (fm.horizontalAdvance(reduced) -10)) &&
#else
while ((currFile->width() < (fm.width(reduced) -10)) &&
#endif
(len > 0)) {
len -= 10;
reduced = compressFilename(f, len);
}
currFile->setText(reduced);
currFile->update();
}
void SearchPkcs11::updateLibs(QString f)
{
QListWidgetItem *i = new QListWidgetItem(nativeSeparator(f));
i->setWhatsThis(f);
liblist->addItem(i);
liblist->update();
}
searchThread::searchThread(QString _dir, const QStringList _ext, bool _recursive)
{
dirname = _dir;
ext = _ext;
recursive = _recursive;
keepOnRunning = true;
}
void searchThread::cancelSearch()
{
keepOnRunning = false;
}
bool searchThread::checkLib(QString file)
{
qint64 size;
int r = -1;
QFile qf(file);
size = qf.size();
if (qf.open(QIODevice::ReadOnly)) {
uchar *p = qf.map(0, size);
r = QByteArray::fromRawData((char*)p, size)
.indexOf("C_GetFunctionList");
qf.unmap(p);
qf.close();
}
return r != -1;
}
void searchThread::search(QString mydir)
{
QDir dir = QDir(mydir);
QStringList files = dir.entryList(
QStringList(ext),
QDir::Files | QDir::Readable);
while (!files.isEmpty() && keepOnRunning) {
QString file = files.takeFirst();
if (file.isEmpty())
continue;
file = mydir + "/" + file;
emit updateCurrFile(file);
if (checkLib(file))
emit updateLibs(file);
}
if (recursive && keepOnRunning) {
QString d;
QStringList dirs = dir.entryList(QStringList(),
QDir::AllDirs | QDir::NoDotAndDotDot);
foreach(d, dirs) {
if (!keepOnRunning)
break;
QString s = mydir + "/" + d;
emit updateCurrFile(s);
search(s);
}
}
}
|