file_db.h 3.88 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2018 CZ.NIC
3 4 5 6 7 8 9 10
 *
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14 15 16 17 18 19 20 21 22 23
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * 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
#pragma once
25 26 27 28

#include <QList>
#include <QString>

29
#include "src/datovka_shared/io/sqlite/db.h"
30
#include "src/datovka_shared/isds/message_interface.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "src/files.h"
#include "src/messages.h"
#include "src/models/filemodel.h"

/*!
 * @brief Encapsulates message database.
 */
class FileDb : public SQLiteDb {

public:

	explicit FileDb(const QString &connectionName);

	/*!
45
	 * @brief Delete files form db witch are older than date.
46
	 *
47 48
	 * @param[in] days How many days.
	 * @return List of message IDs where files were deleted.
49
	 */
50
	QList<qint64> cleanFilesInDb(int days);
51

52
	/*!
53
	 * @brief Copy db.
54
	 *
55
	 * @param[in] newFileName New file path.
56
	 * @return True on success.
57 58
	 *
	 * @note The copy is continued to be used. Original is closed.
59
	 */
60
	bool copyDb(const QString &newFileName);
61

62
	/*!
63
	 * @brief Open a new empty database file.
64
	 *
65
	 * @param[in] newFileName New file path.
66 67
	 * @return True on success.
	 */
68
	bool reopenDb(const QString &newFileName);
69

70 71 72 73 74 75 76 77
	/*!
	 * @brief Move db.
	 *
	 * @param[in] newFileName New file path.
	 * @return True on success.
	 */
	bool moveDb(const QString &newFileName);

78
	/*!
79
	 * @brief Delete all files related to message with given id.
80
	 *
81
	 * @param[in] dmId Message Id.
82 83
	 * @return True on success.
	 */
84
	bool deleteFilesFromDb(qint64 dmId);
85 86

	/*!
87
	 * @brief Get file database size in bytes.
88
	 *
89
	 * @return True size in bytes.
90
	 */
91
	int getDbSizeInBytes(void) const;
92 93

	/*!
94
	 * @brief Get file content from db.
95
	 *
96
	 * @param[in] fileId File Id.
97
	 * @return File data.
98
	 */
99
	Isds::Document getFileFromDb(int fileId) const;
100 101

	/*!
102
	 * @brief Get list of files from db.
103
	 *
104
	 * @param[in] dmId Message Id.
105
	 * @return List of files.
106
	 */
107
	QList<Isds::Document> getFilesFromDb(qint64 dmId) const;
108 109

	/*!
110
	 * @brief Set attachment model from db - for QML listview.
111
	 *
112 113
	 * @param[in,out] model Model to be set.
	 * @param[in] dmId Message Id.
114
	 */
115
	void setFileModelFromDb(FileListModel &model, qint64 dmId) const;
116 117

	/*!
118
	 * @brief Insert or update file data into files table.
119
	 *
120 121
	 * @param[in] dmId Message Id.
	 * @param[in] document Struct holds file data.
122
	 * @return True on success.
123
	 */
124
	bool insertUpdateFileIntoDb(qint64 dmId, const Isds::Document &document);
125 126

	/*!
127
	 * @brief Open database file.
128
	 *
129 130 131
	 * @param[in] fileName File name.
	 * @param[in] dbInMemory Whether to create db in memory.
	 * @return True on success.
132
	 */
133
	bool openDb(const QString &fileName, bool storeToDisk);
134

135
	/*!
Karel Slaný's avatar
Karel Slaný committed
136
	 * @brief Vacuum file database.
137 138 139
	 *
	 * @return True on success.
	 */
Karel Slaný's avatar
Karel Slaný committed
140
	bool vacuumFileDb(void);
141

142
	/*!
143
	 * @brief Search messages with attachment names matching supplied phrase.
144 145
	 *
	 * @param[in] phrase Search phrase.
146
	 * @return List of message IDs whose attachment names contain the phrase.
147
	 */
148
	QList<qint64> searchAttachmentName(const QString &phrase) const;
149

150
protected:
151 152 153 154 155
	/*!
	 * @brief Returns list of tables.
	 *
	 * @return List of pointers to tables.
	 */
156 157
	virtual
	QList<class SQLiteTbl *> listOfTables(void) const Q_DECL_OVERRIDE;
158
};