Commit 29e1a0ef authored by Martin Straka's avatar Martin Straka

Allow to store attachments from zfo to disk

parent 500509f9
......@@ -280,7 +280,7 @@ Component {
anchors.fill: parent
onClicked: {
locker.ignoreNextSuspension()
files.openAttachment(userName, msgId, rFileId)
files.openAttachmentFromDb(userName, msgId, rFileId)
}
}
Rectangle {
......
......@@ -62,6 +62,7 @@ Component {
anchors.fill: parent
onClicked: {
statusBar.visible = false
files.clearFileModelZfo()
pageView.pop(StackView.Immediate)
}
}
......@@ -94,7 +95,7 @@ Component {
MouseArea {
anchors.fill: parent
onClicked: {
console.log("EMAIL")
files.sendAttachmentsWithEmail("", zfoId)
}
}
}
......@@ -106,7 +107,7 @@ Component {
MouseArea {
anchors.fill: parent
onClicked: {
console.log("DISK")
files.saveAttachmentsToDisk("", zfoId)
}
}
}
......@@ -242,7 +243,7 @@ Component {
anchors.fill: parent
onClicked: {
locker.ignoreNextSuspension()
// TODO - files.openAttachment(userName, msgId, rFileId)
files.openAttachment(rFileName, rFileContent)
}
}
Rectangle {
......
......@@ -140,6 +140,17 @@ void Files::clearFileModel(void)
}
/* ========================================================================= */
/*
* Slot: Clear zfo file model in QML.
*/
void Files::clearFileModelZfo(void)
/* ========================================================================= */
{
globFilesModelZfo.clearAll();
}
/* ========================================================================= */
/*
* Func: Delete files from all file databases where file lifetime expired.
......@@ -205,9 +216,9 @@ void Files::fillFileList(const QString &userName, qint64 msgId)
/* ========================================================================= */
/*
* Slot: Open attachment in default application.
* Slot: Open attachment from database.
*/
void Files::openAttachment(const QString &userName, qint64 msgId, int fileId)
void Files::openAttachmentFromDb(const QString &userName, qint64 msgId, int fileId)
/* ========================================================================= */
{
qDebug("%s()", __func__);
......@@ -239,25 +250,53 @@ void Files::openAttachment(const QString &userName, qint64 msgId, int fileId)
return;
}
QByteArray data = QByteArray::fromBase64(file.content.toUtf8());
openAttachment(fileName, file.content);
}
/* ========================================================================= */
/*
* Slot: Open attachment in default application.
*/
void Files::openAttachment(const QString &fileName, const QString &content)
/* ========================================================================= */
{
Q_ASSERT(!fileName.isEmpty());
Q_ASSERT(!content.isEmpty());
if (fileName.isEmpty() || content.isEmpty()) {
qDebug() << "ERROR: File name or its content is empty!";
return;
}
// TODO - here is problem if file is zfo again!
if (isAttachmentZfoFile(fileName)) {
// TODO - what to do?
//parseXMLData(decodeZfoFile(content));
return;
}
QByteArray data = QByteArray::fromBase64(content.toUtf8());
QString filePath = writeFileToTmpDir(fileName, TEMP_DIR_NAME, data);
if (!filePath.isEmpty()) {
qDebug() << filePath;
fileName = writeFileToTmpDir(fileName, TEMP_DIR_NAME, data);
if (!fileName.isEmpty()) {
qDebug() << fileName;
#if defined Q_OS_IOS
UrlOpener urlOpener;
urlOpener.openFile(fileName);
urlOpener.openFile(filePath);
/*
* TODO - we may check exceptions form objective-C (openFile)
* and return as bool.
*/
#else /* !defined Q_OS_IOS */
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(fileName))) {
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(filePath))) {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(tr("Open attachment error"));
msgBox.setText(tr("There is no application to open this file format."));
msgBox.setInformativeText(tr("File: '%1'").arg(fileName));
msgBox.setInformativeText(tr("File: '%1'").arg(filePath));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
......@@ -382,24 +421,24 @@ void Files::saveAttachmentsToDisk(const QString &userName, qint64 msgId)
{
qDebug("%s()", __func__);
if (userName.isEmpty() || msgId <= 0) {
return;
}
QList<FileDb::FileData> filelist;
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
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;
}
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!";
return;
}
filelist = fDb->getFilesFromDb(msgId);
if (filelist.isEmpty()) {
qDebug() << "ERROR: File list is empty!";
return;
}
QList<FileDb::FileData> filelist;
filelist = fDb->getFilesFromDb(msgId);
if (filelist.isEmpty()) {
qDebug() << "ERROR: File list is empty!";
return;
}
QString path = getBasePathBasedOnPlatform();
......@@ -422,9 +461,23 @@ void Files::saveAttachmentsToDisk(const QString &userName, qint64 msgId)
QString destPath;
foreach (const FileDb::FileData &file, filelist) {
QByteArray data = QByteArray::fromBase64(file.content.toUtf8());
destPath = writeFileToDir(path, file.filename, msgId, data);
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);
......@@ -740,12 +793,11 @@ bool Files::parseXMLData(QByteArray data)
html += divEnd;
// set file model
globFilesModelZfo.setZfoFilesToModel(fileList);
foreach (const Files::File &file, fileList) {
qDebug() << file.dmID << file.dmFileDescr << file._dmFileSize << file._icon;
}
// open new qml page
emit pushNewMessageDeatilPage(QString::number(msg.dmID));
// set message deatil string to qml
emit messageDetailChanged(html);
return success;
......
......@@ -42,11 +42,17 @@ public:
*/
Q_INVOKABLE void fillFileList(const QString &userName, qint64 msgId);
/*!
* @brief Open attachment from database.
*/
Q_INVOKABLE void openAttachmentFromDb(const QString &userName,
qint64 msgId, int fileId);
/*!
* @brief Open attachment in default application.
*/
Q_INVOKABLE void openAttachment(const QString &userName, qint64 msgId,
int fileId);
Q_INVOKABLE void openAttachment(const QString &fileName,
const QString &content);
/*!
* @brief Send attachment with email application.
......@@ -65,6 +71,11 @@ public:
*/
Q_INVOKABLE void clearFileModel(void);
/*!
* @brief Clear zfo attachment model.
*/
Q_INVOKABLE void clearFileModelZfo(void);
/*!
* @brief Delete file database.
*/
......
......@@ -30,16 +30,18 @@ FileModelEntry::FileModelEntry(const FileModelEntry &fme)
: m_fileId(fme.m_fileId),
m_dmFileDescr(fme.m_dmFileDescr),
m_dmFileSize(fme.m_dmFileSize),
m_icon(fme.m_icon)
m_icon(fme.m_icon),
m_fileContent(fme.m_fileContent)
{
}
FileModelEntry::FileModelEntry(const int fileId, const QString &dmFileDescr,
const QString &dmFileSize, const QString &icon)
const QString &dmFileSize, const QString &icon, const QString &fileContent)
: m_fileId(fileId),
m_dmFileDescr(dmFileDescr),
m_dmFileSize(dmFileSize),
m_icon(icon)
m_icon(icon),
m_fileContent(fileContent)
{
}
......@@ -78,6 +80,17 @@ void FileModelEntry::setIcon(const QString &icon)
m_icon = icon;
}
QString FileModelEntry::fileContent(void) const
{
return m_fileContent;
}
void FileModelEntry::setFileContent(const QString &fileContent)
{
m_fileContent = fileContent;
}
FileListModel globFilesModel;
FileListModel globFilesModelZfo;
......@@ -103,6 +116,7 @@ QHash<int, QByteArray> FileListModel::roleNames(void) const
roles[ROLE_FILE_NAME] = "rFileName";
roles[ROLE_FILE_SIZE] = "rFileSize";
roles[ROLE_FILE_ICON] = "rFileIcon";
roles[ROLE_FILE_DATA] = "rFileContent";
}
return roles;
}
......@@ -128,6 +142,9 @@ QVariant FileListModel::data(const QModelIndex &index, int role) const
case ROLE_FILE_ICON:
return file.icon();
break;
case ROLE_FILE_DATA:
return file.fileContent();
break;
default:
/* Do nothing. */
break;
......@@ -156,7 +173,7 @@ void FileListModel::setQuery(QSqlQuery &query)
while (query.isActive() && query.isValid()) {
m_files.append(FileModelEntry(query.value(0).toInt(),
query.value(1).toString(), query.value(2).toString(),
query.value(3).toString()));
query.value(3).toString(), ""));
query.next();
}
......@@ -172,8 +189,9 @@ void FileListModel::setZfoFilesToModel(const QList<Files::File> &fileList)
m_files.clear();
foreach (const Files::File &file, fileList) {
m_files.append(FileModelEntry(file.id,
file.dmFileDescr, file._dmFileSize, file._icon));
m_files.append(FileModelEntry(-1,
file.dmFileDescr, file._dmFileSize, file._icon,
file.dmEncodedContent));
}
endResetModel();
......
......@@ -33,7 +33,8 @@ class FileModelEntry {
public:
FileModelEntry(const FileModelEntry &fme);
FileModelEntry(const int fileId, const QString &dmFileDescr,
const QString &dmFileSize, const QString &icon);
const QString &dmFileSize, const QString &icon,
const QString &fileContent);
int fileId(void) const;
QString dmFileDescr(void) const;
......@@ -42,12 +43,15 @@ public:
void setDmFileSize(const QString &dmFileSize);
QString icon(void) const;
void setIcon(const QString &icon);
QString fileContent(void) const;
void setFileContent(const QString &fileContent);
private:
int m_fileId; /*!< File database file identifier. */
QString m_dmFileDescr; /*!< File name. */
QString m_dmFileSize; /*!< File size. */
QString m_icon; /*!< File icon? */
QString m_fileContent; /*!< File content */
};
class FileListModel : public QAbstractListModel
......@@ -62,7 +66,8 @@ public:
ROLE_FILE_ID = Qt::UserRole,
ROLE_FILE_NAME,
ROLE_FILE_SIZE,
ROLE_FILE_ICON
ROLE_FILE_ICON,
ROLE_FILE_DATA
};
/*!
......
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