diff --git a/autotests/filetest.cpp b/autotests/filetest.cpp index 984ab939..079d434f 100644 --- a/autotests/filetest.cpp +++ b/autotests/filetest.cpp @@ -1,159 +1,160 @@ /* Copyright (c) 2018-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "filetest.h" #include "file.h" #include #include QTEST_GUILESS_MAIN(FileTest) FileTest::FileTest(QObject *parent) : QObject(parent) { } void FileTest::shouldHaveDefaultValue() { File f; QVERIFY(f.fileName().isEmpty()); QVERIFY(f.description().isEmpty()); QVERIFY(f.userId().isEmpty()); QVERIFY(f.url().isEmpty()); QVERIFY(f.mimeType().isEmpty()); QVERIFY(f.fileId().isEmpty()); QVERIFY(f.rid().isEmpty()); + QVERIFY(f.uploadedDateTimeStr().isEmpty()); QCOMPARE(f.uploadedAt(), -1); } void FileTest::shouldAssignValue() { File f; const QString url = QStringLiteral("foo"); const QString name = QStringLiteral("bla"); const QString description = QStringLiteral("des"); const QString userId = QStringLiteral("ble"); const QString mimetype = QStringLiteral("ble1"); const qint64 timeUploaded = 55; const QString fileId = QStringLiteral("blabla"); const QString rId = QStringLiteral("blabla22"); f.setUrl(url); f.setFileName(name); f.setDescription(description); f.setUserId(userId); f.setMimeType(mimetype); f.setUploadedAt(timeUploaded); f.setFileId(fileId); f.setRid(rId); QCOMPARE(f.url(), url); QCOMPARE(f.fileName(), name); QCOMPARE(f.description(), description); QCOMPARE(f.userId(), userId); QCOMPARE(f.mimeType(), mimetype); QCOMPARE(f.uploadedAt(), timeUploaded); QCOMPARE(f.fileId(), fileId); QCOMPARE(f.rid(), rId); } void FileTest::shouldCopyValue() { File f; const QString url = QStringLiteral("foo"); const QString name = QStringLiteral("bla"); const QString description = QStringLiteral("des"); const QString userId = QStringLiteral("ble"); const QString mimetype = QStringLiteral("ble1"); const QString fileId = QStringLiteral("blabla"); const QString rId = QStringLiteral("blabla22"); const qint64 timeUploaded = 55; f.setUrl(url); f.setFileName(name); f.setDescription(description); f.setUserId(userId); f.setMimeType(mimetype); f.setUploadedAt(timeUploaded); f.setFileId(fileId); f.setRid(rId); File f2 = f; QCOMPARE(f2, f); } void FileTest::shouldParseFile_data() { QTest::addColumn("fileName"); QTest::addColumn("expectedFile"); QTest::addColumn("usingRestApi"); { File expected; expected.setFileName(QStringLiteral("191135.jpg")); expected.setUrl(QStringLiteral("/ufs/FileSystem:Uploads/ybWLKB4FpCkzQXsa/191135.jpg")); expected.setUserId(QStringLiteral("aX7va58FuNuq4bcti")); expected.setDescription(QString()); expected.setMimeType(QStringLiteral("image/jpeg")); expected.setUploadedAt(1507828418338); expected.setFileId(QStringLiteral("ybWLKB4FepCkzQXsa")); expected.setRid(QStringLiteral("GENERAL")); QTest::newRow("roomfile1") << QStringLiteral("roomfile1") << expected << false; } { //RestAPI File expected; expected.setFileName(QStringLiteral("Clipboard - February 7, 2018 8:59 AM")); expected.setUrl(QStringLiteral("/ufs/FileSystem:Uploads/AoqRSa6GMt3wXCeSo/Clipboard%20-%20February%207,%202018%208:59%20AM")); expected.setUserId(QStringLiteral("vEETYfDxakqpM88Zt")); expected.setDescription(QString()); expected.setMimeType(QStringLiteral("image/png")); expected.setUploadedAt(1517990371911); expected.setFileId(QStringLiteral("AoqRSa6GMt3wXCeSo")); expected.setRid(QStringLiteral("GENERAL")); expected.setUserName(QStringLiteral("bli")); QTest::newRow("roomfile1-restapi") << QStringLiteral("roomfile1") << expected << true; } } void FileTest::shouldParseFile() { QFETCH(QString, fileName); QFETCH(File, expectedFile); QFETCH(bool, usingRestApi); QString originalJsonFile; if (usingRestApi) { originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QStringLiteral("/json/restapi/") + fileName + QStringLiteral(".json"); } else { originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QStringLiteral("/json/") + fileName + QStringLiteral(".json"); } QFile f(originalJsonFile); QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray content = f.readAll(); f.close(); const QJsonDocument doc = QJsonDocument::fromJson(content); const QJsonObject fields = doc.object(); File newFile; newFile.parseFile(fields, usingRestApi); const bool equal = (newFile == expectedFile); if (!equal) { qDebug() << " current value " << newFile; qDebug() << " expected value " << expectedFile; } QVERIFY(equal); } diff --git a/src/apps/qml/ShowFilesInRoomDialog.qml b/src/apps/qml/ShowFilesInRoomDialog.qml index 5e040404..db23ca0e 100644 --- a/src/apps/qml/ShowFilesInRoomDialog.qml +++ b/src/apps/qml/ShowFilesInRoomDialog.qml @@ -1,138 +1,137 @@ /* Copyright (c) 2018-2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import QtQuick.Layouts 1.12 import QtQuick.Controls 2.5 as QQC2 import QtQuick.Window 2.2 import QtQuick 2.9 import KDE.Ruqola.FilesForRoomFilterProxyModel 1.0 import org.kde.kirigami 2.7 as Kirigami import "common" -import "js/message.js" as MessageScript; QQC2.Dialog { id: showFilesInRoomDialog title: i18n("Show Files In Room") signal downloadFile(string file) signal deleteFile(string fileid) property QtObject filesModel x: parent.width / 2 - width / 2 y: parent.height / 2 - height / 2 modal: true standardButtons: QQC2.Dialog.Close function initializeAndOpen() { searchField.text = ""; open(); } ColumnLayout { LineEditWithClearButton { id: searchField placeholderText: i18n("Search File...") Layout.fillWidth: true onTextChanged: { filesModel.setFilterString(text); } } QQC2.Label { text: listview.count === 0 ? i18n("No Attachment found") : i18np("%1 attachment in room", "%1 attachments in room", listview.count) Component.onCompleted: { font.italic = true font.bold = true } } //Add scrollview ListView { id: listview width: 400 height: 200 clip: true // Scrollars QQC2.ScrollIndicator.vertical: QQC2.ScrollIndicator { } QQC2.ScrollIndicator.horizontal: QQC2.ScrollIndicator { } model: filesModel delegate: RowLayout { width: ListView.view.width ColumnLayout { QQC2.Label { text: filename wrapMode: QQC2.Label.Wrap } QQC2.Label { text: (description !== "" ? description : "") visible: description !== "" wrapMode: QQC2.Label.Wrap Component.onCompleted: { font.italic = true } } QQC2.Label { text: username wrapMode: QQC2.Label.NoWrap color: Kirigami.Theme.disabledTextColor Component.onCompleted: { font.italic = true } } QQC2.Label { - text: MessageScript.displayDateTime(timestamp) + text: timestamp wrapMode: QQC2.Label.NoWrap color: Kirigami.Theme.disabledTextColor Component.onCompleted: { font.italic = true } } } Item { Layout.fillWidth: true } DownloadButton { onDownloadButtonClicked: { showFilesInRoomDialog.downloadFile(url) } } DeleteButton { visible: canbedeleted onDeleteButtonClicked: { deleteFileAttachmentDialog.fileId = fileid; deleteFileAttachmentDialog.open(); } } } } } DeleteFileAttachmentDialog { id: deleteFileAttachmentDialog onDeleteAttachment: { showFilesInRoomDialog.deleteFile(fileId) } } } diff --git a/src/ruqolacore/file.cpp b/src/ruqolacore/file.cpp index 2e8a6dca..b1ef04d2 100644 --- a/src/ruqolacore/file.cpp +++ b/src/ruqolacore/file.cpp @@ -1,177 +1,184 @@ /* Copyright (c) 2018-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "file.h" #include "utils.h" +#include File::File() { } void File::parseFile(const QJsonObject &object, bool restApi) { const QJsonObject fields = restApi ? object : object.value(QLatin1String("fields")).toObject(); setUserId(fields.value(QLatin1String("userId")).toString()); setFileName(fields.value(QLatin1String("name")).toString()); setMimeType(fields.value(QLatin1String("type")).toString()); setUrl(fields.value(QLatin1String("url")).toString()); setRid(fields.value(QLatin1String("rid")).toString()); if (restApi) { setUploadedAt(Utils::parseIsoDate(QStringLiteral("uploadedAt"), fields)); } else { setUploadedAt(Utils::parseDate(QStringLiteral("uploadedAt"), fields)); } const QJsonObject user = object.value(QLatin1String("user")).toObject(); setUserName(user.value(QLatin1String("username")).toString()); setFileId(restApi ? object.value(QLatin1String("_id")).toString() : object.value(QLatin1String("id")).toString()); } QString File::fileName() const { return mFileName; } void File::setFileName(const QString &name) { mFileName = name; } QString File::description() const { return mDescription; } void File::setDescription(const QString &description) { mDescription = description; } bool File::operator ==(const File &other) const { return (description() == other.description()) && (fileName() == other.fileName()) && (url() == other.url()) && (userId() == other.userId()) && (mimeType() == other.mimeType()) && (uploadedAt() == other.uploadedAt()) && (fileId() == other.fileId()) && (rid() == other.rid()) && (userName() == other.userName()); } File &File::operator=(const File &other) { mFileName = other.fileName(); mDescription = other.description(); mUrl = other.url(); mUserId = other.userId(); mMimeType = other.mimeType(); mUploadedAt = other.uploadedAt(); mFileId = other.fileId(); mRid = other.rid(); mUserName = other.userName(); return *this; } QString File::userId() const { return mUserId; } void File::setUserId(const QString &userId) { mUserId = userId; } QString File::url() const { return mUrl; } void File::setUrl(const QString &url) { mUrl = url; } QString File::mimeType() const { return mMimeType; } void File::setMimeType(const QString &mimeType) { mMimeType = mimeType; } qint64 File::uploadedAt() const { return mUploadedAt; } void File::setUploadedAt(const qint64 &uploadedAt) { mUploadedAt = uploadedAt; + mUploadedDateTimeStr = QDateTime::fromMSecsSinceEpoch(mUploadedAt).toString(); } QString File::fileId() const { return mFileId; } void File::setFileId(const QString &fileId) { mFileId = fileId; } QString File::rid() const { return mRid; } void File::setRid(const QString &rid) { mRid = rid; } QString File::userName() const { return mUserName; } void File::setUserName(const QString &userName) { mUserName = userName; } +QString File::uploadedDateTimeStr() const +{ + return mUploadedDateTimeStr; +} + QDebug operator <<(QDebug d, const File &t) { d << "Name : " << t.fileName(); d << "Description: " << t.description(); d << "Url :" << t.url(); d << "UserId: " << t.userId(); d << "Mimetype : "<< t.mimeType(); d << "Uploaded time: " << t.uploadedAt(); d << "File Id: " << t.fileId(); d << "Rid: " << t.rid(); d << "Username: " << t.userName(); return d; } diff --git a/src/ruqolacore/file.h b/src/ruqolacore/file.h index 000207cc..957edfde 100644 --- a/src/ruqolacore/file.h +++ b/src/ruqolacore/file.h @@ -1,82 +1,85 @@ /* Copyright (c) 2018-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef FILE_H #define FILE_H #include "libruqola_private_export.h" #include #include #include class LIBRUQOLACORE_TESTS_EXPORT File { Q_GADGET public: File(); void parseFile(const QJsonObject &json, bool restApi = false); Q_REQUIRED_RESULT QString fileName() const; void setFileName(const QString &fileName); Q_REQUIRED_RESULT QString description() const; void setDescription(const QString &description); Q_REQUIRED_RESULT bool operator ==(const File &other) const; File &operator=(const File &other); Q_REQUIRED_RESULT QString userId() const; void setUserId(const QString &userId); Q_REQUIRED_RESULT QString url() const; void setUrl(const QString &url); Q_REQUIRED_RESULT QString mimeType() const; void setMimeType(const QString &mimeType); Q_REQUIRED_RESULT qint64 uploadedAt() const; void setUploadedAt(const qint64 &uploadedAt); Q_REQUIRED_RESULT QString fileId() const; void setFileId(const QString &fileId); Q_REQUIRED_RESULT QString rid() const; void setRid(const QString &rid); Q_REQUIRED_RESULT QString userName() const; void setUserName(const QString &userName); + Q_REQUIRED_RESULT QString uploadedDateTimeStr() const; + private: QString mFileName; QString mDescription; QString mUserId; QString mUrl; QString mMimeType; QString mFileId; QString mRid; QString mUserName; + QString mUploadedDateTimeStr; qint64 mUploadedAt = -1; }; Q_DECLARE_METATYPE(File) Q_DECLARE_TYPEINFO(File, Q_MOVABLE_TYPE); LIBRUQOLACORE_EXPORT QDebug operator <<(QDebug d, const File &t); #endif // FILES_H diff --git a/src/ruqolacore/model/filesforroommodel.cpp b/src/ruqolacore/model/filesforroommodel.cpp index 2496b219..afcca53a 100644 --- a/src/ruqolacore/model/filesforroommodel.cpp +++ b/src/ruqolacore/model/filesforroommodel.cpp @@ -1,98 +1,98 @@ /* Copyright (c) 2018-2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "filesforroommodel.h" #include "rocketchataccount.h" #include "ruqola_debug.h" FilesForRoomModel::FilesForRoomModel(RocketChatAccount *account, QObject *parent) : QAbstractListModel(parent) , mRochetChantAccount(account) { } FilesForRoomModel::~FilesForRoomModel() { } void FilesForRoomModel::setFiles(const QVector &files) { if (rowCount() != 0) { beginRemoveRows(QModelIndex(), 0, mFiles.count() - 1); mFiles.clear(); endRemoveRows(); } if (!files.isEmpty()) { beginInsertRows(QModelIndex(), 0, files.count() - 1); mFiles = files; endInsertRows(); } } int FilesForRoomModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return mFiles.count(); } QVariant FilesForRoomModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= mFiles.count()) { return QVariant(); } const File file = mFiles.at(index.row()); switch (role) { case FileName: return file.fileName(); case UserId: return file.userId(); case MimeType: return file.mimeType(); case Url: return file.url(); case Description: return file.description(); case CanBeDeleted: return mRochetChantAccount->userID() == file.userId(); case FileId: return file.fileId(); case TimeStamp: - return file.uploadedAt(); + return file.uploadedDateTimeStr(); case UserName: return file.userName(); } return {}; } QHash FilesForRoomModel::roleNames() const { QHash roles; roles[FileName] = QByteArrayLiteral("filename"); roles[UserId] = QByteArrayLiteral("userid"); roles[MimeType] = QByteArrayLiteral("mimetype"); roles[Url] = QByteArrayLiteral("url"); roles[Description] = QByteArrayLiteral("description"); roles[CanBeDeleted] = QByteArrayLiteral("canbedeleted"); roles[FileId] = QByteArrayLiteral("fileid"); roles[TimeStamp] = QByteArrayLiteral("timestamp"); roles[UserName] = QByteArrayLiteral("username"); return roles; }