file_db_container.cpp 2.67 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

24
#include <QDir>
25
#include <QFileInfo>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include "src/sqlite/file_db_container.h"

FileDbContainer::FileDbContainer(const QString &connectionName)
    : QMap<QString, FileDb *>(),
    m_connectionName(connectionName)
{
}

FileDbContainer::~FileDbContainer(void)
{
	QMap<QString, FileDb *>::iterator i;

	for (i = this->begin(); i != this->end(); ++i) {
		delete i.value();
	}
}

44
FileDb *FileDbContainer::accessFileDb(const QString &locDir,
45
    const QString &userName, bool storeLocally)
46
{
47
	FileDb *db = Q_NULLPTR;
48 49 50 51 52 53 54 55 56 57
	bool open_ret;

	QString dbFileName = constructDbFileName(userName);

	/* Already opened. */
	if (this->find(dbFileName) != this->end()) {
		return (*this)[dbFileName];
	}

	db = new(std::nothrow) FileDb(dbFileName);
58
	if (Q_NULLPTR == db) {
59
		Q_ASSERT(0);
60
		return Q_NULLPTR;
61 62
	}

63 64 65
	QString location = locDir + QDir::separator() +
	    QDir::toNativeSeparators(dbFileName);

66
	open_ret = db->openDb(location, storeLocally);
67 68
	if (!open_ret) {
		delete db;
69
		return Q_NULLPTR;
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
	}

	this->insert(dbFileName, db);

	return db;
}

bool FileDbContainer::deleteDb(FileDb *db)
{
	if (0 == db) {
		Q_ASSERT(0);
		return false;
	}

	/* Find entry. */
	QMap<QString, FileDb *>::iterator it = this->begin();
	while ((it != this->end()) && (it.value() != db)) {
		++it;
	}
	/* Must exist. */
	if (this->end() == it) {
		Q_ASSERT(0);
		return false;
	}

	/* Remove from container. */
	this->erase(it);

	/* Get file name. */
	QString fileName = db->fileName();

	/* Close database. */
	delete db;

104
	if (fileName == SQLiteDb::memoryLocation) {
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		return true;
	}

	/* Delete file. */
	if (!QFile::remove(fileName)) {
		return false;
	}

	return true;
}

QString FileDbContainer::constructDbFileName(const QString &userName)
{
	return userName + "_file.db";
}