Changeset View
Changeset View
Standalone View
Standalone View
plugins/findthisdevice/findthisdevice_config.cpp
- This file was added.
1 | /** | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "findthisdevice_config.h" | ||||
22 | | ||||
23 | #include "ui_findthisdevice_config.h" | ||||
24 | // Phonon | ||||
25 | #include <phonon/mediaobject.h> | ||||
26 | // KF | ||||
27 | #include <KLocalizedString> | ||||
28 | #include <KPluginFactory> | ||||
29 | // Qt | ||||
30 | #include <QStandardPaths> | ||||
31 | | ||||
32 | | ||||
33 | K_PLUGIN_FACTORY(FindThisDeviceConfigFactory, registerPlugin<FindThisDeviceConfig>();) | ||||
34 | | ||||
35 | | ||||
36 | namespace { | ||||
37 | namespace Strings { | ||||
38 | inline QString defaultSound() { return QStringLiteral("Oxygen-Im-Phone-Ring.ogg"); } | ||||
39 | } | ||||
40 | } | ||||
41 | | ||||
42 | FindThisDeviceConfig::FindThisDeviceConfig(QWidget* parent, const QVariantList& args) | ||||
43 | : KdeConnectPluginKcm(parent, args, QStringLiteral("kdeconnect_findthisdevice_config")) | ||||
44 | , m_ui(new Ui::FindThisDeviceConfigUi()) | ||||
45 | { | ||||
46 | m_ui->setupUi(this); | ||||
47 | | ||||
48 | const QStringList soundDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, | ||||
49 | QStringLiteral("sounds"), | ||||
50 | QStandardPaths::LocateDirectory); | ||||
51 | if (!soundDirs.isEmpty()) { | ||||
52 | m_ui->soundFileRequester->setStartDir(QUrl::fromLocalFile(soundDirs.last())); | ||||
53 | } | ||||
54 | | ||||
55 | connect(m_ui->playSoundButton, &QToolButton::clicked, | ||||
56 | this, &FindThisDeviceConfig::playSound); | ||||
57 | connect(m_ui->soundFileRequester, &KUrlRequester::textChanged, | ||||
58 | this, QOverload<>::of(&FindThisDeviceConfig::changed)); | ||||
59 | } | ||||
60 | | ||||
61 | FindThisDeviceConfig::~FindThisDeviceConfig() | ||||
62 | { | ||||
63 | delete m_ui; | ||||
64 | } | ||||
65 | | ||||
66 | | ||||
67 | void FindThisDeviceConfig::defaults() | ||||
68 | { | ||||
69 | KCModule::defaults(); | ||||
70 | | ||||
71 | m_ui->soundFileRequester->setText(Strings::defaultSound()); | ||||
72 | | ||||
73 | Q_EMIT changed(true); | ||||
74 | } | ||||
75 | | ||||
76 | void FindThisDeviceConfig::load() | ||||
77 | { | ||||
78 | KCModule::load(); | ||||
79 | | ||||
80 | const QString ringTone = config()->get<QString>(QStringLiteral("ringtone"), Strings::defaultSound()); | ||||
81 | m_ui->soundFileRequester->setText(ringTone); | ||||
82 | | ||||
83 | Q_EMIT changed(false); | ||||
84 | } | ||||
85 | | ||||
86 | void FindThisDeviceConfig::save() | ||||
87 | { | ||||
88 | config()->set(QStringLiteral("ringtone"), m_ui->soundFileRequester->text()); | ||||
89 | | ||||
90 | KCModule::save(); | ||||
91 | | ||||
92 | Q_EMIT changed(false); | ||||
93 | } | ||||
94 | | ||||
95 | void FindThisDeviceConfig::playSound() | ||||
96 | { | ||||
97 | const QString soundFilename = m_ui->soundFileRequester->text(); | ||||
98 | | ||||
99 | QUrl soundURL; | ||||
100 | const auto dataLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); | ||||
101 | for (const QString &dataLocation : dataLocations) { | ||||
102 | soundURL = QUrl::fromUserInput(soundFilename, | ||||
103 | dataLocation + QStringLiteral("/sounds"), | ||||
104 | QUrl::AssumeLocalFile); | ||||
105 | if (soundURL.isLocalFile()) { | ||||
106 | if (QFile::exists(soundURL.toLocalFile())) { | ||||
107 | break; | ||||
108 | } | ||||
109 | } else { | ||||
110 | if (soundURL.isValid()) { | ||||
111 | break; | ||||
112 | } | ||||
113 | } | ||||
114 | soundURL.clear(); | ||||
115 | } | ||||
116 | | ||||
117 | Phonon::MediaObject *media = Phonon::createPlayer(Phonon::NotificationCategory, soundURL); | ||||
118 | media->play(); | ||||
119 | connect(media, SIGNAL(finished()), media, SLOT(deleteLater())); | ||||
120 | } | ||||
121 | | ||||
122 | | ||||
123 | #include "findthisdevice_config.moc" |