Skip to content

Commit

Permalink
feat: add record count dbus property
Browse files Browse the repository at this point in the history
Export RecordCount as a property and provide notify signal.

Log: add record count dbus property
  • Loading branch information
asterwyx committed Jan 10, 2024
1 parent 78a720d commit 88f726b
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 29 deletions.
3 changes: 2 additions & 1 deletion dde-osd/src/notification/bubblemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ void BubbleManager::Hide()

uint BubbleManager::recordCount()
{
return uint(m_persistence->getRecordCount());
return m_persistence->recordCount();
}

QStringList BubbleManager::GetAppList()
Expand Down Expand Up @@ -834,6 +834,7 @@ void BubbleManager::initConnections()
}

connect(&SignalBridge::ref(), &SignalBridge::actionInvoked, this, &BubbleManager::ActionInvoked);
connect(m_persistence, &AbstractPersistence::recordCountChanged, this, &BubbleManager::recordCountChanged);
}

void BubbleManager::onPrepareForSleep(bool sleep)

Check warning on line 840 in dde-osd/src/notification/bubblemanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'onPrepareForSleep' is never used.
Expand Down
2 changes: 2 additions & 0 deletions dde-osd/src/notification/bubblemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class BubbleManager : public QObject, public QDBusContext
Q_OBJECT
Q_PROPERTY(QString allSetting READ getAllSetting WRITE setAllSetting)
Q_PROPERTY(QString systemSetting READ getSystemSetting WRITE setSystemSetting)
Q_PROPERTY(uint recordCount READ recordCount NOTIFY recordCountChanged)

public:
explicit BubbleManager(AbstractPersistence *persistence,
Expand Down Expand Up @@ -93,6 +94,7 @@ class BubbleManager : public QObject, public QDBusContext

// Extra DBus APIs
void RecordAdded(const QString &);
void recordCountChanged(uint count);
void AppInfoChanged(const QString &id, uint item, QDBusVariant var);
void SystemInfoChanged(uint item, QDBusVariant var);
void AppAddedSignal(const QString &id);
Expand Down
5 changes: 1 addition & 4 deletions dde-osd/src/notification/notifications_dbus_adaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ QString DDENotifyDBus::getAppSetting(const QString &in0)

uint DDENotifyDBus::recordCount()
{
// handle method call org.deepin.dde.Notification1.recordCount
uint out0;
QMetaObject::invokeMethod(parent(), "recordCount", Q_RETURN_ARG(uint, out0));
return out0;
return qvariant_cast< uint >(parent()->property("recordCount"));
}

void DDENotifyDBus::setAppSetting(const QString &in0)
Expand Down
7 changes: 7 additions & 0 deletions dde-osd/src/notification/notifications_dbus_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@ class DDENotifyDBus: public QDBusAbstractAdaptor
" <signal name=\"systemSettingChanged\">\n"
" <arg type=\"s\"/>\n"
" </signal>\n"
" <signal name=\"recordCountChanged\">\n"
" <arg type=\"u\"/>\n"
" </signal>\n"
" <property access=\"readwrite\" type=\"s\" name=\"allSetting\"/>\n"
" <property access=\"readwrite\" type=\"s\" name=\"systemSetting\"/>\n"
" <property access=\"read\" type=\"u\" name=\"recordCount\"/>\n"
" </interface>\n"
"")
public:
Expand All @@ -216,6 +220,8 @@ class DDENotifyDBus: public QDBusAbstractAdaptor
QString systemSetting() const;
void setSystemSetting(const QString &value);

Q_PROPERTY(uint recordCount READ recordCount NOTIFY recordCountChanged)

public Q_SLOTS: // METHODS
void ClearRecords();
void CloseNotification(uint in0);
Expand Down Expand Up @@ -251,6 +257,7 @@ public Q_SLOTS: // METHODS
void appRemoved(const QString &in0);
void appSettingChanged(const QString &in0);
void systemSettingChanged(const QString &in0);
void recordCountChanged(uint count);
void ShowBubble(const QString &in0, uint in1, const QString &in2, const QString &in3, const QString &in4, const QStringList &in5, const QVariantMap &in6, int in7, const QVariantMap &in8);
};

Expand Down
49 changes: 33 additions & 16 deletions dde-osd/src/notification/persistence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static const QString ColumnTimeout = "Timeout";

Persistence::Persistence(QObject *parent)
: AbstractPersistence(parent)
, m_recordCount(0)
{
const QString dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);

