From ecf4c859e8c6ade48dde8c16440bc6d26c87fab5 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Fri, 28 Aug 2020 16:18:33 +0200 Subject: [PATCH] Add support for documents --- harbour-fernschreiber.pro | 1 + qml/components/AppNotificationItem.qml | 2 +- qml/components/DocumentPreview.qml | 94 ++++++++++++++++++++++++ qml/js/functions.js | 9 ++- qml/pages/ChatPage.qml | 6 ++ src/tdlibwrapper.cpp | 11 ++- src/tdlibwrapper.h | 2 +- translations/harbour-fernschreiber-de.ts | 23 ++++++ translations/harbour-fernschreiber.ts | 23 ++++++ 9 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 qml/components/DocumentPreview.qml diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index c27565a..2470805 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -24,6 +24,7 @@ SOURCES += src/harbour-fernschreiber.cpp \ DISTFILES += qml/harbour-fernschreiber.qml \ qml/components/AudioPreview.qml \ + qml/components/DocumentPreview.qml \ qml/components/ImagePreview.qml \ qml/js/functions.js \ qml/pages/ChatPage.qml \ diff --git a/qml/components/AppNotificationItem.qml b/qml/components/AppNotificationItem.qml index 92a518f..63ec600 100644 --- a/qml/components/AppNotificationItem.qml +++ b/qml/components/AppNotificationItem.qml @@ -66,7 +66,7 @@ Item { MouseArea { anchors.fill: parent onClicked: { - tdLibWrapper.handleAdditionalInformation(notificationItem.additionalInformation); + tdLibWrapper.openFileOnDevice(notificationItem.additionalInformation); } visible: additionalInformation ? true : false } diff --git a/qml/components/DocumentPreview.qml b/qml/components/DocumentPreview.qml new file mode 100644 index 0000000..e530d0a --- /dev/null +++ b/qml/components/DocumentPreview.qml @@ -0,0 +1,94 @@ +/* + Copyright (C) 2020 Sebastian J. Wolf + + This file is part of Fernschreiber. + + Fernschreiber 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 3 of the License, or + (at your option) any later version. + + Fernschreiber 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 Fernschreiber. If not, see . +*/ +import QtQuick 2.5 +import QtGraphicalEffects 1.0 +import Sailfish.Silica 1.0 + +Item { + + id: documentPreviewItem + width: parent.width + height: Theme.itemSizeLarge + + property variant documentData; + + Component.onCompleted: { + updateDocument(); + } + + function updateDocument() { + if (typeof documentData === "object") { + if (documentData.document.local.is_downloading_completed) { + downloadDocumentButton.visible = false; + openDocumentButton.visible = true; + } else { + openDocumentButton.visible = false; + downloadDocumentButton.visible = true; + } + } + } + + Connections { + target: tdLibWrapper + onFileUpdated: { + if (typeof documentData !== "undefined" && fileId === documentData.document.id) { + if (fileInformation.local.is_downloading_completed) { + downloadBusyIndicator.running = false; + documentData.document = fileInformation; + downloadDocumentButton.visible = false; + openDocumentButton.visible = true; + tdLibWrapper.openFileOnDevice(documentData.document.local.path); + } + } + } + } + + Button { + id: downloadDocumentButton + preferredWidth: Theme.buttonWidthMedium + anchors.centerIn: parent + text: qsTr("Download Document") + visible: false + onClicked: { + downloadDocumentButton.visible = false; + downloadBusyIndicator.running = true; + tdLibWrapper.downloadFile(documentData.document.id); + } + } + + BusyIndicator { + id: downloadBusyIndicator + running: false + size: BusyIndicatorSize.Medium + visible: running + anchors.centerIn: parent + } + + Button { + id: openDocumentButton + preferredWidth: Theme.buttonWidthMedium + anchors.centerIn: parent + text: qsTr("Open Document") + visible: false + onClicked: { + tdLibWrapper.openFileOnDevice(documentData.document.local.path); + } + } + +} diff --git a/qml/js/functions.js b/qml/js/functions.js index c517db9..235462f 100644 --- a/qml/js/functions.js +++ b/qml/js/functions.js @@ -69,6 +69,13 @@ function getMessageText(message, simple) { return simple ? qsTr("shared a voice note") : ""; } } + if (message.content['@type'] === 'messageDocument') { + if (message.content.document.file_name !== "") { + return simple ? qsTr("Document: %1").arg(message.content.document.file_name) : message.content.document.file_name + } else { + return simple ? qsTr("shared a document") : ""; + } + } if (message.content['@type'] === 'messageLocation') { return qsTr("shared their location"); } @@ -84,7 +91,7 @@ function getMessageText(message, simple) { if (message.content['@type'] === 'messageChatDeleteMember') { return qsTr("left this chat."); } - return "?"; + return qsTr("Unsupported message: %1").arg(message.content['@type'].substring(7)); } function getDateTimeElapsed(timestamp) { diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index 5eb1518..3137cbc 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -458,6 +458,12 @@ Page { onScreen: chatPage.status === PageStatus.Active } + DocumentPreview { + id: messageDocumentPreview + documentData: ( display.content['@type'] === "messageDocument" ) ? display.content.document : "" + visible: display.content['@type'] === "messageDocument" + } + Timer { id: messageDateUpdater interval: 60000 diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 96dafaf..1c5f5a6 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -262,17 +262,16 @@ void TDLibWrapper::copyFileToDownloads(const QString &filePath) } } -void TDLibWrapper::handleAdditionalInformation(const QString &additionalInformation) +void TDLibWrapper::openFileOnDevice(const QString &filePath) { - qDebug() << "[TDLibWrapper] Additional information: " << additionalInformation; - // For now only used to open downloaded files... + qDebug() << "[TDLibWrapper] Open file on device: " << filePath; QStringList argumentsList; - argumentsList.append(additionalInformation); + argumentsList.append(filePath); bool successfullyStarted = QProcess::startDetached("xdg-open", argumentsList); if (successfullyStarted) { - qDebug() << "Successfully opened file " << additionalInformation; + qDebug() << "Successfully opened file " << filePath; } else { - qDebug() << "Error opening file " << additionalInformation; + qDebug() << "Error opening file " << filePath; } } diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index adcd487..7124590 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -68,7 +68,7 @@ public: Q_INVOKABLE QVariantMap getBasicGroup(const QString &groupId); Q_INVOKABLE QVariantMap getSuperGroup(const QString &groupId); Q_INVOKABLE void copyFileToDownloads(const QString &filePath); - Q_INVOKABLE void handleAdditionalInformation(const QString &additionalInformation); + Q_INVOKABLE void openFileOnDevice(const QString &filePath); Q_INVOKABLE void controlScreenSaver(const bool &enabled); // Direct TDLib functions diff --git a/translations/harbour-fernschreiber-de.ts b/translations/harbour-fernschreiber-de.ts index 333c04a..c7cbb32 100644 --- a/translations/harbour-fernschreiber-de.ts +++ b/translations/harbour-fernschreiber-de.ts @@ -189,6 +189,17 @@ + + DocumentPreview + + Download Document + + + + Open Document + + + ImagePage @@ -363,5 +374,17 @@ shared an animation + + Unsupported message: %1 + + + + Document: %1 + + + + shared a document + + diff --git a/translations/harbour-fernschreiber.ts b/translations/harbour-fernschreiber.ts index 333c04a..c7cbb32 100644 --- a/translations/harbour-fernschreiber.ts +++ b/translations/harbour-fernschreiber.ts @@ -189,6 +189,17 @@ + + DocumentPreview + + Download Document + + + + Open Document + + + ImagePage @@ -363,5 +374,17 @@ shared an animation + + Unsupported message: %1 + + + + Document: %1 + + + + shared a document + +