1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
* -*- coding: utf-8 -*-
*
* Copyright (C) 2023 KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QString>
#include <QDir>
#include "sound-manager.h"
SoundManager* SoundManager::mSoundManager = nullptr;
SoundManager::SoundManager()
{
timer = new QTimer();
connect(timer, &QTimer::timeout, this, &SoundManager::flush_cb);
}
SoundManager::~SoundManager()
{
USD_LOG(LOG_DEBUG,"SoundManager destructor!");
if(mSoundManager) {
delete mSoundManager;
mSoundManager = nullptr;
}
}
void
sample_info_cb (pa_context *c, const pa_sample_info *i, int eol, void *userdata)
{
pa_operation *o;
if (!i) {
USD_LOG(LOG_DEBUG,"can't find sample");
return;
}
USD_LOG(LOG_DEBUG,"Found sample %s", i->name);
/* We only flush those samples which have an XDG sound name
* attached, because only those originate from themeing */
if (!(pa_proplist_gets (i->proplist, PA_PROP_EVENT_ID)))
return;
USD_LOG(LOG_DEBUG,"Dropping sample %s from cache", i->name);
if (!(o = pa_context_remove_sample (c, i->name, NULL, NULL))) {
USD_LOG(LOG_DEBUG,"pa_context_remove_sample (): %s", pa_strerror (pa_context_errno (c)));
return;
}
pa_operation_unref (o);
/* We won't wait until the operation is actually executed to
* speed things up a bit.*/
}
void flush_cache (void)
{
pa_mainloop *ml = NULL;
pa_context *c = NULL;
pa_proplist *pl = NULL;
pa_operation *o = NULL;
if (!(ml = pa_mainloop_new ())) {
USD_LOG(LOG_DEBUG,"Failed to allocate pa_mainloop");
goto fail;
}
if (!(pl = pa_proplist_new ())) {
USD_LOG(LOG_DEBUG,"Failed to allocate pa_proplist");
goto fail;
}
pa_proplist_sets (pl, PA_PROP_APPLICATION_NAME, PACKAGE_NAME);
pa_proplist_sets (pl, PA_PROP_APPLICATION_VERSION, PACKAGE_VERSION);
pa_proplist_sets (pl, PA_PROP_APPLICATION_ID, "org.ukui.SettingsDaemon");
if (!(c = pa_context_new_with_proplist (pa_mainloop_get_api (ml), PACKAGE_NAME, pl))) {
USD_LOG(LOG_DEBUG,"Failed to allocate pa_context");
goto fail;
}
pa_proplist_free (pl);
pl = NULL;
if (pa_context_connect (c, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) {
USD_LOG(LOG_DEBUG,"pa_context_connect(): %s", pa_strerror (pa_context_errno (c)));
goto fail;
}
/* Wait until the connection is established */
while (pa_context_get_state (c) != PA_CONTEXT_READY) {
if (!PA_CONTEXT_IS_GOOD (pa_context_get_state (c))) {
USD_LOG(LOG_DEBUG,"Connection failed: %s", pa_strerror (pa_context_errno (c)));
goto fail;
}
if (pa_mainloop_iterate (ml, TRUE, NULL) < 0) {
USD_LOG(LOG_DEBUG,"pa_mainloop_iterate() failed");
goto fail;
}
}
/* Enumerate all cached samples */
if (!(o = pa_context_get_sample_info_list (c, sample_info_cb, NULL))) {
USD_LOG(LOG_DEBUG,"pa_context_get_sample_info_list(): %s", pa_strerror (pa_context_errno (c)));
goto fail;
}
/* Wait until our operation is finished and there's nothing
* more queued to send to the server */
while (pa_operation_get_state (o) == PA_OPERATION_RUNNING || pa_context_is_pending (c)) {
if (!PA_CONTEXT_IS_GOOD (pa_context_get_state (c))) {
USD_LOG(LOG_DEBUG,"Connection failed: %s", pa_strerror (pa_context_errno (c)));
goto fail;
}
if (pa_mainloop_iterate (ml, TRUE, NULL) < 0) {
USD_LOG(LOG_DEBUG,"pa_mainloop_iterate() failed");
goto fail;
}
}
USD_LOG(LOG_DEBUG,"send over...");
fail:
if (o) {
pa_operation_cancel (o);
pa_operation_unref (o);
}
if (c) {
pa_context_disconnect (c);
pa_context_unref (c);
}
if (pl)
pa_proplist_free (pl);
if (ml)
pa_mainloop_free (ml);
}
bool SoundManager::flush_cb ()
{
flush_cache();
timer->stop();
USD_LOG(LOG_DEBUG,"sound it");
return false;
}
void SoundManager::trigger_flush ()
{
if(timer->isActive())
timer->stop();
/* We delay the flushing a bit so that we can coalesce
* multiple changes into a single cache flush */
timer->start(500);
USD_LOG(LOG_DEBUG,"sound it");
}
/* func: listen for org.mate.sound
*/
void
SoundManager::gsettings_notify_cb (const QString& key)
{
trigger_flush();
}
/*func : listen for follow directory.
* $HOME/.locale/share/sounds
* /usr/share/ukui/
* /usr/share/
* /usr/locale/share/
*/
void
SoundManager::file_monitor_changed_cb (const QString& path)
{
trigger_flush ();
}
bool
SoundManager::register_directory_callback (const QString path,
GError **error)
{
QDir dir;
QFileSystemWatcher* w;
bool succ = false;
w = new QFileSystemWatcher();
if(w->addPath(path)){
connect(w,&QFileSystemWatcher::directoryChanged, this, &SoundManager::file_monitor_changed_cb);
monitors->push_front(w);
succ = true;
}
return succ;
}
bool SoundManager::SoundManagerStart (GError **error)
{
const char *env, *dd;
QString path;
QString homePath;
QStringList pathList;
int pathNum;
int i;
USD_LOG(LOG_DEBUG,"Starting sound manager");
monitors = new QList<QFileSystemWatcher*>();
/* We listen for change of the selected theme ... */
settings = new QGSettings(UKUI_SOUND_SCHEMA);
// QObject::connect(settings, &QGSettings::changed, this, &SoundManager::gsettings_notify_cb);
connect(settings, SIGNAL(changed(QString)), this, SLOT(gsettings_notify_cb(QString)));
/* ... and we listen to changes of the theme base directories
* in $HOME ...*/
homePath = QDir::homePath();
if ((env = getenv ("XDG_DATA_HOME")) && *env == '/') {
path = QString(env) + "/sounds";
}
else if (!homePath.isEmpty()) {
path = homePath + "/.local" + "/share" + "/sounds";
}
else {
path = nullptr;
}
if (!path.isNull() && !path.isEmpty()) {
USD_LOG(LOG_DEBUG,"ready register callback:%s",path.toLatin1().data());
register_directory_callback (path, NULL);
}
/* ... and globally. */
if (!(dd = getenv ("XDG_DATA_DIRS")) || *dd == 0) {
dd = "/usr/local/share:/usr/share";
}
pathList = QString(dd).split(":");
pathNum = pathList.count();
for (i = 0; i < pathNum; ++i) {
USD_LOG(LOG_DEBUG,"ready register callback:%s",pathList.at(i).toLatin1().data());
register_directory_callback (pathList.at(i), NULL);
}
trigger_flush();
return true;
}
void SoundManager::SoundManagerStop ()
{
USD_LOG(LOG_DEBUG,"Stopping sound manager");
if (settings) {
delete settings;
settings = nullptr;
}
while (monitors->length()) {
delete monitors->first();
monitors->removeFirst();
}
delete monitors;
monitors = nullptr;
}
SoundManager *SoundManager::SoundManagerNew ()
{
if(nullptr == mSoundManager)
mSoundManager = new SoundManager();
return mSoundManager;
}
|