aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/systray/e_mod_notifier_host.c
blob: e50047f8c769e211ba8e11ac545cd79eb2881c75 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "e_mod_notifier_host_private.h"

#define WATCHER_BUS "org.kde.StatusNotifierWatcher"
#define WATCHER_PATH "/StatusNotifierWatcher"
#define WATCHER_IFACE "org.kde.StatusNotifierWatcher"

#define ITEM_IFACE "org.kde.StatusNotifierItem"

const char *Category_Names[] = {
   "unknown", "SystemServices", NULL
};

const char *Status_Names[] = {
   "unknown", "Active", "Passive", "NeedsAttention", NULL
};

static const char *box_part_name = "e.dbus_notifier.box";
static Context_Notifier_Host *ctx = NULL;

void
systray_notifier_item_free(Notifier_Item *item)
{
   EDBus_Object *obj;
   EDBus_Signal_Handler *sig;
   Instance_Notifier_Host *host_inst;
   EINA_INLIST_FOREACH(ctx->instances, host_inst)
     {
        Notifier_Item_Icon *ii;
        EINA_INLIST_FOREACH(host_inst->ii_list, ii)
          {
             if (ii->item == item)
               break;
          }
        if (!ii)
          continue;
        host_inst->ii_list = eina_inlist_remove(host_inst->ii_list,
                                                EINA_INLIST_GET(ii));
        evas_object_del(ii->icon);
        free(ii);
        systray_size_updated(host_inst->inst);
     }
   if (item->menu_path)
     e_dbusmenu_unload(item->menu_data);
   eina_stringshare_del(item->bus_id);
   eina_stringshare_del(item->path);
   if (item->attention_icon_name)
     eina_stringshare_del(item->attention_icon_name);
   if (item->icon_name)
     eina_stringshare_del(item->icon_name);
   if (item->icon_path)
     eina_stringshare_del(item->icon_path);
   if (item->id)
     eina_stringshare_del(item->id);
   if (item->menu_path)
     eina_stringshare_del(item->menu_path);
   if (item->title)
     eina_stringshare_del(item->title);
   EINA_LIST_FREE(item->signals, sig)
     edbus_signal_handler_del(sig);
   obj = edbus_proxy_object_get(item->proxy);
   edbus_proxy_unref(item->proxy);
   edbus_object_unref(obj);
   ctx->item_list = eina_inlist_remove(ctx->item_list, EINA_INLIST_GET(item));
   free(item);
}

static void
image_load(const char *name, const char *path, Evas_Object *image)
{
   if (path && strlen(path))
     {
        char buf[1024];
        sprintf(buf, "%s/%s", path, name);
        if (!e_icon_file_set(image, buf))
          e_util_icon_theme_set(image, "dialog-error");
        return;
     }
   if (!e_util_icon_theme_set(image, name))
     e_util_icon_theme_set(image, "dialog-error");
}

static void
_sub_item_clicked_cb(void *data, E_Menu *m __UNUSED__, E_Menu_Item *mi __UNUSED__)
{
   E_DBusMenu_Item *item = data;
   e_dbusmenu_event_send(item, E_DBUSMENU_ITEM_EVENT_CLICKED);
}

static void
_menu_post_deactivate(void *data, E_Menu *m)
{
   Eina_List *iter;
   E_Menu_Item *mi;
   E_Gadcon *gadcon = data;

   if (gadcon)
     e_gadcon_locked_set(gadcon, 0);
   EINA_LIST_FOREACH(m->items, iter, mi)
     {
        if (mi->submenu)
          e_menu_deactivate(mi->submenu);
     }
   e_object_del(E_OBJECT(m));
}

