diff --git a/libukui-notification/CMakeLists.txt b/libukui-notification/CMakeLists.txt index 59f1bc5..6936535 100644 --- a/libukui-notification/CMakeLists.txt +++ b/libukui-notification/CMakeLists.txt @@ -2,6 +2,7 @@ set(VERSION_MAJOR 1) set(VERSION_MINOR 0) set(VERSION_MICRO 0) set(UKUI_NOTIFICATION_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_MICRO}) +set(settings_DIR notification-settings/) find_package(KF5WindowSystem) set(ukui-notification_LIB_SRCS notification-client.cpp @@ -11,7 +12,13 @@ set(ukui-notification_LIB_SRCS ukui-notification_global.h notification-client-private.h utils.h - utils.cpp) + utils.cpp + ${settings_DIR}notification-settings-properties.h + ${settings_DIR}notification-settings-info.h + ${settings_DIR}notification-settings-info.cpp + ${settings_DIR}notification-settings.h + ${settings_DIR}notification-settings.cpp + ${settings_DIR}notification-settings-private.h) set(HEADERS notification-client.h popup-notification.h @@ -31,6 +38,7 @@ target_link_libraries(ukui-notification Qt${QT_VERSION_MAJOR}::DBus Qt${QT_VERSION_MAJOR}::Gui KF5::WindowSystem + ukui-search ) include(CMakePackageConfigHelpers) @@ -71,4 +79,4 @@ install(FILES ${HEADERS} DESTINATION ${HEADERS_INSTALL_DIR}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ukui-notification.pc DESTINATION ${PC_INSTALL_DIR}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ukui-notification-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/ukui-notification-config-version.cmake - DESTINATION ${CMAKECONFIG_INSTALL_DIR}) \ No newline at end of file + DESTINATION ${CMAKECONFIG_INSTALL_DIR}) diff --git a/libukui-notification/notification-settings/notification-settings-info.cpp b/libukui-notification/notification-settings/notification-settings-info.cpp new file mode 100644 index 0000000..f5b6283 --- /dev/null +++ b/libukui-notification/notification-settings/notification-settings-info.cpp @@ -0,0 +1,178 @@ +// +// +// +#include "notification-settings-info.h" +#include + +using namespace UkuiNotification; + +class UkuiNotification::NotificationSettingsInfoPrivate +{ +public: + Property prop; + QString name; + QString displayName; + QVariant::Type valueType; + QString defulatValue; //TODO 使用qstring存还是改为模板取对应类型存储? +}; + +NotificationSettingsInfo::NotificationSettingsInfo():d(new NotificationSettingsInfoPrivate) +{ + d->prop = Property::Empty; + d->name = QStringLiteral("empty"); + d->valueType = QVariant::Invalid; +} + +NotificationSettingsInfo::NotificationSettingsInfo(Property property) + : d(new NotificationSettingsInfoPrivate) +{ + d->prop = property; + + switch (property) { + case Property::AutoStartDisturb: + d->name = QStringLiteral("AutoStartDisturb"); + d->displayName = tr("AutoStartDisturb"); + d->valueType = QVariant::Bool; + break; + + case Property::AutoStartStartTime: + d->name = QStringLiteral("AutoStartStartTime"); + d->displayName = tr("AutoStartStartTime"); + d->valueType = QVariant::String; + break; + + case Property::AutoStartEndTime: + d->name = QStringLiteral("AutoStartEndTime"); + d->displayName = tr("AutoStartEndTime"); + d->valueType = QVariant::String; + break; + + case Property::ProjectionScreenDisturb: + d->name = QStringLiteral("ProjectionScreenDisturb"); + d->displayName = tr("ProjectionScreenDisturb"); + d->valueType = QVariant::Bool; + break; + + case Property::FullScreenDisturb: + d->name = QStringLiteral("FullScreenDisturb"); + d->displayName = tr("FullScreenDisturb"); + d->valueType = QVariant::Bool; + break; + + case Property::AlarmClockDisturb: + d->name = QStringLiteral("AlarmClockDisturb"); + d->displayName = tr("AlarmClockDisturb"); + d->valueType = QVariant::Bool; + break; + + case Property::AppNotificationDisturb: + d->name = QStringLiteral("AppNotificationDisturb"); + d->displayName = tr("AppNotificationDisturb"); + d->valueType = QVariant::Bool; + break; + +// case Property::ApplicationName: +// d->name = QStringLiteral("ApplicationName"); +// d->displayName = tr("ApplicationName"); +// d->valueType = QVariant::Bool; +// break; + + case Property::OnOff: + d->name = QStringLiteral("OnOff"); + d->displayName = tr("OnOff"); + d->valueType = QVariant::Bool; + break; + + case Property::voiceOn: + d->name = QStringLiteral("voiceOn"); + d->displayName = tr("voiceOn"); + d->valueType = QVariant::Bool; + break; + + case Property::voiceFilePath: + d->name = QStringLiteral("voiceFilePath"); + d->displayName = tr("voiceFilePath"); + d->valueType = QVariant::String; + break; + + case Property::ShowContentOnScreenlock: + d->name = QStringLiteral("ShowContentOnScreenlock"); + d->displayName = tr("ShowContentOnScreenlock"); + d->valueType = QVariant::Bool; + break; + + case Property::ShowOnScreenlock: + d->name = QStringLiteral("ShowOnScreenlock"); + d->displayName = tr("ShowOnScreenlock"); + d->valueType = QVariant::Bool; + break; + + case Property::Style: + d->name = QStringLiteral("Style"); + d->displayName = tr("Style"); + d->valueType = QVariant::Int; + break; + // NOTE: new properties must also be added to ::fromName() + } +} + +NotificationSettingsInfo::NotificationSettingsInfo(const NotificationSettingsInfo& pi) + : d(new NotificationSettingsInfoPrivate(*pi.d)) +{ +} + +NotificationSettingsInfo::~NotificationSettingsInfo() = default; + +NotificationSettingsInfo& NotificationSettingsInfo::operator=(const NotificationSettingsInfo& rhs) +{ + *d = *rhs.d; + return *this; +} + +bool NotificationSettingsInfo::operator==(const NotificationSettingsInfo& rhs) const +{ + return d->name == rhs.d->name && d->displayName == rhs.d->displayName && + d->prop == rhs.d->prop && d->valueType == rhs.d->valueType; +} + +QString NotificationSettingsInfo::displayName() const +{ + return d->displayName; +} + +QString NotificationSettingsInfo::name() const +{ + return d->name; +} + +Property NotificationSettingsInfo::property() const +{ + return d->prop; +} + +QVariant::Type NotificationSettingsInfo::valueType() const +{ + return d->valueType; +} + +NotificationSettingsInfo NotificationSettingsInfo::fromName(const QString& name) +{ + static QHash propertyHash = { + { QStringLiteral("AutoStartDisturb"), Property::AutoStartDisturb }, + { QStringLiteral("AutoStartStartTime"), Property::AutoStartStartTime }, + { QStringLiteral("AutoStartEndTime"), Property::AutoStartEndTime }, + { QStringLiteral("ProjectionScreenDisturb"), Property::ProjectionScreenDisturb }, + { QStringLiteral("FullScreenDisturb"), Property::FullScreenDisturb }, + { QStringLiteral("AlarmClockDisturb"), Property::AlarmClockDisturb }, + { QStringLiteral("AppNotificationDisturb"), Property::AppNotificationDisturb }, + //{ QStringLiteral("ApplicationName"), Property::ApplicationName }, + { QStringLiteral("OnOff"), Property::OnOff }, + { QStringLiteral("voiceOn"), Property::voiceOn }, + { QStringLiteral("voiceFilePath"), Property::voiceFilePath }, + { QStringLiteral("ShowContentOnScreenlock"), Property::ShowContentOnScreenlock }, + { QStringLiteral("ShowOnScreenlock"), Property::ShowOnScreenlock }, + { QStringLiteral("Style"), Property::Style }, + }; + + return NotificationSettingsInfo(propertyHash.value(name)); +} diff --git a/libukui-notification/notification-settings/notification-settings-info.h b/libukui-notification/notification-settings/notification-settings-info.h new file mode 100644 index 0000000..181c17d --- /dev/null +++ b/libukui-notification/notification-settings/notification-settings-info.h @@ -0,0 +1,73 @@ +// +// +// + +#ifndef _UKUINOTIFICATION_SETTINGS_INFO_H +#define _UKUINOTIFICATION_SETTINGS_INFO_H + +#include +#include +#include +#include "notification-settings-properties.h" + +#include + +namespace UkuiNotification { + +class NotificationSettingsInfoPrivate; +/** + * \class NotificationSettingsInfo NotificationSettingsInfo.h + * + * The NotificationSettingsInfo class can be used to obtain extra information + * about any property. It is commonly used be indexers in order + * to obtain a translatable name of the property along with + * additional information such as if the property should be indexed. + */ +class Q_DECL_IMPORT NotificationSettingsInfo : public QObject +{ + Q_OBJECT +public: + NotificationSettingsInfo(); + NotificationSettingsInfo(Property property); + NotificationSettingsInfo(const NotificationSettingsInfo& pi); + ~NotificationSettingsInfo(); + + NotificationSettingsInfo& operator=(const NotificationSettingsInfo& rhs); + bool operator==(const NotificationSettingsInfo& rhs) const; + + /** + * The enumeration which represents this property + */ + Property property() const; + + /** + * The internal unique name used to refer to the property + */ + QString name() const; + + /** + * A user visible name of the property + */ + QString displayName() const; + + /** + * The type the value of this property should be. + * Eg - Property::Height should be an integer + */ + QVariant::Type valueType() const; + + /** + * Construct a NotificationSettingsInfo from the internal property name. + * The internal property name is case insensitive + */ + static NotificationSettingsInfo fromName(const QString& name); + +private: + const std::unique_ptr d; +}; + +} // namespace +Q_DECLARE_METATYPE(UkuiNotification::NotificationSettingsInfo) + + +#endif // _UKUINOTIFICATION_SETTINGS_INFO_H diff --git a/libukui-notification/notification-settings/notification-settings-private.h b/libukui-notification/notification-settings/notification-settings-private.h new file mode 100644 index 0000000..5c75449 --- /dev/null +++ b/libukui-notification/notification-settings/notification-settings-private.h @@ -0,0 +1,83 @@ +// +// +// +#ifndef NotificationSettingsPrivate_H +#define NotificationSettingsPrivate_H + +#include "notification-settings/notification-settings.h" +#include "ukui-search/UkuiSearchAppInfoTable" + +namespace UkuiNotification { + +#define NOTIFICATION_SETTINGS_FILE_PATH ".config/ukui-notification/" +#define NOTIFICATION_SETTINGS_FILE_NAME "settings.json" + +#define NOTIFICATION_SETTINGS_VERSION "1.0" + +using namespace UkuiSearch; + +class NotificationSettingsPrivate : public QObject +{ + Q_OBJECT +public: + NotificationSettingsPrivate(NotificationSettings::RoleType role, NotificationSettings* parent); + ~NotificationSettingsPrivate(); + +private: + + /** + * @brief checkSettingsFile + * 检查通知配置文件,不存在则初始化生成默认配置文件,存在则根据配置文件版本判断是否更新配置文件。 + */ + void checkSettingsFile(); + + /** + * @brief createSettingsFile + * 初始化创建通知配置文件。 + */ + void createSettingsFile(); + + /** + * @brief updateSettingsFile + * 更新通知配置文件。 + */ + void updateSettingsFile(); + + /** + * @brief getAppInfo + * 从搜索应用数据库服务中获取desktop信息。 + * @param appInfo + * appInfo中的key为QString类型的desktop文件路径,value为QStringList类型的数据集,目前依次包括local_name、icon,可扩展 + */ + void getAppInfo(QMap appInfoMap); + + void getInitConfigData(QJsonDocument &jsonDocData); + + void initAppDataServiceConnection(); + +public Q_SLOTS: + + void desktopFileAdd(QVector data); + void desktopFileUpdate(QVector data); + void desktopFileDelete(QStringList data); + + void settingsFileChanged(); + +private: + + void initSettingsFileWatchConnections(); + void loadSettingsData(); + void save2SettingsFile(QString fileName, QJsonDocument &jsonDocData); + + NotificationSettings::RoleType m_role; + QVariantMap m_disturbSettings; + QMap m_allAppSettingsMap; + + AppInfoTable * m_appDataServiceClient = nullptr; + + NotificationSettings * q = nullptr; + +}; + +} +#endif // NotificationSettingsPrivate_H diff --git a/libukui-notification/notification-settings/notification-settings-properties.h b/libukui-notification/notification-settings/notification-settings-properties.h new file mode 100644 index 0000000..2664668 --- /dev/null +++ b/libukui-notification/notification-settings/notification-settings-properties.h @@ -0,0 +1,48 @@ +// +// +// +#ifndef UKUINOTIFICATION_SETTINGS_PROPERTIES +#define UKUINOTIFICATION_SETTINGS_PROPERTIES +#include +#include + +namespace UkuiNotification { + +/** + * @brief The Property enum contains all settings property types + * + */ +enum Property { + Empty = 0, + + AutoStartDisturb, //是否在时间段内自动开启勿扰模式,bool类型,默认值false + AutoStartStartTime, //勿扰模式开启时间,QString类型,默认值00:00 + AutoStartEndTime, //勿扰模式结束时间,QString类型,默认值00:00 + ProjectionScreenDisturb, //多屏启动时是否启动勿扰模式,bool类型,默认值false + FullScreenDisturb, //全屏状态下是否启动勿扰模式,bool类型,默认值false + AlarmClockDisturb, //勿扰模式下是否允许闹钟提示,bool类型,默认值false + AppNotificationDisturb, //是否获取来自应用程序的通知,bool类型,默认值true + + //single app notification settings + //ApplicationName, + OnOff, //是否接收当前应用通知,bool类型,默认值true + voiceOn, //是否打开通知提示声,bool类型,默认值true + voiceFilePath, //自定义提示声文件位置,QString类型,默认值为空 + ShowContentOnScreenlock, //锁屏界面是否显示通知内容信息,bool类型,默认值false + ShowOnScreenlock, //锁屏界面是否显示通知,bool类型,默认值false + Style, //通知样式,appNotificationStyle类型,默认值Mutative + + //single app notification settings中的 Style 类型取值范围 + Mutative, //横幅模式,显示在右上角,会自动消失 + Always, //提示模式,会保留在屏幕上,直到被关闭 + None //无,通知不会显示在屏幕上,但是会进入通知中心 +}; + +typedef QMap PropertyMap; +using PropertyMultiMap = QMultiMap; + +} // namespace UkuiNotification + +Q_DECLARE_METATYPE(UkuiNotification::Property) + +#endif diff --git a/libukui-notification/notification-settings/notification-settings-server.cpp b/libukui-notification/notification-settings/notification-settings-server.cpp new file mode 100644 index 0000000..5f56fd6 --- /dev/null +++ b/libukui-notification/notification-settings/notification-settings-server.cpp @@ -0,0 +1,147 @@ +// +// +// +#include "notification-settings-server.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace UkuiNotification; + +NotificationSettingsPrivate::NotificationSettingsPrivate() + :NotificationSettings(NotificationSettings::RoleType::SERVICE) + ,m_appDataServiceClient(new AppInfoTable(this)) +{ + checkSettingsFile(); +} + +NotificationSettingsPrivate::~NotificationSettingsPrivate() +{ +} + +void NotificationSettingsPrivate::checkSettingsFile() +{ + QFile settingsFile(QDir::homePath() + "/" + NOTIFICATION_SETTINGS_FILE_PATH + NOTIFICATION_SETTINGS_FILE_NAME); + if (!settingsFile.exists()) { + createSettingsFile(); + } + + if (!settingsFile.open(QFile::ReadOnly)) { + qWarning() << "NotificationSettingsPrivate: configuration file " << settingsFile.fileName() << "open failed !"; + return; + } + // 读取json数据 + QByteArray byteArray = settingsFile.readAll(); + settingsFile.close(); + QJsonParseError errRpt; + QJsonDocument jsonDocument(QJsonDocument::fromJson(byteArray, &errRpt)); + if (errRpt.error != QJsonParseError::NoError) { + qWarning() << "NotificationSettingsPrivate: Incorrect configuration files. JSON parse error"; + return; + } + + // 获取到Json字符串的根节点 + QJsonObject rootObject = jsonDocument.object(); + QString version = rootObject.find("VERSION").value().toString(); + if (version != QString(NOTIFICATION_SETTINGS_VERSION)) { + qDebug() << "Notification settings version is diffrent, need update! configFile:" << version << " new:" << NOTIFICATION_SETTINGS_VERSION; + updateSettingsFile(); + } +} + +void NotificationSettingsPrivate::createSettingsFile() +{ + QDir dir; + QString configFileDir(QDir::homePath() + "/" + NOTIFICATION_SETTINGS_FILE_PATH); + if (!dir.exists(configFileDir)) { + if (!dir.mkdir(configFileDir)) { + qWarning() << "Unable to create notification settings config file."; + return; + } + } + + QFile settingsFile(QDir::homePath() + "/" + NOTIFICATION_SETTINGS_FILE_PATH + NOTIFICATION_SETTINGS_FILE_NAME); + settingsFile.open(QFile::WriteOnly); + QJsonDocument configData; + getInitConfigData(configData); + if (settingsFile.write(configData.toJson()) == -1) { + qWarning() << "NotificationSettingsPrivate: Error saving configuration file."; + } + settingsFile.flush(); + settingsFile.close(); +} + +void NotificationSettingsPrivate::updateSettingsFile() +{ +//TODO 考虑不同版本设置项名称修改或扩展,考虑高低版本互相切换逻辑保留用户配置 +} + +void NotificationSettingsPrivate::getAppInfo(QMap appInfoMap) +{ + QVector appInfoResults; + if (!m_appDataServiceClient->getAppInfoResults(appInfoResults)) { + qWarning() << "NotificationSettingsPrivate: Error getAppInfoResults." << m_appDataServiceClient->lastError(); + return; + } + for (AppInfoResult &appInfo : appInfoResults) { + appInfoMap.insert(appInfo.desktopPath, QStringList() << appInfo.appLocalName << appInfo.iconName); + } +} + +void NotificationSettingsPrivate::getInitConfigData(QJsonDocument &jsonDocData) +{ + QJsonArray settingsDataArray; + QJsonObject rootObject; + rootObject.insert("VERSION", QString(NOTIFICATION_SETTINGS_VERSION)); + rootObject.insert(QString(this->disturbMetaEnum().valueToKey(DisturbSettingsName::AutoStartDisturb)), "false"); + rootObject.insert(QString(this->disturbMetaEnum().valueToKey(DisturbSettingsName::AutoStartStartTime)), "00:00"); + rootObject.insert(QString(this->disturbMetaEnum().valueToKey(DisturbSettingsName::AutoStartEndTime)), "00:00"); + rootObject.insert(QString(this->disturbMetaEnum().valueToKey(DisturbSettingsName::ProjectionScreenDisturb)), "false"); + rootObject.insert(QString(this->disturbMetaEnum().valueToKey(DisturbSettingsName::FullScreenDisturb)), "false"); + rootObject.insert(QString(this->disturbMetaEnum().valueToKey(DisturbSettingsName::AlarmClockDisturb)), "false"); + rootObject.insert(QString(this->disturbMetaEnum().valueToKey(DisturbSettingsName::AppNotificationDisturb)), "true"); + + QMap appInfoMap; + getAppInfo(appInfoMap); + for (auto &appInfoPair : appInfoMap) { + QJsonObject appObject; + appObject.insert(QString(this->appNotificationSettingsMetaEnum().valueToKey(AppNotificationSettingsName::OnOff)), "true"); + appObject.insert(QString(this->appNotificationSettingsMetaEnum().valueToKey(AppNotificationSettingsName::voiceOn)), "true"); + appObject.insert(QString(this->appNotificationSettingsMetaEnum().valueToKey(AppNotificationSettingsName::voiceFilePath)), ""); + appObject.insert(QString(this->appNotificationSettingsMetaEnum().valueToKey(AppNotificationSettingsName::ShowContentOnScreenlock)), "false"); + appObject.insert(QString(this->appNotificationSettingsMetaEnum().valueToKey(AppNotificationSettingsName::ShowOnScreenlock)), "false"); + appObject.insert(QString(this->appNotificationSettingsMetaEnum().valueToKey(AppNotificationSettingsName::Style)), QString(this->appNotificationStyleMetaEnum().valueToKey(AppNotificationStyle::Mutative))); + rootObject.insert(appInfoPair.first(), appObject); + } + settingsDataArray.append(rootObject); + jsonDocData.setArray(settingsDataArray); +} + +void NotificationSettingsPrivate::initAppDataServiceConnection() +{ + connect(m_appDataServiceClient, &AppInfoTable::DBOpenFailed, this, [&] (){ + qWarning() << "NotificationSettingsPrivate: app database server open failed! " << m_appDataServiceClient->lastError(); + }); + connect(m_appDataServiceClient, &AppInfoTable::appDBItems2BAdd, this, &NotificationSettingsPrivate::desktopFileAdd); + connect(m_appDataServiceClient, &AppInfoTable::appDBItems2BUpdate, this, &NotificationSettingsPrivate::desktopFileUpdate); + connect(m_appDataServiceClient, &AppInfoTable::appDBItems2BDelete, this, &NotificationSettingsPrivate::desktopFileDelete); +} + +void NotificationSettingsPrivate::desktopFileAdd(QVector data) +{ + +} + +void NotificationSettingsPrivate::desktopFileUpdate(QVector data) +{ + +} + +void NotificationSettingsPrivate::desktopFileDelete(QStringList data) +{ + +} diff --git a/libukui-notification/notification-settings/notification-settings.cpp b/libukui-notification/notification-settings/notification-settings.cpp new file mode 100644 index 0000000..62c8df9 --- /dev/null +++ b/libukui-notification/notification-settings/notification-settings.cpp @@ -0,0 +1,220 @@ +// +// +// + +#include "notification-settings.h" +#include "notification-settings-private.h" +#include "notification-settings-properties.h" +#include "notification-settings-info.h" +#include +#include +#include +#include +#include +#include +#include +using namespace UkuiNotification; + +NotificationSettingsPrivate::NotificationSettingsPrivate(NotificationSettings::RoleType role, NotificationSettings* parent) + :m_appDataServiceClient(new AppInfoTable(this)) + ,m_role(role) + ,QObject(parent) + ,q(parent) +{ + checkSettingsFile(); + if (m_role == NotificationSettings::RoleType::SERVICE) { + initAppDataServiceConnection(); + } else if (m_role == NotificationSettings::RoleType::CLIENT) { + loadSettingsData(); + initSettingsFileWatchConnections(); + } +} + +NotificationSettingsPrivate::~NotificationSettingsPrivate() +{ +} + +void NotificationSettingsPrivate::checkSettingsFile() +{ + QFile settingsFile(QDir::homePath() + "/" + NOTIFICATION_SETTINGS_FILE_PATH + NOTIFICATION_SETTINGS_FILE_NAME); + if (!settingsFile.exists()) { + createSettingsFile(); + } + + if (!settingsFile.open(QFile::ReadOnly)) { + qWarning() << "NotificationSettingsPrivate: configuration file " << settingsFile.fileName() << "open failed !"; + return; + } + // 读取json数据 + QByteArray byteArray = settingsFile.readAll(); + settingsFile.close(); + QJsonParseError errRpt; + QJsonDocument jsonDocument(QJsonDocument::fromJson(byteArray, &errRpt)); + if (errRpt.error != QJsonParseError::NoError) { + qWarning() << "NotificationSettingsPrivate: Incorrect configuration files. JSON parse error"; + return; + } + + // 获取到Json字符串的根节点 + QJsonObject rootObject = jsonDocument.object(); + QString version = rootObject.find("VERSION").value().toString(); + if (version != QString(NOTIFICATION_SETTINGS_VERSION)) { + qDebug() << "Notification settings version is diffrent, need update! configFile:" << version << " new:" << NOTIFICATION_SETTINGS_VERSION; + updateSettingsFile(); + } +} + +void NotificationSettingsPrivate::createSettingsFile() +{ + QDir dir; + QString configFileDir(QDir::homePath() + "/" + NOTIFICATION_SETTINGS_FILE_PATH); + if (!dir.exists(configFileDir)) { + if (!dir.mkdir(configFileDir)) { + qWarning() << "Unable to create notification settings config file."; + return; + } + } + + QFile settingsFile(QDir::homePath() + "/" + NOTIFICATION_SETTINGS_FILE_PATH + NOTIFICATION_SETTINGS_FILE_NAME); + settingsFile.open(QFile::WriteOnly); + QJsonDocument configData; + getInitConfigData(configData); + if (settingsFile.write(configData.toJson()) == -1) { + qWarning() << "NotificationSettingsPrivate: Error saving configuration file."; + } + settingsFile.flush(); + settingsFile.close(); +} + +void NotificationSettingsPrivate::updateSettingsFile() +{ +//TODO 考虑不同版本设置项名称修改或扩展,考虑高低版本互相切换逻辑保留用户配置 +} + +void NotificationSettingsPrivate::getAppInfo(QMap appInfoMap) +{ + QVector appInfoResults; + if (!m_appDataServiceClient->getAppInfoResults(appInfoResults)) { + qWarning() << "NotificationSettingsPrivate: Error getAppInfoResults." << m_appDataServiceClient->lastError(); + return; + } + for (AppInfoResult &appInfo : appInfoResults) { + appInfoMap.insert(appInfo.desktopPath, QStringList() << appInfo.appLocalName << appInfo.iconName); + } +} + +void NotificationSettingsPrivate::getInitConfigData(QJsonDocument &jsonDocData) +{ + QJsonArray settingsDataArray; + QJsonObject rootObject; + rootObject.insert("VERSION", QString(NOTIFICATION_SETTINGS_VERSION)); + rootObject.insert(NotificationSettingsInfo(Property::AutoStartDisturb).name(), "false"); + rootObject.insert(NotificationSettingsInfo(Property::AutoStartStartTime).name(), "00:00"); + rootObject.insert(NotificationSettingsInfo(Property::AutoStartEndTime).name(), "00:00"); + rootObject.insert(NotificationSettingsInfo(Property::ProjectionScreenDisturb).name(), "false"); + rootObject.insert(NotificationSettingsInfo(Property::FullScreenDisturb).name(), "false"); + rootObject.insert(NotificationSettingsInfo(Property::AlarmClockDisturb).name(), "false"); + rootObject.insert(NotificationSettingsInfo(Property::AppNotificationDisturb).name(), "true"); + + QMap appInfoMap; + getAppInfo(appInfoMap); + for (auto &appInfoPair : appInfoMap) { + QJsonObject appObject; + appObject.insert(NotificationSettingsInfo(Property::OnOff).name(), "true"); + appObject.insert(NotificationSettingsInfo(Property::voiceOn).name(), "true"); + appObject.insert(NotificationSettingsInfo(Property::voiceFilePath).name(), ""); + appObject.insert(NotificationSettingsInfo(Property::ShowContentOnScreenlock).name(), "false"); + appObject.insert(NotificationSettingsInfo(Property::ShowOnScreenlock).name(), "false"); + appObject.insert(NotificationSettingsInfo(Property::Style).name(), NotificationSettingsInfo(Property::Mutative).name()); + rootObject.insert(appInfoPair.first(), appObject); + } + settingsDataArray.append(rootObject); + jsonDocData.setArray(settingsDataArray); +} + +void NotificationSettingsPrivate::initAppDataServiceConnection() +{ + connect(m_appDataServiceClient, &AppInfoTable::DBOpenFailed, this, [&] (){ + qWarning() << "NotificationSettingsPrivate: app database server open failed! " << m_appDataServiceClient->lastError(); + }); + connect(m_appDataServiceClient, &AppInfoTable::appDBItems2BAdd, this, &NotificationSettingsPrivate::desktopFileAdd); + connect(m_appDataServiceClient, &AppInfoTable::appDBItems2BUpdate, this, &NotificationSettingsPrivate::desktopFileUpdate); + connect(m_appDataServiceClient, &AppInfoTable::appDBItems2BDelete, this, &NotificationSettingsPrivate::desktopFileDelete); +} + +void NotificationSettingsPrivate::desktopFileAdd(QVector data) +{ + +} + +void NotificationSettingsPrivate::desktopFileUpdate(QVector data) +{ + +} + +void NotificationSettingsPrivate::desktopFileDelete(QStringList data) +{ + +} + +void NotificationSettingsPrivate::initSettingsFileWatchConnections() +{ + +} + +void NotificationSettingsPrivate::loadSettingsData() +{ + +} + +void NotificationSettingsPrivate::save2SettingsFile(QString fileName, QJsonDocument &jsonDocData) +{ + QFile file(fileName); + if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) + { + qWarning() << QString("NotificationSettings: Fail to open JSON file: %1, %2, %3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__); + return; + } + QTextStream out(&file); + out << jsonDocData.toJson(QJsonDocument::Indented); + file.close(); +} + +void NotificationSettingsPrivate::settingsFileChanged() +{ + +} + + +NotificationSettings::NotificationSettings(NotificationSettings::RoleType role) +{ + d = new NotificationSettingsPrivate(role, this); +} + +void NotificationSettings::getDisturbSettings(QVariantMap &disturbSettingsMap) +{ + +} + +void NotificationSettings::getAllAppSettings(QMap &allAppSettingsMap) +{ + +} + +void NotificationSettings::getAppSettings(QString appDesktopName, QVariantMap &appSettingsMap) +{ + +} + +void NotificationSettings::setDisturbSettings(QString settingsName, QVariant value) +{ + +} + +void NotificationSettings::setAppNotificationSettings(QString appDesktopPath, QString settingsName, QVariant value) +{ + +} + + + diff --git a/libukui-notification/notification-settings/notification-settings.h b/libukui-notification/notification-settings/notification-settings.h new file mode 100644 index 0000000..e970783 --- /dev/null +++ b/libukui-notification/notification-settings/notification-settings.h @@ -0,0 +1,92 @@ +// +// + +#ifndef UKUI_NOTIFICATION_NOTIFICATION_SETTINGS_H +#define UKUI_NOTIFICATION_NOTIFICATION_SETTINGS_H + +#include +#include +#include +#include +namespace UkuiNotification { + +class NotificationSettingsPrivate; +class NotificationSettings : public QObject +{ + Q_OBJECT +public: + + enum RoleType { + SERVICE = 0, + CLIENT + }; + Q_ENUM(RoleType) + +public: + NotificationSettings(NotificationSettings::RoleType role); + ~NotificationSettings() = default; + + /** + * @brief getDisturbSettings + * 获取当前用户的勿扰模式配置信息。 + * @param disturbSettingsMap + * disturbSettingsMap中的key为qstring类型的disturbSettingsName中的全部项目,例如:"AutoStartDisturb", + * value为disturbSettingsName中每一项对应的类型数据,例如AutoStartDisturb对应的bool类型数据。 + */ + void getDisturbSettings(QVariantMap &disturbSettingsMap); + + /** + * @brief getAllAppSettings + * 获取当前用户全部应用通知相关配置信息。 + * @param allAppSettingsMap + * allAppSettingsMap中的key为qstring类型的应用desktop文件路径内容,value为当前应用的配置项存储在QVariantMap中。 + * QVariantMap中的key为qstring类型的appNotificationSettingsName中的全部项目,例如:"OnOff", + * value为appNotificationSettingsName中每一项对应的类型数据,例如OnOff对应的bool类型数据。 + */ + void getAllAppSettings(QMap &allAppSettingsMap); + + /** + * @brief getAppSettings + * 获取当前用户某个应用通知相关配置信息。 + * @param appDesktopName + * 指定查询的app,qstring类型的应用desktop文件路径内容 + * @param appSettingsMap + * QVariantMap中的key为qstring类型的appNotificationSettingsName中的全部项目,例如:"OnOff", + * value为appNotificationSettingsName中每一项对应的类型数据,例如OnOff对应的bool类型数据。 + */ + void getAppSettings(QString appDesktopName, QVariantMap &appSettingsMap); + + /** + * @brief setSettings + * 设置当前用户的某个勿扰相关配置信息 + * @param settingsName + * disturbSettingsName中的各项内容 + * @param value + * disturbSettingsName中的各项内容对应的类型数据 + */ + void setDisturbSettings(QString settingsName, QVariant value); + + /** + * @brief setAppNotificationSettings + * 设置当前用户的某个应用通知相关配置信息 + * @param appDesktopPath + * 应用desktop文件路径信息 + * @param settingsName + * 需要修改的应用配置项,包括appNotificationSettingsName中的各项配置名称 + * @param value + * appNotificationSettingsName中的各项配置名称对应的类型数据 + */ + void setAppNotificationSettings(QString appDesktopPath, QString settingsName, QVariant value); + +Q_SIGNALS: + + void settingsDataChanged(); + +private: + + NotificationSettingsPrivate * d = nullptr; + +}; +} + +#endif diff --git a/notification-server/CMakeLists.txt b/notification-server/CMakeLists.txt index 4e22560..51aa61e 100644 --- a/notification-server/CMakeLists.txt +++ b/notification-server/CMakeLists.txt @@ -43,4 +43,4 @@ else() endif() set(DBUS_SERVICE_FILE data/org.freedesktop.Notifications.service) install(TARGETS ukui-notification-server DESTINATION /usr/bin) -install(FILES ${DBUS_SERVICE_FILE} DESTINATION /usr/share/dbus-1/services) \ No newline at end of file +install(FILES ${DBUS_SERVICE_FILE} DESTINATION /usr/share/dbus-1/services)