aboutsummaryrefslogtreecommitdiff
path: root/snotif.c
blob: 5d8b3879bed18ffe8be1dd59a98b2043f9ad3a5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "snotif.h"
#include <stdio.h>
#include <stdlib.h>

char* strnotify_params(const struct NotifyParams* params) {
    char time[80] = "";
    if (params->time)
        strftime(time, 80, "%R:%S", localtime(&params->time));
    static char res[180];
    sprintf(
            res,
            "%s [%s] (timeout: %d, replaces: %u) %s\n",
            time, params->app_name, params->expire_timeout, params->replaces_id, params->summary
           );
    return res;
}

void notifs_list_init(struct NotifsList** list) {
    (*list) = malloc(sizeof(struct NotifsList));
    (*list)->list_size = 0;
    (*list)->element_count = 0;

#define INITIAL_SIZE 8
    (*list)->list = calloc(INITIAL_SIZE, sizeof(struct NotifyParams*));
    if ((*list)->list == NULL) {
        fprintf(stderr, "Failed to allocate memory for notification log!\n");
        exit(1);
    }
    (*list)->list_size = INITIAL_SIZE;
}

void notifs_list_set(struct NotifsList* list, struct NotifyParams* notif) {
    while (notif->id > list->list_size) {
        unsigned int new_size = list->list_size * 2;
        struct NotifyParams** temp = realloc(list->list, new_size * sizeof(struct NotifyParams*));
        if (!temp) {
            fprintf(stderr, "Failed to reallocate memory for notification log!\n");
            exit(1);
        }
        list->list = temp;
        for (unsigned int i = list->list_size; i < new_size; i++) {
            list->list[i] = NULL;
        }
        list->list_size = new_size;
    }

    if (notif->id > list->element_count) {
        list->element_count = notif->id;
    }
    list->list[notif->id - 1] = notif;
}

int notifs_list_set_seen(struct NotifsList* list, unsigned int id, bool read) {
    if (id > list-> list_size)
        return 1;
    list->list[id]->seen = read;
    return 0;
}

void notifs_list_free(struct NotifsList* list) {
    for (unsigned int i = 0; i < list->element_count; i++) {
        if (list->list[i]->app_name)
            free(list->list[i]->app_name);
        if (list->list[i]->app_icon)
            free(list->list[i]->app_icon);
        if (list->list[i]->summary)
            free(list->list[i]->summary);
        if (list->list[i]->body)
            free(list->list[i]->body);
        free(list->list[i]);
    }
    free(list->list);
    free(list);
}

void notifs_list_clear(struct NotifsList* list) {
    for (unsigned int i = 0; i < list->element_count; i++) {
        if (list->list[i]->app_name)
            free(list->list[i]->app_name);
        if (list->list[i]->app_icon)
            free(list->list[i]->app_icon);
        if (list->list[i]->summary)
            free(list->list[i]->summary);
        if (list->list[i]->body)
            free(list->list[i]->body);
        free(list->list[i]);
    }
    list->element_count = 0;
}