Commit 1f3ce7cb authored by Martin Straka's avatar Martin Straka

Refactor file model

parent f27bcb2b
......@@ -53,11 +53,17 @@ Component {
property string emailBody
property int zfoType: MsgInfo.TYPE_UNKNOWN
/* Return file size string in bytes or kilobytes */
function getFileSizeString(fileSize) {
return (fileSize >= 1024) ? "~" + Math.ceil(fileSize / 1024) + " kB"
: "~" + (fileSize) + " B"
}
function setTopButtonVisibility() {
/* More than two visible icons causes problems on iOS devices. */
msgEmailButton.visible = !fromLocalDb && (attachmentList.count > 0)
attachmentSaveButon.visible = !fromLocalDb && (attachmentList.count > 0)
msgDownloadButton.visible = fromLocalDb && (attachmentList.count == 0)
msgDownloadButton.visible = fromLocalDb && (attachmentList.count === 0)
attachmentMenuButon.visible = fromLocalDb && (attachmentList.count > 0)
}
......@@ -87,7 +93,7 @@ Component {
attachmentModel.setFromDb(userName, zfoId)
zfoType = MsgInfo.TYPE_MESSAGE
}
emptyList.visible = (attachmentList.count == 0)
emptyList.visible = (attachmentList.count === 0)
setTopButtonVisibility()
}
......@@ -276,7 +282,7 @@ Component {
interactive: true
model: attachmentModel
onCountChanged: {
emptyList.visible = (attachmentList.count == 0)
emptyList.visible = (attachmentList.count === 0)
setTopButtonVisibility()
}
delegate: Rectangle {
......@@ -308,7 +314,7 @@ Component {
Text {
id: filesizeid
anchors.left: parent.left
text: rFileSize
text: getFileSizeString(rFileSize)
color: datovkaPalette.mid
font.pointSize: textFontSizeSmall
renderType: Text.NativeRendering
......
......@@ -52,6 +52,8 @@ Item {
property int max_OVM_ATTACHMENT_SIZE_BYTES: 50000000 //50MB
property int max_OVM_ATTACHMENT_SIZE_MB: 50
property string text_COLOR_RED: "#ff0000"
/* This property holds total attachment size in bytes */
property int totalAttachmentSizeBytes: 0
......@@ -68,7 +70,7 @@ Item {
/* Return file size string in bytes or kilobytes */
function getFileSizeString(fileSize) {
return (fileSize >= 1000) ? "~" + Math.ceil(fileSize / 1000) + " kB"
return (fileSize >= 1024) ? "~" + Math.ceil(fileSize / 1024) + " kB"
: "~" + (fileSize) + " B"
}
......@@ -82,17 +84,17 @@ Item {
}
if (attachmentNumber >= max_ATTACHMENT_FILES) {
attachmentsSizeLabel.text = qsTr("The permitted number of (%1) attachments has been exceeded.").arg(max_ATTACHMENT_FILES)
attachmentsSizeLabel.color = "#ff0000"
attachmentsSizeLabel.color = text_COLOR_RED
return false
}
if (totalAttachmentSizeBytes >= max_OVM_ATTACHMENT_SIZE_BYTES) {
attachmentsSizeLabel.text = qsTr("Total size of attachments exceeds %1 MB.").arg(max_OVM_ATTACHMENT_SIZE_MB)
attachmentsSizeLabel.color = "#ff0000"
attachmentsSizeLabel.color = text_COLOR_RED
return false
}
if (totalAttachmentSizeBytes >= max_ATTACHMENT_SIZE_BYTES) {
attachmentsSizeLabel.text = qsTr("Total size of attachments exceeds %1 MB. Most of the data boxes cannot receive messages larger than %1 MB. However, some OVM data boxes can receive message up to %2 MB.").arg(max_ATTACHMENT_SIZE_MB).arg(max_OVM_ATTACHMENT_SIZE_MB)
attachmentsSizeLabel.color = "#ff0000"
attachmentsSizeLabel.color = text_COLOR_RED
return true
}
if (totalAttachmentSizeBytes >= 1024) {
......@@ -161,8 +163,8 @@ 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, files.getAttachmentFileIcon(fileName))
} 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)
......@@ -216,8 +218,8 @@ 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,
files.getAttachmentFileIcon(fileName))
}
}
pathListModel.clear()
......@@ -583,7 +585,7 @@ Item {
font.pointSize: textFontSizeSmall
}
Text {
text: rFileSize
text: getFileSizeString(rFileSize)
color: datovkaPalette.mid
font.pointSize: textFontSizeSmall
}
......
......@@ -887,10 +887,9 @@ 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.binaryContent().size(),
getAttachmentFileIconFromFileExtension(document.fileDescr()),
document.base64Content(), document.binaryContent().size(),
QString()));
document.base64Content(), QString()));
}
}
if (emailBody != Q_NULLPTR) {
......
......@@ -30,23 +30,21 @@
FileListModel::Entry::Entry(const Entry &fme)
: m_fileId(fme.m_fileId),
m_dmFileDescr(fme.m_dmFileDescr),
m_dmFileSize(fme.m_dmFileSize),
m_fileSize(fme.m_fileSize),
m_icon(fme.m_icon),
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,
qint64 fileSizeBytes, const QString &filePath)
qint64 fileSize, const QString &icon, const QString &fileContent,
const QString &filePath)
: m_fileId(fileId),
m_dmFileDescr(dmFileDescr),
m_dmFileSize(dmFileSize),
m_fileSize(fileSize),
m_icon(icon),
m_fileContent(fileContent),
m_fileSizeBytes(fileSizeBytes),
m_filePath(filePath)
{
}
......@@ -66,14 +64,14 @@ void FileListModel::Entry::setDmFileDescr(const QString &dmFileDescr)
m_dmFileDescr = dmFileDescr;
}
QString FileListModel::Entry::dmFileSize(void) const
qint64 FileListModel::Entry::fileSize(void) const
{
return m_dmFileSize;
return m_fileSize;
}
void FileListModel::Entry::setDmFileSize(const QString &dmFileSize)
void FileListModel::Entry::setFileSize(qint64 fileSize)
{
m_dmFileSize = dmFileSize;
m_fileSize = fileSize;
}
QString FileListModel::Entry::icon(void) const
......@@ -96,16 +94,6 @@ void FileListModel::Entry::setFileContent(const QString &fileContent)
m_fileContent = fileContent;
}
qint64 FileListModel::Entry::fileSizeBytes(void) const
{
return m_fileSizeBytes;
}
void FileListModel::Entry::setFileSizeBytes(qint64 fileSizeBytes)
{
m_fileSizeBytes = fileSizeBytes;
}
QString FileListModel::Entry::filePath(void) const
{
return m_filePath;
......@@ -148,7 +136,6 @@ QHash<int, QByteArray> FileListModel::roleNames(void) const
roles[ROLE_FILE_SIZE] = "rFileSize";
roles[ROLE_FILE_ICON] = "rFileIcon";
roles[ROLE_FILE_DATA] = "rFileContent";
roles[ROLE_FILE_SIZE_BYTES] = "rFileSizeBytes";
roles[ROLE_FILE_PATH] = "rFilePath";
}
return roles;
......@@ -170,7 +157,7 @@ QVariant FileListModel::data(const QModelIndex &index, int role) const
return file.dmFileDescr();
break;
case ROLE_FILE_SIZE:
return file.dmFileSize();
return file.fileSize();
break;
case ROLE_FILE_ICON:
return file.icon();
......@@ -178,9 +165,6 @@ QVariant FileListModel::data(const QModelIndex &index, int role) const
case ROLE_FILE_DATA:
return file.fileContent();
break;
case ROLE_FILE_SIZE_BYTES:
return file.fileSizeBytes();
break;
case ROLE_FILE_PATH:
return file.filePath();
break;
......@@ -218,7 +202,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
......@@ -228,7 +212,7 @@ Qt::ItemFlags FileListModel::flags(const QModelIndex &index) const
void FileListModel::setQuery(QSqlQuery &query)
{
if (query.record().count() != 5) {
if (query.record().count() != 4) {
return;
}
......@@ -239,10 +223,9 @@ 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(),
query.value(3).toString(), QString(),
QByteArray::fromBase64(query.value(4).toByteArray()).size(),
QString()));
query.value(1).toString(),
QByteArray::fromBase64(query.value(2).toByteArray()).size(),
query.value(3).toString(), QString(), QString()));
query.next();
}
......@@ -258,12 +241,11 @@ void FileListModel::appendFileEntry(const Entry &entry)
void FileListModel::appendFileFromPath(int fileId, const QString &fileName,
const QString &filePath, const QString &fileSize, const QString &fileIcon,
qint64 fileSizeBytes)
const QString &filePath, qint64 fileSize, const QString &fileIcon)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_files.append(FileListModel::Entry(fileId, fileName, fileSize,
fileIcon, QString(), fileSizeBytes, filePath));
fileIcon, QString(), filePath));
endInsertRows();
}
......
......@@ -34,31 +34,27 @@ public:
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);
qint64 fileSize, const QString &icon,
const QString &fileContent, 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);
qint64 fileSize(void) const;
void setFileSize(qint64 fileSize);
QString icon(void) const;
void setIcon(const QString &icon);
QString fileContent(void) const;
void setFileContent(const QString &fileContent);
qint64 fileSizeBytes(void) const;
void setFileSizeBytes(qint64 fileSizeBytes);
QString filePath(void) const;
void setFilePath(const QString &filePath);
private:
int m_fileId; /*!< File database file identifier. */
QString m_dmFileDescr; /*!< File name. */
QString m_dmFileSize; /*!< File size qml string. */
qint64 m_fileSize; /*!< Real file size in bytes. */
QString m_icon; /*!< File icon? */
QString m_fileContent; /*!< File content. */
qint64 m_fileSizeBytes; /*!< Real file size in bytes. */
QString m_filePath; /*!< File path. */
};
......@@ -71,7 +67,6 @@ public:
ROLE_FILE_SIZE,
ROLE_FILE_ICON,
ROLE_FILE_DATA,
ROLE_FILE_SIZE_BYTES,
ROLE_FILE_PATH
};
......@@ -181,12 +176,10 @@ public:
* @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 fileSize, const QString &fileIcon);
/*!
* @brief Clears the model.
......
......@@ -133,8 +133,8 @@ void FileDb::setFileModelFromDb(FileListModel &model, const qint64 dmId) const
{
QSqlQuery query(m_db);
QString queryStr = "SELECT id, dmFileDescr, _dmFileSize, "
"_icon, dmEncodedContent FROM files WHERE dmID = :dmID";
QString queryStr = "SELECT id, dmFileDescr, dmEncodedContent, _icon "
"FROM files WHERE dmID = :dmID";
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %s",
query.lastError().text().toUtf8().constData());
......
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