Skip to content

Commit 5d34ef7

Browse files
committed
fix: Avoid empty notification.
as title Log: as title
1 parent f204b7a commit 5d34ef7

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

panels/notification/common/dbaccessor.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,31 @@ class Benchmark
111111
DBAccessor::DBAccessor(const QString &key)
112112
: m_key(key)
113113
{
114-
const auto dataPath = notificationDBPath();
114+
auto dataPath = notificationDBPath();
115115
qInfo(notifyLog) << "DBAccessor's key:" << m_key;
116+
117+
auto isDatabaseCorrupted = [this]() {
118+
QSqlQuery query(m_connection);
119+
if (!query.exec("SELECT 1")) {
120+
qDebug() << "Database maybe corrupted, cannot execute basic query!";
121+
m_connection.close();
122+
return true;
123+
}
124+
return false;
125+
};
126+
116127
if (!dataPath.isEmpty() && open(dataPath)) {
117-
tryToCreateTable();
128+
bool dbOpened = true;
129+
if (isDatabaseCorrupted()) {
130+
QString dirPath = dataPath.left(dataPath.lastIndexOf('/'));
131+
dataPath = dirPath + "/" + "data_new_" + QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss") + ".db";
132+
qWarning() << "original database maybe corrupted, create new one:" << dataPath;
133+
dbOpened = open(dataPath);
134+
}
135+
136+
if (dbOpened) {
137+
tryToCreateTable();
138+
}
118139
}
119140
}
120141

@@ -187,8 +208,8 @@ qint64 DBAccessor::addEntity(const NotifyEntity &entity)
187208
query.bindValue(":processedType", entity.processedType());
188209

189210
if (!query.exec()) {
190-
qWarning(notifyDBLog) << "insert value to database failed: " << query.lastError().text() << entity.bubbleId() << entity.cTime();
191-
return 0;
211+
qWarning(notifyDBLog) << "insert value to database failed: " << query.lastError().text() << query.lastQuery() << entity.bubbleId() << entity.cTime();
212+
return -1;
192213
}
193214

194215
// to get entity's id in database

panels/notification/server/dbusadaptor.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ uint DbusAdaptor::Notify(const QString &appName, uint replacesId, const QString
2727
const QString &body, const QStringList &actions, const QVariantMap &hints,
2828
int expireTimeout)
2929
{
30-
return manager()->Notify(appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout);
30+
uint id = manager()->Notify(appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout);
31+
if (id == std::numeric_limits<uint>::max()) {
32+
QDBusError error(QDBusError::InternalError, "Notify failed.");
33+
QDBusMessage reply = QDBusMessage::createError(error);
34+
35+
return QDBusConnection::sessionBus().send(reply);
36+
}
37+
38+
return id;
3139
}
3240

3341
void DbusAdaptor::CloseNotification(uint id)
@@ -62,7 +70,15 @@ uint DDENotificationDbusAdaptor::Notify(const QString &appName, uint replacesId,
6270
const QString &summary, const QString &body, const QStringList &actions, const QVariantMap &hints,
6371
int expireTimeout)
6472
{
65-
return manager()->Notify(appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout);
73+
uint id = manager()->Notify(appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout);
74+
if (id == std::numeric_limits<uint>::max()) {
75+
QDBusError error(QDBusError::InternalError, "Notify failed.");
76+
QDBusMessage reply = QDBusMessage::createError(error);
77+
78+
return QDBusConnection::sessionBus().send(reply);
79+
}
80+
81+
return id;
6682
}
6783

6884
void DDENotificationDbusAdaptor::CloseNotification(uint id)

panels/notification/server/notificationmanager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ uint NotificationManager::Notify(const QString &appName, uint replacesId, const
237237

238238
if (entity.processedType() != NotifyEntity::None) {
239239
qint64 id = m_persistence->addEntity(entity);
240+
if (id == -1) {
241+
return std::numeric_limits<uint>::max();
242+
}
243+
240244
entity.setId(id);
241245

242246
emitRecordCountChanged();

0 commit comments

Comments
 (0)