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) add_subdirectory(pausemusic)
endif() endif()
if(Qt5Multimedia_FOUND AND KF5PulseAudioQt_FOUND) if(Qt5Multimedia_FOUND AND (KF5PulseAudioQt_FOUND OR WIN32))
add_subdirectory(findthisdevice) add_subdirectory(findthisdevice)
endif() endif()

View file

@ -88,25 +88,9 @@ void FindThisDeviceConfig::save()
void FindThisDeviceConfig::playSound() void FindThisDeviceConfig::playSound()
{ {
const QString soundFilename = m_ui->soundFileRequester->text(); const QString soundFile = m_ui->soundFileRequester->text();
QUrl soundURL; QUrl soundURL = QUrl(soundFile);
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();
}
QMediaPlayer* player = new QMediaPlayer; QMediaPlayer* player = new QMediaPlayer;
player->setAudioRole(QAudio::Role(QAudio::NotificationRole)); player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL); player->setMedia(soundURL);

View file

@ -1,5 +1,6 @@
/** /**
* Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org> * 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 * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as * modify it under the terms of the GNU General Public License as
@ -27,17 +28,13 @@
#include <PulseAudioQt/Context> #include <PulseAudioQt/Context>
#include <PulseAudioQt/Sink> #include <PulseAudioQt/Sink>
#endif #endif
// Qt // Qt
#include <QDBusConnection> #include <QDBusConnection>
#include <QStandardPaths>
#include <QFile>
#include <QUrl>
#include <QMediaPlayer> #include <QMediaPlayer>
K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json") 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) FindThisDevicePlugin::FindThisDevicePlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args) : KdeConnectPlugin(parent, args)
{ {
@ -45,48 +42,14 @@ FindThisDevicePlugin::FindThisDevicePlugin(QObject* parent, const QVariantList&
FindThisDevicePlugin::~FindThisDevicePlugin() = default; FindThisDevicePlugin::~FindThisDevicePlugin() = default;
void FindThisDevicePlugin::connected()
{
}
bool FindThisDevicePlugin::receivePacket(const NetworkPacket& np) bool FindThisDevicePlugin::receivePacket(const NetworkPacket& np)
{ {
Q_UNUSED(np); Q_UNUSED(np);
const QString soundFile = config()->get<QString>(QStringLiteral("ringtone"), defaultSound());
const QString soundFilename = config()->get<QString>(QStringLiteral("ringtone"), defaultSound()); const QUrl soundURL = QUrl(soundFile);
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
if (soundURL.isEmpty()) { 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; return true;
} }
@ -94,27 +57,25 @@ bool FindThisDevicePlugin::receivePacket(const NetworkPacket& np)
player->setAudioRole(QAudio::Role(QAudio::NotificationRole)); player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL); player->setMedia(soundURL);
player->setVolume(100); player->setVolume(100);
player->play();
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
const auto sinks = PulseAudioQt::Context::instance()->sinks(); const auto sinks = PulseAudioQt::Context::instance()->sinks();
QVector<PulseAudioQt::Sink*> mutedSinks; QVector<PulseAudioQt::Sink*> mutedSinks;
for (auto sink : sinks) { for (auto sink : sinks) {
if (sink->isMuted()) { if (sink->isMuted()) {
sink->setMuted(false); sink->setMuted(false);
mutedSinks.append(sink); mutedSinks.append(sink);
} }
} }
connect(player, &QMediaPlayer::stateChanged, this, [player, mutedSinks]{ connect(player, &QMediaPlayer::stateChanged, this, [player, mutedSinks]{
player->deleteLater();
for (auto sink : qAsConst(mutedSinks)) { for (auto sink : qAsConst(mutedSinks)) {
sink->setMuted(true); sink->setMuted(true);
} }
}); });
#endif #endif
player->play();
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
// TODO: ensure to use built-in loudspeakers // TODO: ensure to use built-in loudspeakers
return true; return true;

View file

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