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
|
#include "e_mod_main.h"
static const char E_FILEMAN_BUS_NAME[] = "org.enlightenment.FileManager";
static const char E_FILEMAN_INTERFACE[] = "org.enlightenment.FileManager";
static const char E_FILEMAN_ERROR[] = "org.enlightenment.FileManager.Error";
static const char E_FILEMAN_PATH[] = "/org/enlightenment/FileManager";
typedef struct _E_Fileman_DBus_Daemon E_Fileman_DBus_Daemon;
struct _E_Fileman_DBus_Daemon
{
Eldbus_Connection *conn;
Eldbus_Service_Interface *iface;
};
static Eldbus_Message * _e_fileman_dbus_daemon_open_directory_cb(const Eldbus_Service_Interface *iface, const Eldbus_Message *msg);
static Eldbus_Message *_e_fileman_dbus_daemon_open_file_cb(const Eldbus_Service_Interface *iface, const Eldbus_Message *msg);
static Eldbus_Message *
_e_fileman_dbus_daemon_error(const Eldbus_Message *msg,
const char *error_msg)
{
return eldbus_message_error_new(msg, E_FILEMAN_ERROR, error_msg);
}
static const Eldbus_Method methods[] = {
{ "OpenDirectory", ELDBUS_ARGS({"s", "directory"}), NULL,
_e_fileman_dbus_daemon_open_directory_cb },
{ "OpenFile", ELDBUS_ARGS({"s", "file"}), NULL,
_e_fileman_dbus_daemon_open_file_cb },
{ }
};
static const Eldbus_Service_Interface_Desc desc = {
E_FILEMAN_INTERFACE, methods
};
static void
_e_fileman_dbus_daemon_object_init(E_Fileman_DBus_Daemon *d)
{
d->iface = eldbus_service_interface_register(d->conn, E_FILEMAN_PATH, &desc);
if (!d->iface)
{
fprintf(stderr, "ERROR: cannot add object to %s\n", E_FILEMAN_PATH);
return;
}
}
static void
_e_fileman_dbus_daemon_free(E_Fileman_DBus_Daemon *d)
{
if (d->iface)
eldbus_service_object_unregister(d->iface);
if (d->conn)
eldbus_connection_unref(d->conn);
free(d);
}
static Eldbus_Message *
_e_fileman_dbus_daemon_open_directory_cb(const Eldbus_Service_Interface *iface __UNUSED__,
const Eldbus_Message *msg)
{
const char *directory = NULL, *p;
char *dev, *to_free = NULL;
E_Zone *zone;
if (!eldbus_message_arguments_get(msg, "s", &directory))
{
fprintf(stderr, "Error: getting arguments of OpenDirectory call.\n");
return eldbus_message_method_return_new(msg);
}
if ((!directory) || (directory[0] == '\0'))
return _e_fileman_dbus_daemon_error(msg, "no directory provided.");
zone = e_util_zone_current_get(e_manager_current_get());
if (!zone)
return _e_fileman_dbus_daemon_error(msg, "could not find a zone.");
if (strstr(directory, "://"))
{
Efreet_Uri *uri = efreet_uri_decode(directory);
directory = NULL;
if (uri)
{
if ((uri->protocol) && (strcmp(uri->protocol, "file") == 0))
directory = to_free = strdup(uri->path);
efreet_uri_free(uri);
}
if (!directory)
return _e_fileman_dbus_daemon_error(msg, "unsupported protocol");
}
p = strchr(directory, '/');
if (p)
{
int len = p - directory + 1;
dev = malloc(len + 1);
if (!dev)
{
free(to_free);
return _e_fileman_dbus_daemon_error(msg,
"could not allocate memory.");
}
memcpy(dev, directory, len);
dev[len] = '\0';
if ((dev[0] != '/') && (dev[0] != '~'))
dev[len - 1] = '\0'; /* remove trailing '/' */
directory += p - directory;
}
else
{
dev = strdup(directory);
directory = "/";
}
e_fwin_new(zone->comp, dev, directory);
free(dev);
free(to_free);
return eldbus_message_method_return_new(msg);
}
static Eina_Bool
_mime_shell_script_check(const char *mime)
{
static const struct sh_script_map {
const char *str;
size_t len;
} options[] = {
#define O(x) {x, sizeof(x) - 1}
O("application/x-sh"),
O("application/x-shellscript"),
O("text/x-sh"),
#undef O
{NULL, 0}
};
const struct sh_script_map *itr;
size_t mimelen = strlen(mime);
for (itr = options; itr->str != NULL; itr++)
if ((mimelen == itr->len) && (memcmp(mime, itr->str, mimelen) == 0))
return EINA_TRUE;
return EINA_FALSE;
}
static Eldbus_Message*
_e_fileman_dbus_daemon_open_file_cb(const Eldbus_Service_Interface *iface __UNUSED__,
const Eldbus_Message *msg)
{
Eina_List *handlers;
const char *param_file = NULL, *mime, *errmsg = "unknow error";
char *real_file, *to_free = NULL;
E_Zone *zone;
if (!eldbus_message_arguments_get(msg, "s", ¶m_file))
{
fprintf(stderr, "ERROR: getting arguments of OpenFile call.\n");
return eldbus_message_method_return_new(msg);
}
if ((!param_file) || (param_file[0] == '\0'))
return _e_fileman_dbus_daemon_error(msg, "no file provided.");
zone = e_util_zone_current_get(e_manager_current_get());
if (!zone)
return _e_fileman_dbus_daemon_error(msg, "could not find a zone.");
if (!strstr(param_file, "://"))
{
real_file = ecore_file_realpath(param_file);
if (!real_file)
{
errmsg = "couldn't get realpath for file.";
goto error;
}
}
else
{
Efreet_Uri *uri = efreet_uri_decode(param_file);
real_file = NULL;
if (uri)
{
if ((uri->protocol) && (strcmp(uri->protocol, "file") == 0))
{
real_file = ecore_file_realpath(uri->path);
param_file = to_free = strdup(uri->path);
}
efreet_uri_free(uri);
}
if (!real_file)
{
errmsg = "unsupported protocol";
goto error;
}
}
mime = efreet_mime_type_get(real_file);
if (!mime)
{
errmsg = "couldn't find mime-type";
goto error;
}
if (strcmp(mime, "application/x-desktop") == 0)
{
Efreet_Desktop *desktop = efreet_desktop_new(real_file);
if (!desktop)
{
errmsg = "couldn't open desktop file";
goto error;
}
e_exec(zone, desktop, NULL, NULL, NULL);
efreet_desktop_free(desktop);
goto end;
}
else if ((strcmp(mime, "application/x-executable") == 0) ||
ecore_file_can_exec(param_file))
{
e_exec(zone, NULL, param_file, NULL, NULL);
goto end;
}
else if (_mime_shell_script_check(mime))
{
Eina_Strbuf *b = eina_strbuf_new();
const char *shell = getenv("SHELL");
if (!shell)
{
uid_t uid = getuid();
struct passwd *pw = getpwuid(uid);
if (pw) shell = pw->pw_shell;
}
if (!shell) shell = "/bin/sh";
eina_strbuf_append_printf(b, "%s %s %s",
e_config->exebuf_term_cmd,
shell,
param_file);
e_exec(zone, NULL, eina_strbuf_string_get(b), NULL, NULL);
eina_strbuf_free(b);
goto end;
}
handlers = efreet_util_desktop_mime_list(mime);
if (!handlers)
{
errmsg = "no handlers for given file";
goto end;
}
else
{
Efreet_Desktop *desktop = handlers->data;
Eina_List *files = eina_list_append(NULL, param_file);
e_exec(zone, desktop, NULL, files, NULL);
eina_list_free(files);
EINA_LIST_FREE(handlers, desktop)
efreet_desktop_free(desktop);
}
end:
free(real_file);
free(to_free);
return eldbus_message_method_return_new(msg);
error:
free(real_file);
free(to_free);
return _e_fileman_dbus_daemon_error(msg, errmsg);
}
static E_Fileman_DBus_Daemon *
_e_fileman_dbus_daemon_new(void)
{
E_Fileman_DBus_Daemon *d;
d = calloc(1, sizeof(E_Fileman_DBus_Daemon));
if (!d)
{
perror("ERROR: FILEMAN: cannot allocate fileman dbus daemon memory.");
return NULL;
}
d->conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
if (!d->conn)
goto error;
_e_fileman_dbus_daemon_object_init(d);
eldbus_name_request(d->conn, E_FILEMAN_BUS_NAME,
ELDBUS_NAME_REQUEST_FLAG_REPLACE_EXISTING, NULL, NULL);
return d;
error:
fprintf(stderr, "ERROR: FILEMAN: failed to create daemon at %p\n", d);
_e_fileman_dbus_daemon_free(d);
return NULL;
}
static E_Fileman_DBus_Daemon *_daemon = NULL;
void
e_fileman_dbus_init(void)
{
if (_daemon)
return;
eldbus_init();
_daemon = _e_fileman_dbus_daemon_new();
}
void
e_fileman_dbus_shutdown(void)
{
if (!_daemon)
return;
_e_fileman_dbus_daemon_free(_daemon);
_daemon = NULL;
eldbus_shutdown();
}
|