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