Expand All @@ -54,13 +55,27 @@ Persistence::Persistence(QObject *parent)
m_query.setForwardOnly(true);

attemptCreateTable();

QSqlTableModel tableModel(nullptr, m_dbConnection);
tableModel.setTable(TableName_v2);
tableModel.select();

while (tableModel.canFetchMore())
tableModel.fetchMore();

m_recordCount = static_cast<uint>(tableModel.rowCount());
}

void Persistence::addOne(EntityPtr entity)
{
setRecordCount(recordCount() + doAddOne(entity));
}

uint Persistence::doAddOne(EntityPtr entity)
{
// "cancel"表示正在发送蓝牙文件,不需要发送到通知中心
if (entity->body().contains("%") && entity->actions().contains("cancel")) {
return;
return 0;
}
QString sqlCmd = QString("INSERT INTO %1 (").arg(TableName_v2);
sqlCmd += ColumnIcon + ",";
Expand Down Expand Up @@ -98,7 +113,7 @@ void Persistence::addOne(EntityPtr entity)

if (!m_query.exec()) {
qWarning() << "insert value to database failed: " << m_query.lastError().text() << entity->id() << entity->ctime();
return;
return 0;
} else {
#ifdef QT_DEBUG
qDebug() << "insert value done, time is:" << entity->ctime();
Expand All @@ -108,21 +123,23 @@ void Persistence::addOne(EntityPtr entity)
// to get entity's id in database
if (!m_query.exec(QString("SELECT last_insert_rowid() FROM %1;").arg(TableName_v2))) {
qWarning() << "get entity's id failed: " << m_query.lastError().text() << entity->id() << entity->ctime();
return;
} else {
m_query.next();
entity->setStorageId(m_query.value(0).toString());
#ifdef QT_DEBUG
qDebug() << "get entity's id done:" << entity->id();
#endif
}
return 1;
}

void Persistence::addAll(QList<EntityPtr> entities)
{
uint count = 0;
for (EntityPtr entity : entities) {
addOne(entity);
count += doAddOne(entity);

Check warning on line 140 in dde-osd/src/notification/persistence.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Consider using std::accumulate algorithm instead of a raw loop.
}
setRecordCount(recordCount() + count);
}

void Persistence::removeOne(const QString &id)
Expand All @@ -137,6 +154,7 @@ void Persistence::removeOne(const QString &id)
#ifdef QT_DEBUG
qDebug() << "remove value:" << id;
#endif
setRecordCount(recordCount() - 1);
}
}

Expand All @@ -152,6 +170,7 @@ void Persistence::removeApp(const QString &app_name)
#ifdef QT_DEBUG
qDebug() << "remove value:" << app_name;
#endif
setRecordCount(recordCount() - static_cast<uint>(m_query.numRowsAffected()));
}
}

Expand All @@ -166,6 +185,7 @@ void Persistence::removeAll()
#ifdef QT_DEBUG
qDebug() << "remove all done";
#endif
setRecordCount(0);
}

// Remove the unused space
Expand Down Expand Up @@ -389,20 +409,17 @@ QString Persistence::getFrom(int rowCount, const QString &offsetId)
return QJsonDocument(array).toJson();
}

int Persistence::getRecordCount()
uint Persistence::recordCount()
{
QSqlTableModel *tableModel = new QSqlTableModel(this, m_dbConnection);
tableModel->setTable(TableName_v2);
tableModel->select();

while (tableModel->canFetchMore())
tableModel->fetchMore();

int count = tableModel->rowCount();

delete tableModel;
return m_recordCount;
}

return count;
void Persistence::setRecordCount(uint count)
{
if (m_recordCount == count)
return;
m_recordCount = count;
Q_EMIT recordCountChanged(count);
}

void Persistence::attemptCreateTable()
Expand Down
11 changes: 9 additions & 2 deletions dde-osd/src/notification/persistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class NotificationEntity;
class AbstractPersistence : public QObject
{
Q_OBJECT

Q_PROPERTY(uint recordCount READ recordCount NOTIFY recordCountChanged FINAL)

public:
explicit AbstractPersistence(QObject *parent = nullptr)
: QObject(parent)
Expand All @@ -38,10 +41,11 @@ class AbstractPersistence : public QObject
virtual EntityPtr getNotifyById(const QString &id) { return EntityPtr{}; }

virtual QString getFrom(int rowCount, const QString &offsetId) = 0;
virtual int getRecordCount() = 0;
virtual uint recordCount() = 0;

signals:
void RecordAdded(EntityPtr entity);
void recordCountChanged(uint count);
};

