summaryrefslogtreecommitdiff
path: root/src/lib/elm_app_client.c
blob: 5c4bd31ca43836f881112db040a7f8e5d005053a (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>
#include "elm_priv.h"

#define MY_CLASS ELM_APP_CLIENT_CLASS

#define MY_CLASS_NAME "Elm_App_Client"

typedef struct
{
   Eldbus_Proxy *app_proxy;
   Eina_Hash *views;
} Elm_App_Client_Data;

static void
_sub_path_process(Elm_App_Client *eo, Eldbus_Message_Iter *obj_iter, Elm_App_Client_Data *data, Eina_Bool loading_list)
{
   const char *obj_path;
   Eldbus_Message_Iter *array_iface, *iface;

   eldbus_message_iter_arguments_get(obj_iter, "oa{sa{sv}}", &obj_path, &array_iface);
   while (eldbus_message_iter_get_and_next(array_iface, '{', &iface))
     {
        const char *iface_name;
        Eldbus_Message_Iter *array_props;
        Elm_App_Client_View *view;

        eldbus_message_iter_arguments_get(iface, "sa{sv}", &iface_name,
                                          &array_props);
        if (strcmp(iface_name, "org.enlightenment.ApplicationView1"))
          continue;

        view = eina_hash_find(data->views, obj_path);
        if (view)
          continue;

        view = eo_add(ELM_APP_CLIENT_VIEW_CLASS, eo,
                             elm_app_client_view_path_set(obj_path));
        eina_hash_add(data->views, obj_path, view);
        if (!loading_list)
          eo_do(eo, eo_event_callback_call(ELM_APP_CLIENT_EVENT_VIEW_CREATED, view));
     }
}

static void
_objects_get(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
   Eo *eo = data;
   Elm_App_Client_Data *cdata = eo_data_scope_get(eo, MY_CLASS);
   Eldbus_Message_Iter *array_path, *path;

   if (eldbus_message_error_get(msg, NULL, NULL))
     return;

   if (!eldbus_message_arguments_get(msg, "a{oa{sa{sv}}}", &array_path))
     return;
   while (eldbus_message_iter_get_and_next(array_path, '{', &path))
     _sub_path_process(eo, path, cdata, EINA_TRUE);

   eo_do(eo, eo_event_callback_call(ELM_APP_CLIENT_EVENT_VIEW_LIST_LOADED,
                                    NULL));
}

static void _iface_add(void *data, const Eldbus_Message *msg)
{
   Eo *eo = data;
   Elm_App_Client_Data *cdata = eo_data_scope_get(eo, MY_CLASS);
   Eldbus_Message_Iter *main_iter;

   main_iter = eldbus_message_iter_get(msg);
   _sub_path_process(eo, main_iter, cdata, EINA_FALSE);
}

static void
_iface_del(void *data, const Eldbus_Message *msg)
{
   Eo *eo = data;
   Elm_App_Client_Data *cdata = eo_data_scope_get(eo, MY_CLASS);
   const char *path, *iface;
   Eldbus_Message_Iter *array_iface;

   if (!eldbus_message_arguments_get(msg, "oas", &path, &array_iface))
     return;
   while (eldbus_message_iter_get_and_next(array_iface, 's', &iface))
     {
        Elm_App_Client_View *view;
        Elm_App_View_State view_state = ELM_APP_VIEW_STATE_UNKNOWN;

        if (strcmp(iface, "org.enlightenment.ApplicationView1"))
          continue;

        view = eina_hash_find(cdata->views, path);
        if (!view)
          continue;

        eo_do(view, view_state = elm_app_client_view_state_get());
        if (view_state != ELM_APP_VIEW_STATE_CLOSED)
          {
             elm_app_client_view_internal_state_set(view,
                                                    ELM_APP_VIEW_STATE_SHALLOW);
             continue;
          }

        eina_hash_del(cdata->views, path, NULL);
        eo_do(eo, eo_event_callback_call(ELM_APP_CLIENT_EVENT_VIEW_DELETED,
                                         view));
        eo_del(view);
     }
}

static void
_pkg_name_owner_changed_cb(void *data, const char *bus EINA_UNUSED, const char *old_id EINA_UNUSED, const char *new_id)
{
   Elm_App_Client *eo = data;
   Elm_App_Client_Data *cdata = eo_data_scope_get(eo, MY_CLASS);
   Eina_Iterator *iter;
   Elm_App_Client_View *view;
   Eina_List *views_list = NULL;

   if (!new_id || (new_id[0] == '\0'))
     return;

   iter = eina_hash_iterator_data_new(cdata->views);
   EINA_ITERATOR_FOREACH(iter, view)
     views_list = eina_list_append(views_list, view);
   eina_iterator_free(iter);

   /*
    * remove all views that are closed of the views hash
    * views not closed, only set they to SHALLOW
    */
   EINA_LIST_FREE(views_list, view)
     {
        Elm_App_View_State view_state = ELM_APP_VIEW_STATE_UNKNOWN;
        const char *path = NULL;

        eo_do(view, view_state = elm_app_client_view_state_get(),
              path = elm_app_client_view_path_get());
        if (view_state != ELM_APP_VIEW_STATE_CLOSED)
          {
             elm_app_client_view_internal_state_set(view,
                                                    ELM_APP_VIEW_STATE_SHALLOW);
             continue;
          }

        eina_hash_del(cdata->views, path, NULL);
        eo_do(eo, eo_event_callback_call(ELM_APP_CLIENT_EVENT_VIEW_DELETED,
                                         view));
        eo_del(view);
     }
}

EOLIAN static void
_elm_app_client_constructor(Eo *eo, Elm_App_Client_Data *data, const char *pkg)
{
   Eldbus_Connection *conn;
   Eldbus_Object *obj;
   char *path;

   EINA_SAFETY_ON_NULL_RETURN(pkg);

   data->views = eina_hash_string_small_new(NULL);

   path = _dbus_package_to_path(pkg);

   eldbus_init();
   conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   obj = eldbus_object_get(conn, pkg, path);
   data->app_proxy = eldbus_proxy_get(obj, "org.enlightenment.Application1");
   eldbus_object_managed_objects_get(obj, _objects_get, eo);
   eldbus_object_manager_interfaces_added(obj, _iface_add, eo);
   eldbus_object_manager_interfaces_removed(obj, _iface_del, eo);
   eldbus_name_owner_changed_callback_add(conn, pkg, _pkg_name_owner_changed_cb,
                                          eo, EINA_FALSE);

   free(path);
}

static void
_create_view_cb(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending)
{
   Elm_App_Client_Open_View_Cb cb = eldbus_pending_data_del(pending, "user_cb");
   void *user_data = eldbus_pending_data_del(pending, "user_data");
   const char *error_name, *error_message, *view_path;
   Elm_App_Client *eo = data;
   Elm_App_Client_View *view;
   Elm_App_Client_Data *cdata = eo_data_scope_get(eo, MY_CLASS);

   if (eldbus_message_error_get(msg, &error_name, &error_message))
     {
        if (!cb)
          return;
        if (error_name && !strcmp(error_name, ELDBUS_ERROR_PENDING_CANCELED))
          cb(user_data, NULL, ELM_APP_CLIENT_VIEW_OPEN_CANCELED, NULL);
        else if (error_name && !strcmp(error_name, ELDBUS_ERROR_PENDING_TIMEOUT))
          cb(user_data, NULL, ELM_APP_CLIENT_VIEW_OPEN_TIMEOUT, error_message);
        else
          cb(user_data, NULL, error_name, error_message);
        return;
     }

   if (!eldbus_message_arguments_get(msg, "o", &view_path))
     {
        if (cb)
          cb(user_data, NULL, "Unknow error", NULL);
        return;
     }

   /**
    * Because a IntefaceAdd signal could arrive first
    */
   view = eina_hash_find(cdata->views, view_path);
   if (!view)
     {
        view = eo_add(ELM_APP_CLIENT_VIEW_CLASS, eo,
                             elm_app_client_view_path_set(view_path));
        eina_hash_add(cdata->views, view_path, view);
        eo_do(eo, eo_event_callback_call(ELM_APP_CLIENT_EVENT_VIEW_CREATED,
                                         view));
     }

   if (!view)
     {
        if (cb)
          cb(user_data, NULL, ELM_APP_CLEINT_VIEW_OPEN_ERROR, NULL);
        return;
     }
   if (cb)
     cb(user_data, view, NULL, NULL);
}

EOLIAN static Elm_App_Client_Pending *
_elm_app_client_view_open(Eo *eo, Elm_App_Client_Data *data, Eina_Value *args, Elm_App_Client_Open_View_Cb cb, const void *user_data)
{
   Eldbus_Message *msg;
   Eldbus_Pending *pending;

   msg = eldbus_proxy_method_call_new(data->app_proxy, "CreateView");

   if (args)
     {
        if (!eldbus_message_from_eina_value("a{sv}", msg, args))
          {
             eldbus_message_unref(msg);
             //TODO test to find out what type eina_value must be
             ERR("Eina_Value of args don't have a structure of a{sv}");
             return NULL;
          }
     }
   else
    {
       Eldbus_Message_Iter *main_iter = eldbus_message_iter_get(msg);
       Eldbus_Message_Iter *array;

       eldbus_message_iter_arguments_append(main_iter, "a{sv}", &array);
       eldbus_message_iter_container_close(main_iter, array);
    }

   pending = eldbus_proxy_send(data->app_proxy, msg, _create_view_cb, eo, -1);
   if (user_data)
     eldbus_pending_data_set(pending, "user_data", user_data);
   if (cb)
     eldbus_pending_data_set(pending, "user_cb", cb);

   return pending;
}

EOLIAN static Eina_Iterator*
_elm_app_client_views_get(Eo *eo EINA_UNUSED, Elm_App_Client_Data *data)
{
   return eina_hash_iterator_data_new(data->views);
}

static void
_elm_app_client_view_all_close(Eo *obj EINA_UNUSED, Elm_App_Client_Data *data)
{
   eldbus_proxy_call(data->app_proxy, "CloseAllViews", NULL, NULL, -1, "");
}

EOLIAN static void
_elm_app_client_terminate(Eo *eo EINA_UNUSED, Elm_App_Client_Data *data)
{
   eldbus_proxy_call(data->app_proxy, "Terminate", NULL, NULL, -1, "");
}

EOLIAN static const char*
_elm_app_client_package_get(Eo *eo EINA_UNUSED, Elm_App_Client_Data *data)
{
   Eldbus_Object *obj;

   obj = eldbus_proxy_object_get(data->app_proxy);
   return eldbus_object_bus_name_get(obj);
}

EOLIAN static void
_elm_app_client_view_open_cancel(Eo *eo EINA_UNUSED, Elm_App_Client_Data *_pd EINA_UNUSED, Elm_App_Client_Pending *pending)
{
   eldbus_pending_cancel(pending);
}

EOLIAN static Eo *
_elm_app_client_eo_base_finalize(Eo *obj, Elm_App_Client_Data *data)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(data->views, NULL);

   return eo_do_super_ret(obj, MY_CLASS, obj, eo_finalize());
}

EOLIAN static void
_elm_app_client_eo_base_destructor(Eo *eo, Elm_App_Client_Data *data)
{
   Eldbus_Object *obj;
   Eldbus_Connection *conn;
   Eina_Iterator *iter;
   Elm_App_Client_View *view;

   iter = eina_hash_iterator_data_new(data->views);
   EINA_ITERATOR_FOREACH(iter, view)
     eo_del(view);
   eina_iterator_free(iter);
   eina_hash_free(data->views);

   obj = eldbus_proxy_object_get(data->app_proxy);
   conn = eldbus_object_connection_get(obj);
   eldbus_name_owner_changed_callback_del(conn, eldbus_object_bus_name_get(obj),
                                          _pkg_name_owner_changed_cb, eo);
   eldbus_proxy_unref(data->app_proxy);
   eldbus_object_unref(obj);
   eldbus_connection_unref(conn);
   eldbus_shutdown();

   eo_do_super(eo, MY_CLASS, eo_destructor());
}

#include "elm_app_client.eo.c"