When traversing both queues, traverse in one loop

On actions, where waiting and displayed has to get traversed, traverse
them both in a single loop. Code duplication isn't necessary anymore.
This commit is contained in:
Benedikt Heine 2018-09-12 18:14:15 +02:00
parent 34e97b3e94
commit e1ee87b5d3

View File

@ -169,7 +169,9 @@ int queues_notification_insert(notification *n)
*/ */
static bool queues_stack_duplicate(notification *n) static bool queues_stack_duplicate(notification *n)
{ {
for (GList *iter = g_queue_peek_head_link(displayed); iter; GQueue *allqueues[] = { displayed, waiting };
for (int i = 0; i < sizeof(allqueues)/sizeof(GList*); i++) {
for (GList *iter = g_queue_peek_head_link(allqueues[i]); iter;
iter = iter->next) { iter = iter->next) {
notification *orig = iter->data; notification *orig = iter->data;
if (notification_is_duplicate(orig, n)) { if (notification_is_duplicate(orig, n)) {
@ -181,41 +183,18 @@ static bool queues_stack_duplicate(notification *n)
} else { } else {
orig->progress = n->progress; orig->progress = n->progress;
} }
iter->data = n; iter->data = n;
n->dup_count = orig->dup_count;
signal_notification_closed(orig, 1);
if ( allqueues[i] == displayed )
n->start = time_monotonic_now(); n->start = time_monotonic_now();
n->dup_count = orig->dup_count;
signal_notification_closed(orig, 1);
notification_free(orig); notification_free(orig);
return true; return true;
} }
} }
for (GList *iter = g_queue_peek_head_link(waiting); iter;
iter = iter->next) {
notification *orig = iter->data;
if (notification_is_duplicate(orig, n)) {
/* If the progress differs, probably notify-send was used to update the notification
* So only count it as a duplicate, if the progress was not the same.
* */
if (orig->progress == n->progress) {
orig->dup_count++;
} else {
orig->progress = n->progress;
}
iter->data = n;
n->dup_count = orig->dup_count;
signal_notification_closed(orig, 1);
notification_free(orig);
return true;
}
} }
return false; return false;
@ -224,31 +203,26 @@ static bool queues_stack_duplicate(notification *n)
/* see queues.h */ /* see queues.h */
bool queues_notification_replace_id(notification *new) bool queues_notification_replace_id(notification *new)
{ {
GQueue *allqueues[] = { displayed, waiting };
for (GList *iter = g_queue_peek_head_link(displayed); for (int i = 0; i < sizeof(allqueues)/sizeof(GList*); i++) {
for (GList *iter = g_queue_peek_head_link(allqueues[i]);
iter; iter;
iter = iter->next) { iter = iter->next) {
notification *old = iter->data; notification *old = iter->data;
if (old->id == new->id) { if (old->id == new->id) {
iter->data = new; iter->data = new;
new->dup_count = old->dup_count;
if ( allqueues[i] == displayed ) {
new->start = time_monotonic_now(); new->start = time_monotonic_now();
new->dup_count = old->dup_count;
notification_run_script(new); notification_run_script(new);
}
notification_free(old); notification_free(old);
return true; return true;
} }
} }
for (GList *iter = g_queue_peek_head_link(waiting);
iter;
iter = iter->next) {
notification *old = iter->data;
if (old->id == new->id) {
iter->data = new;
new->dup_count = old->dup_count;
notification_free(old);
return true;
}
} }
return false; return false;
} }
@ -258,25 +232,17 @@ void queues_notification_close_id(int id, enum reason reason)
{ {
notification *target = NULL; notification *target = NULL;
for (GList *iter = g_queue_peek_head_link(displayed); iter; GQueue *allqueues[] = { displayed, waiting };
for (int i = 0; i < sizeof(allqueues)/sizeof(GList*); i++) {
for (GList *iter = g_queue_peek_head_link(allqueues[i]); iter;
iter = iter->next) { iter = iter->next) {
notification *n = iter->data; notification *n = iter->data;
if (n->id == id) { if (n->id == id) {
g_queue_remove(displayed, n); g_queue_remove(allqueues[i], n);
target = n; target = n;
break; break;
} }
} }
for (GList *iter = g_queue_peek_head_link(waiting); iter;
iter = iter->next) {
notification *n = iter->data;
if (n->id == id) {
assert(target == NULL);
g_queue_remove(waiting, n);
target = n;
break;
}
} }
if (target) { if (target) {