mirror of
https://invent.kde.org/network/kdeconnect-kde.git
synced 2025-12-12 20:35:55 +01:00
This also adds a helper function to open the save dialog from QML, and introduces a helper C++ singleton for attachments, as there is no way, as far as I know, to save a file from QML alone. Note that this isn't really "saving" anything, as the file is already on disk in \~/.cache, but most users wouldn't know how to retrieve an attachment from there, so "saving" seems to be a more appropriate terminology. EDIT: @sredman suggested "downloading", which makes sense to me, since from the point of view of the user, it is bringing a file from their phone/remote device to their local one! I also added a small patch to fix the background colour of the attachment viewer being white in dark mode, which I did while working on this. LMK if I should make a separate MR for this!
45 lines
1.6 KiB
C++
45 lines
1.6 KiB
C++
/**
|
|
* SPDX-FileCopyrightText: 2025 Yuki Joou <yukijoou@kemonomimi.gay>
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
|
*/
|
|
|
|
#include "attachmentshelper.h"
|
|
|
|
#include <QFile>
|
|
#include <QFileDialog>
|
|
#include <QMimeDatabase>
|
|
#include <QRegularExpression>
|
|
#include <QStandardPaths>
|
|
#include <QString>
|
|
|
|
#include <KLocalizedString>
|
|
|
|
// For ""_s
|
|
using namespace Qt::Literals::StringLiterals;
|
|
|
|
QObject *AttachmentsHelper::singletonProvider(QQmlEngine * /*engine*/, QJSEngine * /*scriptEngine*/)
|
|
{
|
|
return new AttachmentsHelper();
|
|
}
|
|
|
|
void AttachmentsHelper::saveAttachment(const QString &mimetypeString, const QUrl &source)
|
|
{
|
|
// TODO: Currently, this is always using the system's download folder.
|
|
// As the `share` plugin allows users to set a prefered destination for files,
|
|
// it'd be nice to respect it here.
|
|
auto const downloads = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
|
auto const filename = source.fileName();
|
|
auto const mimetype = QMimeDatabase().mimeTypeForName(mimetypeString);
|
|
|
|
QString outFile = QFileDialog::getSaveFileName(nullptr,
|
|
i18nc("Caption/title for attachment download/save window", "Download attachment"),
|
|
downloads + u"/"_s + filename,
|
|
mimetype.filterString());
|
|
if (outFile.isNull() || outFile.isEmpty()) {
|
|
// No file was selected, aborting
|
|
return;
|
|
}
|
|
QFile::copy(source.toLocalFile(), outFile);
|
|
}
|