Commit dbdfcda0 authored by Martin Straka's avatar Martin Straka

File system code refactoring

parent 39d28829
This diff is collapsed.
......@@ -39,33 +39,46 @@
*/
#define ILL_FNAME_REP "_"
QString getDefaultDbAndConfigLocation(void)
QString datovkaBaseFolderPath(void)
{
return existingWritableLocation(QStandardPaths::AppDataLocation);
return getDefaultAttachmentSaveLocation() + QDir::separator() +
QString(DATOVKA_BASE_DIR_NAME);
}
QString getDefaultAttachmentSaveLocation(void)
QString datovkaCertificateFolderPath(void)
{
return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
return setDatovkaPath(getDefaultAttachmentSaveLocation(),
DATOVKA_CERT_DIR_NAME);
}
QString setFilePath(const QString &basePath, const QString &destDir)
QString datovkaEmailFolderPath(const QString &msgIdStr)
{
QString newPath = basePath;
QString destPath(QString(DATOVKA_DIR_NAME) + QDir::separator()
+ destDir);
QDir dir(basePath);
if (dir.mkpath(destPath)) {
newPath = basePath + QDir::separator() + destPath;
dir.setPath(newPath);
}
return setDatovkaPath(getDefaultAttachmentSaveLocation(),
QString(DATOVKA_MAIL_DIR_NAME) + QDir::separator() + msgIdStr);
}
if (!dir.exists()) {
qCritical() << "Loaction for file could not be created.";
return QString();
QString datovkaMessageFolderPath(const QString &msgIdStr)
{
return setDatovkaPath(getDefaultAttachmentSaveLocation(), msgIdStr);
}
QString datovkaTmpFolderPath(void)
{
return setDatovkaPath(getDefaultAttachmentSaveLocation(),
DATOVKA_TEMP_DIR_NAME);
}
void deleteAllFromFolder(const QString &folderName)
{
QString folderPath =
setDatovkaPath(getDefaultAttachmentSaveLocation(), folderName);
if (folderPath.isEmpty()) {
return;
}
return newPath;
QDir dir(folderPath);
dir.removeRecursively();
}
QString existingWritableLocation(int type)
......@@ -99,6 +112,40 @@ QString existingWritableLocation(int type)
return dirName;
}
QString getDefaultAttachmentSaveLocation(void)
{
return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
}
QString getDefaultDbAndConfigLocation(void)
{
return existingWritableLocation(QStandardPaths::AppDataLocation);
}
QStringList getZfoFilesFromDir(const QString &importDir, bool includeSubDir)
{
QStringList filters("*.zfo");
QStringList fileList, filePathList;
QDir directory(QDir::home());
if (includeSubDir) {
QDirIterator it(importDir, filters, QDir::Files,
QDirIterator::Subdirectories);
while (it.hasNext()) {
filePathList.append(it.next());
}
} else {
directory.setPath(importDir);
fileList = directory.entryList(filters);
foreach (const QString &file, fileList) {
filePathList.append(importDir +
QDir::separator() + file);
}
}
return filePathList;
}
QString nonconflictingFileName(QString filePath)
{
if (filePath.isEmpty()) {
......@@ -126,6 +173,25 @@ QString nonconflictingFileName(QString filePath)
return filePath;
}
QString setDatovkaPath(const QString &basePath, const QString &folderName)
{
QString newPath = basePath;
QString destPath(QString(DATOVKA_BASE_DIR_NAME) + QDir::separator()
+ folderName);
QDir dir(basePath);
if (dir.mkpath(destPath)) {
newPath = basePath + QDir::separator() + destPath;
dir.setPath(newPath);
}
if (!dir.exists()) {
qCritical() << "Loaction for file could not be created.";
return QString();
}
return newPath;
}
QString writeFile(const QString &filePath, const QString &fileName,
const QByteArray &data, bool deleteOnError)
{
......@@ -155,63 +221,3 @@ QString writeFile(const QString &filePath, const QString &fileName,
return fullName;
}
QString writeFileToDir(const QString &dirName, qint64 msgId,
const QString &fileName, const QByteArray &data, bool deleteOnError)
{
if (fileName.isEmpty() || data.isEmpty()) {
return QString();
}
QString dir = dirName;
if (dirName.isEmpty()) {
dir = QString::number(msgId);
} else if (EMAIL_DIR_NAME == dir) {
dir = QString(EMAIL_DIR_NAME) + QDir::separator()
+ QString::number(msgId);
} else if (TEMP_DIR_NAME == dir) {
deleteFilesFromDir(dir);
}
QString filePath = setFilePath(getDefaultAttachmentSaveLocation(), dir);
return writeFile(filePath, fileName, data, deleteOnError);
}
void deleteFilesFromDir(const QString &dirName)
{
QString filePath =
setFilePath(getDefaultAttachmentSaveLocation(), dirName);
if (filePath.isEmpty()) {
return;
}
QDir dir(filePath);
dir.removeRecursively();
}
QStringList getZfoFilesFromDir(const QString &importDir, bool includeSubDir)
{
QStringList filters("*.zfo");
QStringList fileList, filePathList;
QDir directory(QDir::home());
if (includeSubDir) {
QDirIterator it(importDir, filters, QDir::Files,
QDirIterator::Subdirectories);
while (it.hasNext()) {
filePathList.append(it.next());
}
} else {
directory.setPath(importDir);
fileList = directory.entryList(filters);
foreach (const QString &file, fileList) {
filePathList.append(importDir +
QDir::separator() + file);
}
}
return filePathList;
}
......@@ -21,87 +21,84 @@
* the two.
*/
#ifndef _FILESYSTEM_H_
#define _FILESYSTEM_H_
#pragma once
#include <QByteArray>
#include <QString>
#define DATOVKA_DIR_NAME "Datovka"
#define EMAIL_DIR_NAME "Email"
#define TEMP_DIR_NAME "Temp"
/*!
* @brief Datovka file store folder names.
*/
#define DATOVKA_BASE_DIR_NAME "Datovka"
#define DATOVKA_CERT_DIR_NAME "Certs"
#define DATOVKA_MAIL_DIR_NAME "Email"
#define DATOVKA_TEMP_DIR_NAME "Temp"
/*!
* @brief Return default location for database files and datovka.conf.
* @brief Return base Datovka folder path.
*
* @return Location path or empty string on error.
* @return Full path to location where Datovka can store attachments.
*/
QString getDefaultDbAndConfigLocation(void);
QString datovkaBaseFolderPath(void);
/*!
* @brief Return path for attachment saving.
* @brief Return path to Datovka certificate folder.
*
* @return Path to location where attachment can be stored.
* @return Full path to location where certificates can be stored.
*/
QString getDefaultAttachmentSaveLocation(void);
QString datovkaCertificateFolderPath(void);
/*!
* @brief Set file destination path and dir.
* @brief Return path to Datovka email folder.
*
* @param[in] basePath Base path based on the platform.
* @param[in] destDir Destination dir name.
* @return Full path to file.
* @param[in] msgIdStr Message ID string.
* @return Full path to location where files for email can be stored.
*/
QString setFilePath(const QString &basePath, const QString &destDir);
QString datovkaEmailFolderPath(const QString &msgIdStr);
/*!
* @brief Returns name of existing directory to store data to.
* @brief Return path for attachment saving.
*
* @param[in] type Location type, must match supported QStandardPaths::StandardLocation.
* @return Directory name of existing directory or empty string on error.
* @param[in] msgIdStr Message ID string.
* @return Full path to location where attachment files can be stored.
*/
QString existingWritableLocation(int type);
QString datovkaMessageFolderPath(const QString &msgIdStr);
/*!
* @brief Check if file with given path exists.
* @brief Return path for Datovka tmp folder.
*
* @param[in] filePath Full file path.
* @return Full path with non-conflicting file if file already exists.
* @return Full path to location where apliaction temporary files can be stored.
*/
QString nonconflictingFileName(QString filePath);
QString datovkaTmpFolderPath(void);
/*!
* @brief Create file and write data to it.
* @brief Delete all files and subdirs from specific folder.
*
* @param[in] filePath File path.
* @param[in] fileName File name.
* @param[in] data Data to be written into file.
* @param[in] deleteOnError Delete created file when cannot be entirely written.
* @return Full path to written file on success, empty string on failure.
* @param[in] folderName Folder name.
*/
QString writeFile(const QString &filePath, const QString &fileName,
const QByteArray &data, bool deleteOnError = false);
void deleteAllFromFolder(const QString &folderName);
/*!
* @brief Create and write data to specific dir.
* @brief Returns name of existing directory to store data to.
*
* @param[in] dirName Directory name.
* @param[in] msgId Message ID.
* @param[in] fileName File name.
* @param[in] data Data to be written into file.
* @param[in] deleteOnError Delete created file when cannot be entirely written.
* @return Full path to written file on success, empty string on failure.
* @param[in] type Location type, must match supported QStandardPaths::StandardLocation.
* @return Directory name of existing directory or empty string on error.
*/
QString existingWritableLocation(int type);
/*!
* @brief Return path for attachment saving.
*
* @return Path to location where attachment can be stored.
*/
QString writeFileToDir(const QString &dirName, qint64 msgId,
const QString &fileName, const QByteArray &data,
bool deleteOnError = false);
QString getDefaultAttachmentSaveLocation(void);
/*!
* @brief Delete all files and subdirs of specific destination dir.
* @brief Return default location for database files and datovka.conf.
*
* @param[in] dirName Directory dir.
* @return Location path or empty string on error.
*/
void deleteFilesFromDir(const QString &dirName);
QString getDefaultDbAndConfigLocation(void);
/*!
* @brief Get paths of ZFO files from selected folder.
......@@ -112,4 +109,31 @@ void deleteFilesFromDir(const QString &dirName);
*/
QStringList getZfoFilesFromDir(const QString &importDir, bool includeSubDir);
#endif /* _FILESYSTEM_H_ */
/*!
* @brief Check if file with given path exists.
*
* @param[in] filePath Full file path.
* @return Full path with non-conflicting file if file already exists.
*/
QString nonconflictingFileName(QString filePath);
/*!
* @brief Set file destination path and dir.
*
* @param[in] basePath Base path based on the platform.
* @param[in] folderName Target folder name.
* @return Full path to new location.
*/
QString setDatovkaPath(const QString &basePath, const QString &folderName);
/*!
* @brief Create file and write data to it.
*
* @param[in] filePath File path.
* @param[in] fileName File name.
* @param[in] data Data to be written into file.
* @param[in] deleteOnError Delete created file when cannot be entirely written.
* @return Full path to written file on success, empty string on failure.
*/
QString writeFile(const QString &filePath, const QString &fileName,
const QByteArray &data, bool deleteOnError = false);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment