Commit c5e61663 authored by Karel Slaný's avatar Karel Slaný

File model now holds binary data and their respective sizes.

parent 61c09742
......@@ -343,7 +343,7 @@ Component {
console.log("Attachment is ZFO: " + rFileName)
var fileContent = null
if (!fromLocalDb) {
fileContent = files.base64ToRaw(rFileContent)
fileContent = rBinaryContent
} else {
fileContent = files.getFileRawContentFromDb(userName, rFileId)
}
......@@ -355,7 +355,7 @@ Component {
})
} else {
if (!fromLocalDb) {
files.openAttachment(rFileName, rFileContent)
files.openAttachment(rFileName, rBinaryContent)
} else {
files.openAttachmentFromDb(userName, rFileId)
}
......
......@@ -35,17 +35,6 @@ const QSet<QString> knowExp = QSet<QString> () <<
"odt" << "pdf" << "png" << "ppt" << "pptx" << "rtf" << "tiff" <<
"txt" << "wav" << "xls" << "xlsx" << "xml" << "zfo";
QString approximateDataSize(int dataSize)
{
QString str;
if (dataSize >= 1024) {
str = QLatin1String("~") + QString::number(dataSize / 1024) + QLatin1String(" kB");
} else {
str = QLatin1String("~") + QString::number(dataSize) + QLatin1String(" B");
}
return str;
}
QString getAttachmentFileIconFromFileExtension(const QString &fileName)
{
QString img("qrc:/fileicons/fileicon_blank.png");
......
......@@ -25,14 +25,6 @@
#include <QString>
/*!
* @brief Get approximate data chunk size from supplied data size.
*
* @param[in] dataSize Actual data size.
* @return Approximate data size including the unit.
*/
QString approximateDataSize(int dataSize);
/*!
* @brief Get attachment icon from attachment file extension.
*
......
......@@ -174,17 +174,15 @@ void Files::openAttachmentFromDb(const QString &userName, int fileId)
Isds::Document document(fDb->getFileFromDb(fileId));
openAttachment(document.fileDescr(), document.base64Content().toUtf8());
openAttachment(document.fileDescr(), document.binaryContent());
}
void Files::openAttachment(const QString &fileName,
const QByteArray &base64Data)
const QByteArray &binaryData)
{
Q_ASSERT(!fileName.isEmpty());
Q_ASSERT(!base64Data.isEmpty());
if (fileName.isEmpty() || base64Data.isEmpty()) {
if (Q_UNLIKELY(fileName.isEmpty() || binaryData.isEmpty())) {
logErrorNL("%s", "File name or its content is empty.");
Q_ASSERT(0);
return;
}
......@@ -195,8 +193,7 @@ void Files::openAttachment(const QString &fileName,
return;
}
QString filePath(writeFile(appTmpDirPath(), fileName,
base64ToRaw(base64Data)));
QString filePath(writeFile(appTmpDirPath(), fileName, binaryData));
if (!filePath.isEmpty()) {
logInfoNL("Creating temporary file '%s'.",
......@@ -504,11 +501,6 @@ QByteArray Files::rawFileContent(const QString &filePath)
return rawData;
}
QByteArray Files::base64ToRaw(const QByteArray &base64Data)
{
return QByteArray::fromBase64(base64Data);
}
MsgInfo *Files::zfoData(const QVariant &attachModelVariant,
const QByteArray &rawZfoData)
{
......@@ -526,7 +518,7 @@ MsgInfo *Files::zfoData(const QVariant &attachModelVariant,
}
bool Files::setAttachmentModel(FileListModel &attachModel,
const QString &userName, qint64 msgId, bool includeRealFileSize)
const QString &userName, qint64 msgId)
{
debugFuncCall();
......@@ -548,18 +540,8 @@ bool Files::setAttachmentModel(FileListModel &attachModel,
attachModel.clearAll();
if (includeRealFileSize) {
/*
* The real file size is computed from file content and it is
* included in the model. Requires file content from database
* and base64 decode. It is slow method and it is used for
* send message file model only.
*/
fDb->setFileModelFromDbWithFileContent(attachModel, msgId);
} else {
/* Set file model without file content and real file size. */
fDb->setFileModelFromDb(attachModel, msgId);
}
/* Set file model without file content and real file size. */
fDb->setFileModelFromDb(attachModel, msgId);
return true;
}
......@@ -595,18 +577,17 @@ void Files::sendAttachmentEmailZfo(const QVariant &attachModelVariant,
for (int row = 0; row < attachModel->rowCount(); ++row) {
QModelIndex idx(attachModel->index(row));
/*
* On Android the attachment must be saved and the explicitly
* add into the email message.
* On Android the attachment must be saved and then explicitly
* added into the email message.
*/
QByteArray base64Data(attachModel->data(idx,
FileListModel::ROLE_FILE_DATA).toByteArray());
QByteArray binaryData(
attachModel->data(idx, FileListModel::ROLE_BINARY_DATA).toByteArray());
QString attachName(attachModel->data(idx,
FileListModel::ROLE_FILE_NAME).toString());
QString filePath(writeFile(targetPath, attachName,
base64ToRaw(base64Data)));
QString filePath(writeFile(targetPath, attachName, binaryData));
fileList.append(filePath);
addAttachmentToEmailMessage(emailMessage, attachName,
base64Data, boundary);
binaryData.toBase64(), boundary);
}
finishEmailMessage(emailMessage, boundary);
......@@ -712,8 +693,8 @@ void Files::saveAttachmentsToDiskZfo(const QVariant &attachModelVariant,
QModelIndex idx(attachModel->index(row));
destPath = writeFile(targetPath, attachModel->data(idx,
FileListModel::ROLE_FILE_NAME).toString(),
base64ToRaw(attachModel->data(idx,
FileListModel::ROLE_FILE_DATA).toByteArray()));
attachModel->data(idx,
FileListModel::ROLE_BINARY_DATA).toByteArray());
}
attachmentSavingNotification(destPath);
......@@ -900,8 +881,7 @@ bool Files::parseAndShowXmlData(enum MsgInfo::ZfoType type, QString *idStr,
foreach (const Isds::Document &document, message.documents()) {
attachModel->appendFileEntry(
FileListModel::Entry(-1, document.fileDescr(),
approximateDataSize(document.binaryContent().size()),
document.base64Content(), document.binaryContent().size(),
document.binaryContent(), document.binaryContent().size(),
QString()));
}
}
......
......@@ -128,11 +128,11 @@ public:
* @brief Open attachment in default application.
*
* @param[in] fileName File name.
* @param[in] base64Data File content base64.
* @param[in] binaryData Raw file content.
*/
Q_INVOKABLE static
void openAttachment(const QString &fileName,
const QByteArray &base64Data);
const QByteArray &binaryData);
/*!
* @brief Open attachment from path in default application.
......@@ -205,15 +205,6 @@ public:
Q_INVOKABLE static
QByteArray rawFileContent(const QString &filePath);
/*!
* @brief Decodes base64-encoded content.
*
* @param[in] base64Data Base64-encoded data.
* @return Raw data.
*/
Q_INVOKABLE static
QByteArray base64ToRaw(const QByteArray &base64Data);
/*!
* @brief Parse content of ZFO file.
*
......@@ -238,12 +229,11 @@ public:
* @param[out] attachModel Attachment model to be set.
* @param[in] userName Identifies the account.
* @param[in] msgId Message identifier.
* @param[in] includeRealFileSize True if real file size will include in the model.
* @return True if success.
*/
static
bool setAttachmentModel(FileListModel &attachModel,
const QString &userName, qint64 msgId, bool includeRealFileSize);
const QString &userName, qint64 msgId);
/*!
* @brief Send attachment from ZFO with email application.
......
......@@ -23,28 +23,26 @@
#include <QQmlEngine> /* qmlRegisterType */
#include <QSqlRecord>
#include <QStringBuilder>
#include "src/files.h" /* TODO -- Do something with it. */
#include "src/models/filemodel.h"
FileListModel::Entry::Entry(const Entry &fme)
: m_fileId(fme.m_fileId),
m_fileName(fme.m_fileName),
m_fileSizeStr(fme.m_fileSizeStr),
m_fileContent(fme.m_fileContent),
m_fileSizeBytes(fme.m_fileSizeBytes),
m_filePath(fme.m_filePath)
FileListModel::Entry::Entry(const Entry &other)
: m_fileId(other.m_fileId),
m_fileName(other.m_fileName),
m_data(other.m_data),
m_fileSize(other.m_fileSize),
m_filePath(other.m_filePath)
{
}
FileListModel::Entry::Entry(const int fileId, const QString &fileName,
const QString &fileSizeStr, const QString &fileContent,
qint64 fileSizeBytes, const QString &filePath)
const QByteArray &data, qint64 fileSize, const QString &filePath)
: m_fileId(fileId),
m_fileName(fileName),
m_fileSizeStr(fileSizeStr),
m_fileContent(fileContent),
m_fileSizeBytes(fileSizeBytes),
m_data(data),
m_fileSize(fileSize),
m_filePath(filePath)
{
}
......@@ -54,7 +52,7 @@ int FileListModel::Entry::fileId(void) const
return m_fileId;
}
QString FileListModel::Entry::fileName(void) const
const QString &FileListModel::Entry::fileName(void) const
{
return m_fileName;
}
......@@ -64,37 +62,52 @@ void FileListModel::Entry::setFileName(const QString &fileName)
m_fileName = fileName;
}
QString FileListModel::Entry::fileSizeStr(void) const
/*!
* @brief Creates human readable size description string.
*
* @param[in] dataSize Size in bytes.
* @return String containing units.
*/
static
QString approximateDataSize(qint64 dataSize)
{
return m_fileSizeStr;
if (dataSize >= 1024) {
return QStringLiteral("~") % QString::number(dataSize / 1024) % QStringLiteral(" kB");
} else {
return QStringLiteral("~") % QString::number(dataSize) % QStringLiteral(" B");
}
}
void FileListModel::Entry::setFileSizeStr(const QString &fileSizeStr)
QString FileListModel::Entry::fileSizeStr(void) const
{
m_fileSizeStr = fileSizeStr;
if (m_fileSize >= 0) {
return approximateDataSize(m_fileSize);
} else {
return QString();
}
}
QString FileListModel::Entry::fileContent(void) const
const QByteArray &FileListModel::Entry::binaryContent(void) const
{
return m_fileContent;
return m_data;
}
void FileListModel::Entry::setFileContent(const QString &fileContent)
void FileListModel::Entry::setBinaryContent(const QByteArray &data)
{
m_fileContent = fileContent;
m_data = data;
}
qint64 FileListModel::Entry::fileSizeBytes(void) const
qint64 FileListModel::Entry::fileSize(void) const
{
return m_fileSizeBytes;
return m_fileSize;
}
void FileListModel::Entry::setFileSizeBytes(qint64 fileSizeBytes)
void FileListModel::Entry::setFileSize(qint64 fileSize)
{
m_fileSizeBytes = fileSizeBytes;
m_fileSize = fileSize;
}
QString FileListModel::Entry::filePath(void) const
const QString &FileListModel::Entry::filePath(void) const
{
return m_filePath;
}
......@@ -134,7 +147,7 @@ QHash<int, QByteArray> FileListModel::roleNames(void) const
roles[ROLE_FILE_ID] = "rFileId";
roles[ROLE_FILE_NAME] = "rFileName";
roles[ROLE_FILE_SIZE_STR] = "rFileSizeStr";
roles[ROLE_FILE_DATA] = "rFileContent";
roles[ROLE_BINARY_DATA] = "rBinaryContent";
roles[ROLE_FILE_SIZE_BYTES] = "rFileSizeBytes";
roles[ROLE_FILE_PATH] = "rFilePath";
}
......@@ -159,11 +172,11 @@ QVariant FileListModel::data(const QModelIndex &index, int role) const
case ROLE_FILE_SIZE_STR:
return file.fileSizeStr();
break;
case ROLE_FILE_DATA:
return file.fileContent();
case ROLE_BINARY_DATA:
return file.binaryContent();
break;
case ROLE_FILE_SIZE_BYTES:
return file.fileSizeBytes();
return file.fileSize();
break;
case ROLE_FILE_PATH:
return file.filePath();
......@@ -202,7 +215,7 @@ qint64 FileListModel::fileSizeBytesFromRow(int row)
return 0;
}
return m_files[row].fileSizeBytes();
return m_files[row].fileSize();
}
Qt::ItemFlags FileListModel::flags(const QModelIndex &index) const
......@@ -223,30 +236,8 @@ void FileListModel::setQuery(QSqlQuery &query)
query.first();
while (query.isActive() && query.isValid()) {
m_files.append(Entry(query.value(0).toInt(),
query.value(1).toString(), query.value(2).toString(),
QString(), 0, QString()));
query.next();
}
endResetModel();
}
void FileListModel::setQueryWithFileContent(QSqlQuery &query)
{
if (query.record().count() != 3) {
return;
}
beginResetModel();
m_files.clear();
query.first();
while (query.isActive() && query.isValid()) {
m_files.append(Entry(query.value(0).toInt(),
query.value(1).toString(), QString(), QString(),
QByteArray::fromBase64(query.value(2).toByteArray()).size(),
QString()));
query.value(1).toString(), QByteArray(),
query.value(2).toLongLong(), QString()));
query.next();
}
......@@ -264,8 +255,8 @@ void FileListModel::appendFileFromPath(int fileId, const QString &fileName,
const QString &filePath, qint64 fileSizeBytes)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_files.append(FileListModel::Entry(fileId, fileName, QString(),
QString(), fileSizeBytes, filePath));
m_files.append(FileListModel::Entry(fileId, fileName, QByteArray(),
fileSizeBytes, filePath));
endInsertRows();
}
......@@ -285,8 +276,7 @@ FileListModel *FileListModel::fromVariant(const QVariant &modelVariant)
return qobject_cast<FileListModel *>(obj);
}
bool FileListModel::setFromDb(const QString &userName, const QString &msgIdStr,
bool includeRealFileSize)
bool FileListModel::setFromDb(const QString &userName, const QString &msgIdStr)
{
bool ok = false;
qint64 msgId = msgIdStr.toLongLong(&ok);
......@@ -294,8 +284,7 @@ bool FileListModel::setFromDb(const QString &userName, const QString &msgIdStr,
return false;
}
return Files::setAttachmentModel(*this, userName, msgId,
includeRealFileSize);
return Files::setAttachmentModel(*this, userName, msgId);
}
void FileListModel::removeItem(int row)
......
......@@ -32,29 +32,27 @@ class FileListModel : public QAbstractListModel {
public:
class Entry {
public:
Entry(const Entry &fme);
Entry(const Entry &other);
Entry(const int fileId, const QString &fileName,
const QString &fileSizeStr, const QString &fileContent,
qint64 fileSizeBytes, const QString &filePath);
const QByteArray &data, qint64 fileSize,
const QString &filePath);
int fileId(void) const;
QString fileName(void) const;
const QString &fileName(void) const;
void setFileName(const QString &fileName);
QString fileSizeStr(void) const;
void setFileSizeStr(const QString &fileSizeStr);
QString fileContent(void) const;
void setFileContent(const QString &fileContent);
qint64 fileSizeBytes(void) const;
void setFileSizeBytes(qint64 fileSizeBytes);
QString filePath(void) const;
const QByteArray &binaryContent(void) const;
void setBinaryContent(const QByteArray &data);
qint64 fileSize(void) const;
void setFileSize(qint64 fileSize);
const QString &filePath(void) const;
void setFilePath(const QString &filePath);
private:
int m_fileId; /*!< File database identifier. */
QString m_fileName; /*!< File name. */
QString m_fileSizeStr; /*!< Approximate file size string for qml. */
QString m_fileContent; /*!< File content. */
qint64 m_fileSizeBytes; /*!< Real file size in bytes. */
QByteArray m_data; /*!< Binary file content. */
qint64 m_fileSize; /*!< Binary size. */
QString m_filePath; /*!< File path. */
};
......@@ -65,7 +63,7 @@ public:
ROLE_FILE_ID = Qt::UserRole,
ROLE_FILE_NAME,
ROLE_FILE_SIZE_STR,
ROLE_FILE_DATA,
ROLE_BINARY_DATA,
ROLE_FILE_SIZE_BYTES,
ROLE_FILE_PATH
};
......@@ -161,17 +159,6 @@ public:
*/
void setQuery(QSqlQuery &query);
/*!
* @brief Set the content of the model according to the supplied query.
*
* @note Real file size for model is computed from file content and it
* is included in the model. This method is slow and it is used
* in the send message file model only.
*
* @param[in,out] query SQL query result.
*/
void setQueryWithFileContent(QSqlQuery &query);
/*!
* @brief Appends entry line to model.
*
......@@ -219,12 +206,10 @@ public:
*
* @param[in] userName User name identifying account.
* @param[in] msgIdStr String containing message identifier.
* @param[in] includeRealFileSize True if real file size will include into model.
* @return True on success.
*/
Q_INVOKABLE
bool setFromDb(const QString &userName, const QString &msgIdStr,
bool includeRealFileSize);
bool setFromDb(const QString &userName, const QString &msgIdStr);
/*!
* @brief Remove file from attachment model.
......
......@@ -147,25 +147,6 @@ void FileDb::setFileModelFromDb(FileListModel &model, const qint64 dmId) const
}
}
void FileDb::setFileModelFromDbWithFileContent(FileListModel &model,
const qint64 dmId) const
{
QSqlQuery query(m_db);
QString queryStr = "SELECT id, dmFileDescr, dmEncodedContent "
"FROM files WHERE dmID = :dmID";
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %s",
query.lastError().text().toUtf8().constData());
return;
}
query.bindValue(":dmID", dmId);
if (query.exec() && query.isActive()) {
model.setQueryWithFileContent(query);
}
}
Isds::Document FileDb::getFileFromDb(int fileId) const
{
QSqlQuery query(m_db);
......
......@@ -38,7 +38,6 @@
class FileDb : public SQLiteDb {
public:
explicit FileDb(const QString &connectionName);
/*!
......@@ -114,16 +113,6 @@ public:
*/
void setFileModelFromDb(FileListModel &model, qint64 dmId) const;
/*!
* @brief Set attachment model from db. Real file size is computed
* from file content and it is included in the model.
*
* @param[in,out] model Model to be set.
* @param[in] dmId Message Id.
*/
void setFileModelFromDbWithFileContent(FileListModel &model,
qint64 dmId) const;
/*!
* @brief Insert or update file data into files table.
*
......
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