Debian Codegraph @

kysdk-soundeffects/src/ksoundeffects.cpp

raw
1 /*
2 * libkysdk-soundeffects's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhenyu Wang <[email protected]>
20 *
21 */
22
23 #include "ksoundeffects.h"
24 #include <QDBusInterface>
25 #include <QDBusReply>
26 #include <QMediaPlayer>
27 #include <QDebug>
28 #include <QGSettings/QGSettings>
29
30 namespace kdk {
31
32 static QGSettings* g_gsetting=nullptr;
33 static QGSettings* g_styleGsetting=nullptr;
34
35 static QString audioFileFields(SoundType type)
36 {
37 QString str;
38 switch (type)
39 {
40 case AUDIO_VOLUME_CHANGE:
41 str="audio-volume-change";
42 break;
43 case BATTERY_LOW:
44 str="battery-low";
45 break;
46 case COMPLETE:
47 str="complete";
48 break;
49 case DEVICE_ADDED_FAILED:
50 str="device-added-failed";
51 break;
52 case DEVICE_ADDED:
53 str="device-added";
54 break;
55 case DEVICE_REMOVED:
56 str="device-removed";
57 break;
58 case DIALOG_ERROR:
59 str="dialog-error";
60 break;
61 case DIALOG_WARNING:
62 str="dialog-warning";
63 break;
64 case NOTIFICATION_GENERAL:
65 str="notification-general";
66 break;
67 case NOTIFICATION_SPECIAL:
68 str="notification-special";
69 break;
70 case OPERATION_FILE:
71 str="operation-file";
72 break;
73 case OPERATION_UNSUPPORTED:
74 str="operation-unsupported";
75 break;
76 case POWER_PLUG:
77 str="power-plug";
78 break;
79 case SCREEN_CAPTURE:
80 str="screen-capture";
81 break;
82 case SCREEN_RECORD:
83 str="screen-record";
84 break;
85 case START_UP:
86 str="start-up";
87 break;
88 case TRASH_EMPTY:
89 str="trash-empty";
90 break;
91 default:
92 break;
93 }
94 return str;
95 }
96
97 static void themeSoundPlay(SoundType type,QMediaPlayer* player,QString str,QString soundPath)
98 {
99 if(g_gsetting->keys().contains("themeName"))
100 {
101 //初始化样式主题
102 QString widgetThemeName = g_gsetting->get("themeName").toString();
103 if(widgetThemeName == "Light-Seeking")
104 {
105 qDebug()<<"Light-Seeking";
106 soundPath=str.arg("Light-Seeking").arg(audioFileFields(type));
107 }
108 else if(widgetThemeName == "HeYin")
109 {
110 qDebug()<<"HeYin";
111 soundPath=str.arg("HeYin").arg(audioFileFields(type));
112 }
113 player->setSource(QUrl::fromLocalFile(soundPath));
114 if(player->error()==QMediaPlayer::NoError)
115 player->play();
116 else
117 qWarning()<<"Description Failed to play the audio file!";
118 return;
119 }
120 else if(g_styleGsetting->keys().contains("widgetThemeName"))
121 {
122 QString widgetThemeName = g_gsetting->get("widget-theme-name").toString();
123 if(widgetThemeName == "default")
124 {
125 qDebug()<<"default";
126 soundPath=str.arg("Light-Seeking").arg(audioFileFields(type));
127 }
128 else if(widgetThemeName == "fashion")
129 {
130 qDebug()<<"fashion";
131 soundPath=str.arg("HeYin").arg(audioFileFields(type));
132 }
133 player->setSource(QUrl::fromLocalFile(soundPath));
134 if(player->error()==QMediaPlayer::NoError)
135 player->play();
136 else
137 qWarning()<<"Description Failed to play the audio file!";
138 return;
139 }
140 else
141 qWarning()<<"No sound theme or system theme name!";
142 return;
143 }
144 static void playAudioSound(SoundType type)
145 {
146 QMediaPlayer* player = new QMediaPlayer;
147 QString str("/usr/share/sounds/%1/stereo/%2.ogg");
148 QString soundPath;
149 if(!g_gsetting)
150 {
151 if(QGSettings::isSchemaInstalled("org.ukui.style"))
152 {
153 g_styleGsetting = new QGSettings("org.ukui.style");
154 if(!g_styleGsetting)
155 //没有主题 可能是V10
156 return;
157 else
158 {
159 if(QGSettings::isSchemaInstalled("org.ukui.sound"))
160 {
161 g_gsetting = new QGSettings("org.ukui.sound");
162 if(g_gsetting->get("event-sounds").toBool())
163 themeSoundPlay(type,player,str,soundPath);
164 else
165 qWarning()<<"The system tips sound is not enabled";
166 }
167 }
168 }
169 }
170 else
171 {
172 if(g_gsetting->get("event-sounds").toBool())
173 themeSoundPlay(type,player,str,soundPath);
174 else
175 qWarning()<<"The system tips sound is not enabled";
176 }
177 }
178
179 KSoundEffects::KSoundEffects()
180 {
181 }
182
183 void KSoundEffects::playSound(SoundType type)
184 {
185 QDBusInterface interface("org.ukui.sound.theme.player",
186 "/org/ukui/sound/theme/player",
187 "org.ukui.sound.theme.player",
188 QDBusConnection::sessionBus());
189
190 if(!interface.isValid())
191 {
192 qDebug() << QDBusConnection::sessionBus().lastError().message()<<QString("Try to play the audio file");
193 playAudioSound(type);
194 return ;
195 }
196
197 QDBusReply<bool> reply = interface.call("playAlertSound",audioFileFields(type));
198
199 if(reply.isValid())
200 {
201 bool value = reply.value();
202 if(value)
203 qDebug() << QString("Client play alert sound = %1").arg(value);
204 else
205 {
206 qDebug() << QString("Client play alert sound = %1 ,Try to play the audio file").arg(value);
207 playAudioSound(type);
208 }
209 return ;
210 }
211 else
212 {
213 qDebug() << "Play alert sound method call failed ! Try to play the audio file";
214 playAudioSound(type);
215 return ;
216 }
217 return ;
218 }
219
220 }