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