2012-01-22 17:58:23 -05:00
|
|
|
/****************************************************************
|
|
|
|
* RetroShare is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 RetroShare Team
|
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* of the License, or (at your option) 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
****************************************************************/
|
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
#include <QApplication>
|
2012-01-22 17:58:23 -05:00
|
|
|
#include <QSound>
|
2015-06-07 08:40:25 -04:00
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)
|
|
|
|
#include <QAudio>
|
|
|
|
#include <QAudioDeviceInfo>
|
|
|
|
#endif
|
2012-01-22 17:58:23 -05:00
|
|
|
|
|
|
|
#include "SoundManager.h"
|
|
|
|
#include "settings/rsharesettings.h"
|
2015-06-07 08:40:25 -04:00
|
|
|
#include "retroshare/rsinit.h"
|
|
|
|
#include <retroshare/rsplugin.h>
|
2012-01-22 17:58:23 -05:00
|
|
|
|
|
|
|
#define GROUP_MAIN "Sound"
|
|
|
|
#define GROUP_ENABLE "Enable"
|
|
|
|
#define GROUP_SOUNDFILE "SoundFilePath"
|
|
|
|
|
|
|
|
SoundManager *soundManager = NULL;
|
|
|
|
|
2012-10-21 17:38:55 -04:00
|
|
|
SoundEvents::SoundEvents()
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
2012-10-21 17:38:55 -04:00
|
|
|
}
|
2012-01-22 17:58:23 -05:00
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
void SoundEvents::addEvent(const QString &groupName, const QString &eventName, const QString &event, const QString &defaultFilename)
|
2012-10-21 17:38:55 -04:00
|
|
|
{
|
2015-06-07 08:40:25 -04:00
|
|
|
if (event.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-21 17:38:55 -04:00
|
|
|
SoundEventInfo info;
|
|
|
|
info.mGroupName = groupName;
|
|
|
|
info.mEventName = eventName;
|
2015-06-07 08:40:25 -04:00
|
|
|
info.mDefaultFilename = defaultFilename;
|
2012-10-21 17:38:55 -04:00
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
mEventInfos[event] = info;
|
2012-01-22 17:58:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::create()
|
|
|
|
{
|
|
|
|
if (soundManager == NULL) {
|
|
|
|
soundManager = new SoundManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundManager::SoundManager() : QObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
void SoundManager::soundEvents(SoundEvents &events)
|
|
|
|
{
|
2015-06-07 16:23:38 -04:00
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
QDir baseDir = QDir(qApp->applicationDirPath() + "/sounds");
|
|
|
|
#else
|
|
|
|
QDir baseDir = QDir("/usr/share/Retroshare06/sounds");
|
|
|
|
#endif
|
2015-06-07 13:35:58 -04:00
|
|
|
|
|
|
|
events.mDefaultPath = baseDir.absolutePath();
|
2015-06-07 08:40:25 -04:00
|
|
|
|
|
|
|
/* add standard events */
|
|
|
|
events.addEvent(tr("Friend"), tr("Go Online"), SOUND_USER_ONLINE, QFileInfo(baseDir, "online1.wav").absoluteFilePath());
|
|
|
|
events.addEvent(tr("Chatmessage"), tr("New Msg"), SOUND_NEW_CHAT_MESSAGE, QFileInfo(baseDir, "incomingchat.wav").absoluteFilePath());
|
|
|
|
events.addEvent(tr("Message"), tr("Message arrived"), SOUND_MESSAGE_ARRIVED, QFileInfo(baseDir, "receive.wav").absoluteFilePath());
|
|
|
|
events.addEvent(tr("Download"), tr("Download complete"), SOUND_DOWNLOAD_COMPLETE, QFileInfo(baseDir, "ft_complete.wav").absoluteFilePath());
|
|
|
|
|
|
|
|
/* add plugin events */
|
|
|
|
int pluginCount = rsPlugins->nbPlugins();
|
|
|
|
for (int i = 0; i < pluginCount; ++i) {
|
|
|
|
RsPlugin *plugin = rsPlugins->plugin(i);
|
|
|
|
|
|
|
|
if (plugin) {
|
|
|
|
plugin->qt_sound_events(events);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SoundManager::defaultFilename(const QString &event, bool check)
|
|
|
|
{
|
|
|
|
SoundEvents events;
|
|
|
|
soundEvents(events);
|
|
|
|
|
|
|
|
QMap<QString, SoundEvents::SoundEventInfo>::iterator eventIt = events.mEventInfos.find(event);
|
|
|
|
if (eventIt == events.mEventInfos.end()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString filename = eventIt.value().mDefaultFilename;
|
|
|
|
if (filename.isEmpty()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!check) {
|
|
|
|
return convertFilename(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (QFileInfo(filename).exists()) {
|
|
|
|
return convertFilename(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::initDefault()
|
|
|
|
{
|
|
|
|
SoundEvents events;
|
|
|
|
soundEvents(events);
|
|
|
|
|
|
|
|
QString event;
|
|
|
|
foreach (event, events.mEventInfos.keys()) {
|
|
|
|
SoundEvents::SoundEventInfo &eventInfo = events.mEventInfos[event];
|
|
|
|
|
|
|
|
if (QFileInfo(eventInfo.mDefaultFilename).exists()) {
|
|
|
|
setEventFilename(event, convertFilename(eventInfo.mDefaultFilename));
|
|
|
|
setEventEnabled(event, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 13:35:58 -04:00
|
|
|
void SoundManager::setMute(bool m)
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
|
|
|
Settings->beginGroup(GROUP_MAIN);
|
2015-06-07 13:35:58 -04:00
|
|
|
Settings->setValue("mute", m);
|
2012-01-22 17:58:23 -05:00
|
|
|
Settings->endGroup();
|
|
|
|
|
2015-06-07 13:35:58 -04:00
|
|
|
emit mute(m);
|
2012-01-22 17:58:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SoundManager::isMute()
|
|
|
|
{
|
|
|
|
Settings->beginGroup(GROUP_MAIN);
|
|
|
|
bool mute = Settings->value("mute", false).toBool();
|
|
|
|
Settings->endGroup();
|
|
|
|
|
|
|
|
return mute;
|
|
|
|
}
|
|
|
|
|
2012-10-21 17:38:55 -04:00
|
|
|
bool SoundManager::eventEnabled(const QString &event)
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
|
|
|
Settings->beginGroup(GROUP_MAIN);
|
|
|
|
Settings->beginGroup(GROUP_ENABLE);
|
2012-10-21 17:38:55 -04:00
|
|
|
bool enabled = Settings->value(event, false).toBool();
|
2012-01-22 17:58:23 -05:00
|
|
|
Settings->endGroup();
|
|
|
|
Settings->endGroup();
|
|
|
|
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2012-10-21 17:38:55 -04:00
|
|
|
void SoundManager::setEventEnabled(const QString &event, bool enabled)
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
|
|
|
Settings->beginGroup(GROUP_MAIN);
|
|
|
|
Settings->beginGroup(GROUP_ENABLE);
|
2012-10-21 17:38:55 -04:00
|
|
|
Settings->setValue(event, enabled);
|
2012-01-22 17:58:23 -05:00
|
|
|
Settings->endGroup();
|
|
|
|
Settings->endGroup();
|
|
|
|
}
|
|
|
|
|
2012-10-21 17:38:55 -04:00
|
|
|
QString SoundManager::eventFilename(const QString &event)
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
|
|
|
Settings->beginGroup(GROUP_MAIN);
|
|
|
|
Settings->beginGroup(GROUP_SOUNDFILE);
|
2012-10-21 17:38:55 -04:00
|
|
|
QString filename = Settings->value(event).toString();
|
2012-01-22 17:58:23 -05:00
|
|
|
Settings->endGroup();
|
|
|
|
Settings->endGroup();
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2012-10-21 17:38:55 -04:00
|
|
|
void SoundManager::setEventFilename(const QString &event, const QString &filename)
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
|
|
|
Settings->beginGroup(GROUP_MAIN);
|
|
|
|
Settings->beginGroup(GROUP_SOUNDFILE);
|
2012-10-21 17:38:55 -04:00
|
|
|
Settings->setValue(event, filename);
|
2012-01-22 17:58:23 -05:00
|
|
|
Settings->endGroup();
|
|
|
|
Settings->endGroup();
|
|
|
|
}
|
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
QString SoundManager::convertFilename(const QString &filename)
|
2012-01-22 17:58:23 -05:00
|
|
|
{
|
2015-06-07 08:40:25 -04:00
|
|
|
if (RsInit::isPortable ()) {
|
|
|
|
// Save path relative to application path
|
|
|
|
QDir baseDir = QDir(qApp->applicationDirPath());
|
|
|
|
QString relativeFilename = baseDir.relativeFilePath(filename);
|
|
|
|
if (!relativeFilename.startsWith("..")) {
|
|
|
|
// Save only subfolders as relative path
|
|
|
|
return relativeFilename;
|
|
|
|
}
|
2013-10-19 16:59:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-07 08:40:25 -04:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SoundManager::realFilename(const QString &filename)
|
|
|
|
{
|
|
|
|
if (RsInit::isPortable ()) {
|
|
|
|
// Path relative to application path
|
|
|
|
QDir baseDir = QDir(qApp->applicationDirPath());
|
|
|
|
return baseDir.absoluteFilePath(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::play(const QString &event)
|
|
|
|
{
|
2013-10-19 16:59:55 -04:00
|
|
|
if (isMute() || !eventEnabled(event)) {
|
2012-01-22 17:58:23 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString filename = eventFilename(event);
|
|
|
|
playFile(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::playFile(const QString &filename)
|
|
|
|
{
|
|
|
|
if (filename.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-07 08:40:25 -04:00
|
|
|
|
|
|
|
QString playFilename = realFilename(filename);
|
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)
|
|
|
|
if (!QAudioDeviceInfo::availableDevices(QAudio::AudioOutput).isEmpty()) {
|
|
|
|
#else
|
|
|
|
if (QSound::isAvailable()) {
|
|
|
|
#endif
|
|
|
|
QSound::play(playFilename);
|
|
|
|
}
|
2012-01-22 17:58:23 -05:00
|
|
|
}
|