class Persistence : public AbstractPersistence
Expand All @@ -50,6 +54,7 @@ class Persistence : public AbstractPersistence
explicit Persistence(QObject *parent = nullptr);

void addOne(EntityPtr entity) override; //向数据库添加一条通知数据
uint doAddOne(EntityPtr entity);
void addAll(QList<EntityPtr> entities) override; //向数据库添加多条通知数据
void removeOne(const QString &id) override; //根据通知的ID,从数据库删除一条通知.
void removeApp(const QString &app_name) override; //根据App名称从数据库删除App组的通知
Expand All @@ -64,9 +69,10 @@ class Persistence : public AbstractPersistence
// If rowcount is - 1, it is obtained from offset + 1 to the last.
QString getFrom(int rowCount, const QString &offsetId) override;

int getRecordCount() override; //获取通知记录有多少条
uint recordCount() override; //获取通知记录有多少条

private:
void setRecordCount(uint count);
void attemptCreateTable(); //在数据库中尝试创建一个表,记录通知信息
QString ConvertMapToString(const QVariantMap &map); //将QVariantMap类型转换为QString类型
QVariantMap ConvertStringToMap(const QString &text); //将QString类型转换为QVariantMap类型
Expand All @@ -80,6 +86,7 @@ class Persistence : public AbstractPersistence
private:
QSqlDatabase m_dbConnection;
QSqlQuery m_query;
uint m_recordCount;
};

#endif // PERSISTENCE_H
4 changes: 2 additions & 2 deletions dde-osd/src/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ void UnitTest::test_Persistence()
QCOMPARE(notify.value("id").toString(), QString("%1").arg(entity->id()));

QList<EntityPtr> entityList= database.getAllNotify();
QCOMPARE(entityList.size(), database.getRecordCount());
QCOMPARE(entityList.size(), database.recordCount());

database.removeAll();
QCOMPARE(database.getRecordCount(), 0);
QCOMPARE(database.recordCount(), 0);
}


2 changes: 1 addition & 1 deletion dde-osd/tests/mockpersistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MockPersistence : public AbstractPersistence
MOCK_METHOD1(addAll, void(QList<EntityPtr>));
MOCK_METHOD1(removeOne, void(const QString&));
MOCK_METHOD1(removeApp, void(const QString&));
MOCK_METHOD0(getRecordCount, int());
MOCK_METHOD0(recordCount, quint32());
MOCK_METHOD1(getById, QString(const QString&));
MOCK_METHOD2(getFrom, QString(int, const QString&));
};
Expand Down
2 changes: 1 addition & 1 deletion dde-osd/tests/notification-center/ut_notifyListview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class UT_NotifyListview : public testing::Test
EXPECT_CALL(*persistence, removeApp(testing::_)).
WillRepeatedly(testing::Invoke(helper, &PersistenceHelper::removeApp));

EXPECT_CALL(*persistence, getRecordCount()).
EXPECT_CALL(*persistence, recordCount()).
WillRepeatedly(testing::Invoke(helper, &PersistenceHelper::getRecordCount));

EXPECT_CALL(*persistence, getById(testing::_)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UT_NotifyCenterWidget : public testing::Test
EXPECT_CALL(persistence, removeApp(testing::_)).
WillRepeatedly(testing::Invoke(&helper, &PersistenceHelper::removeApp));

EXPECT_CALL(persistence, getRecordCount()).
EXPECT_CALL(persistence, recordCount()).
WillRepeatedly(testing::Invoke(&helper, &PersistenceHelper::getRecordCount));

EXPECT_CALL(persistence, getById(testing::_)).
Expand Down
2 changes: 1 addition & 1 deletion dde-osd/tests/notification/ut_bubblemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class UT_BubbleManager : public testing::Test
EXPECT_CALL(*persistence, removeApp(testing::_)).
WillRepeatedly(testing::Invoke(persistencehelper, &PersistenceHelper::removeApp));

EXPECT_CALL(*persistence, getRecordCount()).
EXPECT_CALL(*persistence, recordCount()).
WillRepeatedly(testing::Invoke(persistencehelper, &PersistenceHelper::getRecordCount));

EXPECT_CALL(*persistence, getById(testing::_)).
Expand Down

0 comments on commit 88f726b

Please sign in to comment.