diff --git a/lib/db_crl.cpp b/lib/db_crl.cpp index 3960f784..30784e5b 100644 --- a/lib/db_crl.cpp +++ b/lib/db_crl.cpp @@ -219,15 +219,15 @@ void db_crl::newItem() ca = cas[0]; break; default: { - itemCombo *c = new itemCombo(NULL); + itemComboCert *c = new itemComboCert(NULL); XcaDialog *d = new XcaDialog(mainwin, revocation, c, tr("Select CA certificate"), QString()); - c->insertPkiItems(cas); + c->insertPkiItems(cas); if (!d->exec()) { delete d; return; } - ca = c->currentPkiItem(); + ca = c->currentPkiItem(); delete d; } } diff --git a/lib/db_temp.cpp b/lib/db_temp.cpp index 703d08c8..f5896e05 100644 --- a/lib/db_temp.cpp +++ b/lib/db_temp.cpp @@ -94,12 +94,12 @@ void db_temp::newItem() pki_temp *temp = NULL; QString type; - itemCombo *ic = new itemCombo(NULL); - ic->insertPkiItems(predefs); + itemComboTemp *ic = new itemComboTemp(NULL); + ic->insertPkiItems(predefs); XcaDialog *dlg = new XcaDialog(mainwin, tmpl, ic, tr("Preset Template values"), QString()); if (dlg->exec()) { - temp = new pki_temp(ic->currentPkiItem()); + temp = new pki_temp(ic->currentPkiItem()); temp->pkiSource = generated; if (temp) { if (runTempDlg(temp)) { diff --git a/lib/db_x509.cpp b/lib/db_x509.cpp index 994da542..75bac2a8 100644 --- a/lib/db_x509.cpp +++ b/lib/db_x509.cpp @@ -1029,7 +1029,7 @@ void db_x509::caProperties(QModelIndex idx) QVariant tmplId = cert->getTemplateSqlId(); pki_temp *templ = mainwin->temps->lookupPki(tmplId); - ui.temp->insertPkiItems(mainwin->temps->getAll()); + ui.temp->insertPkiItems(mainwin->temps->getAll()); ui.temp->setNullItem(tr("No template")); ui.temp->setCurrentIndex(0); if (templ) @@ -1067,7 +1067,7 @@ void db_x509::caProperties(QModelIndex idx) int rows = ui.subjectManager->rowCount(); XSqlQuery q; QSqlError e; - templ = ui.temp->currentPkiItem(); + templ = ui.temp->currentPkiItem(); tmplId = templ ? templ->getSqlItemId() : QVariant(); sl.clear(); diff --git a/lib/pki_x509.cpp b/lib/pki_x509.cpp index d0e1852e..f4c9a709 100644 --- a/lib/pki_x509.cpp +++ b/lib/pki_x509.cpp @@ -105,10 +105,12 @@ QSqlError pki_x509::insertSqlData() if (!isCA()) return q.lastError(); - SQL_PREPARE(q, "INSERT INTO authority (item, crlExpire, crlNo) " - "VALUES (?, ?, 0)"); + SQL_PREPARE(q, "INSERT INTO authority (item, template, crlExpire, crlNo, crlDays) " + "VALUES (?, ?, ?, 0, ?)"); q.bindValue(0, sqlItemId); - q.bindValue(1, now.toPlain()); + q.bindValue(1, caTemplateSqlId); + q.bindValue(2, crlExpire.toPlain()); + q.bindValue(3, crlDays); q.exec(); if (fromDataRevList.size() > 0) fromDataRevList.sqlUpdate(sqlItemId); @@ -606,7 +608,7 @@ void pki_x509::fromData(const unsigned char *p, db_header_t *head) } pki_openssl_error(); /* Superflous CaSerial = */db::stringFromData(ba); - QString __caTemplate = db::stringFromData(ba); + QString caTemplate = db::stringFromData(ba); crlDays = db::intFromData(ba); crlExpire.d2i(ba); pki_openssl_error(); @@ -635,8 +637,17 @@ void pki_x509::fromData(const unsigned char *p, db_header_t *head) my_error(tr("Wrong Size %1").arg(ba.count())); } pki_openssl_error(); + + XSqlQuery q; + SQL_PREPARE(q, "SELECT id FROM items WHERE name=? AND type=?"); + q.bindValue(0, caTemplate); + q.bindValue(1, tmpl); + q.exec(); + if (q.next()) + caTemplateSqlId = q.value(0); } + void pki_x509::writeDefault(const QString fname) { writeCert(fname + QDir::separator() + getIntName() + ".crt", diff --git a/lib/sql.cpp b/lib/sql.cpp index 452c6160..3a017568 100644 --- a/lib/sql.cpp +++ b/lib/sql.cpp @@ -34,44 +34,38 @@ DbTransaction::~DbTransaction() bool DbTransaction::begin(const char *file, int line) { - QSqlDatabase db = QSqlDatabase::database(); - if (db.transaction()) { - has_begun = true; - if (mutex++ == 0) - error = 0; - debug("Begin", file, line); - return true; - } - return false; + mutex++; + has_begun = true; + debug("Begin", file, line); + return mutex > 1 ? true : QSqlDatabase::database().transaction(); } -bool DbTransaction::commit(const char *file, int line) +bool DbTransaction::finish(const char *oper, const char *file, int line) { if (mutex > 0) mutex--; else - qCritical() << "Unbalanced DB Transaction (commit)"; - debug("Commit", file, line); + qCritical() << "Unbalanced DB Transaction in " << oper; + debug(oper, file, line); has_begun = false; if (mutex > 0) return true; + QSqlDatabase db = QSqlDatabase::database(); - return error ? db.rollback() : db.commit(); + int e = error; + error = 0; + return e ? db.rollback() : db.commit(); +} + +bool DbTransaction::commit(const char *file, int line) +{ + return finish("Commit", file, line); } bool DbTransaction::rollback(const char *file, int line) { error++; - if (mutex > 0) - mutex--; - else - qCritical() << "Unbalanced DB Transaction (rollback)"; - debug("Rollback", file, line); - has_begun = false; - if (mutex > 0) - return true; - QSqlDatabase db = QSqlDatabase::database(); - return db.rollback(); + return finish("Rollback", file, line); } bool DbTransaction::done(QSqlError e, const char *file, int line) diff --git a/lib/sql.h b/lib/sql.h index ba31bbcc..e067f824 100644 --- a/lib/sql.h +++ b/lib/sql.h @@ -22,6 +22,7 @@ class DbTransaction static int error; bool has_begun; void debug(const char *func, const char *file, int line); + bool finish(const char *oper, const char *file, int line); public: DbTransaction(); diff --git a/ui/CaProperties.ui b/ui/CaProperties.ui index c998d487..58dca499 100644 --- a/ui/CaProperties.ui +++ b/ui/CaProperties.ui @@ -147,7 +147,7 @@ - + @@ -187,7 +187,7 @@ - itemCombo + itemComboTemp QComboBox
widgets/ItemCombo.h
diff --git a/ui/NewX509.ui b/ui/NewX509.ui index 72cf206e..86dd16f7 100644 --- a/ui/NewX509.ui +++ b/ui/NewX509.ui @@ -104,7 +104,7 @@ 0 - + @@ -198,7 +198,7 @@ - + All certificates in your database that can create valid signatures @@ -259,7 +259,7 @@ - + 0 @@ -425,7 +425,7 @@ - + 0 @@ -1224,7 +1224,22 @@
widgets/clicklabel.h
- itemCombo + itemComboReq + QComboBox +
widgets/ItemCombo.h
+
+ + itemComboKey + QComboBox +
widgets/ItemCombo.h
+
+ + itemComboCert + QComboBox +
widgets/ItemCombo.h
+
+ + itemComboTemp QComboBox
widgets/ItemCombo.h
diff --git a/widgets/ItemCombo.h b/widgets/ItemCombo.h index 1a1e1f0e..5d1a0f3a 100644 --- a/widgets/ItemCombo.h +++ b/widgets/ItemCombo.h @@ -12,31 +12,43 @@ #include #include "lib/pki_base.h" -#include "lib/db_base.h" +#include "lib/pki_x509.h" +#include "lib/pki_x509req.h" +#include "lib/pki_temp.h" +//#include "lib/db_base.h" +template class itemCombo : public QComboBox { public: itemCombo(QWidget *parent) : QComboBox(parent) { } - template void insertPkiItems(QList items) { + void insertPkiItems(QList items) { clear(); foreach(T *p, items) { addItem(p->comboText(), QVariant::fromValue(p)); } } - template T *currentPkiItem() { - return itemData(currentIndex()).value(); + T *currentPkiItem() { + return itemData(currentIndex()).template value(); } void setNullItem(QString text) { - if (itemData(0).value() == NULL) + if (itemData(0).template value() == NULL) removeItem(0); insertItem(0, text, QVariant()); } - int setCurrentPkiItem(pki_base *p) { + int setCurrentPkiItem(T *p) { int idx = findData(QVariant::fromValue(p)); setCurrentIndex(idx); return idx; } }; +//class pki_temp; +//class pki_x509req; +//class pki_x509; +//class pki_key; +typedef class itemCombo itemComboTemp; +typedef class itemCombo itemComboReq; +typedef class itemCombo itemComboCert; +typedef class itemCombo itemComboKey; #endif diff --git a/widgets/NewX509.cpp b/widgets/NewX509.cpp index 945f0d78..c7a69bdc 100644 --- a/widgets/NewX509.cpp +++ b/widgets/NewX509.cpp @@ -81,7 +81,7 @@ NewX509::NewX509(QWidget *parent) fromReqCB->setChecked(false); } else { - reqList->insertPkiItems(requests); + reqList->insertPkiItems(requests); } on_fromReqCB_clicked(); @@ -90,7 +90,7 @@ NewX509::NewX509(QWidget *parent) if (issuers.isEmpty()) { foreignSignRB->setDisabled(true); } else { - certList->insertPkiItems(issuers); + certList->insertPkiItems(issuers); } // set dates to now and now + 1 year @@ -99,7 +99,7 @@ NewX509::NewX509(QWidget *parent) on_applyTime_clicked(); // settings for the templates .... - tempList->insertPkiItems(MainWindow::temps->getAllAndPredefs()); + tempList->insertPkiItems(MainWindow::temps->getAllAndPredefs()); // setup Extended keyusage foreach(int nid, eku_nid) @@ -655,7 +655,7 @@ void NewX509::switchHashAlgo() void NewX509::on_showReqBut_clicked() { - emit showReq(reqList->currentPkiItem()); + emit showReq(reqList->currentPkiItem()); } void NewX509::on_genKeyBut_clicked() @@ -712,7 +712,7 @@ pki_temp *NewX509::currentTemplate() { if (!tempList->isEnabled()) return NULL; - return tempList->currentPkiItem(); + return tempList->currentPkiItem(); } void NewX509::selfComment(QString msg) @@ -770,26 +770,26 @@ void NewX509::newKeyDone(pki_key *nkey) void NewX509::on_usedKeysToo_toggled(bool) { - pki_key *cur = keyList->currentPkiItem(); + pki_key *cur = keyList->currentPkiItem(); keyList->clear(); - keyList->insertPkiItems(usedKeysToo->isChecked() ? + keyList->insertPkiItems(usedKeysToo->isChecked() ? allKeys : unusedKeys); keyList->setCurrentPkiItem(cur); } pki_key *NewX509::getSelectedKey() { - return keyList->currentPkiItem(); + return keyList->currentPkiItem(); } pki_x509 *NewX509::getSelectedSigner() { - return certList->currentPkiItem(); + return certList->currentPkiItem(); } pki_x509req *NewX509::getSelectedReq() { - return reqList->currentPkiItem(); + return reqList->currentPkiItem(); } x509name NewX509::getX509name(int _throw)