Commit e869c21b authored by Martin Straka's avatar Martin Straka

Removed unnecessary and duplicate code in filesystem

parent deae8ec5
......@@ -361,7 +361,7 @@ Component {
if (!fromLocalDb) {
fileContent = files.base64ToRaw(rFileContent)
} else {
fileContent = files.getFileRawContentFromDb(userName, zfoId, rFileId)
fileContent = files.getFileRawContentFromDb(userName, rFileId)
}
pageView.push(pageMessageDetail, {
"pageView": pageView,
......@@ -373,7 +373,7 @@ Component {
if (!fromLocalDb) {
files.openAttachment(rFileName, rFileContent)
} else {
files.openAttachmentFromDb(userName, zfoId, rFileId)
files.openAttachmentFromDb(userName, rFileId)
}
}
}
......
......@@ -453,7 +453,7 @@ Item {
"pageView": pageView,
"statusBar": statusBar,
"fromLocalDb": false,
"rawZfoContent": files.getFileRawContentFromDb(userName, msgId, rFileId)})
"rawZfoContent": files.getFileRawContentFromDb(userName, rFileId)})
} else {
files.openAttachmentFromDb(userName, msgId, rFileId)
}
......
......@@ -68,11 +68,6 @@ void Files::attachmentSavingNotification(const QString &destPath)
QObject::tr("Path: '%1'").arg(fi.absolutePath()));
}
QString Files::fileSaveLocation(void)
{
return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
}
void Files::deleteExpiredFilesFromDbs(int days)
{
qDebug("%s()", __func__);
......@@ -121,18 +116,10 @@ qint64 Files::getAttachmentSizeInBytes(const QString &filePath)
return fileInfo.size();
}
QByteArray Files::getFileRawContentFromDb(const QString &userName,
const QString &msgIdStr, int fileId)
QByteArray Files::getFileRawContentFromDb(const QString &userName, int fileId)
{
qDebug("%s()", __func__);
/* Obtain message identifier. */
bool ok = false;
qint64 msgId = msgIdStr.toLongLong(&ok);
if (!ok || (msgId < 0)) {
return QByteArray();
}
FileDb *fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
......@@ -146,18 +133,10 @@ QByteArray Files::getFileRawContentFromDb(const QString &userName,
return base64ToRaw(file.content.toUtf8());
}
void Files::openAttachmentFromDb(const QString &userName,
const QString &msgIdStr, int fileId)
void Files::openAttachmentFromDb(const QString &userName, int fileId)
{
qDebug("%s()", __func__);
/* Obtain message identifier. */
bool ok = false;
qint64 msgId = msgIdStr.toLongLong(&ok);
if (!ok || (msgId < 0)) {
return;
}
FileDb *fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
......@@ -189,7 +168,7 @@ void Files::openAttachment(const QString &fileName, const QByteArray &base64Data
return;
}
QString filePath(writeFileToTmpDir(fileName, TEMP_DIR_NAME,
QString filePath(writeFileToDir(TEMP_DIR_NAME, -1, fileName,
base64ToRaw(base64Data)));
if (!filePath.isEmpty()) {
......@@ -284,7 +263,7 @@ void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
qCritical() << "ERROR: File name is empty!";
return;
}
fileName = writeFileToEmailDir(fileName, msgId,
fileName = writeFileToDir(EMAIL_DIR_NAME, msgId, fileName,
base64ToRaw(file.content.toUtf8()));
fileList.append(fileName);
addAttachmentToEmailMessage(emailMessage, file.filename,
......@@ -498,8 +477,8 @@ void Files::sendAttachmentEmailZfo(const QVariant &attachModelVariant,
FileListModel::ROLE_FILE_DATA).toByteArray());
QString attachName(attachModel->data(idx,
FileListModel::ROLE_FILE_NAME).toString());
QString filePath(writeFileToEmailDir(attachName, msgId,
base64ToRaw(base64Data)));
QString filePath(writeFileToDir(EMAIL_DIR_NAME, msgId,
attachName, base64ToRaw(base64Data)));
fileList.append(filePath);
addAttachmentToEmailMessage(emailMessage, attachName,
base64Data, boundary);
......@@ -544,8 +523,8 @@ void Files::saveAttachmentsToDiskDb(const QString &userName,
QString destPath;
foreach (const FileDb::FileData &file, filelist) {
destPath = writeFileToDir(fileSaveLocation(), file.filename,
msgId, base64ToRaw(file.content.toUtf8()));
destPath = writeFileToDir(QString(), msgId, file.filename,
base64ToRaw(file.content.toUtf8()));
}
attachmentSavingNotification(destPath);
......@@ -574,10 +553,9 @@ void Files::saveAttachmentsToDiskZfo(const QVariant &attachModelVariant,
QString destPath;
for (int row = 0; row < attachModel->rowCount(); ++row) {
QModelIndex idx(attachModel->index(row));
destPath = writeFileToDir(fileSaveLocation(),
destPath = writeFileToDir(QString(), msgId,
attachModel->data(idx,
FileListModel::ROLE_FILE_NAME).toString(),
msgId,
base64ToRaw(attachModel->data(idx,
FileListModel::ROLE_FILE_DATA).toByteArray()));
}
......@@ -615,8 +593,8 @@ void Files::sendEmail(const QString &emailMessage, const QStringList &fileList,
QDesktopServices::openUrl(QUrl("mailto:?subject=" +
subject + "&body=" + body));
#else
QString tmpEmailFile = writeFileToEmailDir(QString::number(msgId)
+ "_mail.eml", msgId, emailMessage.toUtf8());
QString tmpEmailFile = writeFileToDir(EMAIL_DIR_NAME, msgId,
QString::number(msgId) + "_mail.eml", emailMessage.toUtf8());
QDesktopServices::openUrl(QUrl::fromLocalFile(tmpEmailFile));
#endif
/* TODO -- Handle openUrl() return value. */
......
......@@ -67,14 +67,6 @@ public:
static
void attachmentSavingNotification(const QString &destPath);
/*!
* @brief Obtain path for storing attachments.
*
* @return Path to location where attachment can be stored.
*/
static
QString fileSaveLocation(void);
/*!
* @brief Delete files from all databases where file lifetime expired.
*
......@@ -104,24 +96,20 @@ public:
* @brief Obtain attachment from database.
*
* @param[in] userName User name identifying the account.
* @param[in] msgIdStr String with message id.
* @param[in] fileId Attachment file identifier.
* @return Attachment content.
*/
Q_INVOKABLE static
QByteArray getFileRawContentFromDb(const QString &userName,
const QString &msgIdStr, int fileId);
QByteArray getFileRawContentFromDb(const QString &userName, int fileId);
/*!
* @brief Open attachment from database.
*
* @param[in] userName User name identifying the account.
* @param[in] msgIdStr String with message id.
* @param[in] fileId Attachment file identifier.
*/
Q_INVOKABLE static
void openAttachmentFromDb(const QString &userName,
const QString &msgIdStr, int fileId);
void openAttachmentFromDb(const QString &userName, int fileId);
/*!
* @brief Open attachment in default application.
......
/*
* Copyright (C) 2014-2015 CZ.NIC
* Copyright (C) 2014-2017 CZ.NIC
*
* 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
......@@ -44,34 +44,21 @@
*/
#define ILL_FNAME_REP "_"
QString getBasePathBasedOnPlatform(void)
{
#if defined Q_OS_WINRT
QString dirPath(
QStandardPaths::writableLocation(QStandardPaths::DataLocation));
#elif defined Q_OS_ANDROID
QString dirPath(
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
#else
QString dirPath(
QStandardPaths::writableLocation(QStandardPaths::TempLocation));
#endif
return dirPath;
}
QString getDefaultDbAndConfigLocation(void)
{
return existingWritableLocation(QStandardPaths::AppDataLocation);
}
QString getDefaultAttachmentSaveLocation(void)
{
return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
}
QString setFilePath(const QString &basePath, const QString &destDir)
{
QString newPath = basePath;
QString destPath(QString(DATOVKA_DIR_NAME) + QDir::separator() + destDir);
QString destPath(QString(DATOVKA_DIR_NAME) + QDir::separator()
+ destDir);
QDir dir(basePath);
if (dir.mkpath(destPath)) {
newPath = basePath + QDir::separator() + destPath;
......@@ -86,7 +73,6 @@ QString setFilePath(const QString &basePath, const QString &destDir)
return newPath;
}
QString existingWritableLocation(int type)
{
enum QStandardPaths::StandardLocation locationType;
......@@ -145,7 +131,6 @@ QString nonconflictingFileName(QString filePath)
return filePath;
}
QString writeFile(const QString &filePath, const QString &fileName,
const QByteArray &data, bool deleteOnError)
{
......@@ -153,7 +138,8 @@ QString writeFile(const QString &filePath, const QString &fileName,
nameCopy.replace(QRegExp("[" + QRegExp::escape(ILL_FNAME_CH) + "]"),
ILL_FNAME_REP);
QFile fout(nonconflictingFileName(filePath + QDir::separator() + nameCopy));
QFile fout(nonconflictingFileName(filePath + QDir::separator()
+ nameCopy));
if (!fout.open(QIODevice::WriteOnly)) {
qDebug() << "Cant open file.";
return QString();
......@@ -175,132 +161,34 @@ QString writeFile(const QString &filePath, const QString &fileName,
return fullName;
}
QString writeTemporaryFile(const QString &fileName, const QByteArray &data,
bool deleteOnError)
QString writeFileToDir(const QString &dirName, qint64 msgId,
const QString &fileName, const QByteArray &data, bool deleteOnError)
{
if (fileName.isEmpty()) {
Q_ASSERT(0);
return QString();
}
QString directoryName = getBasePathBasedOnPlatform();
if (directoryName.isEmpty()) {
qDebug() << "No location to write temporary file to.";
return QString();
}
QDir dir(directoryName);
if (!dir.exists()) {
dir.mkpath(".");
}
if (!dir.exists()) {
qDebug() << "Loaction for temporary files could not be created.";
if (fileName.isEmpty() || data.isEmpty()) {
return QString();
}
QString nameCopy(fileName);
nameCopy.replace(QRegExp("[" + QRegExp::escape(ILL_FNAME_CH) + "]"),
ILL_FNAME_REP);
QTemporaryFile fout(directoryName + QDir::separator() + nameCopy);
if (!fout.open()) {
qDebug() << "Cant open file";
return QString();
}
fout.setAutoRemove(false);
/* Get whole path. */
QString fullName = fout.fileName();
int written = fout.write(data);
bool flushed = fout.flush();
fout.close();
QString dir = dirName;
if ((written != data.size()) || !flushed) {
if (deleteOnError) {
QFile::remove(fullName);
}
return QString();
}
return fullName;
}
QString writeFileToDir(const QString &basePath, const QString &fileName,
qint64 msgId, const QByteArray &data, bool deleteOnError)
{
if (fileName.isEmpty() || basePath.isEmpty()) {
return QString();
}
QString filePath = setFilePath(basePath, QString::number(msgId));
return writeFile(filePath, fileName, data, deleteOnError);
}
QString writeFileToTmpDir(const QString &fileName, const QString &destDir,
const QByteArray &data, bool deleteOnError)
{
if (fileName.isEmpty()) {
Q_ASSERT(0);
return QString();
}
QString basePath = getBasePathBasedOnPlatform();
if (basePath.isEmpty()) {
qDebug() << "No location to write file to.";
return QString();
}
deleteFilesFromDir(destDir);
QString filePath = setFilePath(basePath, destDir);
if (filePath.isEmpty()) {
qDebug() << "No location to write file to.";
return QString();
}
return writeFile(filePath, fileName, data, deleteOnError);
}
QString writeFileToEmailDir(const QString &fileName, qint64 msgId,
const QByteArray &data, bool deleteOnError)
{
if (fileName.isEmpty()) {
Q_ASSERT(0);
return QString();
}
QString basePath = getBasePathBasedOnPlatform();
if (basePath.isEmpty()) {
qDebug() << "No location to write file to.";
return QString();
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(basePath,
QString(EMAIL_DIR_NAME) + QDir::separator() + QString::number(msgId));
if (filePath.isEmpty()) {
qDebug() << "No location to write file to.";
return QString();
}
QString filePath = setFilePath(getDefaultAttachmentSaveLocation(), dir);
return writeFile(filePath, fileName, data, deleteOnError);
}
void deleteFilesFromDir(const QString &destDir)
void deleteFilesFromDir(const QString &dirName)
{
QString basePath = getBasePathBasedOnPlatform();
if (basePath.isEmpty()) {
return;
}
QString filePath =
setFilePath(getDefaultAttachmentSaveLocation(), dirName);
QString filePath = setFilePath(basePath, destDir);
if (filePath.isEmpty()) {
return;
}
......
/*
* Copyright (C) 2014-2015 CZ.NIC
* Copyright (C) 2014-2017 CZ.NIC
*
* 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
......@@ -32,129 +32,76 @@
#define EMAIL_DIR_NAME "Email"
#define TEMP_DIR_NAME "Temp"
/*!
* @brief Returns base path to store data based on platform.
* @brief Return default location for database files and datovka.conf.
*
* @return Directory path or empty string on error.
* @return Location path or empty string on error.
*/
QString getBasePathBasedOnPlatform(void);
QString getDefaultDbAndConfigLocation(void);
/*!
* @brief Returns default location for database files and datovka.conf.
* @brief Return path for attachment saving.
*
* @return Location path or empty string on error.
* @return Path to location where attachment can be stored.
*/
QString getDefaultDbAndConfigLocation(void);
QString getDefaultAttachmentSaveLocation(void);
/*!
* @brief Set file destination path and dir.
*
* @param[in] basePath Base path based on the platform.
* @param[in] destDir Destination dir name.
*
* @param[in] basePath Base path based on the platform.
* @param[in] destDir Destination dir name.
* @return Full path to file.
*/
QString setFilePath(const QString &basePath, const QString &destDir);
/*!
* @brief Returns name of existing directory to store data to.
*
* @param[in] type Location type,
* must match supported QStandardPaths::StandardLocation.
* @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 Check if file with given path exists.
*
* @param[in] filePath Full file path.
* @return Full path with non-conflicting file if file already exists,
* null string on error.
* @return Full path with non-conflicting file if file already exists.
*/
QString nonconflictingFileName(QString filePath);
/*!
* @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.
* @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);
/*!
* @brief Create and write data to temporary file.
*
* @param[in] fileName File name.
* @param[in] data Data to be written into file.
* @param[in] deleteOnError Delete created file when it cannot be entirely
* written.
* @return Full path to written file on success, empty string on failure.
*/
QString writeTemporaryFile(const QString &fileName,
const QByteArray &data, bool deleteOnError = false);
/*!
* @brief Create and write data to specific dir.
*
* @param[in] basePath Path to target folder.
* @param[in] fileName File name.
* @param[in] msgId Message ID.
* @param[in] data Data to be written into file.
* @param[in] deleteOnError Delete created file when it cannot be entirely
* written.
* @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.
*/
QString writeFileToDir(const QString &basePath, const QString &fileName,
qint64 msgId, const QByteArray &data, bool deleteOnError = false);
QString writeFileToDir(const QString &dirName, qint64 msgId,
const QString &fileName, const QByteArray &data,
bool deleteOnError = false);
/*!
* @brief Create and write data to datovka tmp dir.
* @brief Delete all files and subdirs of specific destination dir.
*
* @param[in] fileName File name.
* @param[in] destDir Destination dir.
* @param[in] data Data to be written into file.
* @param[in] deleteOnError Delete created file when it cannot be entirely
* written.
* @return Full path to written file on success, empty string on failure.
* @param[in] dirName Directory dir.
*/
QString writeFileToTmpDir(const QString &fileName, const QString &destDir,
const QByteArray &data, bool deleteOnError = false);
/*!
* @brief Create and write data to datovka tmp dir.
*
* @param[in] fileName File name.
* @param[in] msgId Name of tmp dir.
* @param[in] data Data to be written into file.
* @param[in] deleteOnError Delete created file when it cannot be entirely
* written.
* @return Full path to written file on success, empty string on failure.
*/
QString writeFileToEmailDir(const QString &fileName, qint64 msgId,
const QByteArray &data, bool deleteOnError = false);
/*!
* @brief Delete all content of specific destination dir.
*
* @param[in] destDir Destination dir.
*/
void deleteFilesFromDir(const QString &destDir);
void deleteFilesFromDir(const QString &dirName);
#endif /* _FILESYSTEM_H_ */
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