[prevent issues ID:55300,38821,19282,55298 for test_fileselector.c and elc_fileselect...
[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    const char *path = getenv("HOME");
480    if (!path) path = "./";
481    _populate(fs, path, NULL);
482
483 }
484
485 static void
486 _ok(void            *data,
487     Evas_Object *obj __UNUSED__,
488     void *event_info __UNUSED__)
489 {
490    Evas_Object *fs = data;
491    evas_object_smart_callback_call(fs, SIG_DONE,
492                                    (void *)elm_fileselector_selected_get(fs));
493 }
494
495 static void
496 _canc(void            *data,
497       Evas_Object *obj __UNUSED__,
498       void *event_info __UNUSED__)
499 {
500    Evas_Object *fs = data;
501    evas_object_smart_callback_call(fs, SIG_DONE, NULL);
502 }
503
504 static void
505 _anchor_clicked(void            *data,
506                 Evas_Object *obj __UNUSED__,
507                 void            *event_info)
508 {
509    Evas_Object *fs = data;
510    Widget_Data *wd = elm_widget_data_get(fs);
511    Elm_Entry_Anchor_Info *info = event_info;
512    const char *p;
513    if (!wd) return;
514    // keep a ref to path 'couse it will be destroyed by _populate
515    p = eina_stringshare_add(info->name);
516    _populate(fs, p, NULL);
517    evas_object_smart_callback_call(data, SIG_SELECTED, (void *)p);
518    eina_stringshare_del(p);
519 }
520
521 static void
522 _do_anchors(Evas_Object *obj,
523             const char  *path)
524 {
525    Widget_Data *wd = elm_widget_data_get(obj);
526    char **tok, buf[PATH_MAX * 3];
527    int i, j;
528    if (!wd) return;
529    buf[0] = '\0';
530    tok = eina_str_split(path, "/", 0);
531    eina_strlcat(buf, "<a href=/>root</a>", sizeof(buf));
532    for (i = 0; tok[i]; i++)
533      {
534         if ((!tok[i]) || (!tok[i][0])) continue;
535         eina_strlcat(buf, wd->path_separator, sizeof(buf));
536         eina_strlcat(buf, "<a href=", sizeof(buf));
537         for (j = 0; j <= i; j++)
538           {
539              if (strlen(tok[j]) < 1) continue;
540              eina_strlcat(buf, "/", sizeof(buf));
541              eina_strlcat(buf, tok[j], sizeof(buf));
542           }
543         eina_strlcat(buf, ">", sizeof(buf));
544         eina_strlcat(buf, tok[i], sizeof(buf));
545         eina_strlcat(buf, "</a>", sizeof(buf));
546      }
547    free(tok[0]);
548    free(tok);
549
550    elm_entry_entry_set(wd->path_entry, buf);
551 }
552
553 #ifdef HAVE_EIO
554 static Eina_Bool
555 _filter_cb(void *data __UNUSED__, Eio_File *handler, const Eina_File_Direct_Info *info)
556 {
557    const char *filename;
558
559    if (info->path[info->name_start] == '.')
560      return EINA_FALSE;
561
562    filename = eina_stringshare_add(info->path);
563    eio_file_associate_direct_add(handler, "filename", filename, EINA_FREE_CB(eina_stringshare_del));
564
565    if (info->type == EINA_FILE_DIR)
566      {
567         eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_DIRECTORY], NULL);
568         eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_DIRECTORY], NULL);
569      }
570    else
571      {
572         if (evas_object_image_extension_can_load_get(info->path + info->name_start))
573           {
574              eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_FILE_IMAGE], NULL);
575              eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_FILE_IMAGE], NULL);
576           }
577         else
578           {
579              eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_FILE_UNKNOW], NULL);
580              eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_FILE_UNKNOW], NULL);
581           }
582      }
583
584    return EINA_TRUE;
585 }
586
587 static int
588 _file_grid_cmp(const void *a, const void *b)
589 {
590    const Elm_Gengrid_Item *ga = a;
591    const Elm_Gengrid_Item *gb = b;
592    const Elm_Gengrid_Item_Class *ca = elm_gengrid_item_item_class_get(ga);
593    const Elm_Gengrid_Item_Class *cb = elm_gengrid_item_item_class_get(gb);
594
595    if (ca == &grid_itc[ELM_DIRECTORY])
596      {
597         if (cb != &grid_itc[ELM_DIRECTORY])
598           return -1;
599      }
600
601    return strcoll(elm_gengrid_item_data_get(ga), elm_gengrid_item_data_get(gb));
602 }
603
604 static int
605 _file_list_cmp(const void *a, const void *b)
606 {
607    const Elm_Genlist_Item *la = a;
608    const Elm_Genlist_Item *lb = b;
609    const Elm_Genlist_Item_Class *ca = elm_genlist_item_item_class_get(la);
610    const Elm_Genlist_Item_Class *cb = elm_genlist_item_item_class_get(lb);
611
612    if (ca == &list_itc[ELM_DIRECTORY])
613      {
614         if (cb != &list_itc[ELM_DIRECTORY])
615           return -1;
616      }
617
618    return strcoll(elm_genlist_item_data_get(la), elm_genlist_item_data_get(lb));
619 }
620
621 static void
622 _signal_first(Widget_Request *wr)
623 {
624    if (!wr->first) return ;
625    evas_object_smart_callback_call(wr->obj, SIG_DIRECTORY_OPEN, (void *)wr->path);
626    if (!wr->parent)
627      {
628         elm_genlist_clear(wr->wd->files_list);
629         elm_gengrid_clear(wr->wd->files_grid);
630         eina_stringshare_replace(&wr->wd->path, wr->path);
631         _do_anchors(wr->obj, wr->path);
632      }
633
634    if (wr->wd->filename_entry) elm_entry_entry_set(wr->wd->filename_entry, "");
635
636    wr->first = EINA_FALSE;
637 }
638
639 static void
640 _main_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info *info __UNUSED__)
641 {
642    Widget_Request *wr = data;
643
644    if (eio_file_check(handler))
645      return ;
646    if (!wr->wd->files_list || !wr->wd->files_grid || wr->wd->current != handler)
647      {
648         eio_file_cancel(handler);
649         return ;
650      }
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 #ifdef HAVE_EIO
682    wr->wd->current = NULL;
683 #endif
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 #ifdef HAVE_EIO
693    if (wr->wd->current == handler)
694      wr->wd->current = NULL;
695 #endif
696    _widget_request_cleanup(wr);
697 }
698
699 #endif
700
701 static void
702 _populate(Evas_Object      *obj,
703           const char       *path,
704           Elm_Genlist_Item *parent)
705 {
706    Widget_Data *wd = elm_widget_data_get(obj);
707 #ifdef HAVE_EIO
708    Widget_Request *wr;
709 #else
710    Eina_File_Direct_Info *file;
711    Eina_Iterator *it;
712    const char *real;
713    Eina_List *files = NULL, *dirs = NULL;
714 #endif
715
716    if (!wd) return;
717 #ifndef HAVE_EIO
718    if (!ecore_file_is_dir(path)) return ;
719    it = eina_file_stat_ls(path);
720    if (!it) return ;
721    evas_object_smart_callback_call(obj, SIG_DIRECTORY_OPEN, (void *)path);
722    if (!parent)
723      {
724         elm_genlist_clear(wd->files_list);
725         elm_gengrid_clear(wd->files_grid);
726         eina_stringshare_replace(&wd->path, path);
727         _do_anchors(obj, path);
728      }
729
730    if (wd->filename_entry) elm_entry_entry_set(wd->filename_entry, "");
731    EINA_ITERATOR_FOREACH(it, file)
732      {
733         const char *filename;
734
735         if (file->path[file->name_start] == '.')
736           continue ;
737
738         filename = eina_stringshare_add(file->path);
739         if (file->type == EINA_FILE_DIR)
740           dirs = eina_list_append(dirs, filename);
741         else if (!wd->only_folder)
742           files = eina_list_append(files, filename);
743      }
744    eina_iterator_free(it);
745
746    files = eina_list_sort(files, eina_list_count(files),
747                           EINA_COMPARE_CB(strcoll));
748    dirs = eina_list_sort(dirs, eina_list_count(dirs), EINA_COMPARE_CB(strcoll));
749    EINA_LIST_FREE(dirs, real)
750      {
751         if (wd->mode == ELM_FILESELECTOR_LIST)
752           elm_genlist_item_append(wd->files_list, &list_itc[ELM_DIRECTORY],
753                                   real, /* item data */
754                                   parent,
755                                   wd->expand ? ELM_GENLIST_ITEM_SUBITEMS :
756                                   ELM_GENLIST_ITEM_NONE,
757                                   NULL, NULL);
758         else if (wd->mode == ELM_FILESELECTOR_GRID)
759           elm_gengrid_item_append(wd->files_grid, &grid_itc[ELM_DIRECTORY],
760                                   real, /* item data */
761                                   NULL, NULL);
762      }
763
764    EINA_LIST_FREE(files, real)
765      {
766         Elm_Fileselector_Type type = evas_object_image_extension_can_load_fast_get(real) ?
767           ELM_FILE_IMAGE : ELM_FILE_UNKNOW;
768
769         if (wd->mode == ELM_FILESELECTOR_LIST)
770           elm_genlist_item_append(wd->files_list, &list_itc[type],
771                                   real, /* item data */
772                                   parent, ELM_GENLIST_ITEM_NONE,
773                                   NULL, NULL);
774         else if (wd->mode == ELM_FILESELECTOR_GRID)
775           elm_gengrid_item_append(wd->files_grid, &grid_itc[type],
776                                   real, /* item data */
777                                   NULL, NULL);
778      }
779 #else
780    if (wd->current)
781      eio_file_cancel(wd->current);
782    wr = malloc(sizeof (Widget_Request));
783    if (!wr) return ;
784    wr->wd = wd;
785    EINA_REFCOUNT_REF(wr->wd);
786    wr->parent = parent; /* FIXME: should we refcount the parent ? */
787    wr->obj = obj;
788    wr->path = eina_stringshare_add(path);
789    wr->first = EINA_TRUE;
790
791    wd->current = eio_file_stat_ls(path,
792                                   _filter_cb,
793                                   _main_cb,
794                                   _done_cb,
795                                   _error_cb,
796                                   wr);
797 #endif
798 }
799
800 /***  API  ***/
801
802 EAPI Evas_Object *
803 elm_fileselector_add(Evas_Object *parent)
804 {
805    Evas *e;
806    Evas_Object *obj, *ic, *bt, *li, *en, *grid;
807    Widget_Data *wd;
808    unsigned int i;
809    int s;
810
811    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
812
813    EINA_REFCOUNT_INIT(wd);
814
815    ELM_SET_WIDTYPE(widtype, "fileselector");
816    elm_widget_type_set(obj, "fileselector");
817    elm_widget_sub_object_add(parent, obj);
818    elm_widget_data_set(obj, wd);
819    elm_widget_del_hook_set(obj, _del_hook);
820    elm_widget_theme_hook_set(obj, _theme_hook);
821    elm_widget_can_focus_set(obj, EINA_FALSE);
822
823    wd->expand = !!_elm_config->fileselector_expand_enable;
824
825    wd->edje = edje_object_add(e);
826    _elm_theme_object_set(obj, wd->edje, "fileselector", "base", "default");
827    elm_widget_resize_object_set(obj, wd->edje);
828
829    // up btn
830    ic = elm_icon_add(parent);
831    elm_icon_standard_set(ic, "arrow_up");
832    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
833    bt = elm_button_add(parent);
834    elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
835    elm_button_icon_set(bt, ic);
836    elm_object_text_set(bt, E_("Up"));
837    evas_object_size_hint_align_set(bt, 0.0, 0.0);
838
839    evas_object_smart_callback_add(bt, "clicked", _up, obj);
840
841    elm_widget_sub_object_add(obj, bt);
842    wd->up_button = bt;
843
844    // home btn
845    ic = elm_icon_add(parent);
846    elm_icon_standard_set(ic, "home");
847    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
848    bt = elm_button_add(parent);
849    elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
850    elm_button_icon_set(bt, ic);
851    elm_object_text_set(bt, E_("Home"));
852    evas_object_size_hint_align_set(bt, 0.0, 0.0);
853
854    evas_object_smart_callback_add(bt, "clicked", _home, obj);
855
856    elm_widget_sub_object_add(obj, bt);
857    wd->home_button = bt;
858
859    list_itc[ELM_DIRECTORY].func.icon_get = grid_itc[ELM_DIRECTORY].func.icon_get = _itc_icon_folder_get;
860    list_itc[ELM_FILE_IMAGE].func.icon_get = grid_itc[ELM_FILE_IMAGE].func.icon_get = _itc_icon_image_get;
861    list_itc[ELM_FILE_UNKNOW].func.icon_get = grid_itc[ELM_FILE_UNKNOW].func.icon_get = _itc_icon_file_get;
862
863    for (i = 0; i < ELM_FILE_LAST; ++i)
864      {
865         list_itc[i].func.label_get = grid_itc[i].func.label_get = _itc_label_get;
866         list_itc[i].func.state_get = grid_itc[i].func.state_get = _itc_state_get;
867         list_itc[i].func.del = grid_itc[i].func.del = _itc_del;
868      }
869
870    li = elm_genlist_add(parent);
871    elm_widget_mirrored_automatic_set(li, EINA_FALSE);
872    evas_object_size_hint_align_set(li, EVAS_HINT_FILL, EVAS_HINT_FILL);
873    evas_object_size_hint_weight_set(li, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
874    evas_object_size_hint_min_set(li, 100, 100);
875
876    grid = elm_gengrid_add(parent);
877    elm_widget_mirrored_automatic_set(grid, EINA_FALSE);
878    evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
879    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
880
881    s = elm_finger_size_get() * 2;
882    elm_gengrid_item_size_set(grid, s, s);
883    elm_gengrid_align_set(grid, 0.0, 0.0);
884
885    evas_object_smart_callback_add(li, "selected", _sel, obj);
886    evas_object_smart_callback_add(li, "expand,request", _expand_req, obj);
887    evas_object_smart_callback_add(li, "contract,request", _contract_req, obj);
888    evas_object_smart_callback_add(li, "expanded", _expand_done, obj);
889    evas_object_smart_callback_add(li, "contracted", _contract_done, obj);
890
891    evas_object_smart_callback_add(grid, "selected", _sel, obj);
892
893    elm_widget_sub_object_add(obj, li);
894    elm_widget_sub_object_add(obj, grid);
895    wd->files_list = li;
896    wd->files_grid = grid;
897
898    // path entry
899    en = elm_entry_add(parent);
900    elm_entry_scrollable_set(en, EINA_TRUE);
901    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
902    elm_entry_editable_set(en, EINA_FALSE);
903    elm_entry_single_line_set(en, EINA_TRUE);
904    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
905    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
906    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
907
908    evas_object_smart_callback_add(en, "anchor,clicked", _anchor_clicked, obj);
909
910    elm_widget_sub_object_add(obj, en);
911    wd->path_entry = en;
912
913    // filename entry
914    en = elm_entry_add(parent);
915    elm_entry_scrollable_set(en, EINA_TRUE);
916    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
917    elm_entry_editable_set(en, EINA_TRUE);
918    elm_entry_single_line_set(en, EINA_TRUE);
919    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
920    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
921    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
922
923    elm_widget_sub_object_add(obj, en);
924    wd->filename_entry = en;
925
926    elm_fileselector_buttons_ok_cancel_set(obj, EINA_TRUE);
927    elm_fileselector_is_save_set(obj, EINA_FALSE);
928
929    _theme_hook(obj);
930
931    evas_object_smart_callbacks_descriptions_set(obj, _signals);
932    return obj;
933 }
934
935 EAPI void
936 elm_fileselector_is_save_set(Evas_Object *obj,
937                              Eina_Bool    is_save)
938 {
939    ELM_CHECK_WIDTYPE(obj, widtype);
940    Widget_Data *wd = elm_widget_data_get(obj);
941    if (!wd) return;
942
943    elm_object_disabled_set(wd->filename_entry, !is_save);
944
945    if (is_save)
946      edje_object_signal_emit(wd->edje, "elm,state,save,on", "elm");
947    else
948      edje_object_signal_emit(wd->edje, "elm,state,save,off", "elm");
949 }
950
951 EAPI Eina_Bool
952 elm_fileselector_is_save_get(const Evas_Object *obj)
953 {
954    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
955    Widget_Data *wd = elm_widget_data_get(obj);
956    if (!wd) return EINA_FALSE;
957    return elm_object_disabled_get(wd->filename_entry);
958 }
959
960 EAPI void
961 elm_fileselector_folder_only_set(Evas_Object *obj,
962                                  Eina_Bool    only)
963 {
964    ELM_CHECK_WIDTYPE(obj, widtype);
965    Widget_Data *wd = elm_widget_data_get(obj);
966    if (!wd) return;
967    if (wd->only_folder == only) return;
968    wd->only_folder = !!only;
969    if (wd->path) _populate(obj, wd->path, NULL);
970 }
971
972 EAPI Eina_Bool
973 elm_fileselector_folder_only_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 wd->only_folder;
979 }
980
981 EAPI void
982 elm_fileselector_buttons_ok_cancel_set(Evas_Object *obj,
983                                        Eina_Bool    visible)
984 {
985    ELM_CHECK_WIDTYPE(obj, widtype);
986    Widget_Data *wd = elm_widget_data_get(obj);
987    Evas_Object *bt;
988    if (!wd) return;
989
990    if (visible)
991      {
992         // cancel btn
993         bt = elm_button_add(obj);
994         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
995         elm_object_text_set(bt, E_("Cancel"));
996
997         evas_object_smart_callback_add(bt, "clicked", _canc, obj);
998
999         elm_widget_sub_object_add(obj, bt);
1000         wd->cancel_button = bt;
1001
1002         // ok btn
1003         bt = elm_button_add(obj);
1004         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1005         elm_object_text_set(bt, E_("OK"));
1006
1007         evas_object_smart_callback_add(bt, "clicked", _ok, obj);
1008
1009         elm_widget_sub_object_add(obj, bt);
1010         wd->ok_button = bt;
1011
1012         _theme_hook(obj);
1013      }
1014    else
1015      {
1016         evas_object_del(wd->cancel_button);
1017         wd->cancel_button = NULL;
1018         evas_object_del(wd->ok_button);
1019         wd->ok_button = NULL;
1020      }
1021 }
1022
1023 EAPI Eina_Bool
1024 elm_fileselector_buttons_ok_cancel_get(const Evas_Object *obj)
1025 {
1026    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1027    Widget_Data *wd = elm_widget_data_get(obj);
1028    if (!wd) return EINA_FALSE;
1029    return wd->ok_button ? EINA_TRUE : EINA_FALSE;
1030 }
1031
1032 EAPI void
1033 elm_fileselector_expandable_set(Evas_Object *obj,
1034                                 Eina_Bool    expand)
1035 {
1036    ELM_CHECK_WIDTYPE(obj, widtype);
1037    Widget_Data *wd;
1038
1039    wd = elm_widget_data_get(obj);
1040    if (!wd) return;
1041
1042    wd->expand = !!expand;
1043
1044    if (wd->path) _populate(obj, wd->path, NULL);
1045 }
1046
1047 EAPI Eina_Bool
1048 elm_fileselector_expandable_get(const Evas_Object *obj)
1049 {
1050    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1051    Widget_Data *wd = elm_widget_data_get(obj);
1052    if (!wd) return EINA_FALSE;
1053    return wd->expand;
1054 }
1055
1056 EAPI void
1057 elm_fileselector_path_set(Evas_Object *obj,
1058                           const char  *path)
1059 {
1060    ELM_CHECK_WIDTYPE(obj, widtype);
1061    _populate(obj, path, NULL);
1062 }
1063
1064 EAPI const char *
1065 elm_fileselector_path_get(const Evas_Object *obj)
1066 {
1067    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1068    Widget_Data *wd = elm_widget_data_get(obj);
1069    if (!wd) return NULL;
1070    return wd->path;
1071 }
1072
1073 EAPI void
1074 elm_fileselector_mode_set(Evas_Object          *obj,
1075                           Elm_Fileselector_Mode mode)
1076 {
1077    ELM_CHECK_WIDTYPE(obj, widtype);
1078
1079    Widget_Data *wd = elm_widget_data_get(obj);
1080    if (!wd) return;
1081
1082    if (mode == wd->mode) return;
1083
1084    if (mode == ELM_FILESELECTOR_LIST)
1085      {
1086         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1087                                      wd->files_list))
1088           {
1089              evas_object_show(wd->files_list);
1090              evas_object_hide(wd->files_grid);
1091           }
1092         else
1093           evas_object_hide(wd->files_list);
1094      }
1095    else
1096      {
1097         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1098                                      wd->files_grid))
1099           {
1100              evas_object_show(wd->files_grid);
1101              evas_object_hide(wd->files_list);
1102           }
1103         else
1104           evas_object_hide(wd->files_grid);
1105      }
1106
1107    wd->mode = mode;
1108
1109    _populate(obj, wd->path, NULL);
1110 }
1111
1112 EAPI Elm_Fileselector_Mode
1113 elm_fileselector_mode_get(const Evas_Object *obj)
1114 {
1115    ELM_CHECK_WIDTYPE(obj, widtype) ELM_FILESELECTOR_LAST;
1116
1117    Widget_Data *wd = elm_widget_data_get(obj);
1118    if (!wd) return ELM_FILESELECTOR_LAST;
1119
1120    return wd->mode;
1121 }
1122
1123 EAPI const char *
1124 elm_fileselector_selected_get(const Evas_Object *obj)
1125 {
1126    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1127    Widget_Data *wd = elm_widget_data_get(obj);
1128    if (!wd) return NULL;
1129
1130    if (wd->filename_entry)
1131      {
1132         const char *name;
1133         char buf[PATH_MAX];
1134         char *dir;
1135
1136         dir = wd->only_folder ? ecore_file_dir_get(wd->path) : strdup(wd->path);
1137         name = elm_entry_entry_get(wd->filename_entry);
1138         snprintf(buf, sizeof(buf), "%s/%s",
1139                  dir, name);
1140         eina_stringshare_replace(&wd->selection, buf);
1141         if (dir) free(dir);
1142         return wd->selection;
1143      }
1144
1145    if (wd->mode == ELM_FILESELECTOR_LIST)
1146      {
1147         Elm_Genlist_Item *it;
1148         it = elm_genlist_selected_item_get(wd->files_list);
1149         if (it) return elm_genlist_item_data_get(it);
1150      }
1151    else
1152      {
1153         Elm_Gengrid_Item *it;
1154         it = elm_gengrid_selected_item_get(wd->files_grid);
1155         if (it) return elm_gengrid_item_data_get(it);
1156      }
1157
1158    return wd->path;
1159 }
1160
1161 EAPI Eina_Bool
1162 elm_fileselector_selected_set(Evas_Object *obj,
1163                               const char  *path)
1164 {
1165    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1166    Widget_Data *wd = elm_widget_data_get(obj);
1167    if (!wd) return EINA_FALSE;
1168
1169    if (ecore_file_is_dir(path))
1170      _populate(obj, path, NULL);
1171    else
1172      {
1173         if (!ecore_file_exists(path))
1174           return EINA_FALSE;
1175
1176         _populate(obj, ecore_file_dir_get(path), NULL);
1177         if (wd->filename_entry)
1178           {
1179              elm_entry_entry_set(wd->filename_entry,
1180                                           ecore_file_file_get(path));
1181              eina_stringshare_replace(&wd->selection, path);
1182           }
1183      }
1184
1185    return EINA_TRUE;
1186 }
1187