Remove unneeded re-definition of EAPI
[framework/uifw/edbus.git] / src / bin / notification_daemon.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdio.h>
6
7 #include <Ecore.h>
8 #include <E_Notification_Daemon.h>
9
10 typedef struct Daemon_Data Daemon_Data;
11 typedef struct Timer_Data Timer_Data;
12
13 struct Timer_Data
14 {
15   Daemon_Data *d;
16   E_Notification *n;
17 };
18
19 struct Daemon_Data
20 {
21   E_Notification_Daemon *daemon;
22   Eina_List *open_notes;
23
24   Eina_List *history;
25   int history_start;
26   int max_history_length;
27   int default_timeout;
28
29   int next_id;
30 };
31
32 void
33 daemon_note_close(Daemon_Data *dd, E_Notification *n, int reason)
34 {
35   printf("Close notification #%d\n", e_notification_id_get(n));
36
37   if (eina_list_data_find(dd->open_notes, n))
38   {
39     dd->open_notes = eina_list_remove(dd->open_notes, n);
40     e_notification_closed_set(n, 1);
41     e_notification_daemon_signal_notification_closed(dd->daemon, e_notification_id_get(n), reason);
42     e_notification_unref(n);
43   }
44 }
45
46 Eina_Bool
47 cb_note_close_timer(void *data)
48 {
49   Timer_Data *td = data;
50
51   if (!e_notification_closed_get(td->n))
52     daemon_note_close(td->d, td->n, E_NOTIFICATION_CLOSED_EXPIRED);
53
54   e_notification_unref(td->n);
55   free(td);
56
57   return ECORE_CALLBACK_CANCEL;
58 }
59
60 void
61 daemon_note_show(Daemon_Data *d, E_Notification *n)
62 {
63   e_notification_ref(n);
64   d->open_notes = eina_list_append(d->open_notes, n); 
65   e_notification_ref(n);
66   d->history = eina_list_append(d->history, n); 
67
68   // adjust history
69   if ((int) eina_list_count(d->history) > d->max_history_length)
70   {
71     E_Notification *old;
72     old = eina_list_data_get(d->history);
73     d->history = eina_list_remove_list(d->history, d->history);
74     d->history_start = e_notification_id_get(old) + 1;
75     e_notification_unref(old);
76   }
77
78   {
79     int timeout;
80
81     timeout = e_notification_timeout_get(n);
82     Timer_Data *td = calloc(1, sizeof(Timer_Data));
83     td->d = d;
84     e_notification_ref(n);
85     td->n = n;
86     ecore_timer_add(timeout == -1 ? d->default_timeout : (float)timeout / 1000, cb_note_close_timer, td);
87   }
88
89   printf("Received notification from %s:\n%s\n%s\n\n", 
90     e_notification_app_name_get(n),
91     e_notification_summary_get(n), e_notification_body_get(n)
92   );
93 }
94
95 E_Notification *
96 daemon_note_open_find(Daemon_Data *d, unsigned int id)
97 {
98   E_Notification *n;
99   Eina_List *l;
100
101   EINA_LIST_FOREACH(d->open_notes, l, n)
102     if (e_notification_id_get(n) == id) return n;
103
104   return NULL;
105 }
106
107 E_Notification *
108 daemon_note_history_find(Daemon_Data *d, int id)
109 {
110   if (id < d->history_start) return NULL;
111
112   // TODO
113
114   return NULL;
115 }
116
117
118
119 int
120 cb_notify(E_Notification_Daemon *daemon, E_Notification *n)
121 {
122   Daemon_Data *dd;
123   unsigned int replaces_id;
124   unsigned int new_id;
125
126   dd = e_notification_daemon_data_get(daemon);
127   replaces_id = e_notification_replaces_id_get(n);
128   if (replaces_id) 
129   {
130     // close old one flagged as replaced
131   }
132
133   new_id = dd->next_id++;
134   e_notification_id_set(n, new_id);
135
136   daemon_note_show(dd, n);
137
138   return new_id;
139 }
140
141 void
142 cb_close_notification(E_Notification_Daemon *daemon, unsigned int notification_id)
143 {
144   Daemon_Data *dd;
145   E_Notification *n;
146   dd = e_notification_daemon_data_get(daemon);
147   n = daemon_note_open_find(dd, notification_id);
148   if (n)
149     daemon_note_close(dd, n, E_NOTIFICATION_CLOSED_REQUESTED);
150   // else send error?
151 }
152
153
154 int
155 main()
156 {
157   E_Notification_Daemon *d;
158   E_Notification *n;
159   Daemon_Data *dd;
160
161
162   ecore_init();
163
164   dd = calloc(1, sizeof(Daemon_Data));
165   dd->open_notes = NULL;
166   dd->history = NULL;
167   dd->next_id = dd->history_start = 1;
168   dd->max_history_length = 5;
169   dd->default_timeout = 5;
170
171   /* set up the daemon */
172   d = e_notification_daemon_add("e_notification_module", "Enlightenment");
173   e_notification_daemon_data_set(d, dd);
174   dd->daemon = d;
175   e_notification_daemon_callback_notify_set(d, cb_notify);
176   e_notification_daemon_callback_close_notification_set(d, cb_close_notification);
177
178   ecore_main_loop_begin();
179   while (dd->open_notes)
180     {
181        n = eina_list_data_get(dd->open_notes);
182        e_notification_unref(n);
183        dd->open_notes = eina_list_remove_list(dd->open_notes, dd->open_notes);
184     }
185   while (dd->history)
186     {
187        n = eina_list_data_get(dd->history);
188        e_notification_unref(n);
189        dd->history = eina_list_remove_list(dd->history, dd->history);
190     }
191   free(dd);
192   e_notification_daemon_free(d);
193   ecore_shutdown();
194
195   return 0;
196 }