elementary: use Eio when available for fileselector.
[framework/uifw/elementary.git] / src / lib / elc_fileselector.c
1 /**
2  * @defgroup Fileselector Fileselector
3  *
4  * A fileselector is a widget that allows a user to navigate through a
5  * tree of files.  It contains buttons for Home(~) and Up(..) as well
6  * as cancel/ok buttons to confirm/cancel a selection.  This widget is
7  * currently very much in progress.
8  *
9  * TODO
10  * child elements focusing support
11  * userdefined icon/label cb
12  * show/hide/add buttons ???
13  * show/Hide hidden files
14  * double click to choose a file
15  * multiselection
16  * make variable/function names that are sensible
17  * Filter support
18  *
19  * Signals that you can add callbacks for are:
20  *
21  * "selected" - the user clicks on a file
22  * "directory,open" - the list is populated with new content.
23  *                    event_info is a directory.
24  * "done" - the user clicks on the ok or cancel button
25  */
26
27 #include <Elementary.h>
28 #include "elm_priv.h"
29
30 #ifdef HAVE_EIO
31 # include <Eio.h>
32 #endif
33
34 typedef struct _Widget_Data Widget_Data;
35
36 struct _Widget_Data
37 {
38    EINA_REFCOUNT;
39
40    Evas_Object *edje;
41    Evas_Object *filename_entry;
42    Evas_Object *path_entry;
43    Evas_Object *files_list;
44    Evas_Object *files_grid;
45    Evas_Object *up_button;
46    Evas_Object *home_button;
47
48    Evas_Object *ok_button;
49    Evas_Object *cancel_button;
50
51    const char  *path;
52    const char  *selection;
53    Ecore_Idler *sel_idler;
54
55    const char  *path_separator;
56
57 #ifdef HAVE_EIO
58    Eio_File    *current;
59 #endif
60
61    Elm_Fileselector_Mode mode;
62
63    Eina_Bool    only_folder : 1;
64    Eina_Bool    expand : 1;
65 };
66
67 struct sel_data
68 {
69    Evas_Object *fs;
70    const char  *path;
71 };
72
73 typedef struct _Widget_Request Widget_Request;
74 struct _Widget_Request
75 {
76    Widget_Data *wd;
77    Elm_Genlist_Item *parent;
78
79    Evas_Object *obj;
80    const char *path;
81    Eina_Bool first : 1;
82 };
83
84 typedef enum {
85   ELM_DIRECTORY = 0,
86   ELM_FILE_IMAGE = 1,
87   ELM_FILE_UNKNOW = 2,
88   ELM_FILE_LAST
89 } Elm_Fileselector_Type;
90
91 static Elm_Genlist_Item_Class list_itc[ELM_FILE_LAST] = {
92   { "default", { NULL, NULL, NULL, NULL, NULL }, NULL },
93   { "default", { NULL, NULL, NULL, NULL, NULL }, NULL },
94   { "default", { NULL, NULL, NULL, NULL, NULL }, NULL }
95 };
96 static Elm_Gengrid_Item_Class grid_itc[ELM_FILE_LAST] = {
97   { "default", { NULL, NULL, NULL, NULL } },
98   { "default", { NULL, NULL, NULL, NULL } },
99   { "default", { NULL, NULL, NULL, NULL } }
100 };
101
102 static const char *widtype = NULL;
103
104 static const char SIG_DIRECTORY_OPEN[] = "directory,open";
105 static const char SIG_DONE[] = "done";
106 static const char SIG_SELECTED[] = "selected";
107 static const Evas_Smart_Cb_Description _signals[] = {
108    {SIG_DIRECTORY_OPEN, "s"},
109    {SIG_DONE, "s"},
110    {SIG_SELECTED, "s"},
111    {NULL, NULL}
112 };
113
114 static void _populate(Evas_Object      *obj,
115                       const char       *path,
116                       Elm_Genlist_Item *parent);
117 static void _do_anchors(Evas_Object *obj,
118                         const char  *path);
119
120 /***  ELEMENTARY WIDGET  ***/
121 static void
122 _widget_data_free(Widget_Data *wd)
123 {
124    if (wd->path) eina_stringshare_del(wd->path);
125    if (wd->selection) eina_stringshare_del(wd->selection);
126    if (wd->sel_idler)
127      {
128         void *sd;
129
130         sd = ecore_idler_del(wd->sel_idler);
131         free(sd);
132      }
133    free(wd);
134 }
135
136 static void
137 _del_hook(Evas_Object *obj)
138 {
139    Widget_Data *wd;
140
141    wd = elm_widget_data_get(obj);
142    if (!wd) return;
143
144    eio_file_cancel(wd->current);
145
146    wd->files_list = NULL;
147    wd->files_grid = NULL;
148
149    EINA_REFCOUNT_UNREF(wd)
150      _widget_data_free(wd);
151 }
152
153 static void
154 _sizing_eval(Evas_Object *obj)
155 {
156    Widget_Data *wd = elm_widget_data_get(obj);
157    Evas_Coord minw = -1, minh = -1;
158    if (!wd) return;
159    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
160    edje_object_size_min_restricted_calc(wd->edje, &minw, &minh, minw, minh);
161    evas_object_size_hint_min_set(obj, minw, minh);
162 }
163
164 static void
165 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
166 {
167    Widget_Data *wd = elm_widget_data_get(obj);
168    if (!wd) return;
169    elm_widget_mirrored_set(wd->cancel_button, rtl);
170    elm_widget_mirrored_set(wd->ok_button, rtl);
171    elm_widget_mirrored_set(wd->files_list, rtl);
172    elm_widget_mirrored_set(wd->up_button, rtl);
173    elm_widget_mirrored_set(wd->home_button, rtl);
174    edje_object_mirrored_set(wd->edje, rtl);
175 }
176
177 static void
178 _theme_hook(Evas_Object *obj)
179 {
180    Widget_Data *wd = elm_widget_data_get(obj);
181    const char *style = elm_widget_style_get(obj);
182    const char *data;
183    char buf[1024];
184
185    if (!wd) return;
186    _elm_widget_mirrored_reload(obj);
187
188    _elm_theme_object_set(obj, wd->edje, "fileselector", "base", style);
189
190    if (elm_object_disabled_get(obj))
191      edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
192
193    data = edje_object_data_get(wd->edje, "path_separator");
194    if (data)
195      wd->path_separator = data;
196    else
197      wd->path_separator = "/";
198
199    if (!style) style = "default";
200    snprintf(buf, sizeof(buf), "fileselector/%s", style);
201
202 #define SWALLOW(part_name, object_ptn)                                \
203   if (object_ptn)                                                     \
204     {                                                                 \
205        elm_widget_style_set(object_ptn, buf);                         \
206        if (edje_object_part_swallow(wd->edje, part_name, object_ptn)) \
207          evas_object_show(object_ptn);                                \
208        else                                                           \
209          evas_object_hide(object_ptn);                                \
210     }
211    SWALLOW("elm.swallow.up", wd->up_button);
212    SWALLOW("elm.swallow.home", wd->home_button);
213
214    if (wd->mode == ELM_FILESELECTOR_LIST)
215      {
216         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
217                                      wd->files_list))
218           {
219              evas_object_show(wd->files_list);
220              evas_object_hide(wd->files_grid);
221           }
222         else
223           evas_object_hide(wd->files_list);
224      }
225    else
226      {
227         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
228                                      wd->files_grid))
229           {
230              evas_object_show(wd->files_grid);
231              evas_object_hide(wd->files_list);
232           }
233         else
234           evas_object_hide(wd->files_grid);
235      }
236
237    SWALLOW("elm.swallow.filename", wd->filename_entry);
238    SWALLOW("elm.swallow.path", wd->path_entry);
239
240    snprintf(buf, sizeof(buf), "fileselector/actions/%s", style);
241    SWALLOW("elm.swallow.cancel", wd->cancel_button);
242    SWALLOW("elm.swallow.ok", wd->ok_button);
243 #undef SWALLOW
244
245    edje_object_message_signal_process(wd->edje);
246    _mirrored_set(obj, elm_widget_mirrored_get(obj));
247    edje_object_scale_set
248      (wd->edje, elm_widget_scale_get(obj) * _elm_config->scale);
249    _sizing_eval(obj);
250 }
251
252 /***  GENLIST "MODEL"  ***/
253 static char *
254 _itc_label_get(void              *data,
255                Evas_Object *obj   __UNUSED__,
256                const char *source __UNUSED__)
257 {
258    return strdup(ecore_file_file_get(data)); /* NOTE this will be
259                                               * free() by the
260                                               * caller */
261 }
262
263 static Evas_Object *
264 _itc_icon_folder_get(void        *data __UNUSED__,
265                      Evas_Object *obj,
266                      const char  *source)
267 {
268    Evas_Object *ic;
269
270    if (strcmp(source, "elm.swallow.icon")) return NULL;
271
272    ic = elm_icon_add(obj);
273    elm_icon_standard_set(ic, "folder");
274
275    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
276                                     1, 1);
277    return ic;
278 }
279
280 static Evas_Object *
281 _itc_icon_image_get(void        *data,
282                     Evas_Object *obj,
283                     const char  *source)
284 {
285    const char *filename = data;
286    Evas_Object *ic;
287
288    if (strcmp(source, "elm.swallow.icon")) return NULL;
289
290    ic = elm_icon_add(obj);
291    elm_icon_standard_set(ic, "image");
292    elm_icon_thumb_set(ic, filename, NULL);
293
294    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
295                                     1, 1);
296    return ic;
297 }
298
299 static Evas_Object *
300 _itc_icon_file_get(void        *data __UNUSED__,
301                    Evas_Object *obj,
302                    const char  *source)
303 {
304    Evas_Object *ic;
305
306    if (strcmp(source, "elm.swallow.icon")) return NULL;
307
308    ic = elm_icon_add(obj);
309    elm_icon_standard_set(ic, "file");
310
311    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
312                                     1, 1);
313    return ic;
314 }
315
316 static Eina_Bool
317 _itc_state_get(void *data         __UNUSED__,
318                Evas_Object *obj   __UNUSED__,
319                const char *source __UNUSED__)
320 {
321    return EINA_FALSE;
322 }
323
324 static void
325 _itc_del(void            *data,
326          Evas_Object *obj __UNUSED__)
327 {
328    eina_stringshare_del(data);
329 }
330
331 static void
332 _expand_done(void            *data,
333              Evas_Object *obj __UNUSED__,
334              void            *event_info)
335 {
336    Elm_Genlist_Item *it = event_info;
337    const char *path = elm_genlist_item_data_get(it);
338    _populate(data, path, it);
339 }
340
341 static void
342 _contract_done(void *data       __UNUSED__,
343                Evas_Object *obj __UNUSED__,
344                void            *event_info)
345 {
346    Elm_Genlist_Item *it = event_info;
347    elm_genlist_item_subitems_clear(it);
348 }
349
350 static void
351 _expand_req(void *data       __UNUSED__,
352             Evas_Object *obj __UNUSED__,
353             void            *event_info)
354 {
355    Elm_Genlist_Item *it = event_info;
356    elm_genlist_item_expanded_set(it, 1);
357 }
358
359 static void
360 _contract_req(void *data       __UNUSED__,
361               Evas_Object *obj __UNUSED__,
362               void            *event_info)
363 {
364    Elm_Genlist_Item *it = event_info;
365    elm_genlist_item_expanded_set(it, 0);
366 }
367
368 /***  PRIVATES  ***/
369 static Eina_Bool
370 _sel_do(void *data)
371 {
372    struct sel_data *sd;
373    const char *path;
374    Widget_Data *wd;
375    const char *p;
376
377    sd = data;
378    wd = elm_widget_data_get(sd->fs);
379    path = sd->path;
380
381    if ((!wd->only_folder) && ecore_file_is_dir(path))
382      {
383         if (wd->expand && wd->mode == ELM_FILESELECTOR_LIST)
384           {
385              _do_anchors(sd->fs, path);
386              elm_entry_entry_set(wd->filename_entry, "");
387           }
388         else
389           {
390              /* keep a ref to path 'couse it will be destroyed by _populate */
391              p = eina_stringshare_add(path);
392              _populate(sd->fs, p, NULL);
393              eina_stringshare_del(p);
394           }
395         goto end;
396      }
397    else /* navigating through folders only or file is not a dir. */
398      {
399         if (wd->expand && wd->mode == ELM_FILESELECTOR_LIST)
400           _do_anchors(sd->fs, path);
401         else if (wd->only_folder)
402           {
403              /* keep a ref to path 'couse it will be destroyed by _populate */
404              p = eina_stringshare_add(path);
405              _populate(sd->fs, p, NULL);
406              eina_stringshare_del(p);
407           }
408         elm_entry_entry_set(wd->filename_entry,
409                                      ecore_file_file_get(path));
410      }
411
412    evas_object_smart_callback_call(sd->fs, SIG_SELECTED, (void *)path);
413
414 end:
415    wd->sel_idler = NULL;
416    free(sd);
417    return ECORE_CALLBACK_CANCEL;
418 }
419
420 static void
421 _sel(void            *data,
422      Evas_Object *obj __UNUSED__,
423      void            *event_info)
424 {
425    struct sel_data *sd;
426    Widget_Data *wd;
427    void *old_sd;
428    char *dir;
429
430    wd = elm_widget_data_get(data);
431    if (!wd) return;
432
433    sd = malloc(sizeof(*sd));
434    sd->fs = data;
435    sd->path = wd->mode == ELM_FILESELECTOR_LIST ?
436        elm_genlist_item_data_get(event_info) :
437        elm_gengrid_item_data_get(event_info);
438
439    if (!sd->path)
440      {
441         eina_stringshare_replace(&wd->path, "");
442         goto end;
443      }
444
445    dir = wd->only_folder ? strdup(sd->path) : ecore_file_dir_get(sd->path);
446    if (dir)
447      {
448         eina_stringshare_replace(&wd->path, dir);
449         free(dir);
450      }
451    else
452      {
453         eina_stringshare_replace(&wd->path, "");
454      }
455
456 end:
457    if (wd->sel_idler)
458      {
459         old_sd = ecore_idler_del(wd->sel_idler);
460         free(old_sd);
461      }
462    wd->sel_idler = ecore_idler_add(_sel_do, sd);
463 }
464
465 static void
466 _up(void            *data,
467     Evas_Object *obj __UNUSED__,
468     void *event_info __UNUSED__)
469 {
470    Evas_Object *fs = data;
471    char *parent;
472
473    Widget_Data *wd = elm_widget_data_get(fs);
474    if (!wd) return;
475    parent = ecore_file_dir_get(wd->path);
476    _populate(fs, parent, NULL);
477    free(parent);
478 }
479
480 static void
481 _home(void            *data,
482       Evas_Object *obj __UNUSED__,
483       void *event_info __UNUSED__)
484 {
485    Evas_Object *fs = data;
486    _populate(fs, getenv("HOME"), NULL);
487 }
488
489 static void
490 _ok(void            *data,
491     Evas_Object *obj __UNUSED__,
492     void *event_info __UNUSED__)
493 {
494    Evas_Object *fs = data;
495    evas_object_smart_callback_call(fs, SIG_DONE,
496                                    (void *)elm_fileselector_selected_get(fs));
497 }
498
499 static void
500 _canc(void            *data,
501       Evas_Object *obj __UNUSED__,
502       void *event_info __UNUSED__)
503 {
504    Evas_Object *fs = data;
505    evas_object_smart_callback_call(fs, SIG_DONE, NULL);
506 }
507
508 static void
509 _anchor_clicked(void            *data,
510                 Evas_Object *obj __UNUSED__,
511                 void            *event_info)
512 {
513    Evas_Object *fs = data;
514    Widget_Data *wd = elm_widget_data_get(fs);
515    Elm_Entry_Anchor_Info *info = event_info;
516    const char *p;
517    if (!wd) return;
518    // keep a ref to path 'couse it will be destroyed by _populate
519    p = eina_stringshare_add(info->name);
520    _populate(fs, p, NULL);
521    evas_object_smart_callback_call(data, SIG_SELECTED, (void *)p);
522    eina_stringshare_del(p);
523 }
524
525 static void
526 _do_anchors(Evas_Object *obj,
527             const char  *path)
528 {
529    Widget_Data *wd = elm_widget_data_get(obj);
530    char **tok, buf[PATH_MAX * 3];
531    int i, j;
532    if (!wd) return;
533    buf[0] = '\0';
534    tok = eina_str_split(path, "/", 0);
535    eina_strlcat(buf, "<a href=/>root</a>", sizeof(buf));
536    for (i = 0; tok[i]; i++)
537      {
538         if ((!tok[i]) || (!tok[i][0])) continue;
539         eina_strlcat(buf, wd->path_separator, sizeof(buf));
540         eina_strlcat(buf, "<a href=", sizeof(buf));
541         for (j = 0; j <= i; j++)
542           {
543              if (strlen(tok[j]) < 1) continue;
544              eina_strlcat(buf, "/", sizeof(buf));
545              eina_strlcat(buf, tok[j], sizeof(buf));
546           }
547         eina_strlcat(buf, ">", sizeof(buf));
548         eina_strlcat(buf, tok[i], sizeof(buf));
549         eina_strlcat(buf, "</a>", sizeof(buf));
550      }
551    free(tok[0]);
552    free(tok);
553
554    elm_entry_entry_set(wd->path_entry, buf);
555 }
556
557 #ifdef HAVE_EIO
558 static Eina_Bool
559 _filter_cb(void *data __UNUSED__, Eio_File *handler, const Eina_File_Direct_Info *info)
560 {
561    const char *filename;
562
563    if (info->path[info->name_start] == '.')
564      return EINA_FALSE;
565
566    filename = eina_stringshare_add(info->path);
567    eio_file_associate_direct_add(handler, "filename", filename, EINA_FREE_CB(eina_stringshare_del));
568
569    if (info->type == EINA_FILE_DIR)
570      {
571         eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_DIRECTORY], NULL);
572         eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_DIRECTORY], NULL);
573      }
574    else
575      {
576         if (evas_object_image_extension_can_load_get(info->path + info->name_start))
577           {
578              eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_FILE_IMAGE], NULL);
579              eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_FILE_IMAGE], NULL);
580           }
581         else
582           {
583              eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_FILE_UNKNOW], NULL);
584              eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_FILE_UNKNOW], NULL);
585           }
586      }
587
588    return EINA_TRUE;
589 }
590
591 static int
592 _file_grid_cmp(const void *a, const void *b)
593 {
594    const Elm_Gengrid_Item *ga = a;
595    const Elm_Gengrid_Item *gb = b;
596    const Elm_Gengrid_Item_Class *ca = elm_gengrid_item_item_class_get(ga);
597    const Elm_Gengrid_Item_Class *cb = elm_gengrid_item_item_class_get(gb);
598
599    if (ca == &grid_itc[ELM_DIRECTORY])
600      {
601         if (cb != &grid_itc[ELM_DIRECTORY])
602           return -1;
603      }
604
605    return strcoll(elm_gengrid_item_data_get(ga), elm_gengrid_item_data_get(gb));
606 }
607
608 static int
609 _file_list_cmp(const void *a, const void *b)
610 {
611    const Elm_Genlist_Item *la = a;
612    const Elm_Genlist_Item *lb = b;
613    const Elm_Genlist_Item_Class *ca = elm_genlist_item_item_class_get(la);
614    const Elm_Genlist_Item_Class *cb = elm_genlist_item_item_class_get(lb);
615
616    if (ca == &list_itc[ELM_DIRECTORY])
617      {
618         if (cb != &list_itc[ELM_DIRECTORY])
619           return -1;
620      }
621
622    return strcoll(elm_genlist_item_data_get(la), elm_genlist_item_data_get(lb));
623 }
624
625 static void
626 _signal_first(Widget_Request *wr)
627 {
628    if (!wr->first) return ;
629    evas_object_smart_callback_call(wr->obj, SIG_DIRECTORY_OPEN, (void *)wr->path);
630    if (!wr->parent)
631      {
632         elm_genlist_clear(wr->wd->files_list);
633         elm_gengrid_clear(wr->wd->files_grid);
634         eina_stringshare_replace(&wr->wd->path, wr->path);
635         _do_anchors(wr->obj, wr->path);
636      }
637
638    if (wr->wd->filename_entry) elm_entry_entry_set(wr->wd->filename_entry, "");
639
640    wr->first = EINA_FALSE;
641 }
642
643 static void
644 _main_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info *info __UNUSED__)
645 {
646    Widget_Request *wr = data;
647
648    if (eio_file_check(handler))
649      return ;
650    if (!wr->wd->files_list || !wr->wd->files_grid) return ;
651
652    _signal_first(wr);
653
654    if (wr->wd->mode == ELM_FILESELECTOR_LIST)
655      elm_genlist_item_direct_sorted_insert(wr->wd->files_list, eio_file_associate_find(handler, "type/list"),
656                                            eina_stringshare_ref(eio_file_associate_find(handler, "filename")),
657                                            wr->parent, ELM_GENLIST_ITEM_NONE, _file_list_cmp, NULL, NULL);
658    else if (wr->wd->mode == ELM_FILESELECTOR_GRID)
659      elm_gengrid_item_direct_sorted_insert(wr->wd->files_grid, eio_file_associate_find(handler, "type/grid"),
660                                            eina_stringshare_ref(eio_file_associate_find(handler, "filename")),
661                                            _file_grid_cmp, NULL, NULL);
662 }
663
664 static void
665 _widget_request_cleanup(Widget_Request *wr)
666 {
667    EINA_REFCOUNT_UNREF(wr->wd)
668      _widget_data_free(wr->wd);
669
670    eina_stringshare_del(wr->path);
671    free(wr);
672 }
673
674 static void
675 _done_cb(void *data, Eio_File *handler __UNUSED__)
676 {
677    Widget_Request *wr = data;
678
679    _signal_first(wr);
680
681    wr->wd->current = NULL;
682    _widget_request_cleanup(wr);
683 }
684
685 static void
686 _error_cb(void *data, Eio_File *handler, int error __UNUSED__)
687 {
688    Widget_Request *wr = data;
689
690    if (wr->wd->current == handler)
691      wr->wd->current = NULL;
692    _widget_request_cleanup(wr);
693 }
694
695 #endif
696
697 static void
698 _populate(Evas_Object      *obj,
699           const char       *path,
700           Elm_Genlist_Item *parent)
701 {
702    Widget_Data *wd = elm_widget_data_get(obj);
703 #ifdef HAVE_EIO
704    Widget_Request *wr;
705 #else
706    Eina_File_Direct_Info *file;
707    Eina_Iterator *it;
708    const char *real;
709    Eina_List *files = NULL, *dirs = NULL;
710 #endif
711
712    if (!wd) return;
713 #ifndef HAVE_EIO
714    if (!ecore_file_is_dir(path)) return ;
715    it = eina_file_stat_ls(path);
716    if (!it) return ;
717    evas_object_smart_callback_call(obj, SIG_DIRECTORY_OPEN, (void *)path);
718    if (!parent)
719      {
720         elm_genlist_clear(wd->files_list);
721         elm_gengrid_clear(wd->files_grid);
722         eina_stringshare_replace(&wd->path, path);
723         _do_anchors(obj, path);
724      }
725
726    if (wd->filename_entry) elm_entry_entry_set(wd->filename_entry, "");
727    EINA_ITERATOR_FOREACH(it, file)
728      {
729         const char *filename;
730
731         if (file->path[file->name_start] == '.')
732           continue ;
733
734         filename = eina_stringshare_add(file->path);
735         if (file->type == EINA_FILE_DIR)
736           dirs = eina_list_append(dirs, filename);
737         else if (!wd->only_folder)
738           files = eina_list_append(files, filename);
739      }
740    eina_iterator_free(it);
741
742    files = eina_list_sort(files, eina_list_count(files),
743                           EINA_COMPARE_CB(strcoll));
744    dirs = eina_list_sort(dirs, eina_list_count(dirs), EINA_COMPARE_CB(strcoll));
745    EINA_LIST_FREE(dirs, real)
746      {
747         if (wd->mode == ELM_FILESELECTOR_LIST)
748           elm_genlist_item_append(wd->files_list, &list_itc[ELM_DIRECTORY],
749                                   real, /* item data */
750                                   parent,
751                                   wd->expand ? ELM_GENLIST_ITEM_SUBITEMS :
752                                   ELM_GENLIST_ITEM_NONE,
753                                   NULL, NULL);
754         else if (wd->mode == ELM_FILESELECTOR_GRID)
755           elm_gengrid_item_append(wd->files_grid, &grid_itc[ELM_DIRECTORY],
756                                   real, /* item data */
757                                   NULL, NULL);
758      }
759
760    EINA_LIST_FREE(files, real)
761      {
762         Elm_Fileselector_Type type = evas_object_image_extension_can_load_fast_get(real) ?
763           ELM_FILE_IMAGE : ELM_FILE_UNKNOW;
764
765         if (wd->mode == ELM_FILESELECTOR_LIST)
766           elm_genlist_item_append(wd->files_list, &list_itc[type],
767                                   real, /* item data */
768                                   parent, ELM_GENLIST_ITEM_NONE,
769                                   NULL, NULL);
770         else if (wd->mode == ELM_FILESELECTOR_GRID)
771           elm_gengrid_item_append(wd->files_grid, &grid_itc[type],
772                                   real, /* item data */
773                                   NULL, NULL);
774      }
775 #else
776    if (wd->current)
777      eio_file_cancel(wd->current);
778    wr = malloc(sizeof (Widget_Request));
779    if (!wr) return ;
780    wr->wd = wd;
781    EINA_REFCOUNT_REF(wr->wd);
782    wr->parent = parent; /* FIXME: should we refcount the parent ? */
783    wr->obj = obj;
784    wr->path = eina_stringshare_add(path);
785    wr->first = EINA_TRUE;
786
787    wd->current = eio_file_stat_ls(path,
788                                   _filter_cb,
789                                   _main_cb,
790                                   _done_cb,
791                                   _error_cb,
792                                   wr);
793 #endif
794 }
795
796 /***  API  ***/
797
798 /**
799  * Add a new Fileselector object
800  *
801  * @param parent The parent object
802  * @return The new object or NULL if it cannot be created
803  *
804  * @ingroup Fileselector
805  */
806 EAPI Evas_Object *
807 elm_fileselector_add(Evas_Object *parent)
808 {
809    Evas *e;
810    Evas_Object *obj, *ic, *bt, *li, *en, *grid;
811    Widget_Data *wd;
812    unsigned int i;
813    int s;
814
815    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
816
817    EINA_REFCOUNT_INIT(wd);
818
819    ELM_SET_WIDTYPE(widtype, "fileselector");
820    elm_widget_type_set(obj, "fileselector");
821    elm_widget_sub_object_add(parent, obj);
822    elm_widget_data_set(obj, wd);
823    elm_widget_del_hook_set(obj, _del_hook);
824    elm_widget_theme_hook_set(obj, _theme_hook);
825    elm_widget_can_focus_set(obj, EINA_FALSE);
826
827    wd->expand = !!_elm_config->fileselector_expand_enable;
828
829    wd->edje = edje_object_add(e);
830    _elm_theme_object_set(obj, wd->edje, "fileselector", "base", "default");
831    elm_widget_resize_object_set(obj, wd->edje);
832
833    // up btn
834    ic = elm_icon_add(parent);
835    elm_icon_standard_set(ic, "arrow_up");
836    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
837    bt = elm_button_add(parent);
838    elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
839    elm_button_icon_set(bt, ic);
840    elm_button_label_set(bt, E_("Up"));
841    evas_object_size_hint_align_set(bt, 0.0, 0.0);
842
843    evas_object_smart_callback_add(bt, "clicked", _up, obj);
844
845    elm_widget_sub_object_add(obj, bt);
846    wd->up_button = bt;
847
848    // home btn
849    ic = elm_icon_add(parent);
850    elm_icon_standard_set(ic, "home");
851    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
852    bt = elm_button_add(parent);
853    elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
854    elm_button_icon_set(bt, ic);
855    elm_button_label_set(bt, E_("Home"));
856    evas_object_size_hint_align_set(bt, 0.0, 0.0);
857
858    evas_object_smart_callback_add(bt, "clicked", _home, obj);
859
860    elm_widget_sub_object_add(obj, bt);
861    wd->home_button = bt;
862
863    list_itc[ELM_DIRECTORY].func.icon_get = grid_itc[ELM_DIRECTORY].func.icon_get = _itc_icon_folder_get;
864    list_itc[ELM_FILE_IMAGE].func.icon_get = grid_itc[ELM_FILE_IMAGE].func.icon_get = _itc_icon_image_get;
865    list_itc[ELM_FILE_UNKNOW].func.icon_get = grid_itc[ELM_FILE_UNKNOW].func.icon_get = _itc_icon_file_get;
866
867    for (i = 0; i < ELM_FILE_LAST; ++i)
868      {
869         list_itc[i].func.label_get = grid_itc[i].func.label_get = _itc_label_get;
870         list_itc[i].func.state_get = grid_itc[i].func.state_get = _itc_state_get;
871         list_itc[i].func.del = grid_itc[i].func.del = _itc_del;
872      }
873
874    li = elm_genlist_add(parent);
875    elm_widget_mirrored_automatic_set(li, EINA_FALSE);
876    evas_object_size_hint_align_set(li, EVAS_HINT_FILL, EVAS_HINT_FILL);
877    evas_object_size_hint_weight_set(li, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
878    evas_object_size_hint_min_set(li, 100, 100);
879
880    grid = elm_gengrid_add(parent);
881    elm_widget_mirrored_automatic_set(grid, EINA_FALSE);
882    evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
883    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
884
885    s = elm_finger_size_get() * 2;
886    elm_gengrid_item_size_set(grid, s, s);
887    elm_gengrid_align_set(grid, 0.0, 0.0);
888
889    evas_object_smart_callback_add(li, "selected", _sel, obj);
890    evas_object_smart_callback_add(li, "expand,request", _expand_req, obj);
891    evas_object_smart_callback_add(li, "contract,request", _contract_req, obj);
892    evas_object_smart_callback_add(li, "expanded", _expand_done, obj);
893    evas_object_smart_callback_add(li, "contracted", _contract_done, obj);
894
895    evas_object_smart_callback_add(grid, "selected", _sel, obj);
896
897    elm_widget_sub_object_add(obj, li);
898    elm_widget_sub_object_add(obj, grid);
899    wd->files_list = li;
900    wd->files_grid = grid;
901
902    // path entry
903    en = elm_entry_add(parent);
904    elm_entry_scrollable_set(en, EINA_TRUE);
905    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
906    elm_entry_editable_set(en, EINA_FALSE);
907    elm_entry_single_line_set(en, EINA_TRUE);
908    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
909    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
910    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
911
912    evas_object_smart_callback_add(en, "anchor,clicked", _anchor_clicked, obj);
913
914    elm_widget_sub_object_add(obj, en);
915    wd->path_entry = en;
916
917    // filename entry
918    en = elm_entry_add(parent);
919    elm_entry_scrollable_set(en, EINA_TRUE);
920    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
921    elm_entry_editable_set(en, EINA_TRUE);
922    elm_entry_single_line_set(en, EINA_TRUE);
923    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
924    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
925    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
926
927    elm_widget_sub_object_add(obj, en);
928    wd->filename_entry = en;
929
930    elm_fileselector_buttons_ok_cancel_set(obj, EINA_TRUE);
931    elm_fileselector_is_save_set(obj, EINA_FALSE);
932
933    _theme_hook(obj);
934
935    evas_object_smart_callbacks_descriptions_set(obj, _signals);
936    return obj;
937 }
938
939 /**
940  * This enables/disables the file name entry box where the user can
941  * type in a name for the file to be saved as.
942  *
943  * @param obj The fileselector object
944  * @param is_save If true, the fileselector is a save dialog
945  *
946  * @ingroup Fileselector
947  */
948 EAPI void
949 elm_fileselector_is_save_set(Evas_Object *obj,
950                              Eina_Bool    is_save)
951 {
952    ELM_CHECK_WIDTYPE(obj, widtype);
953    Widget_Data *wd = elm_widget_data_get(obj);
954    if (!wd) return;
955
956    elm_object_disabled_set(wd->filename_entry, !is_save);
957
958    if (is_save)
959      edje_object_signal_emit(wd->edje, "elm,state,save,on", "elm");
960    else
961      edje_object_signal_emit(wd->edje, "elm,state,save,off", "elm");
962 }
963
964 /**
965  * This returns whether the fileselector is a "save" type fileselector
966  *
967  * @param obj The fileselector object
968  * @return If true, the fileselector is a save type.
969  *
970  * @ingroup Fileselector
971  */
972 EAPI Eina_Bool
973 elm_fileselector_is_save_get(const Evas_Object *obj)
974 {
975    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
976    Widget_Data *wd = elm_widget_data_get(obj);
977    if (!wd) return EINA_FALSE;
978    return elm_object_disabled_get(wd->filename_entry);
979 }
980
981 /**
982  * This enables/disables folder-only view in the fileselector.
983  *
984  * @param obj The fileselector object
985  * @param only If true, the fileselector will only display directories.
986  * If false, files are displayed also.
987  *
988  * @ingroup Fileselector
989  */
990 EAPI void
991 elm_fileselector_folder_only_set(Evas_Object *obj,
992                                  Eina_Bool    only)
993 {
994    ELM_CHECK_WIDTYPE(obj, widtype);
995    Widget_Data *wd = elm_widget_data_get(obj);
996    if (!wd) return;
997    if (wd->only_folder == only) return;
998    wd->only_folder = !!only;
999    if (wd->path) _populate(obj, wd->path, NULL);
1000 }
1001
1002 /**
1003  * This gets the state of file display in the fileselector.
1004  *
1005  * @param obj The fileselector object
1006  * @return If true, files are not being shown in the fileselector.
1007  * If false, files are being shown.
1008  *
1009  * @ingroup Fileselector
1010  */
1011 EAPI Eina_Bool
1012 elm_fileselector_folder_only_get(const Evas_Object *obj)
1013 {
1014    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1015    Widget_Data *wd = elm_widget_data_get(obj);
1016    if (!wd) return EINA_FALSE;
1017    return wd->only_folder;
1018 }
1019
1020 /**
1021  * This enables/disables the ok,cancel buttons.
1022  *
1023  * @param obj The fileselector object
1024  * @param only If true, a box containing ok and cancel buttons is created.
1025  * If false, the box and the buttons are destroyed.
1026  *
1027  * @ingroup Fileselector
1028  */
1029 EAPI void
1030 elm_fileselector_buttons_ok_cancel_set(Evas_Object *obj,
1031                                        Eina_Bool    visible)
1032 {
1033    ELM_CHECK_WIDTYPE(obj, widtype);
1034    Widget_Data *wd = elm_widget_data_get(obj);
1035    Evas_Object *bt;
1036    if (!wd) return;
1037
1038    if (visible)
1039      {
1040         // cancel btn
1041         bt = elm_button_add(obj);
1042         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1043         elm_button_label_set(bt, E_("Cancel"));
1044
1045         evas_object_smart_callback_add(bt, "clicked", _canc, obj);
1046
1047         elm_widget_sub_object_add(obj, bt);
1048         wd->cancel_button = bt;
1049
1050         // ok btn
1051         bt = elm_button_add(obj);
1052         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1053         elm_button_label_set(bt, E_("OK"));
1054
1055         evas_object_smart_callback_add(bt, "clicked", _ok, obj);
1056
1057         elm_widget_sub_object_add(obj, bt);
1058         wd->ok_button = bt;
1059
1060         _theme_hook(obj);
1061      }
1062    else
1063      {
1064         evas_object_del(wd->cancel_button);
1065         wd->cancel_button = NULL;
1066         evas_object_del(wd->ok_button);
1067         wd->ok_button = NULL;
1068      }
1069 }
1070
1071 /**
1072  * This gets the state of the box containing ok and cancel buttons.
1073  *
1074  * @param obj The fileselector object
1075  * @return If true, the box exists.
1076  * If false, the box does not exist.
1077  *
1078  * @ingroup Fileselector
1079  */
1080 EAPI Eina_Bool
1081 elm_fileselector_buttons_ok_cancel_get(const Evas_Object *obj)
1082 {
1083    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1084    Widget_Data *wd = elm_widget_data_get(obj);
1085    if (!wd) return EINA_FALSE;
1086    return wd->ok_button ? EINA_TRUE : EINA_FALSE;
1087 }
1088
1089 /**
1090  * This enables a tree view in the fileselector, <b>if in @c
1091  * ELM_FILESELECTOR_LIST mode</b>. If it's in other mode, the changes
1092  * made by this function will only be visible when one switches back
1093  * to list mode.
1094  *
1095  * @param obj The fileselector object
1096  * @param expand If true, tree view is enabled.
1097  * If false, tree view is disabled.
1098  *
1099  * In a tree view, arrows are created on the sides of directories,
1100  * allowing them to expand in place.
1101  *
1102  * @ingroup Fileselector
1103  */
1104 EAPI void
1105 elm_fileselector_expandable_set(Evas_Object *obj,
1106                                 Eina_Bool    expand)
1107 {
1108    ELM_CHECK_WIDTYPE(obj, widtype);
1109    Widget_Data *wd;
1110
1111    wd = elm_widget_data_get(obj);
1112    if (!wd) return;
1113
1114    wd->expand = !!expand;
1115
1116    if (wd->path) _populate(obj, wd->path, NULL);
1117 }
1118
1119 /**
1120  * This gets the state of tree view in the fileselector.
1121  *
1122  * @param obj The fileselector object
1123  * @return If true, tree view is enabled and folders will be expandable.
1124  * If false, tree view is disabled.
1125  *
1126  * @ingroup Fileselector
1127  */
1128 EAPI Eina_Bool
1129 elm_fileselector_expandable_get(const Evas_Object *obj)
1130 {
1131    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1132    Widget_Data *wd = elm_widget_data_get(obj);
1133    if (!wd) return EINA_FALSE;
1134    return wd->expand;
1135 }
1136
1137 /**
1138  * This sets the path that the fileselector will display.
1139  *
1140  * @param obj The fileselector object
1141  * @param path The path of the fileselector
1142  *
1143  * @ingroup Fileselector
1144  */
1145 EAPI void
1146 elm_fileselector_path_set(Evas_Object *obj,
1147                           const char  *path)
1148 {
1149    ELM_CHECK_WIDTYPE(obj, widtype);
1150    _populate(obj, path, NULL);
1151 }
1152
1153 /**
1154  * This gets the path that the fileselector displays.
1155  *
1156  * @param obj The fileselector object
1157  * @return The path that the fileselector is displaying
1158  *
1159  * @ingroup Fileselector
1160  */
1161 EAPI const char *
1162 elm_fileselector_path_get(const Evas_Object *obj)
1163 {
1164    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1165    Widget_Data *wd = elm_widget_data_get(obj);
1166    if (!wd) return NULL;
1167    return wd->path;
1168 }
1169
1170 /**
1171  * This sets the mode in which the fileselector will display files.
1172  *
1173  * @param obj The fileselector object
1174
1175  * @param mode The mode of the fileselector, being it one of @c
1176  * ELM_FILESELECTOR_LIST (default) or @c ELM_FILESELECTOR_GRID. The
1177  * first one, naturally, will display the files in a list. By using
1178  * elm_fileselector_expandable_set(), the user will trigger a tree
1179  * view for that list. The latter will make the widget to display its
1180  * entries in a grid form.
1181  *
1182  * @see elm_fileselector_expandable_set().
1183  *
1184  * @ingroup Fileselector
1185  */
1186 EAPI void
1187 elm_fileselector_mode_set(Evas_Object          *obj,
1188                           Elm_Fileselector_Mode mode)
1189 {
1190    ELM_CHECK_WIDTYPE(obj, widtype);
1191
1192    Widget_Data *wd = elm_widget_data_get(obj);
1193    if (!wd) return;
1194
1195    if (mode == wd->mode) return;
1196
1197    if (mode == ELM_FILESELECTOR_LIST)
1198      {
1199         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1200                                      wd->files_list))
1201           {
1202              evas_object_show(wd->files_list);
1203              evas_object_hide(wd->files_grid);
1204           }
1205         else
1206           evas_object_hide(wd->files_list);
1207      }
1208    else
1209      {
1210         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1211                                      wd->files_grid))
1212           {
1213              evas_object_show(wd->files_grid);
1214              evas_object_hide(wd->files_list);
1215           }
1216         else
1217           evas_object_hide(wd->files_grid);
1218      }
1219
1220    wd->mode = mode;
1221
1222    _populate(obj, wd->path, NULL);
1223 }
1224
1225 /**
1226  * This gets the mode in which the fileselector is displaying files.
1227  *
1228  * @param obj The fileselector object
1229  * @return The mode in which the fileselector is at
1230  *
1231  * @ingroup Fileselector
1232  */
1233 EAPI Elm_Fileselector_Mode
1234 elm_fileselector_mode_get(const Evas_Object *obj)
1235 {
1236    ELM_CHECK_WIDTYPE(obj, widtype) ELM_FILESELECTOR_LAST;
1237
1238    Widget_Data *wd = elm_widget_data_get(obj);
1239    if (!wd) return ELM_FILESELECTOR_LAST;
1240
1241    return wd->mode;
1242 }
1243
1244 /**
1245  * This gets the currently selected path in the file selector.
1246  *
1247  * @param obj The file selector object
1248  * @return The absolute path of the selected object in the fileselector
1249  *
1250  * @ingroup Fileselector
1251  */
1252 EAPI const char *
1253 elm_fileselector_selected_get(const Evas_Object *obj)
1254 {
1255    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1256    Widget_Data *wd = elm_widget_data_get(obj);
1257    if (!wd) return NULL;
1258
1259    if (wd->filename_entry)
1260      {
1261         const char *name;
1262         char buf[PATH_MAX];
1263
1264         name = elm_entry_entry_get(wd->filename_entry);
1265         snprintf(buf, sizeof(buf), "%s/%s",
1266                  wd->only_folder ? ecore_file_dir_get(wd->path) : wd->path,
1267                  name);
1268         eina_stringshare_replace(&wd->selection, buf);
1269         return wd->selection;
1270      }
1271
1272    if (wd->mode == ELM_FILESELECTOR_LIST)
1273      {
1274         Elm_Genlist_Item *it;
1275         it = elm_genlist_selected_item_get(wd->files_list);
1276         if (it) return elm_genlist_item_data_get(it);
1277      }
1278    else
1279      {
1280         Elm_Gengrid_Item *it;
1281         it = elm_gengrid_selected_item_get(wd->files_grid);
1282         if (it) return elm_gengrid_item_data_get(it);
1283      }
1284
1285    return wd->path;
1286 }
1287
1288 /**
1289  * This sets the currently selected path in the file selector.
1290  *
1291  * @param obj The file selector object
1292  * @param path The path to a file or directory
1293  * @return @c EINA_TRUE on success, @c EINA_FALSE on failure. The
1294  * latter case occurs if the directory or file pointed to do not
1295  * exist.
1296  *
1297  * @ingroup Fileselector
1298  */
1299 EAPI Eina_Bool
1300 elm_fileselector_selected_set(Evas_Object *obj,
1301                               const char  *path)
1302 {
1303    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1304    Widget_Data *wd = elm_widget_data_get(obj);
1305    if (!wd) return EINA_FALSE;
1306
1307    if (ecore_file_is_dir(path))
1308      _populate(obj, path, NULL);
1309    else
1310      {
1311         if (!ecore_file_exists(path))
1312           return EINA_FALSE;
1313
1314         _populate(obj, ecore_file_dir_get(path), NULL);
1315         if (wd->filename_entry)
1316           {
1317              elm_entry_entry_set(wd->filename_entry,
1318                                           ecore_file_file_get(path));
1319              eina_stringshare_replace(&wd->selection, path);
1320           }
1321      }
1322
1323    return EINA_TRUE;
1324 }
1325