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