Commit e8d4e6bd authored by Martin Straka's avatar Martin Straka

Files refactoring: removed some repeat code

parent c6f22482
......@@ -361,7 +361,7 @@ Component {
if (!fromLocalDb) {
fileContent = files.base64ToRaw(rFileContent)
} else {
fileContent = files.getAttachmentDb(userName, zfoId, rFileId)
fileContent = files.getFileRawContentFromDb(userName, zfoId, rFileId)
}
pageView.push(pageMessageDetail, {
"pageView": pageView,
......
......@@ -453,7 +453,7 @@ Item {
"pageView": pageView,
"statusBar": statusBar,
"fromLocalDb": false,
"rawZfoContent": files.getAttachmentDb(userName, msgId, rFileId)})
"rawZfoContent": files.getFileRawContentFromDb(userName, msgId, rFileId)})
} else {
files.openAttachmentFromDb(userName, msgId, rFileId)
}
......
......@@ -47,36 +47,6 @@
#include "src/crypto/crypto.h"
#endif
/*!
* @brief Obtain attachment from database.
*
* @param[out] file File data to be acquired.
* @param[in] userName User name identifying the account.
* @param[in] msgIdStr String with message id.
* @param[in] fileId Attachment file identifier.
*/
static
void getAttchment(FileDb::FileData &file, const QString &userName,
qint64 msgId, int fileId)
{
file.filename = QString();
file.content = QString();
if (userName.isEmpty() || (msgId <= 0) || (fileId < 0)) {
return;
}
FileDb *fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
return;
}
file = fDb->getFileContentFromDb(fileId);
}
Files::Files(QObject *parent) : QObject(parent)
{
}
......@@ -150,7 +120,7 @@ qint64 Files::getAttachmentSizeInBytes(const QString &filePath)
return fileInfo.size();
}
QByteArray Files::getAttachmentDb(const QString &userName,
QByteArray Files::getFileRawContentFromDb(const QString &userName,
const QString &msgIdStr, int fileId)
{
qDebug("%s()", __func__);
......@@ -162,10 +132,17 @@ QByteArray Files::getAttachmentDb(const QString &userName,
return QByteArray();
}
FileDb::FileData file;
getAttchment(file, userName, msgId, fileId);
FileDb *fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
return QByteArray::fromBase64(file.content.toUtf8());
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
return QByteArray();
}
FileDb::FileData file = fDb->getFileContentFromDb(fileId);
return base64ToRaw(file.content.toUtf8());
}
void Files::openAttachmentFromDb(const QString &userName,
......@@ -180,15 +157,16 @@ void Files::openAttachmentFromDb(const QString &userName,
return;
}
FileDb::FileData file;
getAttchment(file, userName, msgId, fileId);
FileDb *fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
Q_ASSERT(!file.filename.isEmpty());
if (file.filename.isEmpty()) {
qDebug() << "ERROR: File name is empty!";
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
return;
}
FileDb::FileData file = fDb->getFileContentFromDb(fileId);
openAttachment(file.filename, file.content.toUtf8());
}
......@@ -210,8 +188,8 @@ void Files::openAttachment(const QString &fileName, const QByteArray &base64Data
return;
}
QByteArray rawData(QByteArray::fromBase64(base64Data));
QString filePath(writeFileToTmpDir(fileName, TEMP_DIR_NAME, rawData));
QString filePath(writeFileToTmpDir(fileName, TEMP_DIR_NAME,
base64ToRaw(base64Data)));
if (!filePath.isEmpty()) {
qInfo() << "Creating temporary file" << filePath;
......@@ -305,8 +283,8 @@ void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
qDebug() << "ERROR: File name is empty!";
return;
}
QByteArray data = QByteArray::fromBase64(file.content.toUtf8());
fileName = writeFileToEmailDir(fileName, msgId, data);
fileName = writeFileToEmailDir(fileName, msgId,
base64ToRaw(file.content.toUtf8()));
fileList.append(fileName);
addAttachmentToEmailMessage(emailMessage, file.filename,
file.content.toUtf8(), boundary);
......@@ -471,9 +449,8 @@ bool Files::setAttachmentModel(FileListModel &attachModel,
{
qDebug("%s()", __func__);
FileDb *fDb = Q_NULLPTR;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
FileDb *fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
......@@ -519,11 +496,10 @@ void Files::sendAttachmentEmailZfo(const QVariant &attachModelVariant,
*/
QByteArray base64Data(attachModel->data(idx,
FileListModel::ROLE_FILE_DATA).toByteArray());
QByteArray rawData(QByteArray::fromBase64(base64Data));
QString attachName(attachModel->data(idx,
FileListModel::ROLE_FILE_NAME).toString());
QString filePath(
writeFileToEmailDir(attachName, msgId, rawData));
QString filePath(writeFileToEmailDir(attachName, msgId,
base64ToRaw(base64Data)));
fileList.append(filePath);
addAttachmentToEmailMessage(emailMessage, attachName,
base64Data, boundary);
......@@ -573,9 +549,8 @@ void Files::saveAttachmentsToDiskDb(const QString &userName,
QString destPath;
foreach (const FileDb::FileData &file, filelist) {
QByteArray data = QByteArray::fromBase64(file.content.toUtf8());
destPath = writeFileToDir(fileSaveLocation(), file.filename,
msgId, data);
msgId, base64ToRaw(file.content.toUtf8()));
}
attachmentSavingNotification(destPath);
......@@ -604,12 +579,12 @@ void Files::saveAttachmentsToDiskZfo(const QVariant &attachModelVariant,
QString destPath;
for (int row = 0; row < attachModel->rowCount(); ++row) {
QModelIndex idx(attachModel->index(row));
QByteArray data(QByteArray::fromBase64(attachModel->data(
idx, FileListModel::ROLE_FILE_DATA).toByteArray()));
destPath = writeFileToDir(fileSaveLocation(),
attachModel->data(idx,
FileListModel::ROLE_FILE_NAME).toString(),
msgId, data);
msgId,
base64ToRaw(attachModel->data(idx,
FileListModel::ROLE_FILE_DATA).toByteArray()));
}
attachmentSavingNotification(destPath);
......@@ -695,7 +670,7 @@ QByteArray Files::decodeZfoFile(const QByteArray &base64ZfoData)
}
/* decode signature from base64 and obtain something CMS message */
return getXmlFromCms(QByteArray::fromBase64(base64ZfoData));
return getXmlFromCms(base64ToRaw(base64ZfoData));
}
bool Files::parseXmlData(enum MsgInfo::ZfoType *type, QString *idStr,
......
......@@ -102,7 +102,7 @@ public:
* @return Attachment content.
*/
Q_INVOKABLE static
QByteArray getAttachmentDb(const QString &userName,
QByteArray getFileRawContentFromDb(const QString &userName,
const QString &msgIdStr, int fileId);
/*!
......
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