findthisdevice: Fix getting defaultSounds in the QML app

Now use the same code as the C++ kcm
This commit is contained in:
Carl Schwan 2024-10-05 20:46:54 +02:00 committed by Albert Vaca Cintora
parent b40f2a785f
commit 20bba74b0b
7 changed files with 106 additions and 50 deletions

View file

@ -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 target_link_libraries(kdeconnect_findthisdevice
kdeconnectcore kdeconnectcore
Qt::Core Qt::Core
Qt::Multimedia Qt::Multimedia
Qt::DBus Qt::DBus
Qt::Qml
) )
if (NOT WIN32) if (NOT WIN32)
@ -13,12 +31,15 @@ if (NOT WIN32)
) )
endif() 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) ki18n_wrap_ui(kdeconnect_findthisdevice_config findthisdevice_config.ui)
target_link_libraries(kdeconnect_findthisdevice_config target_link_libraries(kdeconnect_findthisdevice_config
kdeconnectpluginkcm kdeconnectpluginkcm
Qt::Multimedia Qt::Multimedia
Qt::Qml
KF6::I18n KF6::I18n
KF6::CoreAddons KF6::CoreAddons
KF6::ConfigWidgets KF6::ConfigWidgets

View file

@ -5,6 +5,7 @@
*/ */
#include "findthisdevice_config.h" #include "findthisdevice_config.h"
#include "findthisdevicehelper.h"
#include "findthisdeviceplugin.h" #include "findthisdeviceplugin.h"
// KF // KF
@ -40,7 +41,7 @@ void FindThisDeviceConfig::defaults()
{ {
KCModule::defaults(); KCModule::defaults();
m_ui.soundFileRequester->setText(defaultSound()); m_ui.soundFileRequester->setText(FindThisDeviceHelper::defaultSound());
markAsChanged(); markAsChanged();
} }
@ -48,7 +49,7 @@ void FindThisDeviceConfig::load()
{ {
KCModule::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); m_ui.soundFileRequester->setText(ringTone);
} }

View file

@ -0,0 +1,50 @@
/**
* SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
* SPDX-FileCopyrightText: 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
* SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu>
*
* 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 <Windows.h>
#define INFO_BUFFER_SIZE 32767
#else
#include <QFile>
#include <QStandardPaths>
#endif
#include <QDebug>
#include <QUrl>
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();
}

View file

@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#pragma once
#include <QObject>
#include <qqmlregistration.h>
class FindThisDeviceHelper : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
public:
static Q_INVOKABLE QString defaultSound();
};

View file

@ -22,11 +22,13 @@
#include <QAudioOutput> #include <QAudioOutput>
#include "findthisdevicehelper.h"
K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json") K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json")
void FindThisDevicePlugin::receivePacket(const NetworkPacket & /*np*/) 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); const QUrl soundURL = QUrl::fromLocalFile(soundFile);
if (soundURL.isEmpty()) { if (soundURL.isEmpty()) {

View file

@ -9,16 +9,6 @@
#include <core/kdeconnectplugin.h> #include <core/kdeconnectplugin.h>
#ifdef Q_OS_WIN
#include <QDebug>
#include <Windows.h>
#define INFO_BUFFER_SIZE 32767
#else
#include <QFile>
#include <QStandardPaths>
#include <QUrl>
#endif
#define PACKET_TYPE_FINDMYPHONE_REQUEST QStringLiteral("kdeconnect.findmyphone.request") #define PACKET_TYPE_FINDMYPHONE_REQUEST QStringLiteral("kdeconnect.findmyphone.request")
class FindThisDevicePlugin : public KdeConnectPlugin class FindThisDevicePlugin : public KdeConnectPlugin
@ -32,33 +22,3 @@ public:
QString dbusPath() const override; QString dbusPath() const override;
void receivePacket(const NetworkPacket &np) 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();
}

View file

@ -7,9 +7,11 @@
import QtQuick 2.15 import QtQuick 2.15
import QtQuick.Controls 2.15 as QQC2 import QtQuick.Controls 2.15 as QQC2
import QtQuick.Layouts 1.15 import QtQuick.Layouts 1.15
import QtQuick.Dialogs as Dialogs
import QtMultimedia
import org.kde.kirigami 2.20 as Kirigami import org.kde.kirigami 2.20 as Kirigami
import Qt.labs.platform 1.1
import org.kde.kdeconnect 1.0 import org.kde.kdeconnect 1.0
import org.kde.kdeconnect.private.findthisdevice as FindThisDevice
Kirigami.ScrollablePage { Kirigami.ScrollablePage {
id: root id: root
@ -23,14 +25,12 @@ Kirigami.ScrollablePage {
} }
Kirigami.FormLayout { Kirigami.FormLayout {
FileDialog { Dialogs.FileDialog {
id: fileDialog id: fileDialog
currentFile: path.text currentFile: path.text
onAccepted: { onAccepted: {
path.text = currentFile.toString().replace("file://", "") path.text = currentFile.toString().replace("file://", "")
} }
}
KdeConnectPluginConfig { KdeConnectPluginConfig {
id: config id: config
@ -38,7 +38,7 @@ Kirigami.ScrollablePage {
pluginName: "kdeconnect_findthisdevice" pluginName: "kdeconnect_findthisdevice"
onConfigChanged: { 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 { QQC2.Button {
text: i18nc("@action:button", "Choose file")
display: QQC2.Button.IconOnly
icon.name: "document-open" icon.name: "document-open"
onClicked: fileDialog.open() onClicked: fileDialog.open()
} }