Ref T494, CNotificationSounds refactring

* use Q_DECLARE_FLAGS NotificationFlag/Notification for sounds, not the weird version from very early swift days
* splt into h/cpp files
* added new sounds
This commit is contained in:
Klaus Basan
2018-12-31 06:42:59 +01:00
committed by Mat Sutcliffe
parent dc8ea83839
commit f8c17e10e8
14 changed files with 133 additions and 57 deletions

View File

@@ -11,6 +11,7 @@
#include <QChar>
#include <QtGlobal>
#include <QStringBuilder>
using namespace BlackMisc::Audio;
@@ -23,37 +24,32 @@ namespace BlackMisc
this->initDefaultValues();
}
bool CSettings::getNotificationFlag(CNotificationSounds::Notification notification) const
bool CSettings::isNotificationFlagSet(CNotificationSounds::NotificationFlag notification) const
{
const int i = static_cast<int>(notification);
if (i >= m_notificationFlags.length()) return true; // default
QChar f = m_notificationFlags.at(i);
return '1' == f;
return this->getNotification().testFlag(notification);
}
void CSettings::setNotificationFlag(CNotificationSounds::NotificationFlag notification, bool value)
{
if (value)
{
m_notification |= notification;
}
else
{
m_notification &= ~notification;
}
}
QString CSettings::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
QString s("Notification flags:");
s.append(" ").append(m_notificationFlags);
return s;
return u"Notification flags: " % CNotificationSounds::toString(this->getNotification());
}
void CSettings::initDefaultValues()
{
this->initNotificationFlags();
this->setNotification(CNotificationSounds::AllNotifications);
}
void CSettings::initNotificationFlags()
{
// if we add flags in the future, we automatically extend ...
constexpr int l = 1 + static_cast<int>(CNotificationSounds::Notification::NotificationsLoadSounds);
if (this->m_notificationFlags.length() < l)
{
int cl = m_notificationFlags.length();
this->m_notificationFlags.append(QString(l - cl, '1'));
}
}
} // namespace
} // namespace

View File

@@ -34,13 +34,19 @@ namespace BlackMisc
CSettings();
//! Notification flag (play notification?)
bool getNotificationFlag(BlackMisc::Audio::CNotificationSounds::Notification notification) const;
bool isNotificationFlagSet(CNotificationSounds::NotificationFlag notification) const;
//! Notification flag (play notification?)
void setNotificationFlag(BlackMisc::Audio::CNotificationSounds::Notification notification, bool value);
void setNotificationFlag(CNotificationSounds::NotificationFlag notification, bool value);
//! Get notification
CNotificationSounds::Notification getNotification() const { return static_cast<CNotificationSounds::Notification>(m_notification); }
//! Set notification
void setNotification(CNotificationSounds::Notification notification) { m_notification = static_cast<int>(notification); }
//! Settings value
BlackMisc::CStatusMessage value(const QString &path, const QString &command, const BlackMisc::CVariant &value, bool &changedFlag);
CStatusMessage value(const QString &path, const QString &command, const CVariant &value, bool &changedFlag);
//! Init with meaningful default values
void initDefaultValues();
@@ -49,12 +55,12 @@ namespace BlackMisc
QString convertToQString(bool i18n = false) const;
private:
QString m_notificationFlags; //!< play notification for notification x, a little trick to use a string here (streamable, hashable, ..)
int m_notification = static_cast<int>(CNotificationSounds::DefaultNotifications); //!< play notification for notification x, a little trick to use a string here (streamable, hashable, ..)
void initNotificationFlags(); //!< init flags
BLACK_METACLASS(
CSettings,
BLACK_METAMEMBER(notificationFlags)
BLACK_METAMEMBER(notification)
);
};
} // namespace

View File

