Warning, /network/neochat/src/qml/FileDelegate.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2018-2019 Black Hat <bhat@encom.eu.org>
0002 // SPDX-License-Identifier: GPL-3.0-only
0003 
0004 import QtQuick
0005 import QtQuick.Controls as QQC2
0006 import QtQuick.Layouts
0007 import Qt.labs.platform
0008 
0009 import org.kde.coreaddons
0010 import org.kde.kirigami as Kirigami
0011 
0012 import org.kde.neochat
0013 import org.kde.neochat.config
0014 
0015 /**
0016  * @brief A timeline delegate for an file message.
0017  *
0018  * @inherit MessageDelegate
0019  */
0020 MessageDelegate {
0021     id: root
0022 
0023     /**
0024      * @brief The media info for the event.
0025      *
0026      * This should consist of the following:
0027      *  - source - The mxc URL for the media.
0028      *  - mimeType - The MIME type of the media.
0029      *  - mimeIcon - The MIME icon name.
0030      *  - size - The file size in bytes.
0031      */
0032     required property var mediaInfo
0033 
0034     /**
0035      * @brief Whether the media has been downloaded.
0036      */
0037     readonly property bool downloaded: root.progressInfo && root.progressInfo.completed
0038 
0039     /**
0040      * @brief Whether the file should be automatically opened when downloaded.
0041      */
0042     property bool autoOpenFile: false
0043 
0044     onDownloadedChanged: if (autoOpenFile) {
0045         openSavedFile();
0046     }
0047 
0048     onOpenContextMenu: RoomManager.viewEventMenu(eventId, author, delegateType, plainText, "", "", mediaInfo.mimeType, progressInfo)
0049 
0050     function saveFileAs() {
0051         const dialog = fileDialog.createObject(QQC2.ApplicationWindow.overlay)
0052         dialog.open()
0053         dialog.currentFile = dialog.folder + "/" + currentRoom.fileNameToDownload(root.eventId)
0054     }
0055 
0056     function openSavedFile() {
0057         UrlHelper.openUrl(root.progressInfo.localPath);
0058     }
0059 
0060     bubbleContent: RowLayout {
0061         spacing: Kirigami.Units.largeSpacing
0062 
0063         states: [
0064             State {
0065                 name: "downloadedInstant"
0066                 when: root.progressInfo.completed && autoOpenFile
0067 
0068                 PropertyChanges {
0069                     target: openButton
0070                     icon.name: "document-open"
0071                     onClicked: openSavedFile()
0072                 }
0073 
0074                 PropertyChanges {
0075                     target: downloadButton
0076                     icon.name: "download"
0077                     QQC2.ToolTip.text: i18nc("tooltip for a button on a message; offers ability to download its file", "Download")
0078                     onClicked: saveFileAs()
0079                 }
0080             },
0081             State {
0082                 name: "downloaded"
0083                 when: root.progressInfo.completed && !autoOpenFile
0084 
0085                 PropertyChanges {
0086                     target: openButton
0087                     visible: false
0088                 }
0089 
0090                 PropertyChanges {
0091                     target: downloadButton
0092                     icon.name: "document-open"
0093                     QQC2.ToolTip.text: i18nc("tooltip for a button on a message; offers ability to open its downloaded file with an appropriate application", "Open File")
0094                     onClicked: openSavedFile()
0095                 }
0096             },
0097             State {
0098                 name: "downloading"
0099                 when: root.progressInfo.active
0100 
0101                 PropertyChanges {
0102                     target: openButton
0103                     visible: false
0104                 }
0105 
0106                 PropertyChanges {
0107                     target: sizeLabel
0108                     text: i18nc("file download progress", "%1 / %2", Format.formatByteSize(root.progressInfo.progress), Format.formatByteSize(root.progressInfo.total))
0109                 }
0110                 PropertyChanges {
0111                     target: downloadButton
0112                     icon.name: "media-playback-stop"
0113                     QQC2.ToolTip.text: i18nc("tooltip for a button on a message; stops downloading the message's file", "Stop Download")
0114                     onClicked: currentRoom.cancelFileTransfer(root.eventId)
0115                 }
0116             },
0117             State {
0118                 name: "raw"
0119                 when: true
0120 
0121                 PropertyChanges {
0122                     target: downloadButton
0123                     onClicked: root.saveFileAs()
0124                 }
0125             }
0126         ]
0127 
0128         Kirigami.Icon {
0129             source: root.mediaInfo.mimeIcon
0130             fallback: "unknown"
0131         }
0132 
0133         ColumnLayout {
0134             spacing: 0
0135             QQC2.Label {
0136                 Layout.fillWidth: true
0137                 text: root.display
0138                 wrapMode: Text.Wrap
0139                 elide: Text.ElideRight
0140             }
0141             QQC2.Label {
0142                 id: sizeLabel
0143                 Layout.fillWidth: true
0144                 text: Format.formatByteSize(root.mediaInfo.size)
0145                 opacity: 0.7
0146                 elide: Text.ElideRight
0147                 maximumLineCount: 1
0148             }
0149         }
0150 
0151         QQC2.Button {
0152             id: openButton
0153             icon.name: "document-open"
0154             onClicked: {
0155                 autoOpenFile = true;
0156                 currentRoom.downloadTempFile(root.eventId);
0157             }
0158 
0159             QQC2.ToolTip.text: i18nc("tooltip for a button on a message; offers ability to open its downloaded file with an appropriate application", "Open File")
0160             QQC2.ToolTip.visible: hovered
0161             QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
0162         }
0163 
0164         QQC2.Button {
0165             id: downloadButton
0166             icon.name: "download"
0167 
0168             QQC2.ToolTip.text: i18nc("tooltip for a button on a message; offers ability to download its file", "Download")
0169             QQC2.ToolTip.visible: hovered
0170             QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
0171         }
0172 
0173         Component {
0174             id: fileDialog
0175 
0176             FileDialog {
0177                 fileMode: FileDialog.SaveFile
0178                 folder: Config.lastSaveDirectory.length > 0 ? Config.lastSaveDirectory : StandardPaths.writableLocation(StandardPaths.DownloadLocation)
0179                 onAccepted: {
0180                     Config.lastSaveDirectory = folder
0181                     Config.save()
0182                     if (autoOpenFile) {
0183                         UrlHelper.copyTo(root.progressInfo.localPath, file)
0184                     } else {
0185                         currentRoom.download(root.eventId, file);
0186                     }
0187                 }
0188             }
0189         }
0190     }
0191 }