Skip to content

Commit

Permalink
[notificationmanager] Simplify unique notification identifiers allocator
Browse files Browse the repository at this point in the history
The logic for notification identifier allocator is hard to read and it can
in theory return values that are still in active use.

Simplify the code by removing the failure path that is not going to get
triggered in real life.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Aug 28, 2019
1 parent aab8dd3 commit 69564d1
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/notifications/notificationmanager.cpp
Expand Up @@ -498,19 +498,19 @@ QString NotificationManager::systemApplicationName() const

uint NotificationManager::nextAvailableNotificationID()
{
bool idIncreased = false;

// Try to find an unused ID. Increase the ID at least once but only up to 2^32-1 times.
for (uint i = 0; i < UINT32_MAX && (!idIncreased || m_notifications.contains(m_previousNotificationID)); i++, idIncreased = true) {
m_previousNotificationID++;

if (m_previousNotificationID == 0) {
// 0 is not a valid ID so skip it
m_previousNotificationID = 1;
}
/* Find an unused ID. It is assumed that we will never end up
* in a situation where even close to significant portion of
* all possible ID numbers would be in use. If that ever happens
* this will turn into forever loop.
*/
for (;;) {
uint id = ++m_previousNotificationID;
// 0 is not a valid ID so skip it
if (!id)
continue;
if (!m_notifications.contains(id))
return id;
}

return m_previousNotificationID;
}

void NotificationManager::removeNotificationsWithCategory(const QString &category)
Expand Down

0 comments on commit 69564d1

Please sign in to comment.