From f2085a9890f68a0a19ee6214ffb2b644001ce0e5 Mon Sep 17 00:00:00 2001 From: iaom Date: Tue, 31 Jan 2023 17:39:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 73 ++++++++++++++ CMakeLists.txt | 20 ++++ README.en.md | 34 +------ README.md | 33 ------- libukui-notification/CMakeLists.txt | 0 notification-server/CMakeLists.txt | 21 ++++ .../dbus/org.freedesktop.Notifications.xml | 41 ++++++++ .../dbus/org.ukui.notificationServer.xml | 7 ++ notification-server/main.cpp | 20 ++++ notification-server/server-private.h | 56 +++++++++++ notification-server/server.cpp | 99 +++++++++++++++++++ notification-server/server.h | 25 +++++ .../translations/ukui-notification_zh_CN.ts | 4 + 13 files changed, 367 insertions(+), 66 deletions(-) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 libukui-notification/CMakeLists.txt create mode 100644 notification-server/CMakeLists.txt create mode 100644 notification-server/dbus/org.freedesktop.Notifications.xml create mode 100644 notification-server/dbus/org.ukui.notificationServer.xml create mode 100644 notification-server/main.cpp create mode 100644 notification-server/server-private.h create mode 100644 notification-server/server.cpp create mode 100644 notification-server/server.h create mode 100644 notification-server/translations/ukui-notification_zh_CN.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2cf9fc5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.14) + +project(ukui-notification LANGUAGES CXX) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOUIC ON) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +include(GNUInstallDirs) +find_package(QT NAMES Qt6 Qt5 COMPONENTS Core LinguistTools DBus REQUIRED) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core LinguistTools DBus REQUIRED) +include_directories() +add_subdirectory(notification-server) +add_subdirectory(libukui-notification) + + diff --git a/README.en.md b/README.en.md index edc975d..73dba88 100644 --- a/README.en.md +++ b/README.en.md @@ -1,36 +1,4 @@ # ukui-notification #### Description -通知中心 - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +ukui-notification diff --git a/README.md b/README.md index c4fcf9e..ceabfa5 100644 --- a/README.md +++ b/README.md @@ -2,36 +2,3 @@ #### 介绍 通知中心 - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/libukui-notification/CMakeLists.txt b/libukui-notification/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/notification-server/CMakeLists.txt b/notification-server/CMakeLists.txt new file mode 100644 index 0000000..f33db43 --- /dev/null +++ b/notification-server/CMakeLists.txt @@ -0,0 +1,21 @@ +set(notificationServer_SRCS + server.h + server-private.h + server.cpp + main.cpp + ) +qt_add_dbus_adaptor(notificationServer_SRCS dbus/org.freedesktop.Notifications.xml server-private.h NotificationServer::ServerPrivate) +qt_add_dbus_adaptor(notificationServer_SRCS dbus/org.ukui.notificationServer.xml server-private.h NotificationServer::ServerPrivate) + +add_executable(ukui-notification-server ${notificationServer_SRCS}) +target_link_libraries(ukui-notification-server + PRIVATE + Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::DBus + ) +set(TS_FILES ukui-notification_zh_CN.ts) +if(COMMAND qt_create_translation) + qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR}/translations ${TS_FILES}) +else() + qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR}/translations ${TS_FILES}) +endif() \ No newline at end of file diff --git a/notification-server/dbus/org.freedesktop.Notifications.xml b/notification-server/dbus/org.freedesktop.Notifications.xml new file mode 100644 index 0000000..55fe73c --- /dev/null +++ b/notification-server/dbus/org.freedesktop.Notifications.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/notification-server/dbus/org.ukui.notificationServer.xml b/notification-server/dbus/org.ukui.notificationServer.xml new file mode 100644 index 0000000..d6bb928 --- /dev/null +++ b/notification-server/dbus/org.ukui.notificationServer.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/notification-server/main.cpp b/notification-server/main.cpp new file mode 100644 index 0000000..1d6114a --- /dev/null +++ b/notification-server/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QTranslator translator; + const QStringList uiLanguages = QLocale::system().uiLanguages(); + for (const QString &locale : uiLanguages) { + const QString baseName = "ukui-notification_" + QLocale(locale).name(); + if (translator.load(":/i18n/" + baseName)) { + a.installTranslator(&translator); + break; + } + } + + return a.exec(); +} diff --git a/notification-server/server-private.h b/notification-server/server-private.h new file mode 100644 index 0000000..8a52b7b --- /dev/null +++ b/notification-server/server-private.h @@ -0,0 +1,56 @@ +// +// Created by zpf on 23-1-31. +// + +#ifndef UKUI_NOTIFICATION_SERVER_PRIVATE_H +#define UKUI_NOTIFICATION_SERVER_PRIVATE_H +#include +#include +class QDBusServiceWatcher; +namespace NotificationServer { + +class ServerPrivate : public QObject, protected QDBusContext +{ + Q_OBJECT +public: + ServerPrivate(QObject *parent); + ~ServerPrivate(); + bool init(); + + static QString notificationServiceName(); + static QString notificationServicePath(); + static QString notificationServiceInterface(); + + QStringList GetCapabilities() const; + uint Notify(const QString &app_name, + uint replaces_id, + const QString &app_icon, + const QString &summary, + const QString &body, + const QStringList &actions, + const QVariantMap &hints, + int timeout); + void CloseNotification(uint id); + QString GetServerInformation(QString &vendor, QString &version, QString &specVersion) const; + + /** + * 注册成为通知中心客户端 + */ + void RegisterClient(); + /** + * 注销 + */ + void UnRegisterClient(); +Q_SIGNALS: + void NotificationClosed(uint id, uint reason); + void ActionInvoked(uint id, const QString &actionKey); + void ActivationToken(uint id, const QString &ActivationToken); +private: + QDBusServiceWatcher *m_notificationWatchers = nullptr; +}; + + + +} // NotificationServer + +#endif //UKUI_NOTIFICATION_SERVER_PRIVATE_H diff --git a/notification-server/server.cpp b/notification-server/server.cpp new file mode 100644 index 0000000..fd130b4 --- /dev/null +++ b/notification-server/server.cpp @@ -0,0 +1,99 @@ +// +// Created by zpf on 23-1-31. +// +#include +#include +#include "server.h" +#include "server-private.h" +#include "notificationsadaptor.h" + +namespace NotificationServer { +ServerPrivate::ServerPrivate(QObject *parent) : QObject(parent), m_notificationWatchers(new QDBusServiceWatcher(this)) +{ + m_notificationWatchers->setConnection(QDBusConnection::sessionBus()); + m_notificationWatchers->setWatchMode(QDBusServiceWatcher::WatchForUnregistration); + connect(m_notificationWatchers, &QDBusServiceWatcher::serviceUnregistered, m_notificationWatchers, &QDBusServiceWatcher::removeWatchedService); +} + +ServerPrivate::~ServerPrivate() = default; + +QStringList ServerPrivate::GetCapabilities() const +{ + return QStringList(); +} + +uint ServerPrivate::Notify(const QString &app_name, uint replaces_id, const QString &app_icon, const QString &summary, + const QString &body, const QStringList &actions, const QVariantMap &hints, int timeout) +{ + return 0; +} + +QString ServerPrivate::GetServerInformation(QString &vendor, QString &version, QString &specVersion) const +{ + return QString(); +} + +void ServerPrivate::CloseNotification(uint id) +{ + +} + +bool ServerPrivate::init() +{ + new NotificationsAdaptor(this); + if(QDBusConnection::sessionBus().registerObject(notificationServicePath(), this)) { + qWarning() << "Failed to register Notification DBus object!"; + return false; + } + auto registration = QDBusConnection::sessionBus().interface()->registerService(notificationServiceName(), + QDBusConnectionInterface::ReplaceExistingService, + QDBusConnectionInterface::DontAllowReplacement); + if (registration.value() != QDBusConnectionInterface::ServiceRegistered) { + qWarning() << "Failed to register Notification service on DBus!"; + return false; + } + return true; +} + +QString ServerPrivate::notificationServiceName() +{ + return QStringLiteral("org.freedesktop.Notifications"); +} + +QString ServerPrivate::notificationServicePath() +{ + return QStringLiteral("/org/freedesktop/Notifications"); +} + +QString ServerPrivate::notificationServiceInterface() +{ + return QStringLiteral("org.freedesktop.Notifications");; +} + +void ServerPrivate::RegisterClient() +{ + m_notificationWatchers->addWatchedService(message().service()); +} + +void ServerPrivate::UnRegisterClient() +{ + m_notificationWatchers->removeWatchedService(message().service()); +} + +Server::Server(QObject *parent): QObject(parent), d(new ServerPrivate(this)) +{ +} + +Server &Server::self() +{ + static Server self; + return self; +} + +bool Server::init() +{ + return d->init(); +} + +Server::~Server() = default; +} // NotificationServer \ No newline at end of file diff --git a/notification-server/server.h b/notification-server/server.h new file mode 100644 index 0000000..0a54cb0 --- /dev/null +++ b/notification-server/server.h @@ -0,0 +1,25 @@ +// +// Created by zpf on 23-1-31. +// + +#ifndef UKUI_NOTIFICATION_SERVER_H +#define UKUI_NOTIFICATION_SERVER_H +#include +namespace NotificationServer { +class ServerPrivate; +class Server : public QObject +{ + Q_OBJECT +public: + static Server &self(); + ~Server(); + bool init(); +private: + explicit Server(QObject *parent = nullptr); + Q_DISABLE_COPY(Server) + ServerPrivate *d = nullptr; +}; + +} // NotificationServer + +#endif //UKUI_NOTIFICATION_SERVER_H diff --git a/notification-server/translations/ukui-notification_zh_CN.ts b/notification-server/translations/ukui-notification_zh_CN.ts new file mode 100644 index 0000000..e5ca8aa --- /dev/null +++ b/notification-server/translations/ukui-notification_zh_CN.ts @@ -0,0 +1,4 @@ + + + +