You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.6 KiB
114 lines
3.6 KiB
/** @id $Id$ |
|
|
|
This file has been added: |
|
- by bootstrap.sh |
|
- on Thu, 08 October 2015 11:56:48 +0200 |
|
|
|
*/ |
|
// 1 2 3 4 5 6 7 8 |
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890 |
|
|
|
#ifndef CERTMAN_HXX |
|
#define CERTMAN_HXX |
|
|
|
#include <certificate.hxx> |
|
#include <openfromurl.hxx> |
|
#include <ui_certman.hxx> |
|
#include <QMainWindow> |
|
#include <QFileDialog> |
|
#include <QFileInfo> |
|
#include <QMessageBox> |
|
#include <QRegExp> |
|
|
|
#include <QDebug> |
|
|
|
/// Main Window |
|
/** Main window for certman */ |
|
class CertMan: public QMainWindow, protected Ui::CertMan { |
|
Q_OBJECT; |
|
public: |
|
explicit CertMan(QWidget *parent = 0): |
|
QMainWindow(parent), |
|
_openFromURL(this) { |
|
setupUi(this); |
|
connect(&_openFromURL, SIGNAL(certificate(QUrl, QSslCertificate)), |
|
SLOT(certificate(QUrl, QSslCertificate))); |
|
} |
|
bool open(const QString& fileName) { |
|
bool status(true); |
|
QFile file(fileName); |
|
file.open(QIODevice::ReadOnly); |
|
while (!file.atEnd()) { |
|
QSslCertificate c(&file); |
|
if (!c.isNull()) { |
|
Certificate* win(new Certificate(_mdi)); |
|
_mdi->addSubWindow(win)->setWindowTitle(QFileInfo(fileName).fileName()); |
|
win->show(); |
|
win->certificate(c); |
|
} else { |
|
status = false; |
|
qDebug()<<QObject::trUtf8("Cannot read PEM certificate from file: %1") |
|
.arg(fileName); |
|
} |
|
} |
|
file.close(); |
|
return status; |
|
} |
|
virtual ~CertMan() {} |
|
protected slots: |
|
void on__actionOpenCertificate_triggered() { |
|
QString fileName = QFileDialog::getOpenFileName(this); |
|
if (fileName.isEmpty()) return; |
|
if (!open(fileName)) |
|
QMessageBox::warning(this, "Error Loading File", |
|
QString("Error loading file:\n%1").arg(fileName)); |
|
} |
|
void on__actionOpenFromURL_triggered() { |
|
if (_openFromURL.exec()==QDialog::Accepted) { |
|
_openFromURL.read(); |
|
} |
|
} |
|
void certificate(QUrl url, QSslCertificate cert) { |
|
qDebug()<<"got certificate: "<<cert.subjectInfo(QSslCertificate::CommonName).join("/"); |
|
Certificate* win(new Certificate(_mdi)); |
|
_mdi->addSubWindow(win)->setWindowTitle |
|
(url.host()+" - "+ |
|
cert.subjectInfo(QSslCertificate::CommonName).join("/")); |
|
win->show(); |
|
win->certificate(cert); |
|
} |
|
void on__actionSaveAs_triggered() { |
|
QMdiSubWindow* win(_mdi->currentSubWindow()); |
|
if (!win) return; |
|
Certificate* cert(dynamic_cast<Certificate*>(win->widget())); |
|
QString fileName(QFileDialog::getSaveFileName |
|
(this, |
|
win->windowTitle(), |
|
win->windowTitle() |
|
.replace(" ", "_").replace(QRegExp("\\.(pem|der)$"), "") |
|
+".pem", |
|
tr("Certificates (*.der *.pem *.txt)"))); |
|
if (fileName.isEmpty()) return; |
|
QFile file(fileName); |
|
file.open(QIODevice::WriteOnly); |
|
QFileInfo fileInfo(fileName); |
|
if (fileInfo.suffix()=="der") { |
|
QDataStream out(&file); |
|
out<<cert->certificate().toDer(); |
|
} else if (fileInfo.suffix()=="pem") { |
|
QDataStream out(&file); |
|
out<<cert->certificate().toPem(); |
|
} else if (fileInfo.suffix()=="txt") { |
|
QTextStream out(&file); |
|
out<<cert->certificate().toText(); |
|
} else { // defaults to pem |
|
QDataStream out(&file); |
|
out<<cert->certificate().toPem(); |
|
} |
|
file.close(); |
|
} |
|
private: |
|
OpenFromURL _openFromURL; |
|
}; |
|
|
|
#endif
|
|
|