Commit 9e1de371 authored by Karel Slaný's avatar Karel Slaný

Deleted global FileListModel globFilesModelZfo.

parent 81c9e553
......@@ -179,7 +179,7 @@ Component {
} else if (index == 1) {
files.sendAttachmentsWithEmailFromDb(userName, msgId)
} else if (index == 2) {
files.saveAttachmentsToDisk(userName, msgId)
files.saveAttachmentsToDiskDb(userName, msgId)
} else if (index == 3) {
if (files.deleteAttachmentsFromDb(userName, msgId)) {
files.clearFileModel()
......
......@@ -179,7 +179,11 @@ Component {
MouseArea {
anchors.fill: parent
onClicked: {
files.saveAttachmentsToDisk("", zfoId)
if (!fromLocalDb) {
files.saveAttachmentsToDiskZfo(attachmentModel, zfoId)
} else {
files.saveAttachmentsToDiskDb(userName, zfoId)
}
}
}
}
......
......@@ -350,88 +350,6 @@ void Files::sendAttachmentsWithEmailFromDb(const QString &userName,
sendEmail(emailMessage, fileList, subject, body, msgId);
}
void Files::saveAttachmentsToDisk(const QString &userName, qint64 msgId)
{
qDebug("%s()", __func__);
QList<FileDb::FileData> filelist;
if (!userName.isEmpty()) {
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!";
return;
}
filelist = fDb->getFilesFromDb(msgId);
if (filelist.isEmpty()) {
qDebug() << "ERROR: File list is empty!";
return;
}
}
QString path = getBasePathBasedOnPlatform();
#if defined Q_OS_IOS
/* There is only one location where files or data can store
* permanently and share with Desktop.
* See: https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
*/
path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
/* Another platform can select destination for attachment saving. */
path = QFileDialog::getExistingDirectory(0,
tr("Select directory"), path,
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
#endif
QString destPath;
if (userName.isEmpty()) {
for (int i = 0; i < globFilesModelZfo.rowCount(); ++i) {
QModelIndex index = globFilesModelZfo.index(i);
QByteArray data = QByteArray::fromBase64(
globFilesModelZfo.data(
index, FileListModel::ROLE_FILE_DATA).toByteArray());
destPath = writeFileToDir(path,
globFilesModelZfo.data(
index, FileListModel::ROLE_FILE_NAME).toString(),
msgId,
data);
}
} else {
foreach (const FileDb::FileData &file, filelist) {
QByteArray data = QByteArray::fromBase64(file.content.toUtf8());
destPath = writeFileToDir(path, file.filename, msgId, data);
}
}
QFileInfo fi(destPath);
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Attachment saving"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
if (!destPath.isEmpty()) {
msgBox.setIcon(QMessageBox::Information);
msgBox.setText(tr("Attachments have been saved."));
msgBox.setInformativeText(tr("Path: '%1'").arg(fi.absolutePath()));
} else {
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(tr("Attachments have not been saved!"));
msgBox.setInformativeText(tr("Path: '%1'").arg(fi.absolutePath()));
}
msgBox.exec();
}
void Files::deleteFileDb(const QString &userName)
{
qDebug("%s()", __func__);
......@@ -635,14 +553,8 @@ void Files::sendAttachmentEmailZfo(const QVariant &attachModelVariant,
qDebug("%s()", __func__);
/* Obtain pointer to attachment model. */
const FileListModel *attachModel = Q_NULLPTR;
{
if (!attachModelVariant.canConvert<QObject *>()) {
return;
}
QObject *obj = qvariant_cast<QObject *>(attachModelVariant);
attachModel = qobject_cast<FileListModel *>(obj);
}
const FileListModel *attachModel =
FileListModel::modelFromVariant(attachModelVariant);
if (attachModel == Q_NULLPTR) {
Q_ASSERT(0);
return;
......@@ -690,6 +602,145 @@ void Files::sendAttachmentEmailZfo(const QVariant &attachModelVariant,
sendEmail(emailMessage, fileList, subject, body, msgId);
}
/*!
* @brief Obtain path for storing attachments.
*
* @return Path to location where attachment can be stored.
*/
static
QString obtainStoragePath(void)
{
QString path(getBasePathBasedOnPlatform());
#if defined Q_OS_IOS
/* There is only one location where files or data can store
* permanently and share with Desktop.
* See: https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
*/
path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
/* Another platform can select destination for attachment saving. */
path = QFileDialog::getExistingDirectory(Q_NULLPTR,
QObject::tr("Select directory"), path,
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
#endif
return path;
}
/*!
* @brief Generates a notification dialogue about the result of attachment
* saving.
*
* @param[in] destPath Path to which the attachment was saved.
*/
static
void attachmentSavingNotification(const QString &destPath)
{
QFileInfo fi(destPath);
QMessageBox msgBox;
msgBox.setWindowTitle(QObject::tr("Attachment saving"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
if (!destPath.isEmpty()) {
msgBox.setIcon(QMessageBox::Information);
msgBox.setText(QObject::tr("Attachments have been saved."));
} else {
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(QObject::tr("Attachments have not been saved!"));
}
msgBox.setInformativeText(
QObject::tr("Path: '%1'").arg(fi.absolutePath()));
msgBox.exec();
}
void Files::saveAttachmentsToDiskDb(const QString &userName,
const QString &msgIdStr)
{
qDebug("%s()", __func__);
/* User name must be supplied. */
if (userName.isEmpty()) {
Q_ASSERT(0);
return;
}
/* Obtain message identifier. */
bool ok = false;
qint64 msgId = msgIdStr.toLongLong(&ok);
if (!ok || (msgId < 0)) {
return;
}
QList<FileDb::FileData> filelist;
{
FileDb *fDb = Q_NULLPTR;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
return;
}
filelist = fDb->getFilesFromDb(msgId);
if (filelist.isEmpty()) {
qCritical() << "File list is empty!";
return;
}
}
QString path(obtainStoragePath());
QString destPath;
foreach (const FileDb::FileData &file, filelist) {
QByteArray data = QByteArray::fromBase64(file.content.toUtf8());
destPath = writeFileToDir(path, file.filename, msgId, data);
}
attachmentSavingNotification(destPath);
}
void Files::saveAttachmentsToDiskZfo(const QVariant &attachModelVariant,
const QString &msgIdStr)
{
qDebug("%s()", __func__);
/* Obtain pointer to attachment model. */
const FileListModel *attachModel =
FileListModel::modelFromVariant(attachModelVariant);
if (attachModel == Q_NULLPTR) {
Q_ASSERT(0);
return;
}
/* Obtain message identifier. */
bool ok = false;
qint64 msgId = msgIdStr.toLongLong(&ok);
if (!ok || (msgId < 0)) {
return;
}
QString path(obtainStoragePath());
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(path,
attachModel->data(idx,
FileListModel::ROLE_FILE_NAME).toString(),
msgId, data);
}
attachmentSavingNotification(destPath);
}
void Files::deleteTmpFileFromStorage(const QString &filePath)
{
#if defined Q_OS_IOS
......
......@@ -65,12 +65,6 @@ public:
Q_INVOKABLE void sendAttachmentsWithEmailFromDb(const QString &userName,
qint64 msgId);
/*!
* @brief Save attachments to disk.
*/
Q_INVOKABLE void saveAttachmentsToDisk(const QString &userName,
qint64 msgId);
/*!
* @brief Clear attachment model.
*/
......@@ -201,16 +195,13 @@ public:
const QString &userName, qint64 msgId);
/*!
* @brief Send attachment from zfo with email application.
* @brief Send attachment from ZFO with email application.
*
* @note Some weird stuff happens in QML when passing attachment model
* directly as constant reference. Wrong constructors are called
* and no data are passed. That's because we are using a QVariant.
* Also, QML does not know qint64 therefore we use a QStrting as
* @note QML does not know qint64 therefore we use a QStrting as
* message id.
*
* @param[in] attachModelVariant QVariant holding attachment model.
* @param[in] msgId Message id.
* @param[in] msgIdStr String with message id.
* @param[in] subject Email subject.
* @param[in] body Email body.
*/
......@@ -218,6 +209,26 @@ public:
void sendAttachmentEmailZfo(const QVariant &attachModelVariant,
const QString &msgIdStr, QString subject, QString body);
/*!
* @brief Save attachments from database to disk.
*
* @param[in] userName User name identifying the account.
* @param[in] msgIdStr String with message id.
*/
Q_INVOKABLE static
void saveAttachmentsToDiskDb(const QString &userName,
const QString &msgIdStr);
/*!
* @brief Save attachments from ZFO to disk.
*
* @param[in] attachModelVariant QVariant holding attachment model.
* @param[in] msgIdStr String with message id.
*/
Q_INVOKABLE static
void saveAttachmentsToDiskZfo(const QVariant &attachModelVariant,
const QString &msgIdStr);
/*!
* @brief Delete temporary file from storage.
*
......
......@@ -92,7 +92,6 @@ void FileListModel::Entry::setFileContent(const QString &fileContent)
}
FileListModel globFilesModel;
FileListModel globFilesModelZfo;
void FileListModel::declareQML(void)
{
......@@ -208,6 +207,16 @@ void FileListModel::clearAll(void)
endResetModel();
}
FileListModel *FileListModel::modelFromVariant(
const QVariant &attachModelVariant)
{
if (!attachModelVariant.canConvert<QObject *>()) {
return Q_NULLPTR;
}
QObject *obj = qvariant_cast<QObject *>(attachModelVariant);
return qobject_cast<FileListModel *>(obj);
}
bool FileListModel::setZfo(const QByteArray &rawZfoData)
{
return Files::setAttachmentModel(*this, rawZfoData);
......
......@@ -145,6 +145,19 @@ public:
*/
void clearAll(void);
/*!
* @brief Converts QVariant obtained from QML into model pointer.
*
* @note Some weird stuff happens in QML when passing attachment model
* directly as constant reference. Wrong constructors are called
* and no data are passed. That's because we are using a QVariant.
*
* @param[in] attachModelVariant QVariant holding attachment model.
* @return Pointer to model if it could be acquired, Q_NULLPTR else.
*/
static
FileListModel *modelFromVariant(const QVariant &attachModelVariant);
/*!
* @brief Set content from ZFO content.
*
......@@ -175,6 +188,5 @@ private:
Q_DECLARE_METATYPE(FileListModel)
extern FileListModel globFilesModel;
extern FileListModel globFilesModelZfo;
#endif /* _FILEMODEL_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