Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions panels/notification/server/notifyserverapplet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
#include "notifyserverapplet.h"
#include "notificationmanager.h"
#include "dbusadaptor.h"
#include "pluginfactory.h"

Check warning on line 8 in panels/notification/server/notifyserverapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "pluginfactory.h" not found.

#include <QCoreApplication>

Check warning on line 10 in panels/notification/server/notifyserverapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCoreApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QEvent>

Check warning on line 11 in panels/notification/server/notifyserverapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QEvent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QThread>

Check warning on line 12 in panels/notification/server/notifyserverapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QThread> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QLoggingCategory>

Check warning on line 13 in panels/notification/server/notifyserverapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace notification {
Q_DECLARE_LOGGING_CATEGORY(notifyLog)
Expand All @@ -25,13 +27,37 @@
NotifyServerApplet::~NotifyServerApplet()
{
qDebug(notifyLog) << "Exit notification server.";
if (m_manager) {
m_manager->deleteLater();
}

constexpr int kWaitTimeoutMs = 3000;
constexpr int kTerminateWaitMs = 1000;

if (m_worker) {
m_worker->exit();
m_worker->wait();
m_worker->deleteLater();
if (m_worker->isRunning()) {
m_worker->quit();

if (!m_worker->wait(kWaitTimeoutMs)) {
qWarning(notifyLog)
<< "Worker thread did not exit in time, terminating.";

m_worker->terminate();

if (!m_worker->wait(kTerminateWaitMs)) {
qCritical(notifyLog)
<< "Worker thread terminate timeout.";
}
}
}

if (m_manager) {
delete m_manager;
m_manager = nullptr;
}

delete m_worker;
m_worker = nullptr;
} else if (m_manager) {
delete m_manager;
m_manager = nullptr;
}
}

Expand All @@ -42,12 +68,22 @@

bool NotifyServerApplet::init()
{
// Reentrancy protection
if (m_manager || m_worker) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init不会执行多次,不用判断,另外应该是你的那个test存在问题,应该不用测直接赋值m_manager,m_worker这种场景,

qWarning(notifyLog) << "NotifyServerApplet is already initialized.";
return true;
}

DApplet::init();

m_manager = new NotificationManager();

if (!m_manager->registerDbusService()) {
qWarning(notifyLog) << QString("Can't register Notifications to the D-Bus object.");

delete m_manager;
m_manager = nullptr;

return false;
}

Expand All @@ -60,52 +96,66 @@

m_worker = new QThread();
m_manager->moveToThread(m_worker);

// Register Qt asynchronous cleanup
// connect(m_worker, &QThread::finished, m_manager, &QObject::deleteLater);
// connect(m_worker, &QThread::finished, m_worker, &QObject::deleteLater);

m_worker->start();
return true;
}

void NotifyServerApplet::actionInvoked(qint64 id, uint bubbleId, const QString &actionKey)
{
CHECK_MANAGER();
QMetaObject::invokeMethod(m_manager, "actionInvoked", Qt::DirectConnection, Q_ARG(qint64, id), Q_ARG(uint, bubbleId), Q_ARG(QString, actionKey));
}

void NotifyServerApplet::actionInvoked(qint64 id, const QString &actionKey)
{
CHECK_MANAGER();
QMetaObject::invokeMethod(m_manager, "actionInvoked", Qt::DirectConnection, Q_ARG(qint64, id), Q_ARG(QString, actionKey));
}

void NotifyServerApplet::notificationClosed(qint64 id, uint bubbleId, uint reason)
{
CHECK_MANAGER();
QMetaObject::invokeMethod(m_manager, "notificationClosed", Qt::DirectConnection, Q_ARG(qint64, id), Q_ARG(uint, bubbleId), Q_ARG(uint, reason));
}

QVariant NotifyServerApplet::appValue(const QString &appId, int configItem)
{
CHECK_MANAGER_RET({});
return m_manager->GetAppInfo(appId, configItem);
}

void NotifyServerApplet::removeNotification(qint64 id)
{
CHECK_MANAGER();
m_manager->removeNotification(id);
}

void NotifyServerApplet::removeNotifications(const QString &appName)
{
CHECK_MANAGER();
m_manager->removeNotifications(appName);
}

void NotifyServerApplet::removeNotifications()
{
CHECK_MANAGER();
m_manager->removeNotifications();
}

void NotifyServerApplet::removeExpiredNotifications()
{
CHECK_MANAGER();
m_manager->removeExpiredNotifications();
}

void NotifyServerApplet::setBlockClosedId(qint64 id)
{
CHECK_MANAGER();
m_manager->setBlockClosedId(id);
}

Expand Down
20 changes: 18 additions & 2 deletions panels/notification/server/notifyserverapplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@

#pragma once

#include <QPointer>

Check warning on line 7 in panels/notification/server/notifyserverapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPointer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "applet.h"

Check warning on line 8 in panels/notification/server/notifyserverapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "applet.h" not found.

#define CHECK_MANAGER() \
do { \
if (!m_manager) { \
qWarning(notifyLog) << "NotificationManager is null."; \
return; \
} \
} while(0)

#define CHECK_MANAGER_RET(val) \
do { \
if (!m_manager) { \
qWarning(notifyLog) << "NotificationManager is null."; \
return val; \
} \
} while(0)
namespace notification {

class NotificationManager;
Expand Down Expand Up @@ -34,8 +50,8 @@
void setBlockClosedId(qint64 id);

private:
NotificationManager *m_manager = nullptr;
QThread *m_worker = nullptr;
QPointer<NotificationManager> m_manager;
QPointer<QThread> m_worker;
};

}
Loading
Loading