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