re-write findthisdevice plugin + add support for Windows

This commit is contained in:
Piyush Aggarwal 2019-09-19 22:45:39 +00:00
parent 7eeed141f0
commit c1f8d689db
4 changed files with 60 additions and 75 deletions

View file

@ -34,7 +34,7 @@ if(NOT SAILFISHOS)
add_subdirectory(pausemusic)
endif()
if(Qt5Multimedia_FOUND AND KF5PulseAudioQt_FOUND)
if(Qt5Multimedia_FOUND AND (KF5PulseAudioQt_FOUND OR WIN32))
add_subdirectory(findthisdevice)
endif()

View file

@ -88,25 +88,9 @@ void FindThisDeviceConfig::save()
void FindThisDeviceConfig::playSound()
{
const QString soundFilename = m_ui->soundFileRequester->text();
const QString soundFile = m_ui->soundFileRequester->text();
QUrl soundURL;
const auto dataLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
for (const QString &dataLocation : dataLocations) {
soundURL = QUrl::fromUserInput(soundFilename,
dataLocation + QStringLiteral("/sounds"),
QUrl::AssumeLocalFile);
if (soundURL.isLocalFile()) {
if (QFile::exists(soundURL.toLocalFile())) {
break;
}
} else {
if (soundURL.isValid()) {
break;
}
}
soundURL.clear();
}
QUrl soundURL = QUrl(soundFile);
QMediaPlayer* player = new QMediaPlayer;
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL);

View file

@ -1,5 +1,6 @@
/**
* Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
* Copyright 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -27,17 +28,13 @@
#include <PulseAudioQt/Context>
#include <PulseAudioQt/Sink>
#endif
// Qt
#include <QDBusConnection>
#include <QStandardPaths>
#include <QFile>
#include <QUrl>
#include <QMediaPlayer>
K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json")
Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_FINDTHISDEVICE, "kdeconnect.plugin.findthisdevice")
FindThisDevicePlugin::FindThisDevicePlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
{
@ -45,48 +42,14 @@ FindThisDevicePlugin::FindThisDevicePlugin(QObject* parent, const QVariantList&
FindThisDevicePlugin::~FindThisDevicePlugin() = default;
void FindThisDevicePlugin::connected()
{
}
bool FindThisDevicePlugin::receivePacket(const NetworkPacket& np)
{
Q_UNUSED(np);
const QString soundFilename = config()->get<QString>(QStringLiteral("ringtone"), defaultSound());
QUrl soundURL;
#ifdef Q_OS_WIN
QString winDirPath = qEnvironmentVariable("WINDIR") + QStringLiteral("/media");
if (!winDirPath.isEmpty()) {
soundURL = QUrl::fromUserInput(soundFilename,
winDirPath,
QUrl::AssumeLocalFile);
}
else {
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sounds, system doesn't know WINDIR : " << soundFilename;
}
#else
const auto dataLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
for (const QString &dataLocation : dataLocations) {
soundURL = QUrl::fromUserInput(soundFilename,
dataLocation + QStringLiteral("/sounds"),
QUrl::AssumeLocalFile);
if (soundURL.isLocalFile()) {
if (QFile::exists(soundURL.toLocalFile())) {
break;
}
} else {
if (soundURL.isValid()) {
break;
}
}
soundURL.clear();
}
#endif
const QString soundFile = config()->get<QString>(QStringLiteral("ringtone"), defaultSound());
const QUrl soundURL = QUrl(soundFile);
if (soundURL.isEmpty()) {
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sounds, could not find ring tone" << soundFilename;
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified.";
return true;
}
@ -94,27 +57,25 @@ bool FindThisDevicePlugin::receivePacket(const NetworkPacket& np)
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL);
player->setVolume(100);
player->play();
#ifndef Q_OS_WIN
const auto sinks = PulseAudioQt::Context::instance()->sinks();
QVector<PulseAudioQt::Sink*> mutedSinks;
for (auto sink : sinks) {
if (sink->isMuted()) {
sink->setMuted(false);
mutedSinks.append(sink);
}
}
connect(player, &QMediaPlayer::stateChanged, this, [player, mutedSinks]{
player->deleteLater();
for (auto sink : qAsConst(mutedSinks)) {
sink->setMuted(true);
}
});
#endif
player->play();
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
// TODO: ensure to use built-in loudspeakers
return true;

View file

@ -1,5 +1,6 @@
/**
* Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
* Copyright 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -22,19 +23,24 @@
#define FINDTHISDEVICEPLUGIN_H
#include <core/kdeconnectplugin.h>
#ifdef Q_OS_WIN
#include <Windows.h>
#define INFO_BUFFER_SIZE 32767
#else
#include <QStandardPaths>
#include <QFile>
#include <QUrl>
#endif
// Qt
#include <QLoggingCategory>
#define PACKET_TYPE_FINDMYPHONE_REQUEST QStringLiteral("kdeconnect.findmyphone.request")
Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_PLUGIN_FINDTHISDEVICE)
inline QString defaultSound(){
#ifdef Q_OS_WIN
return QStringLiteral("Ring01.wav");
#else
return QStringLiteral("Oxygen-Im-Phone-Ring.ogg");
#endif
static const QLoggingCategory &KDECONNECT_PLUGIN_FINDTHISDEVICE()
{
static const QLoggingCategory category("kdeconnect.plugin.findthisdevice");
return category;
}
class FindThisDevicePlugin
@ -47,9 +53,43 @@ public:
explicit FindThisDevicePlugin(QObject* parent, const QVariantList& args);
~FindThisDevicePlugin() override;
void connected() override {};
QString dbusPath() const override;
void connected() override;
bool 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)) {
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "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() && QFile::exists(soundURL.toLocalFile())) || soundURL.isValid()) {
break;
}
}
#endif
if (soundURL.isEmpty()) {
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Could not find default ring tone.";
}
return soundURL.toString();
}
#endif //FINDTHISDEVICEPLUGIN_H