[SVN Migration r61148]
[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    if (wd->current)
146      eio_file_cancel(wd->current);
147 #endif
148
149    wd->files_list = NULL;
150    wd->files_grid = NULL;
151
152    EINA_REFCOUNT_UNREF(wd, _widget_data_free);
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 || wr->wd->current != handler)
653      {
654         eio_file_cancel(handler);
655         return ;
656      }
657
658    _signal_first(wr);
659
660    if (wr->wd->mode == ELM_FILESELECTOR_LIST)
661      elm_genlist_item_direct_sorted_insert(wr->wd->files_list, eio_file_associate_find(handler, "type/list"),
662                                            eina_stringshare_ref(eio_file_associate_find(handler, "filename")),
663                                            wr->parent, ELM_GENLIST_ITEM_NONE, _file_list_cmp, NULL, NULL);
664    else if (wr->wd->mode == ELM_FILESELECTOR_GRID)
665      elm_gengrid_item_direct_sorted_insert(wr->wd->files_grid, eio_file_associate_find(handler, "type/grid"),
666                                            eina_stringshare_ref(eio_file_associate_find(handler, "filename")),
667                                            _file_grid_cmp, NULL, NULL);
668 }
669
670 static void
671 _widget_request_cleanup(Widget_Request *wr)
672 {
673    EINA_REFCOUNT_UNREF(wr->wd, _widget_data_free);
674
675    eina_stringshare_del(wr->path);
676    free(wr);
677 }
678
679 static void
680 _done_cb(void *data, Eio_File *handler __UNUSED__)
681 {
682    Widget_Request *wr = data;
683
684    _signal_first(wr);
685
686 #ifdef HAVE_EIO
687    wr->wd->current = NULL;
688 #endif
689    _widget_request_cleanup(wr);
690 }
691
692 static void
693 _error_cb(void *data, Eio_File *handler, int error __UNUSED__)
694 {
695    Widget_Request *wr = data;
696
697 #ifdef HAVE_EIO
698    if (wr->wd->current == handler)
699      wr->wd->current = NULL;
700 #endif
701    _widget_request_cleanup(wr);
702 }
703
704 #endif
705
706 static void
707 _populate(Evas_Object      *obj,
708           const char       *path,
709           Elm_Genlist_Item *parent)
710 {
711    Widget_Data *wd = elm_widget_data_get(obj);
712 #ifdef HAVE_EIO
713    Widget_Request *wr;
714 #else
715    Eina_File_Direct_Info *file;
716    Eina_Iterator *it;
717    const char *real;
718    Eina_List *files = NULL, *dirs = NULL;
719 #endif
720
721    if (!wd) return;
722 #ifndef HAVE_EIO
723    if (!ecore_file_is_dir(path)) return ;
724    it = eina_file_stat_ls(path);
725    if (!it) return ;
726    evas_object_smart_callback_call(obj, SIG_DIRECTORY_OPEN, (void *)path);
727    if (!parent)
728      {
729         elm_genlist_clear(wd->files_list);
730         elm_gengrid_clear(wd->files_grid);
731         eina_stringshare_replace(&wd->path, path);
732         _do_anchors(obj, path);
733      }
734
735    if (wd->filename_entry) elm_entry_entry_set(wd->filename_entry, "");
736    EINA_ITERATOR_FOREACH(it, file)
737      {
738         const char *filename;
739
740         if (file->path[file->name_start] == '.')
741           continue ;
742
743         filename = eina_stringshare_add(file->path);
744         if (file->type == EINA_FILE_DIR)
745           dirs = eina_list_append(dirs, filename);
746         else if (!wd->only_folder)
747           files = eina_list_append(files, filename);
748      }
749    eina_iterator_free(it);
750
751    files = eina_list_sort(files, eina_list_count(files),
752                           EINA_COMPARE_CB(strcoll));
753    dirs = eina_list_sort(dirs, eina_list_count(dirs), EINA_COMPARE_CB(strcoll));
754    EINA_LIST_FREE(dirs, real)
755      {
756         if (wd->mode == ELM_FILESELECTOR_LIST)
757           elm_genlist_item_append(wd->files_list, &list_itc[ELM_DIRECTORY],
758                                   real, /* item data */
759                                   parent,
760                                   wd->expand ? ELM_GENLIST_ITEM_SUBITEMS :
761                                   ELM_GENLIST_ITEM_NONE,
762                                   NULL, NULL);
763         else if (wd->mode == ELM_FILESELECTOR_GRID)
764           elm_gengrid_item_append(wd->files_grid, &grid_itc[ELM_DIRECTORY],
765                                   real, /* item data */
766                                   NULL, NULL);
767      }
768
769    EINA_LIST_FREE(files, real)
770      {
771         Elm_Fileselector_Type type = evas_object_image_extension_can_load_fast_get(real) ?
772           ELM_FILE_IMAGE : ELM_FILE_UNKNOW;
773
774         if (wd->mode == ELM_FILESELECTOR_LIST)
775           elm_genlist_item_append(wd->files_list, &list_itc[type],
776                                   real, /* item data */
777                                   parent, ELM_GENLIST_ITEM_NONE,
778                                   NULL, NULL);
779         else if (wd->mode == ELM_FILESELECTOR_GRID)
780           elm_gengrid_item_append(wd->files_grid, &grid_itc[type],
781                                   real, /* item data */
782                                   NULL, NULL);
783      }
784 #else
785    if (wd->current)
786      eio_file_cancel(wd->current);
787    wr = malloc(sizeof (Widget_Request));
788    if (!wr) return ;
789    wr->wd = wd;
790    EINA_REFCOUNT_REF(wr->wd);
791    wr->parent = parent; /* FIXME: should we refcount the parent ? */
792    wr->obj = obj;
793    wr->path = eina_stringshare_add(path);
794    wr->first = EINA_TRUE;
795
796    wd->current = eio_file_stat_ls(path,
797                                   _filter_cb,
798                                   _main_cb,
799                                   _done_cb,
800                                   _error_cb,
801                                   wr);
802 #endif
803 }
804
805 /***  API  ***/
806
807 /**
808  * Add a new Fileselector object
809  *
810  * @param parent The parent object
811  * @return The new object or NULL if it cannot be created
812  *
813  * @ingroup Fileselector
814  */
815 EAPI Evas_Object *
816 elm_fileselector_add(Evas_Object *parent)
817 {
818    Evas *e;
819    Evas_Object *obj, *ic, *bt, *li, *en, *grid;
820    Widget_Data *wd;
821    unsigned int i;
822    int s;
823
824    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
825
826    EINA_REFCOUNT_INIT(wd);
827
828    ELM_SET_WIDTYPE(widtype, "fileselector");
829    elm_widget_type_set(obj, "fileselector");
830    elm_widget_sub_object_add(parent, obj);
831    elm_widget_data_set(obj, wd);
832    elm_widget_del_hook_set(obj, _del_hook);
833    elm_widget_theme_hook_set(obj, _theme_hook);
834    elm_widget_can_focus_set(obj, EINA_FALSE);
835
836    wd->expand = !!_elm_config->fileselector_expand_enable;
837
838    wd->edje = edje_object_add(e);
839    _elm_theme_object_set(obj, wd->edje, "fileselector", "base", "default");
840    elm_widget_resize_object_set(obj, wd->edje);
841
842    // up btn
843    ic = elm_icon_add(parent);
844    elm_icon_standard_set(ic, "arrow_up");
845    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
846    bt = elm_button_add(parent);
847    elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
848    elm_button_icon_set(bt, ic);
849    elm_object_text_set(bt, E_("Up"));
850    evas_object_size_hint_align_set(bt, 0.0, 0.0);
851
852    evas_object_smart_callback_add(bt, "clicked", _up, obj);
853
854    elm_widget_sub_object_add(obj, bt);
855    wd->up_button = bt;
856
857    // home btn
858    ic = elm_icon_add(parent);
859    elm_icon_standard_set(ic, "home");
860    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
861    bt = elm_button_add(parent);
862    elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
863    elm_button_icon_set(bt, ic);
864    elm_object_text_set(bt, E_("Home"));
865    evas_object_size_hint_align_set(bt, 0.0, 0.0);
866
867    evas_object_smart_callback_add(bt, "clicked", _home, obj);
868
869    elm_widget_sub_object_add(obj, bt);
870    wd->home_button = bt;
871
872    list_itc[ELM_DIRECTORY].func.icon_get = grid_itc[ELM_DIRECTORY].func.icon_get = _itc_icon_folder_get;
873    list_itc[ELM_FILE_IMAGE].func.icon_get = grid_itc[ELM_FILE_IMAGE].func.icon_get = _itc_icon_image_get;
874    list_itc[ELM_FILE_UNKNOW].func.icon_get = grid_itc[ELM_FILE_UNKNOW].func.icon_get = _itc_icon_file_get;
875
876    for (i = 0; i < ELM_FILE_LAST; ++i)
877      {
878         list_itc[i].func.label_get = grid_itc[i].func.label_get = _itc_label_get;
879         list_itc[i].func.state_get = grid_itc[i].func.state_get = _itc_state_get;
880         list_itc[i].func.del = grid_itc[i].func.del = _itc_del;
881      }
882
883    li = elm_genlist_add(parent);
884    elm_widget_mirrored_automatic_set(li, EINA_FALSE);
885    evas_object_size_hint_align_set(li, EVAS_HINT_FILL, EVAS_HINT_FILL);
886    evas_object_size_hint_weight_set(li, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
887    evas_object_size_hint_min_set(li, 100, 100);
888
889    grid = elm_gengrid_add(parent);
890    elm_widget_mirrored_automatic_set(grid, EINA_FALSE);
891    evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
892    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
893
894    s = elm_finger_size_get() * 2;
895    elm_gengrid_item_size_set(grid, s, s);
896    elm_gengrid_align_set(grid, 0.0, 0.0);
897
898    evas_object_smart_callback_add(li, "selected", _sel, obj);
899    evas_object_smart_callback_add(li, "expand,request", _expand_req, obj);
900    evas_object_smart_callback_add(li, "contract,request", _contract_req, obj);
901    evas_object_smart_callback_add(li, "expanded", _expand_done, obj);
902    evas_object_smart_callback_add(li, "contracted", _contract_done, obj);
903
904    evas_object_smart_callback_add(grid, "selected", _sel, obj);
905
906    elm_widget_sub_object_add(obj, li);
907    elm_widget_sub_object_add(obj, grid);
908    wd->files_list = li;
909    wd->files_grid = grid;
910
911    // path entry
912    en = elm_entry_add(parent);
913    elm_entry_scrollable_set(en, EINA_TRUE);
914    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
915    elm_entry_editable_set(en, EINA_FALSE);
916    elm_entry_single_line_set(en, EINA_TRUE);
917    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
918    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
919    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
920
921    evas_object_smart_callback_add(en, "anchor,clicked", _anchor_clicked, obj);
922
923    elm_widget_sub_object_add(obj, en);
924    wd->path_entry = en;
925
926    // filename entry
927    en = elm_entry_add(parent);
928    elm_entry_scrollable_set(en, EINA_TRUE);
929    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
930    elm_entry_editable_set(en, EINA_TRUE);
931    elm_entry_single_line_set(en, EINA_TRUE);
932    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
933    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
934    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
935
936    elm_widget_sub_object_add(obj, en);
937    wd->filename_entry = en;
938
939    elm_fileselector_buttons_ok_cancel_set(obj, EINA_TRUE);
940    elm_fileselector_is_save_set(obj, EINA_FALSE);
941
942    _theme_hook(obj);
943
944    evas_object_smart_callbacks_descriptions_set(obj, _signals);
945    return obj;
946 }
947
948 /**
949  * This enables/disables the file name entry box where the user can
950  * type in a name for the file to be saved as.
951  *
952  * @param obj The fileselector object
953  * @param is_save If true, the fileselector is a save dialog
954  *
955  * @ingroup Fileselector
956  */
957 EAPI void
958 elm_fileselector_is_save_set(Evas_Object *obj,
959                              Eina_Bool    is_save)
960 {
961    ELM_CHECK_WIDTYPE(obj, widtype);
962    Widget_Data *wd = elm_widget_data_get(obj);
963    if (!wd) return;
964
965    elm_object_disabled_set(wd->filename_entry, !is_save);
966
967    if (is_save)
968      edje_object_signal_emit(wd->edje, "elm,state,save,on", "elm");
969    else
970      edje_object_signal_emit(wd->edje, "elm,state,save,off", "elm");
971 }
972
973 /**
974  * This returns whether the fileselector is a "save" type fileselector
975  *
976  * @param obj The fileselector object
977  * @return If true, the fileselector is a save type.
978  *
979  * @ingroup Fileselector
980  */
981 EAPI Eina_Bool
982 elm_fileselector_is_save_get(const Evas_Object *obj)
983 {
984    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
985    Widget_Data *wd = elm_widget_data_get(obj);
986    if (!wd) return EINA_FALSE;
987    return elm_object_disabled_get(wd->filename_entry);
988 }
989
990 /**
991  * This enables/disables folder-only view in the fileselector.
992  *
993  * @param obj The fileselector object
994  * @param only If true, the fileselector will only display directories.
995  * If false, files are displayed also.
996  *
997  * @ingroup Fileselector
998  */
999 EAPI void
1000 elm_fileselector_folder_only_set(Evas_Object *obj,
1001                                  Eina_Bool    only)
1002 {
1003    ELM_CHECK_WIDTYPE(obj, widtype);
1004    Widget_Data *wd = elm_widget_data_get(obj);
1005    if (!wd) return;
1006    if (wd->only_folder == only) return;
1007    wd->only_folder = !!only;
1008    if (wd->path) _populate(obj, wd->path, NULL);
1009 }
1010
1011 /**
1012  * This gets the state of file display in the fileselector.
1013  *
1014  * @param obj The fileselector object
1015  * @return If true, files are not being shown in the fileselector.
1016  * If false, files are being shown.
1017  *
1018  * @ingroup Fileselector
1019  */
1020 EAPI Eina_Bool
1021 elm_fileselector_folder_only_get(const Evas_Object *obj)
1022 {
1023    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1024    Widget_Data *wd = elm_widget_data_get(obj);
1025    if (!wd) return EINA_FALSE;
1026    return wd->only_folder;
1027 }
1028
1029 /**
1030  * This enables/disables the ok,cancel buttons.
1031  *
1032  * @param obj The fileselector object
1033  * @param only If true, a box containing ok and cancel buttons is created.
1034  * If false, the box and the buttons are destroyed.
1035  *
1036  * @ingroup Fileselector
1037  */
1038 EAPI void
1039 elm_fileselector_buttons_ok_cancel_set(Evas_Object *obj,
1040                                        Eina_Bool    visible)
1041 {
1042    ELM_CHECK_WIDTYPE(obj, widtype);
1043    Widget_Data *wd = elm_widget_data_get(obj);
1044    Evas_Object *bt;
1045    if (!wd) return;
1046
1047    if (visible)
1048      {
1049         // cancel btn
1050         bt = elm_button_add(obj);
1051         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1052         elm_object_text_set(bt, E_("Cancel"));
1053
1054         evas_object_smart_callback_add(bt, "clicked", _canc, obj);
1055
1056         elm_widget_sub_object_add(obj, bt);
1057         wd->cancel_button = bt;
1058
1059         // ok btn
1060         bt = elm_button_add(obj);
1061         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1062         elm_object_text_set(bt, E_("OK"));
1063
1064         evas_object_smart_callback_add(bt, "clicked", _ok, obj);
1065
1066         elm_widget_sub_object_add(obj, bt);
1067         wd->ok_button = bt;
1068
1069         _theme_hook(obj);
1070      }
1071    else
1072      {
1073         evas_object_del(wd->cancel_button);
1074         wd->cancel_button = NULL;
1075         evas_object_del(wd->ok_button);
1076         wd->ok_button = NULL;
1077      }
1078 }
1079
1080 /**
1081  * This gets the state of the box containing ok and cancel buttons.
1082  *
1083  * @param obj The fileselector object
1084  * @return If true, the box exists.
1085  * If false, the box does not exist.
1086  *
1087  * @ingroup Fileselector
1088  */
1089 EAPI Eina_Bool
1090 elm_fileselector_buttons_ok_cancel_get(const Evas_Object *obj)
1091 {
1092    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1093    Widget_Data *wd = elm_widget_data_get(obj);
1094    if (!wd) return EINA_FALSE;
1095    return wd->ok_button ? EINA_TRUE : EINA_FALSE;
1096 }
1097
1098 /**
1099  * This enables a tree view in the fileselector, <b>if in @c
1100  * ELM_FILESELECTOR_LIST mode</b>. If it's in other mode, the changes
1101  * made by this function will only be visible when one switches back
1102  * to list mode.
1103  *
1104  * @param obj The fileselector object
1105  * @param expand If true, tree view is enabled.
1106  * If false, tree view is disabled.
1107  *
1108  * In a tree view, arrows are created on the sides of directories,
1109  * allowing them to expand in place.
1110  *
1111  * @ingroup Fileselector
1112  */
1113 EAPI void
1114 elm_fileselector_expandable_set(Evas_Object *obj,
1115                                 Eina_Bool    expand)
1116 {
1117    ELM_CHECK_WIDTYPE(obj, widtype);
1118    Widget_Data *wd;
1119
1120    wd = elm_widget_data_get(obj);
1121    if (!wd) return;
1122
1123    wd->expand = !!expand;
1124
1125    if (wd->path) _populate(obj, wd->path, NULL);
1126 }
1127
1128 /**
1129  * This gets the state of tree view in the fileselector.
1130  *
1131  * @param obj The fileselector object
1132  * @return If true, tree view is enabled and folders will be expandable.
1133  * If false, tree view is disabled.
1134  *
1135  * @ingroup Fileselector
1136  */
1137 EAPI Eina_Bool
1138 elm_fileselector_expandable_get(const Evas_Object *obj)
1139 {
1140    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1141    Widget_Data *wd = elm_widget_data_get(obj);
1142    if (!wd) return EINA_FALSE;
1143    return wd->expand;
1144 }
1145
1146 /**
1147  * This sets the path that the fileselector will display.
1148  *
1149  * @param obj The fileselector object
1150  * @param path The path of the fileselector
1151  *
1152  * @ingroup Fileselector
1153  */
1154 EAPI void
1155 elm_fileselector_path_set(Evas_Object *obj,
1156                           const char  *path)
1157 {
1158    ELM_CHECK_WIDTYPE(obj, widtype);
1159    _populate(obj, path, NULL);
1160 }
1161
1162 /**
1163  * This gets the path that the fileselector displays.
1164  *
1165  * @param obj The fileselector object
1166  * @return The path that the fileselector is displaying
1167  *
1168  * @ingroup Fileselector
1169  */
1170 EAPI const char *
1171 elm_fileselector_path_get(const Evas_Object *obj)
1172 {
1173    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1174    Widget_Data *wd = elm_widget_data_get(obj);
1175    if (!wd) return NULL;
1176    return wd->path;
1177 }
1178
1179 /**
1180  * This sets the mode in which the fileselector will display files.
1181  *
1182  * @param obj The fileselector object
1183
1184  * @param mode The mode of the fileselector, being it one of @c
1185  * ELM_FILESELECTOR_LIST (default) or @c ELM_FILESELECTOR_GRID. The
1186  * first one, naturally, will display the files in a list. By using
1187  * elm_fileselector_expandable_set(), the user will trigger a tree
1188  * view for that list. The latter will make the widget to display its
1189  * entries in a grid form.
1190  *
1191  * @see elm_fileselector_expandable_set().
1192  *
1193  * @ingroup Fileselector
1194  */
1195 EAPI void
1196 elm_fileselector_mode_set(Evas_Object          *obj,
1197                           Elm_Fileselector_Mode mode)
1198 {
1199    ELM_CHECK_WIDTYPE(obj, widtype);
1200
1201    Widget_Data *wd = elm_widget_data_get(obj);
1202    if (!wd) return;
1203
1204    if (mode == wd->mode) return;
1205
1206    if (mode == ELM_FILESELECTOR_LIST)
1207      {
1208         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1209                                      wd->files_list))
1210           {
1211              evas_object_show(wd->files_list);
1212              evas_object_hide(wd->files_grid);
1213           }
1214         else
1215           evas_object_hide(wd->files_list);
1216      }
1217    else
1218      {
1219         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1220                                      wd->files_grid))
1221           {
1222              evas_object_show(wd->files_grid);
1223              evas_object_hide(wd->files_list);
1224           }
1225         else
1226           evas_object_hide(wd->files_grid);
1227      }
1228
1229    wd->mode = mode;
1230
1231    _populate(obj, wd->path, NULL);
1232 }
1233
1234 /**
1235  * This gets the mode in which the fileselector is displaying files.
1236  *
1237  * @param obj The fileselector object
1238  * @return The mode in which the fileselector is at
1239  *
1240  * @ingroup Fileselector
1241  */
1242 EAPI Elm_Fileselector_Mode
1243 elm_fileselector_mode_get(const Evas_Object *obj)
1244 {
1245    ELM_CHECK_WIDTYPE(obj, widtype) ELM_FILESELECTOR_LAST;
1246
1247    Widget_Data *wd = elm_widget_data_get(obj);
1248    if (!wd) return ELM_FILESELECTOR_LAST;
1249
1250    return wd->mode;
1251 }
1252
1253 /**
1254  * This gets the currently selected path in the file selector.
1255  *
1256  * @param obj The file selector object
1257  * @return The absolute path of the selected object in the fileselector
1258  *
1259  * @ingroup Fileselector
1260  */
1261 EAPI const char *
1262 elm_fileselector_selected_get(const Evas_Object *obj)
1263 {
1264    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1265    Widget_Data *wd = elm_widget_data_get(obj);
1266    if (!wd) return NULL;
1267
1268    if (wd->filename_entry)
1269      {
1270         const char *name;
1271         char buf[PATH_MAX];
1272
1273         name = elm_entry_entry_get(wd->filename_entry);
1274         snprintf(buf, sizeof(buf), "%s/%s",
1275                  wd->only_folder ? ecore_file_dir_get(wd->path) : wd->path,
1276                  name);
1277         eina_stringshare_replace(&wd->selection, buf);
1278         return wd->selection;
1279      }
1280
1281    if (wd->mode == ELM_FILESELECTOR_LIST)
1282      {
1283         Elm_Genlist_Item *it;
1284         it = elm_genlist_selected_item_get(wd->files_list);
1285         if (it) return elm_genlist_item_data_get(it);
1286      }
1287    else
1288      {
1289         Elm_Gengrid_Item *it;
1290         it = elm_gengrid_selected_item_get(wd->files_grid);
1291         if (it) return elm_gengrid_item_data_get(it);
1292      }
1293
1294    return wd->path;
1295 }
1296
1297 /**
1298  * This sets the currently selected path in the file selector.
1299  *
1300  * @param obj The file selector object
1301  * @param path The path to a file or directory
1302  * @return @c EINA_TRUE on success, @c EINA_FALSE on failure. The
1303  * latter case occurs if the directory or file pointed to do not
1304  * exist.
1305  *
1306  * @ingroup Fileselector
1307  */
1308 EAPI Eina_Bool
1309 elm_fileselector_selected_set(Evas_Object *obj,
1310                               const char  *path)
1311 {
1312    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1313    Widget_Data *wd = elm_widget_data_get(obj);
1314    if (!wd) return EINA_FALSE;
1315
1316    if (ecore_file_is_dir(path))
1317      _populate(obj, path, NULL);
1318    else
1319      {
1320         if (!ecore_file_exists(path))
1321           return EINA_FALSE;
1322
1323         _populate(obj, ecore_file_dir_get(path), NULL);
1324         if (wd->filename_entry)
1325           {
1326              elm_entry_entry_set(wd->filename_entry,
1327                                           ecore_file_file_get(path));
1328              eina_stringshare_replace(&wd->selection, path);
1329           }
1330      }
1331
1332    return EINA_TRUE;
1333 }
1334