@@ -0,0 +1,61 @@
/* Copyright (C) 2018
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "notificationsounds.h"
#include <QStringList>
#include <QString>
namespace BlackMisc
{
namespace Audio
{
const QString &CNotificationSounds::flagToString(CNotificationSounds::NotificationFlag notification)
{
static const QString unknown("unknown");
static const QString error("error");
static const QString login("login");
static const QString logoff("logoff");
static const QString privateMsg("private msg.");
static const QString mentioned("cs mentioned");
static const QString joined("room joined");
static const QString left("room left");
static const QString ptt("PTT click");
static const QString load("load sounds");
switch (notification)
{
case NotificationError: return error;
case NotificationLogin: return login;
case NotificationLogoff: return logoff;
case NotificationTextMessagePrivate: return privateMsg;
case NotificationTextCallsignMentioned: return mentioned;
case NotificationVoiceRoomJoined: return joined;
case NotificationVoiceRoomLeft: return left;
case PTTClick: return ptt;
case LoadSounds: return load;
default: break;
}
return unknown;
}
const QString CNotificationSounds::toString(Notification notification)
{
QStringList n;
if (notification.testFlag(NotificationError)) n << flagToString(NotificationError);
if (notification.testFlag(NotificationLogin)) n << flagToString(NotificationLogin);
if (notification.testFlag(NotificationLogoff)) n << flagToString(NotificationLogoff);
if (notification.testFlag(NotificationTextMessagePrivate)) n << flagToString(NotificationTextMessagePrivate);
if (notification.testFlag(NotificationTextCallsignMentioned)) n << flagToString(NotificationTextCallsignMentioned);
if (notification.testFlag(NotificationVoiceRoomJoined)) n << flagToString(NotificationVoiceRoomJoined);
if (notification.testFlag(NotificationVoiceRoomLeft)) n << flagToString(NotificationVoiceRoomLeft);
if (notification.testFlag(LoadSounds)) n << flagToString(LoadSounds);
return n.join(", ");
}
} // ns
} // ns

View File

@@ -12,6 +12,7 @@
#ifndef BLACKMISC_NOTIFICATIONSOUNDS_H
#define BLACKMISC_NOTIFICATIONSOUNDS_H
#include "blackmisc/blackmiscexport.h"
#include <QMetaType>
namespace BlackMisc
@@ -23,7 +24,7 @@ namespace BlackMisc
* \remarks Currently located in project BlackMisc (i.e. outside project BlackSound)
* as this allows to trigger sounds without using Multimedia libraries.
*/
struct CNotificationSounds
struct BLACKMISC_EXPORT CNotificationSounds
{
//! How to play?
enum PlayMode
@@ -34,21 +35,38 @@ namespace BlackMisc
};
//! Play notification
enum Notification
enum NotificationFlag
{
NotificationError = 0,
NotificationLogin,
NotificationLogoff,
NotificationTextMessagePrivate,
NotificationVoiceRoomJoined,
NotificationVoiceRoomLeft,
NotificationsLoadSounds //!< end marker and force loading of sounds, keep as last element
NoNotifications = 0,
NotificationError = 1 << 0,
NotificationLogin = 1 << 1,
NotificationLogoff = 1 << 2,
NotificationTextMessagePrivate = 1 << 3,
NotificationTextCallsignMentioned = 1 << 4,
NotificationVoiceRoomJoined = 1 << 5,
NotificationVoiceRoomLeft = 1 << 6,
PTTClick = 1 << 7,
LoadSounds = 1 << 8, //!< end marker and force loading of sounds, keep as last element
AllTextNotifications = NotificationTextMessagePrivate | NotificationTextCallsignMentioned,
AllLoginNotifications = NotificationLogin | NotificationLogoff,
AllVoiceRoomNotifications = NotificationVoiceRoomJoined | NotificationVoiceRoomLeft,
AllNotifications = NotificationError | AllTextNotifications | AllLoginNotifications | AllVoiceRoomNotifications,
DefaultNotifications = NotificationError | NotificationTextMessagePrivate | AllLoginNotifications | AllVoiceRoomNotifications,
All = AllNotifications | PTTClick
};
Q_DECLARE_FLAGS(Notification, NotificationFlag)
//! As string
static const QString &flagToString(NotificationFlag notification);
//! As string
static const QString toString(Notification notification);
};
} // ns
} // ns
Q_DECLARE_METATYPE(BlackMisc::Audio::CNotificationSounds::PlayMode)
Q_DECLARE_METATYPE(BlackMisc::Audio::CNotificationSounds::Notification)
Q_DECLARE_METATYPE(BlackMisc::Audio::CNotificationSounds::NotificationFlag)
#endif // guard

View File

@@ -25,9 +25,9 @@ namespace BlackMisc
CSettings::registerMetadata();
CVoiceSetup::registerMetadata();
// struct
qDBusRegisterMetaType<BlackMisc::Audio::CNotificationSounds::PlayMode>();
qDBusRegisterMetaType<BlackMisc::Audio::CNotificationSounds::Notification>();
// ENUMs
qDBusRegisterMetaType<CNotificationSounds::PlayMode>();
qDBusRegisterMetaType<CNotificationSounds::NotificationFlag>();
}
} // ns
} // ns