diff --git a/panels/notification/common/dbaccessor.cpp b/panels/notification/common/dbaccessor.cpp index 5703702d9..6ef958e36 100644 --- a/panels/notification/common/dbaccessor.cpp +++ b/panels/notification/common/dbaccessor.cpp @@ -111,10 +111,31 @@ class Benchmark DBAccessor::DBAccessor(const QString &key) : m_key(key) { - const auto dataPath = notificationDBPath(); + auto dataPath = notificationDBPath(); qInfo(notifyLog) << "DBAccessor's key:" << m_key; + + auto isDatabaseCorrupted = [this]() { + QSqlQuery query(m_connection); + if (!query.exec("SELECT 1")) { + qDebug() << "Database maybe corrupted, cannot execute basic query!"; + m_connection.close(); + return true; + } + return false; + }; + if (!dataPath.isEmpty() && open(dataPath)) { - tryToCreateTable(); + bool dbOpened = true; + if (isDatabaseCorrupted()) { + QString dirPath = dataPath.left(dataPath.lastIndexOf('/')); + dataPath = dirPath + "/" + "data_new_" + QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss") + ".db"; + qWarning() << "original database maybe corrupted, create new one:" << dataPath; + dbOpened = open(dataPath); + } + + if (dbOpened) { + tryToCreateTable(); + } } } @@ -187,8 +208,8 @@ qint64 DBAccessor::addEntity(const NotifyEntity &entity) query.bindValue(":processedType", entity.processedType()); if (!query.exec()) { - qWarning(notifyDBLog) << "insert value to database failed: " << query.lastError().text() << entity.bubbleId() << entity.cTime(); - return 0; + qWarning(notifyDBLog) << "insert value to database failed: " << query.lastError().text() << query.lastQuery() << entity.bubbleId() << entity.cTime(); + return -1; } // to get entity's id in database diff --git a/panels/notification/server/dbusadaptor.cpp b/panels/notification/server/dbusadaptor.cpp index 9adb6d652..51116236a 100644 --- a/panels/notification/server/dbusadaptor.cpp +++ b/panels/notification/server/dbusadaptor.cpp @@ -27,7 +27,15 @@ uint DbusAdaptor::Notify(const QString &appName, uint replacesId, const QString const QString &body, const QStringList &actions, const QVariantMap &hints, int expireTimeout) { - return manager()->Notify(appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout); + uint id = manager()->Notify(appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout); + if (id == std::numeric_limits::max()) { + QDBusError error(QDBusError::InternalError, "Notify failed."); + QDBusMessage reply = QDBusMessage::createError(error); + + return QDBusConnection::sessionBus().send(reply); + } + + return id; } void DbusAdaptor::CloseNotification(uint id) @@ -62,7 +70,15 @@ uint DDENotificationDbusAdaptor::Notify(const QString &appName, uint replacesId, const QString &summary, const QString &body, const QStringList &actions, const QVariantMap &hints, int expireTimeout) { - return manager()->Notify(appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout); + uint id = manager()->Notify(appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout); + if (id == std::numeric_limits::max()) { + QDBusError error(QDBusError::InternalError, "Notify failed."); + QDBusMessage reply = QDBusMessage::createError(error); + + return QDBusConnection::sessionBus().send(reply); + } + + return id; } void DDENotificationDbusAdaptor::CloseNotification(uint id) diff --git a/panels/notification/server/notificationmanager.cpp b/panels/notification/server/notificationmanager.cpp index 04f99cfee..ed6c70d5b 100644 --- a/panels/notification/server/notificationmanager.cpp +++ b/panels/notification/server/notificationmanager.cpp @@ -237,6 +237,10 @@ uint NotificationManager::Notify(const QString &appName, uint replacesId, const if (entity.processedType() != NotifyEntity::None) { qint64 id = m_persistence->addEntity(entity); + if (id == -1) { + return std::numeric_limits::max(); + } + entity.setId(id); emitRecordCountChanged();