From 20bba74b0b2819b9ac2e782a547fc83adc883a0a Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Sat, 5 Oct 2024 20:46:54 +0200 Subject: [PATCH] findthisdevice: Fix getting defaultSounds in the QML app Now use the same code as the C++ kcm --- plugins/findthisdevice/CMakeLists.txt | 25 +++++++++- .../findthisdevice/findthisdevice_config.cpp | 5 +- .../findthisdevice/findthisdevicehelper.cpp | 50 +++++++++++++++++++ plugins/findthisdevice/findthisdevicehelper.h | 20 ++++++++ .../findthisdevice/findthisdeviceplugin.cpp | 4 +- plugins/findthisdevice/findthisdeviceplugin.h | 40 --------------- .../kdeconnect_findthisdevice_config.qml | 12 +++-- 7 files changed, 106 insertions(+), 50 deletions(-) create mode 100644 plugins/findthisdevice/findthisdevicehelper.cpp create mode 100644 plugins/findthisdevice/findthisdevicehelper.h diff --git a/plugins/findthisdevice/CMakeLists.txt b/plugins/findthisdevice/CMakeLists.txt index 9d204b2f3..b5fa1abd8 100644 --- a/plugins/findthisdevice/CMakeLists.txt +++ b/plugins/findthisdevice/CMakeLists.txt @@ -1,10 +1,28 @@ -kdeconnect_add_plugin(kdeconnect_findthisdevice SOURCES findthisdeviceplugin.cpp) +# Target kdeconnect_findthisdevice_qmlhelper + +ecm_add_qml_module(kdeconnect_findthisdevice_qmlhelper + URI "org.kde.kdeconnect.private.findthisdevice" + GENERATE_PLUGIN_SOURCE +) + +target_sources(kdeconnect_findthisdevice_qmlhelper PRIVATE findthisdevicehelper.cpp) + +target_link_libraries(kdeconnect_findthisdevice_qmlhelper PRIVATE + Qt::Qml +) + +ecm_finalize_qml_module(kdeconnect_findthisdevice_qmlhelper DESTINATION ${KDE_INSTALL_QMLDIR}) + +# Target kdeconnect_findthisdevice + +kdeconnect_add_plugin(kdeconnect_findthisdevice SOURCES findthisdeviceplugin.cpp findthisdevicehelper.cpp) target_link_libraries(kdeconnect_findthisdevice kdeconnectcore Qt::Core Qt::Multimedia Qt::DBus + Qt::Qml ) if (NOT WIN32) @@ -13,12 +31,15 @@ if (NOT WIN32) ) endif() -kdeconnect_add_kcm(kdeconnect_findthisdevice_config SOURCES findthisdevice_config.cpp) +# Target kdeconnect_findthisdevice_config + +kdeconnect_add_kcm(kdeconnect_findthisdevice_config SOURCES findthisdevice_config.cpp findthisdevicehelper.cpp) ki18n_wrap_ui(kdeconnect_findthisdevice_config findthisdevice_config.ui) target_link_libraries(kdeconnect_findthisdevice_config kdeconnectpluginkcm Qt::Multimedia + Qt::Qml KF6::I18n KF6::CoreAddons KF6::ConfigWidgets diff --git a/plugins/findthisdevice/findthisdevice_config.cpp b/plugins/findthisdevice/findthisdevice_config.cpp index e5d06ef1b..7bdf38988 100644 --- a/plugins/findthisdevice/findthisdevice_config.cpp +++ b/plugins/findthisdevice/findthisdevice_config.cpp @@ -5,6 +5,7 @@ */ #include "findthisdevice_config.h" +#include "findthisdevicehelper.h" #include "findthisdeviceplugin.h" // KF @@ -40,7 +41,7 @@ void FindThisDeviceConfig::defaults() { KCModule::defaults(); - m_ui.soundFileRequester->setText(defaultSound()); + m_ui.soundFileRequester->setText(FindThisDeviceHelper::defaultSound()); markAsChanged(); } @@ -48,7 +49,7 @@ void FindThisDeviceConfig::load() { KCModule::load(); - const QString ringTone = config()->getString(QStringLiteral("ringtone"), defaultSound()); + const QString ringTone = config()->getString(QStringLiteral("ringtone"), FindThisDeviceHelper::defaultSound()); m_ui.soundFileRequester->setText(ringTone); } diff --git a/plugins/findthisdevice/findthisdevicehelper.cpp b/plugins/findthisdevice/findthisdevicehelper.cpp new file mode 100644 index 000000000..7ab74ba28 --- /dev/null +++ b/plugins/findthisdevice/findthisdevicehelper.cpp @@ -0,0 +1,50 @@ +/** + * SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau + * SPDX-FileCopyrightText: 2019 Piyush Aggarwal + * SPDX-FileCopyrightText: 2024 Carl Schwan + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#include "findthisdevicehelper.h" + +#ifdef Q_OS_WIN +#include +#define INFO_BUFFER_SIZE 32767 +#else +#include +#include +#endif + +#include +#include + +QString FindThisDeviceHelper::defaultSound() +{ + QString dirPath; + QUrl soundURL; +#ifdef Q_OS_WIN + wchar_t infoBuf[INFO_BUFFER_SIZE]; + if (!GetWindowsDirectory(infoBuf, INFO_BUFFER_SIZE)) { + qWarning() << "Error with getting the Windows Directory."; + } else { + dirPath = QString::fromStdWString(infoBuf) + QStringLiteral("/media"); + if (!dirPath.isEmpty()) { + soundURL = QUrl::fromUserInput(QStringLiteral("Ring01.wav"), dirPath, QUrl::AssumeLocalFile); + } + } +#else + const QStringList dataLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); + for (const QString &dataLocation : dataLocations) { + dirPath = dataLocation + QStringLiteral("/sounds"); + soundURL = QUrl::fromUserInput(QStringLiteral("Oxygen-Im-Phone-Ring.ogg"), dirPath, QUrl::AssumeLocalFile); + if ((soundURL.isLocalFile() && soundURL.isValid() && QFile::exists(soundURL.toLocalFile()))) { + break; + } + } +#endif + if (soundURL.isEmpty()) { + qWarning() << "Could not find default ring tone."; + } + return soundURL.toLocalFile(); +} diff --git a/plugins/findthisdevice/findthisdevicehelper.h b/plugins/findthisdevice/findthisdevicehelper.h new file mode 100644 index 000000000..c2e1bdb8e --- /dev/null +++ b/plugins/findthisdevice/findthisdevicehelper.h @@ -0,0 +1,20 @@ +/* + * SPDX-FileCopyrightText: 2024 Carl Schwan + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#pragma once + +#include +#include + +class FindThisDeviceHelper : public QObject +{ + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + +public: + static Q_INVOKABLE QString defaultSound(); +}; diff --git a/plugins/findthisdevice/findthisdeviceplugin.cpp b/plugins/findthisdevice/findthisdeviceplugin.cpp index ddce10da1..c9668703d 100644 --- a/plugins/findthisdevice/findthisdeviceplugin.cpp +++ b/plugins/findthisdevice/findthisdeviceplugin.cpp @@ -22,11 +22,13 @@ #include +#include "findthisdevicehelper.h" + K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json") void FindThisDevicePlugin::receivePacket(const NetworkPacket & /*np*/) { - const QString soundFile = config()->getString(QStringLiteral("ringtone"), defaultSound()); + const QString soundFile = config()->getString(QStringLiteral("ringtone"), FindThisDeviceHelper::defaultSound()); const QUrl soundURL = QUrl::fromLocalFile(soundFile); if (soundURL.isEmpty()) { diff --git a/plugins/findthisdevice/findthisdeviceplugin.h b/plugins/findthisdevice/findthisdeviceplugin.h index 34149ddff..5db09dddd 100644 --- a/plugins/findthisdevice/findthisdeviceplugin.h +++ b/plugins/findthisdevice/findthisdeviceplugin.h @@ -9,16 +9,6 @@ #include -#ifdef Q_OS_WIN -#include -#include -#define INFO_BUFFER_SIZE 32767 -#else -#include -#include -#include -#endif - #define PACKET_TYPE_FINDMYPHONE_REQUEST QStringLiteral("kdeconnect.findmyphone.request") class FindThisDevicePlugin : public KdeConnectPlugin @@ -32,33 +22,3 @@ public: QString dbusPath() const override; void receivePacket(const NetworkPacket &np) override; }; - -inline QString defaultSound() -{ - QString dirPath; - QUrl soundURL; -#ifdef Q_OS_WIN - wchar_t infoBuf[INFO_BUFFER_SIZE]; - if (!GetWindowsDirectory(infoBuf, INFO_BUFFER_SIZE)) { - qWarning() << "Error with getting the Windows Directory."; - } else { - dirPath = QString::fromStdWString(infoBuf) + QStringLiteral("/media"); - if (!dirPath.isEmpty()) { - soundURL = QUrl::fromUserInput(QStringLiteral("Ring01.wav"), dirPath, QUrl::AssumeLocalFile); - } - } -#else - const QStringList dataLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); - for (const QString &dataLocation : dataLocations) { - dirPath = dataLocation + QStringLiteral("/sounds"); - soundURL = QUrl::fromUserInput(QStringLiteral("Oxygen-Im-Phone-Ring.ogg"), dirPath, QUrl::AssumeLocalFile); - if ((soundURL.isLocalFile() && soundURL.isValid() && QFile::exists(soundURL.toLocalFile()))) { - break; - } - } -#endif - if (soundURL.isEmpty()) { - qWarning() << "Could not find default ring tone."; - } - return soundURL.toLocalFile(); -} diff --git a/plugins/findthisdevice/kdeconnect_findthisdevice_config.qml b/plugins/findthisdevice/kdeconnect_findthisdevice_config.qml index efc723faf..dfea4685d 100644 --- a/plugins/findthisdevice/kdeconnect_findthisdevice_config.qml +++ b/plugins/findthisdevice/kdeconnect_findthisdevice_config.qml @@ -7,9 +7,11 @@ import QtQuick 2.15 import QtQuick.Controls 2.15 as QQC2 import QtQuick.Layouts 1.15 +import QtQuick.Dialogs as Dialogs +import QtMultimedia import org.kde.kirigami 2.20 as Kirigami -import Qt.labs.platform 1.1 import org.kde.kdeconnect 1.0 +import org.kde.kdeconnect.private.findthisdevice as FindThisDevice Kirigami.ScrollablePage { id: root @@ -23,14 +25,12 @@ Kirigami.ScrollablePage { } Kirigami.FormLayout { - FileDialog { + Dialogs.FileDialog { id: fileDialog currentFile: path.text - onAccepted: { path.text = currentFile.toString().replace("file://", "") } - } KdeConnectPluginConfig { id: config @@ -38,7 +38,7 @@ Kirigami.ScrollablePage { pluginName: "kdeconnect_findthisdevice" onConfigChanged: { - path.text = getString("ringtone", StandardPaths.writableLocation(StandardPaths.DownloadsLocation).toString().replace("file://", "")) + path.text = getString("ringtone", FindThisDevice.FindThisDeviceHelper.defaultSound()) } } @@ -50,6 +50,8 @@ Kirigami.ScrollablePage { } QQC2.Button { + text: i18nc("@action:button", "Choose file") + display: QQC2.Button.IconOnly icon.name: "document-open" onClicked: fileDialog.open() }