1 /* vi: set sw=4 ts=4:
2 *
3 * Copyright (C) 2001 - 2014 Christian Hohnstaedt.
4 *
5 * All rights reserved.
6 */
7
8
9 #include "db_temp.h"
10 #include "func.h"
11 #include "widgets/XcaWarning.h"
12 #include "widgets/NewX509.h"
13 #include <QFileDialog>
14 #include <QDir>
15 #include <QContextMenuEvent>
16 #include <QAction>
17 #include <QInputDialog>
18 #include <QFileInfo>
19
db_temp()20 db_temp::db_temp() : db_x509name("templates")
21 {
22 sqlHashTable = "templates";
23 pkitype << tmpl;
24
25 updateHeaders();
26 loadContainer();
27
28 QDir dir;
29 if (!dir.cd(getPrefix()))
30 return;
31 dir.setFilter(QDir::Files | QDir::NoSymLinks);
32 QFileInfoList list = dir.entryInfoList();
33 load_temp l;
34 pki_temp *tmpl = new pki_temp(tr("Empty template"));
35 tmpl->setAsPreDefined();
36 predefs << tmpl;
37
38 for (int i = 0; i < list.size(); ++i) {
39 QFileInfo fileInfo = list.at(i);
40 QString name = getPrefix() + "/" + fileInfo.fileName();
41 if (!name.endsWith(".xca", Qt::CaseInsensitive))
42 continue;
43 try {
44 tmpl = dynamic_cast<pki_temp*>(l.loadItem(name));
45 if (tmpl) {
46 tmpl->setAsPreDefined();
47 predefs << tmpl;
48 }
49 } catch(errorEx &err) {
50 XCA_WARN(tr("Bad template: %1")
51 .arg(nativeSeparator(name)));
52 }
53 }
54 }
55
~db_temp()56 db_temp::~db_temp()
57 {
58 qDeleteAll(predefs);
59 }
60
newPKI(enum pki_type type)61 pki_base *db_temp::newPKI(enum pki_type type)
62 {
63 (void)type;
64 return new pki_temp("");
65 }
66
getPredefs() const67 QList<pki_temp *> db_temp::getPredefs() const
68 {
69 return predefs;
70 }
71
alterTemp(pki_temp * temp)72 bool db_temp::alterTemp(pki_temp *temp)
73 {
74 XSqlQuery q;
75 QSqlError e;
76
77 Transaction;
78 if (!TransBegin())
79 return false;
80 SQL_PREPARE(q, "UPDATE templates SET version=?, template=? WHERE item=?");
81 q.bindValue(0, TMPL_VERSION);
82 q.bindValue(1, temp->toB64Data());
83 q.bindValue(2, temp->getSqlItemId());
84 q.exec();
85 e = q.lastError();
86 XCA_SQLERROR(e);
87 if (e.isValid()) {
88 TransRollback();
89 return false;
90 }
91 updateItem(temp, temp->getIntName(), temp->getComment());
92 TransCommit();
93 return true;
94 }
95
load()96 void db_temp::load()
97 {
98 load_temp l;
99 load_default(l);
100 }
101
store(QModelIndex index)102 void db_temp::store(QModelIndex index)
103 {
104 pki_temp *temp = fromIndex<pki_temp>(index);
105
106 if (!index.isValid() || !temp)
107 return;
108
109 QString fn = Settings["workingdir"] +
110 temp->getUnderlinedName() + ".xca";
111 QString s = QFileDialog::getSaveFileName(NULL,
112 tr("Save template as"), fn,
113 tr("XCA templates ( *.xca );; All files ( * )"));
114 if (s.isEmpty())
115 return;
116
117 update_workingdir(s);
118 try {
119 XFile file(s);
120 file.open_key();
121 temp->writeTemp(file);
122 }
123 catch (errorEx &err) {
124 XCA_ERROR(err);
125 }
126 }
127