summaryrefslogtreecommitdiff
path: root/src/lib/elementary/elm_sys_notify.c
blob: 3fe67396b8adf86221482b291a76002eb9f88b81 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>

#include "elm_priv.h"

#include "elm_sys_notify_dbus.eo.h"
#include "elm_sys_notify_dbus.eo.legacy.h"

#include "elm_sys_notify_cocoa.eo.h"
#include "elm_sys_notify_cocoa.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 Eo_Class *(*Class_Get_Func)(void);

typedef struct
{
   const char          *name;
   Class_Get_Func       func;
} Sys_Notify;

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) add the appropriate class getter (generated by Eolian)
 *
 * The rest of the code relies on the Srv enum and _sys_notify
 * to register/unregister notification servers, as well
 * as the ELM_SYS_NOTIFY_SERVER environment variable
 */

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

static const Sys_Notify _sys_notify[__SRV_LAST] =
{
   [SRV_DBUS] = {
      .name = "dbus",
      .func = elm_sys_notify_dbus_class_get,
   },
   [SRV_COCOA] = {
      .name = "cocoa",
      .func = elm_sys_notify_cocoa_class_get,
   },
};

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

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

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

   obj = eo_constructor(eo_super(obj, MY_CLASS));
   _singleton = obj;

   return obj;
}

EOLIAN static void
_elm_sys_notify_eo_base_destructor(Eo                  *obj,
                                   Elm_Sys_Notify_Data *sd  EINA_UNUSED)
{
   eo_destructor(eo_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)))
               eo_del(sd->servers[i]);
          }
        else
          {
             /* If server is required, create when nonexistant */
             if (servers & (1 << i))
               {
                  class_get = _sys_notify[i].func;
                  if (!class_get)
                    {
                       CRI("Unsupported notification server");
                       return EINA_FALSE;
                    }

                  sd->servers[i] = eo_add(class_get(), NULL);
                  printf("--> cocoa added\n");
                  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(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(Eo   *obj EINA_UNUSED,
                              void *sd  EINA_UNUSED)
{
   if (!_singleton)
     _singleton = eo_add(MY_CLASS, NULL);
   return _singleton;
}

EOLIAN static void
_elm_sys_notify_class_constructor(Eo_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_sys_notify_singleton_get();
   if (manager)
     {
        elm_obj_sys_notify_servers_set(manager, ELM_SYS_NOTIFY_SERVER_NONE);
        eo_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;
   const char *env;

   /* 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_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;
     }

   /* Environment will override the default choice */
   env = getenv("ELM_SYS_NOTIFY_SERVER");

   /* Register available notification servers */
   for (i = SRV_DBUS; i < __SRV_LAST; ++i)
     {
        if (env)
          {
             /*
              * When env is specified, select the matching server only
              */
             if (!strcmp(env, _sys_notify[i].name))
               {
                  servers |= (1 << i);
                  printf("--> Found %s\n", env);
                  break;
               }
          }
        else
          {
             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"