summaryrefslogtreecommitdiff
path: root/src/lib/elementary/elm_sys_notify.c
blob: b31fee4bee6f9a686f7225691b320159c8fafdcf (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>

#include "elm_priv.h"

#include "elm_sys_notify_interface_eo.h"
#include "elm_sys_notify_eo.h"
#include "elm_sys_notify_dbus_eo.h"
#include "elm_sys_notify_dbus_eo.legacy.h"

#define MY_CLASS ELM_SYS_NOTIFY_CLASS

#define MY_CLASS_NAME        "Elm_Sys_Notify"
#define MY_CLASS_NAME_LEGACY "elm_sys_notify"

EAPI int ELM_EVENT_SYS_NOTIFY_NOTIFICATION_CLOSED = 0;
EAPI int ELM_EVENT_SYS_NOTIFY_ACTION_INVOKED      = 0;

typedef const Efl_Class *(*Class_Get_Func)(void);

static Elm_Sys_Notify *_singleton = NULL;

/*
 * Registration of notification servers is done UNIQUELY
 * in the two structures below.
 *   1) ALWAYS add a SRV_XXX before __SRV_LAST
 *   2) copy the #if ... #else ... #endif with the appropriate class
 *      getter (generated by Eolian) or NULL when unsupported
 *
 * The rest of the code relies on the Srv enum and _class_getters
 * to register/unregister notification servers.
 */

typedef enum
{
   SRV_DBUS = 0,
   __SRV_LAST /* Sentinel */
} Srv;

static Class_Get_Func _class_getters[__SRV_LAST] =
{
#ifdef ELM_SYS_NOTIFY_DBUS_CLASS
   [SRV_DBUS] = elm_sys_notify_dbus_class_get
#else
   [SRV_DBUS] = NULL
#endif
};



typedef struct
{
   Eo *servers[__SRV_LAST];
} Elm_Sys_Notify_Data;

/*============================================================================*
 *                  Constructor/Destructor - Singleton setup                  *
 *============================================================================*/

EOLIAN static Efl_Object *
_elm_sys_notify_efl_object_constructor(Eo                  *obj,
                                    Elm_Sys_Notify_Data *sd  EINA_UNUSED)
{
   if (_singleton != NULL)
     {
        ERR("Attempted to create another system notification manager");
        return NULL;
     }

   obj = efl_constructor(efl_super(obj, MY_CLASS));
   _singleton = obj;

   return obj;
}

EOLIAN static void
_elm_sys_notify_efl_object_destructor(Eo                  *obj,
                                   Elm_Sys_Notify_Data *sd  EINA_UNUSED)
{
   efl_destructor(efl_super(obj, MY_CLASS));
   _singleton = NULL;
}


/*============================================================================*
 *                           Notification Interface                           *
 *============================================================================*/

EOLIAN static void
_elm_sys_notify_elm_sys_notify_interface_send(const Eo *obj EINA_UNUSED,
                                              Elm_Sys_Notify_Data *sd,
                                              unsigned int replaces_id,
                                              const char *icon,
                                              const char *summary,
                                              const char *body,
                                              Elm_Sys_Notify_Urgency urgency,
                                              int timeout,
                                              Elm_Sys_Notify_Send_Cb cb,
                                              const void *cb_data)
{
   Srv i;

   /* Propagate to all registered servers */
   for (i = SRV_DBUS; i < __SRV_LAST; ++i)
     if (sd->servers[i])
       elm_obj_sys_notify_interface_send(sd->servers[i], replaces_id, icon, summary, body, urgency, timeout, cb, cb_data);
}

EOLIAN static void
_elm_sys_notify_elm_sys_notify_interface_simple_send(const Eo *obj EINA_UNUSED,
                                                     Elm_Sys_Notify_Data *sd,
                                                     const char *icon,
                                                     const char *summary,
                                                     const char *body)
{
   Srv i;

   /* Propagate to all registered servers */
   for (i = SRV_DBUS; i < __SRV_LAST; ++i)
     if (sd->servers[i])
       elm_obj_sys_notify_interface_simple_send(sd->servers[i], icon, summary, body);
}

EOLIAN static void
_elm_sys_notify_elm_sys_notify_interface_close(const Eo *obj EINA_UNUSED,
                                               Elm_Sys_Notify_Data *sd,
                                               unsigned int id)
{
   Srv i;

   /* Propagate to all registered servers */
   for (i = SRV_DBUS; i < __SRV_LAST; ++i)
     if (sd->servers[i])
       elm_obj_sys_notify_interface_close(sd->servers[i], id);
}


/*============================================================================*
 *                                   Methods                                  *
 *============================================================================*/

EOLIAN static Eina_Bool
_elm_sys_notify_servers_set(Eo                     *obj  EINA_UNUSED,
                            Elm_Sys_Notify_Data    *sd,
                            Elm_Sys_Notify_Server   servers)
{
   Srv i;
   Class_Get_Func class_get;

   /* Update the notification servers */
   for (i = SRV_DBUS; i < __SRV_LAST; ++i)
     {
        if (sd->servers[i])
          {
             /* Delete if server type is not provided */
             if (!(servers & (1 << i)))
               efl_del(sd->servers[i]);
          }
        else
          {
             /* If server is required, create when nonexistant */
             if (servers & (1 << i))
               {
                  class_get = _class_getters[i];
                  if (!class_get)
                    {
                       CRI("Unsupported notification server");
                       return EINA_FALSE;
                    }

                  sd->servers[i] = efl_add(class_get(), NULL);
                  if (EINA_UNLIKELY(!(sd->servers[i])))
                    {
                       CRI("Failed to create notification server");
                       return EINA_FALSE;
                    }
               }
          }
     }

   return EINA_TRUE;
}

EOLIAN static Elm_Sys_Notify_Server
_elm_sys_notify_servers_get(const Eo            *obj EINA_UNUSED,
                            Elm_Sys_Notify_Data *sd)
{
   Elm_Sys_Notify_Server servers = ELM_SYS_NOTIFY_SERVER_NONE;
   Srv i;

   for (i = SRV_DBUS; i < __SRV_LAST; ++i)
     if (sd->servers[i])
       servers |= (1 << i);

   return servers;
}

EOLIAN static Elm_Sys_Notify *
_elm_sys_notify_singleton_get(void)
{
   if (!_singleton)
     _singleton = efl_add(MY_CLASS, efl_main_loop_get());
   return _singleton;
}

EOLIAN static void
_elm_sys_notify_class_constructor(Efl_Class *klass EINA_UNUSED)
{
   ELM_EVENT_SYS_NOTIFY_NOTIFICATION_CLOSED = ecore_event_type_new();
   ELM_EVENT_SYS_NOTIFY_ACTION_INVOKED = ecore_event_type_new();
}

/*============================================================================*
 *                                 Legacy API                                 *
 *============================================================================*/

void
_elm_unneed_sys_notify(void)
{
   Elm_Sys_Notify *manager = elm_obj_sys_notify_singleton_get();
   if (manager)
     {
        elm_obj_sys_notify_servers_set(manager, ELM_SYS_NOTIFY_SERVER_NONE);
        efl_del(manager);
     }
}

EAPI Eina_Bool
elm_need_sys_notify(void)
{
   Elm_Sys_Notify_Server servers = ELM_SYS_NOTIFY_SERVER_NONE;
   Elm_Sys_Notify *manager;
   Srv i;

   /* In theory, there can be N notification managers, but
    * in the implementation there will be only one: the
    * singleton which is initialized here. */
   manager = elm_obj_sys_notify_singleton_get();
   if (EINA_UNLIKELY(!manager))
     {
        CRI("Failed to get notification manager");
        return EINA_FALSE;
     }

   /* If servers have alread been configured, this is not the right
    * function to be called! */
   if (elm_sys_notify_servers_get(manager) != ELM_SYS_NOTIFY_SERVER_NONE)
     {
        ERR("Notification servers have already been configured. "
            "Use elm_sys_notify_servers_set() instead.");
        return EINA_FALSE;
     }

   /* Register available notification servers */
   for (i = SRV_DBUS; i < __SRV_LAST; ++i)
     if (_class_getters[i])
       servers |= (1 << i);

   /* If no server are available, don't even bother... */
   if (servers == ELM_SYS_NOTIFY_SERVER_NONE)
     return EINA_FALSE;

   return elm_sys_notify_servers_set(manager, servers);
}

EAPI void
elm_sys_notify_send(unsigned int            replaces_id,
                    const char             *icon,
                    const char             *summary,
                    const char             *body,
                    Elm_Sys_Notify_Urgency  urgency,
                    int                     timeout,
                    Elm_Sys_Notify_Send_Cb  cb,
                    const void             *cb_data)
{
   elm_obj_sys_notify_interface_send(_singleton, replaces_id, icon, summary, body, urgency, timeout, cb, cb_data);
}

EAPI void
elm_sys_notify_close(unsigned int id)
{
   elm_obj_sys_notify_interface_close(_singleton, id);
}

#include "elm_sys_notify_eo.c"