create rotation job and EAPI e_border_rotation_set
[platform/core/uifw/e17.git] / src / bin / e_fm_cmdline.c
1 # ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 # endif
4
5 #include <Ecore.h>
6 #include <Ecore_File.h>
7 #include <Ecore_Getopt.h>
8 #include <E_DBus.h>
9 #include <unistd.h>
10
11 static E_DBus_Connection *conn = NULL;
12 static int retval = EXIT_SUCCESS;
13 static int pending = 0;
14
15 static void
16 fm_open_reply(void *data __UNUSED__, DBusMessage *msg __UNUSED__, DBusError *err)
17 {
18    if (dbus_error_is_set(err))
19      {
20         retval = EXIT_FAILURE;
21         fprintf(stderr, "ERROR: %s: %s", err->name, err->message);
22      }
23
24    pending--;
25    if (!pending) ecore_main_loop_quit();
26 }
27
28 static Eina_Bool
29 fm_error_quit_last(void *data __UNUSED__)
30 {
31    if (!pending) ecore_main_loop_quit();
32    return EINA_FALSE;
33 }
34
35 static void
36 fm_open(const char *path)
37 {
38    DBusMessage *msg;
39    Eina_Bool sent;
40    const char *method;
41    char *p;
42
43    if (path[0] == '/')
44      p = strdup(path);
45    else
46      {
47         char buf[PATH_MAX];
48         if (!getcwd(buf, sizeof(buf)))
49           {
50              fprintf(stderr,
51                      "ERROR: Could not get current working directory: %s\n",
52                      strerror(errno));
53              ecore_idler_add(fm_error_quit_last, NULL);
54              return;
55           }
56         if (strcmp(path, ".") == 0)
57           p = strdup(buf);
58         else
59           {
60              char tmp[PATH_MAX];
61              snprintf(tmp, sizeof(tmp), "%s/%s", buf, path);
62              p = strdup(tmp);
63           }
64      }
65
66    EINA_LOG_DBG("'%s' -> '%s'", path, p);
67    if ((!p) || (p[0] == '\0'))
68      {
69         fprintf(stderr, "ERROR: Could not get path '%s'\n", path);
70         ecore_idler_add(fm_error_quit_last, NULL);
71         free(p);
72         return;
73      }
74
75    if (ecore_file_is_dir(p))
76      method = "OpenDirectory";
77    else
78      method = "OpenFile";
79
80    msg = dbus_message_new_method_call
81      ("org.enlightenment.FileManager",
82       "/org/enlightenment/FileManager",
83       "org.enlightenment.FileManager", method);
84    if (!msg)
85      {
86         fputs("ERROR: Could not create DBus Message\n", stderr);
87         ecore_idler_add(fm_error_quit_last, NULL);
88         free(p);
89         return;
90      }
91
92    dbus_message_append_args(msg, DBUS_TYPE_STRING, &p, DBUS_TYPE_INVALID);
93    free(p);
94
95    sent = !!e_dbus_message_send(conn, msg, fm_open_reply, -1, NULL);
96    dbus_message_unref(msg);
97
98    if (!sent)
99      {
100         fputs("ERROR: Could not send DBus Message\n", stderr);
101         ecore_idler_add(fm_error_quit_last, NULL);
102         return;
103      }
104
105    pending++;
106 }
107
108 static const Ecore_Getopt options = {
109    "enlightenment_filemanager",
110    "%prog [options] [file-or-folder1] ... [file-or-folderN]",
111    PACKAGE_VERSION,
112    "(C) 2012 Gustavo Sverzut Barbieri and others",
113    "BSD 2-Clause",
114    "Opens the Enlightenment File Manager at a given folders.",
115    EINA_FALSE,
116    {
117       ECORE_GETOPT_VERSION('V', "version"),
118       ECORE_GETOPT_COPYRIGHT('C', "copyright"),
119       ECORE_GETOPT_LICENSE('L', "license"),
120       ECORE_GETOPT_HELP('h', "help"),
121       ECORE_GETOPT_SENTINEL
122    }
123 };
124
125 EAPI int
126 main(int argc, char *argv[])
127 {
128    Eina_Bool quit_option = EINA_FALSE;
129    Ecore_Getopt_Value values[] = {
130      ECORE_GETOPT_VALUE_BOOL(quit_option),
131      ECORE_GETOPT_VALUE_BOOL(quit_option),
132      ECORE_GETOPT_VALUE_BOOL(quit_option),
133      ECORE_GETOPT_VALUE_BOOL(quit_option),
134      ECORE_GETOPT_VALUE_NONE
135    };
136    int args;
137
138    args = ecore_getopt_parse(&options, values, argc, argv);
139    if (args < 0)
140      {
141         fputs("ERROR: Could not parse command line options.\n", stderr);
142         return EXIT_FAILURE;
143      }
144
145    if (quit_option) return EXIT_SUCCESS;
146
147    ecore_init();
148    ecore_file_init();
149    e_dbus_init();
150
151    conn = e_dbus_bus_get(DBUS_BUS_SESSION);
152    if (!conn)
153      {
154         fputs("ERROR: Could not DBus SESSION bus.\n", stderr);
155         retval = EXIT_FAILURE;
156         goto end;
157      }
158
159    retval = EXIT_SUCCESS;
160
161    if (args == argc) fm_open(".");
162    else
163      {
164         for (; args < argc; args++)
165           fm_open(argv[args]);
166      }
167
168    ecore_main_loop_begin();
169
170  end:
171    e_dbus_shutdown();
172    ecore_file_shutdown();
173    ecore_shutdown();
174    return retval;
175 }