File: db_temp.cpp

package info (click to toggle)
xca 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,328 kB
  • sloc: cpp: 30,584; sh: 341; xml: 74; makefile: 56; python: 34
file content (118 lines) | stat: -rw-r--r-- 2,574 bytes parent folder | download
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
/* vi: set sw=4 ts=4:
 *
 * Copyright (C) 2001 - 2014 Christian Hohnstaedt.
 *
 * All rights reserved.
 */


#include "db_temp.h"
#include "pki_temp.h"
#include "load_obj.h"
#include "func.h"
#include "XcaWarningCore.h"
#include <QDir>
#include <QFileInfo>

db_temp::db_temp() : db_x509name("templates")
{
	/* XCA loads templates from private space ($HOME/.local/)
	 * Host specific (/usr/local) and distribution (/usr)
	 * The first <name>.xca found avoids other <name>.xca to be loaded
	 */
	QSet<QString> template_files;
	load_temp l;

	sqlHashTable = "templates";
	pkitype << tmpl;

	updateHeaders();
	loadContainer();

	pki_temp *tmpl = new pki_temp(tr("Empty template"));
	tmpl->setAsPreDefined();
	predefs << tmpl;

	QStringList dirs = QStandardPaths::standardLocations(
	                     QStandardPaths::AppDataLocation);
#ifdef INSTALL_DATA_PREFIX
	dirs << QString(INSTALL_DATA_PREFIX);
#endif
	foreach(QString d, dirs)
	{
		qDebug() << "Looking for templates at" << d;
		QFileInfoList list = QDir(d).entryInfoList(
				QStringList("*.xca"),
				QDir::Files | QDir::NoSymLinks |
                                QDir::NoDot | QDir::Readable);

		foreach(QFileInfo fileInfo, list) {
			if (template_files.contains(fileInfo.fileName()))
				continue;

			qDebug() << "Loading template" << fileInfo.fileName()
					<< fileInfo.absoluteFilePath();
			try {
				tmpl = dynamic_cast<pki_temp*>(l.loadItem(
						fileInfo.absoluteFilePath()));
				if (tmpl) {
					tmpl->setAsPreDefined();
					predefs << tmpl;
					template_files << fileInfo.fileName();
				}
			} catch(errorEx &) {
				XCA_WARN(tr("Bad template: %1")
					.arg(nativeSeparator(
						fileInfo.absoluteFilePath())));
			}
		}
	}
}

db_temp::~db_temp()
{
	qDeleteAll(predefs);
}

pki_base *db_temp::newPKI(enum pki_type type)
{
	(void)type;
	return new pki_temp("");
}

QList<pki_temp *> db_temp::getPredefs() const
{
	return predefs;
}

bool db_temp::alterTemp(pki_temp *temp)
{
	XSqlQuery q;
	QSqlError e;

	Transaction;
	if (!TransBegin())
		return false;
	SQL_PREPARE(q, "UPDATE templates SET version=?, template=? WHERE item=?");
	q.bindValue(0, TMPL_VERSION);
	q.bindValue(1, temp->toB64Data());
	q.bindValue(2, temp->getSqlItemId());
	q.exec();
	e = q.lastError();
	XCA_SQLERROR(e);
	if (e.isValid()) {
		TransRollback();
		return false;
	}
	updateItem(temp);
	TransCommit();
	return true;
}

void db_temp::exportItem(const QModelIndex &index,
                        const pki_export *, XFile &file) const
{
	pki_temp *temp = fromIndex<pki_temp>(index);
	if (temp)
		temp->writeTemp(file);
}