stringshare stuff in e-notify
[framework/uifw/edbus.git] / src / lib / notification / notification.c
1 #include "E_Notify.h"
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <inttypes.h>
6 #include "e_notify_private.h"
7
8 /* private functions */
9 static Eina_List * e_notification_action_list_new();
10 static E_Notification_Action *e_notification_action_new(const char *id, const char *name);
11
12 /* (con|de)structor */
13
14 EAPI E_Notification *
15 e_notification_full_new(const char *app_name, unsigned int replaces_id, const char *app_icon, const char *summary, const char *body, int expire_timeout)
16 {
17   E_Notification *n;
18
19   n = e_notification_new();
20   if (!n) return NULL;
21
22   n->app_name = eina_stringshare_add(app_name); 
23   n->replaces_id = replaces_id;
24   n->app_icon = eina_stringshare_add(app_icon); 
25   n->summary = eina_stringshare_add(summary); 
26   n->body = eina_stringshare_add(body);
27   n->expire_timeout = expire_timeout;
28
29
30   return n;
31 }
32
33 EAPI E_Notification *
34 e_notification_new()
35 {
36   E_Notification *n;
37   n = calloc(1, sizeof(E_Notification));
38   if (!n) return NULL;
39   n->refcount = 1;
40
41   return n;
42 }
43
44 EAPI void
45 e_notification_ref(E_Notification *n)
46 {
47   n->refcount++;
48 }
49
50 EAPI void
51 e_notification_unref(E_Notification *n)
52 {
53   if (--n->refcount == 0) e_notification_free(n);
54 }
55
56 EAPI void
57 e_notification_free(E_Notification *n)
58 {
59   if (!n) return;
60
61   eina_stringshare_del(n->app_name);
62   eina_stringshare_del(n->app_icon);
63   eina_stringshare_del(n->summary);
64   eina_stringshare_del(n->body);
65
66   eina_list_free(n->actions);
67
68   eina_stringshare_del(n->hints.category);
69   eina_stringshare_del(n->hints.desktop);
70   eina_stringshare_del(n->hints.sound_file);
71   if (n->hints.image_data) e_notification_image_free(n->hints.image_data);
72   if (n->hints.icon_data) e_notification_image_free(n->hints.icon_data);
73   free(n);
74 }
75
76 /* mutators */
77 EAPI void
78 e_notification_id_set(E_Notification *note, unsigned int id)
79 {
80   note->id = id;
81 }
82
83 EAPI void
84 e_notification_app_name_set(E_Notification *note, const char *app_name)
85 {
86   eina_stringshare_replace(&note->app_name, app_name);
87 }
88
89 EAPI void
90 e_notification_app_icon_set(E_Notification *note, const char *app_icon)
91 {
92   eina_stringshare_replace(&note->app_icon, app_icon);
93 }
94
95 EAPI void
96 e_notification_summary_set(E_Notification *note, const char *summary)
97 {
98   eina_stringshare_replace(&note->summary, summary);
99 }
100
101 EAPI void
102 e_notification_body_set(E_Notification *note, const char *body)
103 {
104   eina_stringshare_replace(&note->body, body);
105 }
106
107 EAPI void
108 e_notification_action_add(E_Notification *n, const char *action_id, const char *action_name)
109 {
110   E_Notification_Action *a;
111
112   if (!n->actions) 
113     n->actions = e_notification_action_list_new();
114
115   a = e_notification_action_new(action_id, action_name);
116   n->actions = eina_list_append(n->actions, a);
117 }
118
119
120 EAPI void
121 e_notification_replaces_id_set(E_Notification *note, int replaces_id)
122 {
123   note->replaces_id = replaces_id;
124 }
125
126 EAPI void
127 e_notification_timeout_set(E_Notification *note, int timeout)
128 {
129   note->expire_timeout = timeout;
130 }
131
132 EAPI void
133 e_notification_closed_set(E_Notification *note, unsigned char closed)
134 {
135   note->closed = closed;
136 }
137
138
139 /* accessors */
140 EAPI unsigned int
141 e_notification_id_get(E_Notification *note)
142 {
143   return note->id;
144 }
145
146 EAPI const char *
147 e_notification_app_name_get(E_Notification *note)
148 {
149   return note->app_name;
150 }
151
152 EAPI const char *
153 e_notification_app_icon_get(E_Notification *note)
154 {
155   return note->app_icon;
156 }
157
158 EAPI const char *
159 e_notification_summary_get(E_Notification *note)
160 {
161   return note->summary;
162 }
163
164 EAPI const char *
165 e_notification_body_get(E_Notification *note)
166 {
167   return note->body;
168 }
169
170 EAPI Eina_List *
171 e_notification_actions_get(E_Notification *note)
172 {
173   return note->actions;
174 }
175
176 EAPI int
177 e_notification_replaces_id_get(E_Notification *note)
178 {
179   return note->replaces_id;
180 }
181
182 EAPI int
183 e_notification_timeout_get(E_Notification *note)
184 {
185   return note->expire_timeout;
186 }
187
188 EAPI unsigned char
189 e_notification_closed_get(E_Notification *note)
190 {
191   return note->closed;
192 }
193
194 /***** actions *****/
195
196 static Eina_List *
197 e_notification_action_list_new()
198 {
199   Eina_List *alist;
200
201   alist = NULL;
202   return alist;
203 }
204
205 static E_Notification_Action *
206 e_notification_action_new(const char *id, const char *name)
207 {
208   E_Notification_Action *act;
209   act = malloc(sizeof(E_Notification_Action));
210   act->id = eina_stringshare_add(id);
211   act->name = eina_stringshare_add(name);
212   return act;
213 }
214
215
216 /********* hints *******/
217
218
219 EAPI void 
220 e_notification_hint_urgency_set(E_Notification *n, char urgency)
221 {
222   n->hints.urgency = urgency;
223   n->hint_flags |= E_NOTIFICATION_HINT_URGENCY;
224 }
225
226 EAPI void 
227 e_notification_hint_category_set(E_Notification *n, const char *category)
228 {
229   eina_stringshare_replace(&n->hints.category, category);
230   n->hint_flags |= E_NOTIFICATION_HINT_CATEGORY;
231 }
232
233 EAPI void 
234 e_notification_hint_desktop_set(E_Notification *n, const char *desktop)
235 {
236   eina_stringshare_replace(&n->hints.desktop, desktop);
237   n->hint_flags |= E_NOTIFICATION_HINT_DESKTOP;
238 }
239
240 EAPI void 
241 e_notification_hint_sound_file_set(E_Notification *n, const char *sound_file)
242 {
243   eina_stringshare_replace(&n->hints.sound_file, sound_file);
244   n->hint_flags |= E_NOTIFICATION_HINT_SOUND_FILE;
245 }
246
247 EAPI void 
248 e_notification_hint_suppress_sound_set(E_Notification *n, char suppress_sound)
249 {
250   n->hints.suppress_sound = suppress_sound;
251   n->hint_flags |= E_NOTIFICATION_HINT_SUPPRESS_SOUND;
252 }
253
254 EAPI void 
255 e_notification_hint_xy_set(E_Notification *n, int x, int y)
256 {
257   n->hints.x = x;
258   n->hints.y = y;
259   n->hint_flags |= E_NOTIFICATION_HINT_XY;
260 }
261
262 EAPI void 
263 e_notification_hint_image_data_set(E_Notification *n, E_Notification_Image *image)
264 {
265   n->hints.image_data = image;
266 }
267
268
269 EAPI char  
270 e_notification_hint_urgency_get(E_Notification *n)
271 {
272   return (n->hint_flags & E_NOTIFICATION_HINT_URGENCY ? n->hints.urgency : 1);
273 }
274
275 EAPI const char *
276 e_notification_hint_category_get(E_Notification *n)
277 {
278   return n->hints.category;
279 }
280
281 EAPI const char *
282 e_notification_hint_desktop_get(E_Notification *n)
283 {
284   return n->hints.desktop;
285 }
286
287 EAPI const char *
288 e_notification_hint_sound_file_get(E_Notification *n)
289 {
290   return n->hints.sound_file;
291 }
292
293 EAPI char  
294 e_notification_hint_suppress_sound_get(E_Notification *n)
295 {
296   return n->hints.suppress_sound;
297 }
298
299 EAPI int  
300 e_notification_hint_xy_get(E_Notification *n, int *x, int *y)
301 {
302   if (x) *x = n->hints.x;
303   if (y) *y = n->hints.y;
304
305   return (n->hint_flags & E_NOTIFICATION_HINT_XY ? 1 : 0);
306 }
307
308 EAPI E_Notification_Image *
309 e_notification_hint_image_data_get(E_Notification *n)
310 {
311   return n->hints.image_data;
312 }
313
314
315 EAPI E_Notification_Image *
316 e_notification_hint_icon_data_get(E_Notification *n)
317 {
318   return n->hints.icon_data;
319 }
320
321 EAPI E_Notification_Image *
322 e_notification_image_new()
323 {
324   E_Notification_Image *img;
325
326   img = calloc(1, sizeof(E_Notification_Image));
327   return img;
328 }
329
330 EAPI void
331 e_notification_image_free(E_Notification_Image *img)
332 {
333   if (img->data) free(img->data);
334   if (img) free(img);
335 }
336
337
338 EAPI Evas_Object *
339 e_notification_image_evas_object_add(Evas *evas, E_Notification_Image *img)
340 {
341   int *imgdata;
342   Evas_Object *o = NULL;
343
344   if (!evas || !img) return NULL;
345
346   o = evas_object_image_add(evas);
347   evas_object_resize(o, img->width, img->height);
348   evas_object_image_alpha_set(o, img->has_alpha);
349   evas_object_image_size_set(o, img->width, img->height);
350   evas_object_image_fill_set(o, 0, 0, img->width, img->height);
351   imgdata = evas_object_image_data_get(o, 1);
352
353   if (img->bits_per_sample == 8)
354     {
355        /* Although not specified.
356         * The data are very likely to come from a GdkPixbuf
357         * which align each row on a 4-bytes boundary when using RGB.
358         * And is RGBA otherwise.
359         */
360        int x, y;
361        int32_t *dest;
362        unsigned char *src;
363        for (y = 0; y < img->height; y++)
364          {
365             src  = img->data + y * img->rowstride;
366             dest = imgdata + y * img->width;
367
368             for (x = 0; x < img->width; x++, src += img->channels, dest++)
369               {
370                  if (img->has_alpha)
371                    {
372                       *dest  = (*(src + 2) * *(src + 3) / 255);
373                       *dest += (*(src + 1) * *(src + 3) / 255) << 8;
374                       *dest += (*(src + 0) * *(src + 3) / 255) << 16;
375                       *dest += *(src + 3) << 24;
376                    }
377                  else
378                    {
379                       *dest  = *(src + 2);
380                       *dest += *(src + 1) << 8;
381                       *dest += *(src + 0) << 16;
382                       *dest += 255 << 24;
383                    }
384               }
385          }
386     }
387
388   return o;
389 }