static E_Menu *
_item_submenu_new(E_DBusMenu_Item *item, E_Menu_Item *mi)
{
   E_DBusMenu_Item *child;
   E_Menu *m;
   E_Menu_Item *submi;

   m = e_menu_new();
   e_menu_post_deactivate_callback_set(m, _menu_post_deactivate, NULL);
   if (mi)
     e_menu_item_submenu_set(mi, m);

   EINA_INLIST_FOREACH(item->sub_items, child)
     {
        if (!child->visible) continue;
        submi = e_menu_item_new(m);
        if (child->type == E_DBUSMENU_ITEM_TYPE_SEPARATOR)
          e_menu_item_separator_set(submi, 1);
        else
          {
             e_menu_item_label_set(submi, child->label);
             e_menu_item_callback_set(submi, _sub_item_clicked_cb, child);
             if (!child->enabled) e_menu_item_disabled_set(submi, 1);
             if (child->toggle_type == E_DBUSMENU_ITEM_TOGGLE_TYPE_CHECKMARK)
               e_menu_item_check_set(submi, 1);
             else if (child->toggle_type == E_DBUSMENU_ITEM_TOGGLE_TYPE_RADIO)
               e_menu_item_radio_set(submi, 1);
             if (child->toggle_type)
               e_menu_item_toggle_set(submi, child->toggle_state);
             if (eina_inlist_count(child->sub_items))
               _item_submenu_new(child, submi);
             e_util_menu_item_theme_icon_set(submi, child->icon_name);
          }
     }
   return m;
}

static void
_item_menu_new(Notifier_Item_Icon *ii)
{
   E_DBusMenu_Item *root_item;
   E_Menu *m;
   E_Zone *zone;
   int x, y;
   E_Gadcon *gadcon = evas_object_data_get(ii->icon, "gadcon");
   EINA_SAFETY_ON_NULL_RETURN(gadcon);
   if (!ii->item->dbus_item) return;
   root_item = ii->item->dbus_item;
   EINA_SAFETY_ON_FALSE_RETURN(root_item->is_submenu);

   m = _item_submenu_new(root_item, NULL);
   e_gadcon_locked_set(gadcon, 1);
   e_menu_post_deactivate_callback_set(m, _menu_post_deactivate, gadcon);

   zone = e_util_zone_current_get(e_manager_current_get());
   ecore_x_pointer_xy_get(zone->container->win, &x, &y);
   e_menu_activate_mouse(m, zone, x, y, 1, 1, E_MENU_POP_DIRECTION_DOWN,
                         ecore_x_current_time_get());
}

void
_clicked_item_cb(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event)
{
   Notifier_Item_Icon *ii = data;
   Evas_Event_Mouse_Down *ev = event;

   if (ev->button != 1) return;
   _item_menu_new(ii);
}

void
systray_notifier_update_menu(void *data, E_DBusMenu_Item *new_root_item)
{
   Notifier_Item *item = data;
   item->dbus_item = new_root_item;
}

static void
image_scale(Instance_Notifier_Host *notifier_inst, Notifier_Item_Icon *ii)
{
   Evas_Coord sz;
   switch (systray_gadcon_get(notifier_inst->inst)->orient)
     {
      case E_GADCON_ORIENT_HORIZ:
      case E_GADCON_ORIENT_TOP:
      case E_GADCON_ORIENT_BOTTOM:
      case E_GADCON_ORIENT_CORNER_TL:
      case E_GADCON_ORIENT_CORNER_TR:
      case E_GADCON_ORIENT_CORNER_BL:
      case E_GADCON_ORIENT_CORNER_BR:
        sz = systray_gadcon_get(notifier_inst->inst)->shelf->h;
        break;

      case E_GADCON_ORIENT_VERT:
      case E_GADCON_ORIENT_LEFT:
      case E_GADCON_ORIENT_RIGHT:
      case E_GADCON_ORIENT_CORNER_LT:
      case E_GADCON_ORIENT_CORNER_RT:
      case E_GADCON_ORIENT_CORNER_LB:
      case E_GADCON_ORIENT_CORNER_RB:
      default:
        sz = systray_gadcon_get(notifier_inst->inst)->shelf->w;
        break;
     }
   sz = sz - 5;
   evas_object_resize(ii->icon, sz, sz);
}

