ukui-notification/libukui-notification/notification-settings/notification-global-setting...

146 lines
5.3 KiB
C++

//
// Created by zpf on 2023/2/20.
//
#include "notification-global-settings.h"
#include <QJsonObject>
#include <QQmlEngine>
#include <QDebug>
#include "settings-manager.h"
#include "settings-properties.h"
#include "settings-properties-info.h"
namespace UkuiNotification{
class NotificationGlobalSettingsPrivate
{
friend class NotificationGlobalSettings;
private:
QJsonObject m_settings;
};
}
using namespace UkuiNotification;// UkuiNotification
NotificationGlobalSettings::NotificationGlobalSettings(QObject *parent) : QObject(parent), d(new NotificationGlobalSettingsPrivate())
{
qmlRegisterUncreatableType<UkuiNotification::NotificationGlobalSettings>("org.ukui.notification.client", 1, 0, "NotificationGlobalSettings", "");
connect(SettingsManager::self(), &SettingsManager::settingsDataChanged, this, &NotificationGlobalSettings::settingsDataChanged);
d->m_settings = SettingsManager::self()->getGlobalSettings();
}
NotificationGlobalSettings::~NotificationGlobalSettings()
{
if(d) {
delete d;
d = nullptr;
}
}
bool NotificationGlobalSettings::scheduleTurnOnDND()
{
return d->m_settings.value(SettingsPropertiesInfo(SettingsProperty::ScheduleTurnOnDND).name()).toVariant().toBool();
}
QTime NotificationGlobalSettings::scheduleTurnOnDNDTime()
{
return QTime::fromString(d->m_settings.value(SettingsPropertiesInfo(SettingsProperty::ScheduleTurnOnDNDTime).name()).toString(), "HHmm");
}
QTime NotificationGlobalSettings::scheduleTurnOffDNDTime()
{
return QTime::fromString(d->m_settings.value(SettingsPropertiesInfo(SettingsProperty::ScheduleTurnOffDNDTime).name()).toString(), "HHmm");
}
bool NotificationGlobalSettings::DNDWhileMultiScreen()
{
return d->m_settings.value(SettingsPropertiesInfo(SettingsProperty::DNDWhileMultiScreen).name()).toVariant().toBool();
}
bool NotificationGlobalSettings::DNDWhileFullScreen()
{
return d->m_settings.value(SettingsPropertiesInfo(SettingsProperty::DNDWhileFullScreen).name()).toVariant().toBool();
}
bool NotificationGlobalSettings::notifyAlarmWhileDND()
{
return d->m_settings.value(SettingsPropertiesInfo(SettingsProperty::NotifyAlarmWhileDND).name()).toVariant().toBool();
}
bool NotificationGlobalSettings::receiveNotificationsFromApps()
{
return d->m_settings.value(SettingsPropertiesInfo(SettingsProperty::ReceiveNotificationsFromApps).name()).toVariant().toBool();
}
void NotificationGlobalSettings::settingsDataChanged()
{
QJsonObject data = SettingsManager::self()->getGlobalSettings();
for(SettingsProperty::Property property : GLOBAL_SETTINGS) {
QString name = SettingsPropertiesInfo(property).name();
QJsonValue value = data.value(name);
if(d->m_settings.value(name) != value) {
d->m_settings.insert(name, value);
switch (property) {
case SettingsProperty::ScheduleTurnOnDND:
Q_EMIT scheduleTurnOnDNDChanged(value.toVariant().toBool());
break;
case SettingsProperty::ScheduleTurnOnDNDTime:
Q_EMIT scheduleTurnOnDNDTimeChanged(QTime::fromString(value.toString(), "HHmm"));
break;
case SettingsProperty::ScheduleTurnOffDNDTime:
Q_EMIT scheduleTurnOffDNDTimeChanged(QTime::fromString(value.toString(), "HHmm"));
break;
case SettingsProperty::DNDWhileMultiScreen:
Q_EMIT DNDWhileMultiScreenChanged(value.toVariant().toBool());
break;
case SettingsProperty::DNDWhileFullScreen:
Q_EMIT DNDWhileFullScreenChanged(value.toVariant().toBool());
break;
case SettingsProperty::NotifyAlarmWhileDND:
Q_EMIT notifyAlarmWhileDNDChanged(value.toVariant().toBool());
break;
case SettingsProperty::ReceiveNotificationsFromApps:
Q_EMIT receiveNotificationsFromAppsChanged(value.toVariant().toBool());
break;
default:
break;
}
}
}
}
void NotificationGlobalSettings::setScheduleTurnOnDND(bool enable)
{
SettingsManager::self()->setGlobalSettings(SettingsProperty::ScheduleTurnOnDND, enable);
}
void NotificationGlobalSettings::setScheduleTurnOnDNDTime(const QTime &time)
{
SettingsManager::self()->setGlobalSettings(SettingsProperty::ScheduleTurnOnDNDTime, time);
}
void NotificationGlobalSettings::setScheduleTurnOffDNDTime(const QTime &time)
{
SettingsManager::self()->setGlobalSettings(SettingsProperty::ScheduleTurnOffDNDTime, time);
}
void NotificationGlobalSettings::setDNDWhileMultiScreen(bool enable)
{
SettingsManager::self()->setGlobalSettings(SettingsProperty::DNDWhileMultiScreen, enable);
}
void NotificationGlobalSettings::setDNDWhileFullScreen(bool enable)
{
SettingsManager::self()->setGlobalSettings(SettingsProperty::DNDWhileFullScreen, enable);
}
void NotificationGlobalSettings::setNotifyAlarmWhileDND(bool enable)
{
SettingsManager::self()->setGlobalSettings(SettingsProperty::NotifyAlarmWhileDND, enable);
}
void NotificationGlobalSettings::setReceiveNotificationsFromApps(bool enable)
{
SettingsManager::self()->setGlobalSettings(SettingsProperty::ReceiveNotificationsFromApps, enable);
}