Commit 110a8c64 authored by Martin Straka's avatar Martin Straka

Simplified file model and code refactoring

parent 3e7af529
......@@ -289,7 +289,7 @@ Component {
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
sourceSize.height: imgHeightHeader * 1.2
source: rFileIcon
source: files.getAttachmentFileIcon(rFileName)
}
Item {
anchors.left: imageAttachment.right
......@@ -308,7 +308,7 @@ Component {
Text {
id: filesizeid
anchors.left: parent.left
text: rFileSize
text: rFileSizeStr
color: datovkaPalette.mid
font.pointSize: textFontSizeSmall
renderType: Text.NativeRendering
......
......@@ -163,8 +163,7 @@ Item {
// Zfo file must exist in the local database so zfoSize must be > 0
if (zfoSize > 0) {
totalAttachmentSizeBytes += zfoSize;
sendMsgAttachmentModel.appendFileFromPath(FileIdType.DB_ZFO_ID, fileName, "",
getFileSizeString(zfoSize), files.getAttachmentFileIcon(fileName), zfoSize)
sendMsgAttachmentModel.appendFileFromPath(FileIdType.DB_ZFO_ID, fileName, "", zfoSize)
} else {
mainPanel.visible = false
errorText.text = qsTr("ZFO file of message %1 is not available. Download complete message to get it before forwarding.").arg(msgId)
......@@ -218,8 +217,7 @@ Item {
var fileSizeBytes = files.getAttachmentSizeInBytes(pathListModel.get(j).path)
totalAttachmentSizeBytes += fileSizeBytes;
sendMsgAttachmentModel.appendFileFromPath(FileIdType.NO_FILE_ID,
fileName, pathListModel.get(j).path, getFileSizeString(fileSizeBytes),
files.getAttachmentFileIcon(fileName), fileSizeBytes)
fileName, pathListModel.get(j).path, fileSizeBytes)
}
}
pathListModel.clear()
......@@ -563,7 +561,7 @@ Item {
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
sourceSize.height: attachmentListSend.delegateHeight * 0.8
source: rFileIcon
source: files.getAttachmentFileIcon(rFileName)
}
Item {
anchors.left: imageAttachment.right
......
......@@ -549,8 +549,15 @@ bool Files::setAttachmentModel(FileListModel &attachModel,
attachModel.clearAll();
if (includeRealFileSize) {
fDb->setFileModelFromDbWithRealFileSize(attachModel, msgId);
/*
* 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);
}
return true;
......@@ -894,7 +901,6 @@ bool Files::parseAndShowXmlData(enum MsgInfo::ZfoType type, QString *idStr,
attachModel->appendFileEntry(
FileListModel::Entry(-1, document.fileDescr(),
approximateDataSize(document.binaryContent().size()),
getAttachmentFileIconFromFileExtension(document.fileDescr()),
document.base64Content(), document.binaryContent().size(),
QString()));
}
......
......@@ -29,22 +29,20 @@
FileListModel::Entry::Entry(const Entry &fme)
: m_fileId(fme.m_fileId),
m_dmFileDescr(fme.m_dmFileDescr),
m_dmFileSize(fme.m_dmFileSize),
m_icon(fme.m_icon),
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 int fileId, const QString &dmFileDescr,
const QString &dmFileSize, const QString &icon, const QString &fileContent,
FileListModel::Entry::Entry(const int fileId, const QString &fileName,
const QString &fileSizeStr, const QString &fileContent,
qint64 fileSizeBytes, const QString &filePath)
: m_fileId(fileId),
m_dmFileDescr(dmFileDescr),
m_dmFileSize(dmFileSize),
m_icon(icon),
m_fileName(fileName),
m_fileSizeStr(fileSizeStr),
m_fileContent(fileContent),
m_fileSizeBytes(fileSizeBytes),
m_filePath(filePath)
......@@ -56,34 +54,24 @@ int FileListModel::Entry::fileId(void) const
return m_fileId;
}
QString FileListModel::Entry::dmFileDescr(void) const
QString FileListModel::Entry::fileName(void) const
{
return m_dmFileDescr;
return m_fileName;
}
void FileListModel::Entry::setDmFileDescr(const QString &dmFileDescr)
void FileListModel::Entry::setFileName(const QString &fileName)
{
m_dmFileDescr = dmFileDescr;
m_fileName = fileName;
}
QString FileListModel::Entry::dmFileSize(void) const
QString FileListModel::Entry::fileSizeStr(void) const
{
return m_dmFileSize;
return m_fileSizeStr;
}
void FileListModel::Entry::setDmFileSize(const QString &dmFileSize)
void FileListModel::Entry::setFileSizeStr(const QString &fileSizeStr)
{
m_dmFileSize = dmFileSize;
}
QString FileListModel::Entry::icon(void) const
{
return m_icon;
}
void FileListModel::Entry::setIcon(const QString &icon)
{
m_icon = icon;
m_fileSizeStr = fileSizeStr;
}
QString FileListModel::Entry::fileContent(void) const
......@@ -145,8 +133,7 @@ QHash<int, QByteArray> FileListModel::roleNames(void) const
if (roles.isEmpty()) {
roles[ROLE_FILE_ID] = "rFileId";
roles[ROLE_FILE_NAME] = "rFileName";
roles[ROLE_FILE_SIZE] = "rFileSize";
roles[ROLE_FILE_ICON] = "rFileIcon";
roles[ROLE_FILE_SIZE_STR] = "rFileSizeStr";
roles[ROLE_FILE_DATA] = "rFileContent";
roles[ROLE_FILE_SIZE_BYTES] = "rFileSizeBytes";
roles[ROLE_FILE_PATH] = "rFilePath";
......@@ -167,13 +154,10 @@ QVariant FileListModel::data(const QModelIndex &index, int role) const
return file.fileId();
break;
case ROLE_FILE_NAME:
return file.dmFileDescr();
return file.fileName();
break;
case ROLE_FILE_SIZE:
return file.dmFileSize();
break;
case ROLE_FILE_ICON:
return file.icon();
case ROLE_FILE_SIZE_STR:
return file.fileSizeStr();
break;
case ROLE_FILE_DATA:
return file.fileContent();
......@@ -228,7 +212,7 @@ Qt::ItemFlags FileListModel::flags(const QModelIndex &index) const
void FileListModel::setQuery(QSqlQuery &query)
{
if (query.record().count() != 4) {
if (query.record().count() != 3) {
return;
}
......@@ -240,7 +224,7 @@ void FileListModel::setQuery(QSqlQuery &query)
while (query.isActive() && query.isValid()) {
m_files.append(Entry(query.value(0).toInt(),
query.value(1).toString(), query.value(2).toString(),
query.value(3).toString(), QString(), 0, QString()));
QString(), 0, QString()));
query.next();
}
......@@ -249,7 +233,7 @@ void FileListModel::setQuery(QSqlQuery &query)
void FileListModel::setQueryWithFileContent(QSqlQuery &query)
{
if (query.record().count() != 5) {
if (query.record().count() != 4) {
return;
}
......@@ -260,9 +244,8 @@ void FileListModel::setQueryWithFileContent(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(),
query.value(3).toString(), QString(),
QByteArray::fromBase64(query.value(4).toByteArray()).size(),
query.value(1).toString(), query.value(2).toString(), QString(),
QByteArray::fromBase64(query.value(3).toByteArray()).size(),
QString()));
query.next();
}
......@@ -277,14 +260,12 @@ void FileListModel::appendFileEntry(const Entry &entry)
endInsertRows();
}
void FileListModel::appendFileFromPath(int fileId, const QString &fileName,
const QString &filePath, const QString &fileSize, const QString &fileIcon,
qint64 fileSizeBytes)
const QString &filePath, qint64 fileSizeBytes)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_files.append(FileListModel::Entry(fileId, fileName, fileSize,
fileIcon, QString(), fileSizeBytes, filePath));
m_files.append(FileListModel::Entry(fileId, fileName, QString(),
QString(), fileSizeBytes, filePath));
endInsertRows();
}
......
......@@ -33,18 +33,15 @@ public:
class Entry {
public:
Entry(const Entry &fme);
Entry(const int fileId, const QString &dmFileDescr,
const QString &dmFileSize, const QString &icon,
const QString &fileContent, qint64 fileSize,
const QString &filePath);
Entry(const int fileId, const QString &fileName,
const QString &fileSizeStr, const QString &fileContent,
qint64 fileSizeBytes, const QString &filePath);
int fileId(void) const;
QString dmFileDescr(void) const;
void setDmFileDescr(const QString &dmFileDescr);
QString dmFileSize(void) const;
void setDmFileSize(const QString &dmFileSize);
QString icon(void) const;
void setIcon(const QString &icon);
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;
......@@ -53,12 +50,11 @@ public:
void setFilePath(const QString &filePath);
private:
int m_fileId; /*!< File database file identifier. */
QString m_dmFileDescr; /*!< File name. */
QString m_dmFileSize; /*!< Approximate file size for qml. */
QString m_icon; /*!< File icon? */
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. */
qint64 m_fileSizeBytes; /*!< Real file size in bytes. */
QString m_filePath; /*!< File path. */
};
......@@ -68,8 +64,7 @@ public:
enum Roles {
ROLE_FILE_ID = Qt::UserRole,
ROLE_FILE_NAME,
ROLE_FILE_SIZE,
ROLE_FILE_ICON,
ROLE_FILE_SIZE_STR,
ROLE_FILE_DATA,
ROLE_FILE_SIZE_BYTES,
ROLE_FILE_PATH
......@@ -169,8 +164,9 @@ public:
/*!
* @brief Set the content of the model according to the supplied query.
*
* @note Real file size for model is calculated from file content.
* This method is slow and it is used in the send message page only.
* @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.
*/
......@@ -189,14 +185,11 @@ public:
* @param[in] fileId File id.
* @param[in] fileName File name.
* @param[in] filePath File path.
* @param[in] fileSize File size.
* @param[in] fileIcon File icon.
* @param[in] fileSizeBytes Real file size in bytes.
*/
Q_INVOKABLE
void appendFileFromPath(int fileId, const QString &fileName,
const QString &filePath, const QString &fileSize,
const QString &fileIcon, qint64 fileSizeBytes);
const QString &filePath, qint64 fileSizeBytes);
/*!
* @brief Clears the model.
......@@ -226,7 +219,7 @@ 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 to model.
* @param[in] includeRealFileSize True if real file size will include into model.
* @return True on success.
*/
Q_INVOKABLE
......
......@@ -701,7 +701,7 @@ void IsdsWrapper::sendMessage(const QString &userName, qint64 dmID,
foreach (const FileListModel::Entry &file, attachModel->allEntries()) {
Isds::Document document;
document.setFileDescr(file.dmFileDescr());
document.setFileDescr(file.fileName());
document.setFileMetaType(fileMetaType);
/*
......
......@@ -133,7 +133,7 @@ void FileDb::setFileModelFromDb(FileListModel &model, const qint64 dmId) const
{
QSqlQuery query(m_db);
QString queryStr = "SELECT id, dmFileDescr, _dmFileSize, _icon "
QString queryStr = "SELECT id, dmFileDescr, _dmFileSize "
"FROM files WHERE dmID = :dmID";
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %s",
......@@ -147,12 +147,12 @@ void FileDb::setFileModelFromDb(FileListModel &model, const qint64 dmId) const
}
}
void FileDb::setFileModelFromDbWithRealFileSize(FileListModel &model,
void FileDb::setFileModelFromDbWithFileContent(FileListModel &model,
const qint64 dmId) const
{
QSqlQuery query(m_db);
QString queryStr = "SELECT id, dmFileDescr, _dmFileSize, _icon, "
QString queryStr = "SELECT id, dmFileDescr, _dmFileSize, "
"dmEncodedContent FROM files WHERE dmID = :dmID";
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %s",
......
......@@ -107,7 +107,7 @@ public:
QList<Isds::Document> getFilesFromDb(qint64 dmId) const;
/*!
* @brief Set attachment model from db - for QML listview.
* @brief Set attachment model from db for QML listview.
*
* @param[in,out] model Model to be set.
* @param[in] dmId Message Id.
......@@ -115,12 +115,13 @@ public:
void setFileModelFromDb(FileListModel &model, qint64 dmId) const;
/*!
* @brief Set attachment model from db. Real file size will include in the model.
* @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 setFileModelFromDbWithRealFileSize(FileListModel &model,
void setFileModelFromDbWithFileContent(FileListModel &model,
qint64 dmId) const;
/*!
......
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