static void
_systray_notifier_inst_item_update(Instance_Notifier_Host *host_inst, Notifier_Item *item, Eina_Bool search)
{
   Notifier_Item_Icon *ii = NULL;
   if (!search)
     goto jump_search;
   EINA_INLIST_FOREACH(host_inst->ii_list, ii)
     {
        if (ii->item == item)
          break;
     }
jump_search:
   if (!ii)
     {
        ii = calloc(1, sizeof(Notifier_Item_Icon));
        ii->item = item;
        host_inst->ii_list = eina_inlist_append(host_inst->ii_list,
                                                      EINA_INLIST_GET(ii));
     }

   if (!ii->icon)
     {
        ii->icon = e_icon_add(evas_object_evas_get(host_inst->edje));
        EINA_SAFETY_ON_NULL_RETURN(ii->icon);
        image_scale(host_inst, ii);
        evas_object_data_set(ii->icon, "gadcon", host_inst->gadcon);
        evas_object_event_callback_add(ii->icon, EVAS_CALLBACK_MOUSE_DOWN,
                                       _clicked_item_cb, ii);
     }
   switch (item->status)
     {
      case STATUS_ACTIVE:
        {
           image_load(item->icon_name, item->icon_path, ii->icon);
           if (!evas_object_visible_get(ii->icon))
             {
                systray_edje_box_append(host_inst->inst, box_part_name,
                                        ii->icon);
                evas_object_show(ii->icon);
             }
           break;
        }
      case STATUS_PASSIVE:
        {
           if (evas_object_visible_get(ii->icon))
             {
                systray_edje_box_remove(host_inst->inst, box_part_name,
                                        ii->icon);
                evas_object_hide(ii->icon);
             }
           break;
        }
      case STATUS_ATTENTION:
        {
           image_load(item->attention_icon_name, item->icon_path, ii->icon);
           if (!evas_object_visible_get(ii->icon))
             {
                systray_edje_box_append(host_inst->inst, box_part_name,
                                        ii->icon);
                evas_object_show(ii->icon);
             }
           break;
        }
      default:
        {
           WRN("unhandled status");
           break;
        }
     }
   systray_size_updated(host_inst->inst);
}

void
systray_notifier_item_update(Notifier_Item *item)
{
   Instance_Notifier_Host *inst;
   EINA_INLIST_FOREACH(ctx->instances, inst)
     _systray_notifier_inst_item_update(inst, item, EINA_TRUE);
}

Instance_Notifier_Host *
systray_notifier_host_new(Instance *inst, E_Gadcon *gadcon)
{
   Instance_Notifier_Host *host_inst = NULL;
   Notifier_Item *item;
   host_inst = calloc(1, sizeof(Instance_Notifier_Host));
   EINA_SAFETY_ON_NULL_RETURN_VAL(host_inst, NULL);
   host_inst->inst = inst;
   host_inst->edje = systray_edje_get(inst);
   host_inst->gadcon = gadcon;
   ctx->instances = eina_inlist_append(ctx->instances, EINA_INLIST_GET(host_inst));

   EINA_INLIST_FOREACH(ctx->item_list, item)
     _systray_notifier_inst_item_update(host_inst, item, EINA_FALSE);

   return host_inst;
}

void
systray_notifier_host_free(Instance_Notifier_Host *notifier)
{
   while (notifier->ii_list)
     {
        Notifier_Item_Icon *ii = EINA_INLIST_CONTAINER_GET(notifier->ii_list, Notifier_Item_Icon);
        notifier->ii_list = eina_inlist_remove(notifier->ii_list,
                                               notifier->ii_list);
        free(ii);
     }
   ctx->instances = eina_inlist_remove(ctx->instances, EINA_INLIST_GET(notifier));
   free(notifier);
}

void
systray_notifier_host_init(void)
{
   ctx = calloc(1, sizeof(Context_Notifier_Host));
   EINA_SAFETY_ON_NULL_RETURN(ctx);
   systray_notifier_dbus_init(ctx);
}

void
systray_notifier_host_shutdown(void)
{
   systray_notifier_dbus_shutdown(ctx);
   free(ctx);
   ctx = NULL;
}