summaryrefslogtreecommitdiff
path: root/legacy/efreet/src/bin/efreetd_dbus.c
blob: 5a69b68b641bcfac3289e7497277a67fa3faa564 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Ecore.h>
#include <EDBus.h>

#include "efreetd.h"
#include "efreetd_cache.h"

#define BUS "org.enlightenment.Efreet"
#define PATH "/org/enlightenment/Efreet"
#define INTERFACE "org.enlightenment.Efreet"

/* internal */
enum
{
   EFREET_SIGNAL_ICON_CACHE_UPDATE = 0,
   EFREET_SIGNAL_DESKTOP_CACHE_UPDATE
};

static EDBus_Connection *conn;
static EDBus_Service_Interface *iface;

static Ecore_Timer *shutdown = NULL;
static int clients = 0;

static Eina_Bool
do_shutdown(void *data __UNUSED__)
{
   quit();
   return ECORE_CALLBACK_CANCEL;
}

static void
disconnected(void *context __UNUSED__, EDBus_Connection *connection __UNUSED__, void *event_info __UNUSED__)
{
   INF("disconnected");
   quit();
}

static void
client_name_owner_changed_cb(void *data, const char *bus, const char *old_id, const char *new_id)
{
   if (new_id[0])
     return;
   edbus_name_owner_changed_callback_del(conn, bus,
                                         client_name_owner_changed_cb, NULL);
   clients--;
   if (clients <= 0)
     {
        clients = 0;
        if (shutdown) ecore_timer_del(shutdown);
        shutdown = ecore_timer_add(10.0, do_shutdown, NULL);
     }
}

static EDBus_Message *
do_register(const EDBus_Service_Interface *ifc __UNUSED__, const EDBus_Message *message)
{
   EDBus_Message *reply;
   const char *lang;

   if (!edbus_message_arguments_get(message, "s", &lang))
     {
        ERR("Error getting arguments.");
        return NULL;
     }
   setenv("LANG", lang, 1);

   clients++;
   if (shutdown) ecore_timer_del(shutdown);
   shutdown = NULL;

   edbus_name_owner_changed_callback_add(conn,
                                         edbus_message_sender_get(message),
                                         client_name_owner_changed_cb, NULL,
                                         EINA_FALSE);
   reply = edbus_message_method_return_new(message);
   edbus_message_arguments_append(reply, "b", cache_desktop_exists());
   return reply;
}

static EDBus_Message *
add_desktop_dirs(const EDBus_Service_Interface *ifc __UNUSED__, const EDBus_Message *message)
{
   EDBus_Message_Iter *array = NULL;
   const char *dir;

   if (!edbus_message_arguments_get(message, "as", &array))
     {
        ERR("Error getting arguments.");
        return NULL;
     }

   while (edbus_message_iter_get_and_next(array, 's', &dir))
     {
        cache_desktop_dir_add(dir);
     }

   return NULL;
}

static EDBus_Message *
add_icon_dirs(const EDBus_Service_Interface *ifc __UNUSED__, const EDBus_Message *message)
{
   EDBus_Message_Iter *array = NULL;
   const char *dir;

   if (!edbus_message_arguments_get(message, "as", &array))
     {
        ERR("Error getting arguments.");
        return NULL;
     }

   while (edbus_message_iter_get_and_next(array, 's', &dir))
     {
        cache_icon_dir_add(dir);
     }

   return NULL;
}

static EDBus_Message *
build_desktop_cache(const EDBus_Service_Interface *ifc __UNUSED__, const EDBus_Message *message __UNUSED__)
{
   const char *lang;

   if (!edbus_message_arguments_get(message, "s", &lang))
     {
        ERR("Error getting arguments.");
        return NULL;
     }
   setenv("LANG", lang, 1);

   cache_desktop_update();
   return NULL;
}

static EDBus_Message *
add_icon_exts(const EDBus_Service_Interface *ifc __UNUSED__, const EDBus_Message *message)
{
   EDBus_Message_Iter *array = NULL;
   const char *ext;

   if (!edbus_message_arguments_get(message, "as", &array))
     {
        ERR("Error getting arguments.");
        return NULL;
     }

   while (edbus_message_iter_get_and_next(array, 's', &ext))
     {
        cache_icon_ext_add(ext);
     }

   return NULL;
}

static const EDBus_Signal signals[] = {
     [EFREET_SIGNAL_ICON_CACHE_UPDATE] = {"IconCacheUpdate", EDBUS_ARGS({ "b", "update" }), 0},
     [EFREET_SIGNAL_DESKTOP_CACHE_UPDATE] = {"DesktopCacheUpdate", EDBUS_ARGS({ "b", "update" }), 0},
     { NULL, NULL, 0 }
};

static const EDBus_Method methods[] = {
       {
          "Register", EDBUS_ARGS({"s", "lang info"}), EDBUS_ARGS({"b", "cache exists"}),
          do_register, 0
       },
       {
          "AddDesktopDirs", EDBUS_ARGS({"as", "dirs"}), NULL,
          add_desktop_dirs, EDBUS_METHOD_FLAG_NOREPLY
       },
       {
          "BuildDesktopCache", EDBUS_ARGS({"s", "lang info"}), NULL,
          build_desktop_cache, EDBUS_METHOD_FLAG_NOREPLY
       },
       {
          "AddIconDirs", EDBUS_ARGS({"as", "dirs"}), NULL,
          add_icon_dirs, EDBUS_METHOD_FLAG_NOREPLY
       },
       {
          "AddIconExts", EDBUS_ARGS({"as", "exts"}), NULL,
          add_icon_exts, EDBUS_METHOD_FLAG_NOREPLY
       },
       { NULL, NULL, NULL, NULL, 0 }
};

static const EDBus_Service_Interface_Desc desc = {
   INTERFACE, methods, signals, NULL, NULL, NULL
};

static void
on_name_request(void *data __UNUSED__, const EDBus_Message *msg, EDBus_Pending *pending __UNUSED__)
{
   unsigned int reply;

   if (edbus_message_error_get(msg, NULL, NULL))
     {
        ERR("error on on_name_request");
        quit();
        return;
     }

   if (!edbus_message_arguments_get(msg, "u", &reply))
     {
        ERR("error getting arguments on on_name_request");
        quit();
        return;
     }

   if (reply != EDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
     {
        ERR("error name already in use");
        quit();
        return;
     }
   INF("name requested");
}

/* external */
void
send_signal_icon_cache_update(Eina_Bool update)
{
   edbus_service_signal_emit(iface, EFREET_SIGNAL_ICON_CACHE_UPDATE, update);
}

void
send_signal_desktop_cache_update(Eina_Bool update)
{
   edbus_service_signal_emit(iface, EFREET_SIGNAL_DESKTOP_CACHE_UPDATE, update);
}

Eina_Bool
dbus_init(void)
{
   if (!edbus_init()) return EINA_FALSE;

   conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
   if (!conn) goto conn_error;

   edbus_connection_event_callback_add(conn,
      EDBUS_CONNECTION_EVENT_DISCONNECTED, disconnected, NULL);
   iface = edbus_service_interface_register(conn, PATH, &desc);
   edbus_name_request(conn, BUS, EDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
                      on_name_request, NULL);

   return EINA_TRUE;
conn_error:
   edbus_shutdown();
   return EINA_FALSE;
}

Eina_Bool
dbus_shutdown(void)
{
   edbus_connection_unref(conn);
   edbus_shutdown();
   return EINA_TRUE;

}