summaryrefslogtreecommitdiff
path: root/src/modules/systray/e_mod_notifier_host.c
blob: 9f7dd900ba7548424973510f226a7013b37e5053 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "e_mod_main.h"
#include "e_mod_notifier_host_private.h"

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

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

static Eina_List *traybars; //known traybars
static Eina_List *items; //list of known Notifier_Item items
 static void _systray_size_apply_do(Edje_Object *obj);

static void
_systray_theme(Evas_Object *o, const char *shelf_style, const char *gc_style)
{
   const char base_theme[] = "base/theme/modules/systray";
   char path[PATH_MAX];
   char buf[128], *p;
   size_t len, avail;

   snprintf(path, sizeof(path), "%s/e-module-systray.edj", e_module_dir_get(systray_mod));

   len = eina_strlcpy(buf, "e/modules/systray/main", sizeof(buf));
   if (len >= sizeof(buf))
     goto fallback;
   p = buf + len;
   *p = '/';
   p++;
   avail = sizeof(buf) - len - 2;

   if (shelf_style && gc_style)
     {
        size_t r;
        r = snprintf(p, avail, "%s/%s", shelf_style, gc_style);
        if (r < avail && e_theme_edje_object_set(o, base_theme, buf))
          return;
     }

   if (shelf_style)
     {
        size_t r;
        r = eina_strlcpy(p, shelf_style, avail);
        if (r < avail && e_theme_edje_object_set(o, base_theme, buf))
          return;
     }

   if (gc_style)
     {
        size_t r;
        r = eina_strlcpy(p, gc_style, avail);
        if (r < avail && e_theme_edje_object_set(o, base_theme, buf))
          return;
     }

   if (e_theme_edje_object_set(o, base_theme, "e/modules/systray/main"))
     return;

   if (shelf_style && gc_style)
     {
        size_t r;
        r = snprintf(p, avail, "%s/%s", shelf_style, gc_style);
        if (r < avail && edje_object_file_set(o, path, buf))
          return;
     }

   if (shelf_style)
     {
        size_t r;
        r = eina_strlcpy(p, shelf_style, avail);
        if (r < avail && edje_object_file_set(o, path, buf))
          return;
     }

   if (gc_style)
     {
        size_t r;
        r = eina_strlcpy(p, gc_style, avail);
        if (r < avail && edje_object_file_set(o, path, buf))
          return;
     }

fallback:
   edje_object_file_set(o, path, "e/modules/systray/main");
}

static void
image_load(const char *name, const char *path, uint32_t *imgdata, int w, int h, Evas_Object *image)
{
   const char **ext, *exts[] =
   {
      ".png",
      ".jpg",
      NULL
   };
   if (path && path[0])
     {
        char buf[PATH_MAX];
        const char **theme, *themes[] = { e_config->icon_theme, "hicolor", NULL };

        for (theme = themes; *theme; theme++)
          {
             struct stat st;
             unsigned int *i, sizes[] = { 16, 22, 24, 32, 36, 40, 48, 64, 72, 96, 128, 192, 256, 512, 0 };

             snprintf(buf, sizeof(buf), "%s/%s", path, *theme);
             if (stat(buf, &st)) continue;
             for (i = sizes; *i; i++)
               {
                  snprintf(buf, sizeof(buf), "%s/%s/%ux%u", path, *theme, *i, *i);
                  if (stat(buf, &st)) continue;
                  for (ext = exts; *ext; ext++)
                    {
                       snprintf(buf, sizeof(buf), "%s/%s/%ux%u/apps/%s%s", path, *theme, *i, *i, name, *ext);
                       if (e_icon_file_set(image, buf)) return;
                    }
               }
          }
     }
   if (name && name[0] && e_util_icon_theme_set(image, name)) return;
   if (imgdata)
     {
        Evas_Object *o;

        o = evas_object_image_filled_add(evas_object_evas_get(image));
        evas_object_image_alpha_set(o, 1);
        evas_object_image_size_set(o, w, h);
        evas_object_image_data_set(o, imgdata);
        e_icon_image_object_set(image, o);
     }
   else
     e_util_icon_theme_set(image, "dialog-error");
}

static void
image_scale(Evas_Object *icon)
{
    //TODO make it the size of the shelf
    //FIXME delayed on "after" zmike rewriting gadgets
    evas_object_size_hint_min_set(icon, 30, 30);
}
static void
_sub_item_clicked_cb(void *data, E_Menu *m EINA_UNUSED, E_Menu_Item *mi EINA_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;

   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 (child->sub_items)
               _item_submenu_new(child, submi);
             e_util_menu_item_theme_icon_set(submi, child->icon_name);
          }
     }
   return m;
}

void
_clicked_item_cb(void *data, Evas *evas, Evas_Object *obj EINA_UNUSED, void *event)
{
   Notifier_Item *item = data;
   Evas_Event_Mouse_Down *ev = event;
   E_DBusMenu_Item *root_item;
   E_Menu *m;
   int x, y;

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

   root_item = item->dbus_item;
   EINA_SAFETY_ON_FALSE_RETURN(root_item->is_submenu);

   m = _item_submenu_new(root_item, NULL);

   ecore_evas_pointer_xy_get(e_comp->ee, &x, &y);
   e_menu_activate_mouse(m, e_zone_current_get(), x, y, 1, 1, E_MENU_POP_DIRECTION_DOWN, ev->timestamp);
   evas_event_feed_mouse_up(evas, ev->button,
                         EVAS_BUTTON_NONE, ev->timestamp, NULL);
}
static Evas_Object*
_visualize_add(Evas_Object *parent, Notifier_Item *item)
{
    Evas_Object *obj;

    obj = e_icon_add(parent);
    image_load(item->icon_name,
               item->icon_path,
               item->imgdata,
               30, 30, obj);
    e_util_icon_theme_set(obj, "dialog-error");
    image_scale(obj);
    evas_object_show(obj);
    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
                                   _clicked_item_cb, item);
    printf("Add vis %p\n", obj);

    return obj;
}

static void
_visualize_update(Evas_Object *icon, Notifier_Item *item)
{
   printf("Update vis %p\n", icon);
   switch (item->status)
     {
      case STATUS_ACTIVE:
        {
           image_load(item->icon_name, item->icon_path, item->imgdata, item->imgw, item->imgh, icon);
           evas_object_show(icon);
           break;
        }
      case STATUS_PASSIVE:
        {
           evas_object_hide(icon);
           break;
        }
      case STATUS_ATTENTION:
        {
           image_load(item->attention_icon_name, item->icon_path, item->attnimgdata, item->attnimgw, item->attnimgh, icon);
           evas_object_show(icon);
           break;
        }
      default:
        {
           WRN("unhandled status");
           break;
        }
     }
}

static Eina_Bool
_host_del_cb(void *data, Eo *obj, const Eo_Event_Description2 *desc, void *event)
{
   traybars = eina_list_remove(traybars, obj);
   return EO_CALLBACK_CONTINUE;
}

Evas_Object*
systray_notifier_host_new(const char *shelfstyle, const char *style)
{
   Edje_Object *gadget;

   gadget = edje_object_add(e_comp->evas);
   _systray_theme(gadget, shelfstyle, style);
   evas_object_show(gadget);

   //add a new visualization to every single item for this tray bar
   {
      Eina_List *node;
      Notifier_Item *item;

      EINA_LIST_FOREACH(items, node, item)
        {
           Evas_Object *icon;
           //create new icon
           icon = _visualize_add(gadget, item);

           //add data
           _visualize_update(icon, item);

           item->icons = eina_list_append(item->icons, icon);

           edje_object_part_box_append(gadget, "box", icon);
           _systray_size_apply_do(gadget);
        }
   }

   traybars = eina_list_append(traybars, gadget);

   return gadget;
}

static Context_Notifier_Host *ctx;

void
systray_notifier_host_init(void)
{

   ctx = calloc(1, sizeof(Context_Notifier_Host));
   systray_notifier_dbus_init(ctx);
}

void
systray_notifier_host_shutdown(void)
{
   systray_notifier_dbus_shutdown(ctx);
}

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

void
systray_notifier_item_update(Notifier_Item *item)
{
   Eina_List *node;
   Evas_Object *icon;

   EINA_LIST_FOREACH(item->icons, node, icon)
     {
        _visualize_update(icon, item);
     }
}
void
systray_notifier_item_free(Notifier_Item *item)
{
    Evas_Object *icon;

    EINA_LIST_FREE(item->icons, icon)
      evas_object_del(icon);
}

void
systray_notifier_item_new(Notifier_Item *item)
{
    Evas_Object *traybar;
    Eina_List *node;

    printf("Item new %p\n", item);

    EINA_LIST_FOREACH(traybars, node, traybar)
      {
         Evas_Object *icon;

         icon = _visualize_add(traybar, item);
         edje_object_part_box_append(traybar, "box", icon);
         _systray_size_apply_do(traybar);

         item->icons = eina_list_append(item->icons, icon);
      }
}

/**
 * Hacky code to recalc the minsize of a object
 */
 static void
_systray_size_apply_do(Edje_Object *obj)
{
   Evas_Coord w, h;

   edje_object_message_signal_process(obj);
   edje_object_size_min_calc(obj, &w, &h);
   evas_object_size_hint_min_set(obj, w, h);
   _hack_get_me_the_correct_min_size(obj);
   printf("New min size %d %d\n", w, h);
}