elementary/gengrid - deprecated 3 APIs.
[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   { "default", { NULL, NULL, NULL, NULL } },
83   { "default", { NULL, NULL, NULL, NULL } },
84   { "default", { NULL, NULL, NULL, NULL } }
85 };
86 static Elm_Gengrid_Item_Class grid_itc[ELM_FILE_LAST] = {
87   { "default", { NULL, NULL, NULL, NULL } },
88   { "default", { NULL, NULL, NULL, NULL } },
89   { "default", { NULL, NULL, NULL, NULL } }
90 };
91
92 static const char *widtype = NULL;
93
94 static const char SIG_DIRECTORY_OPEN[] = "directory,open";
95 static const char SIG_DONE[] = "done";
96 static const char SIG_SELECTED[] = "selected";
97 static const Evas_Smart_Cb_Description _signals[] = {
98    {SIG_DIRECTORY_OPEN, "s"},
99    {SIG_DONE, "s"},
100    {SIG_SELECTED, "s"},
101    {NULL, NULL}
102 };
103
104 static void _populate(Evas_Object      *obj,
105                       const char       *path,
106                       Elm_Object_Item  *parent);
107 static void _do_anchors(Evas_Object *obj,
108                         const char  *path);
109
110 /***  ELEMENTARY WIDGET  ***/
111 static void
112 _widget_data_free(Widget_Data *wd)
113 {
114    if (wd->path) eina_stringshare_del(wd->path);
115    if (wd->selection) eina_stringshare_del(wd->selection);
116    if (wd->sel_idler)
117      {
118         void *sd;
119
120         sd = ecore_idler_del(wd->sel_idler);
121         free(sd);
122      }
123    free(wd);
124 }
125
126 static void
127 _del_hook(Evas_Object *obj)
128 {
129    Widget_Data *wd;
130
131    wd = elm_widget_data_get(obj);
132    if (!wd) return;
133
134 #ifdef HAVE_EIO
135    if (wd->current)
136      eio_file_cancel(wd->current);
137 #endif
138
139    wd->files_list = NULL;
140    wd->files_grid = NULL;
141
142    EINA_REFCOUNT_UNREF(wd)
143      _widget_data_free(wd);
144 }
145
146 static void
147 _sizing_eval(Evas_Object *obj)
148 {
149    Widget_Data *wd = elm_widget_data_get(obj);
150    Evas_Coord minw = -1, minh = -1;
151    if (!wd) return;
152    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
153    edje_object_size_min_restricted_calc(wd->edje, &minw, &minh, minw, minh);
154    evas_object_size_hint_min_set(obj, minw, minh);
155 }
156
157 static void
158 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
159 {
160    Widget_Data *wd = elm_widget_data_get(obj);
161    if (!wd) return;
162    elm_widget_mirrored_set(wd->cancel_button, rtl);
163    elm_widget_mirrored_set(wd->ok_button, rtl);
164    elm_widget_mirrored_set(wd->files_list, rtl);
165    elm_widget_mirrored_set(wd->up_button, rtl);
166    elm_widget_mirrored_set(wd->home_button, rtl);
167    edje_object_mirrored_set(wd->edje, rtl);
168 }
169
170 static void
171 _theme_hook(Evas_Object *obj)
172 {
173    Widget_Data *wd = elm_widget_data_get(obj);
174    const char *style = elm_widget_style_get(obj);
175    const char *data;
176    char buf[1024];
177
178    if (!wd) return;
179    _elm_widget_mirrored_reload(obj);
180
181    _elm_theme_object_set(obj, wd->edje, "fileselector", "base", style);
182
183    if (elm_object_disabled_get(obj))
184      edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
185
186    data = edje_object_data_get(wd->edje, "path_separator");
187    if (data)
188      wd->path_separator = data;
189    else
190      wd->path_separator = "/";
191
192    if (!style) style = "default";
193    snprintf(buf, sizeof(buf), "fileselector/%s", style);
194
195 #define SWALLOW(part_name, object_ptn)                                \
196   if (object_ptn)                                                     \
197     {                                                                 \
198        elm_widget_style_set(object_ptn, buf);                         \
199        if (edje_object_part_swallow(wd->edje, part_name, object_ptn)) \
200          evas_object_show(object_ptn);                                \
201        else                                                           \
202          evas_object_hide(object_ptn);                                \
203     }
204    SWALLOW("elm.swallow.up", wd->up_button);
205    SWALLOW("elm.swallow.home", wd->home_button);
206
207    if (wd->mode == ELM_FILESELECTOR_LIST)
208      {
209         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
210                                      wd->files_list))
211           {
212              evas_object_show(wd->files_list);
213              evas_object_hide(wd->files_grid);
214           }
215         else
216           evas_object_hide(wd->files_list);
217      }
218    else
219      {
220         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
221                                      wd->files_grid))
222           {
223              evas_object_show(wd->files_grid);
224              evas_object_hide(wd->files_list);
225           }
226         else
227           evas_object_hide(wd->files_grid);
228      }
229
230    SWALLOW("elm.swallow.filename", wd->filename_entry);
231    SWALLOW("elm.swallow.path", wd->path_entry);
232
233    snprintf(buf, sizeof(buf), "fileselector/actions/%s", style);
234    SWALLOW("elm.swallow.cancel", wd->cancel_button);
235    SWALLOW("elm.swallow.ok", wd->ok_button);
236 #undef SWALLOW
237
238    edje_object_message_signal_process(wd->edje);
239    _mirrored_set(obj, elm_widget_mirrored_get(obj));
240    edje_object_scale_set
241      (wd->edje, elm_widget_scale_get(obj) * _elm_config->scale);
242    _sizing_eval(obj);
243 }
244
245 /***  GENLIST "MODEL"  ***/
246 static char *
247 _itc_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    Elm_Object_Item *gg_it = event_info;
423
424    wd = elm_widget_data_get(data);
425    if (!wd) return;
426
427    sd = malloc(sizeof(*sd));
428    sd->fs = data;
429    sd->path = wd->mode == elm_object_item_data_get(gg_it);
430
431    if (!sd->path)
432      {
433         eina_stringshare_replace(&wd->path, "");
434         goto end;
435      }
436
437    dir = wd->only_folder ? strdup(sd->path) : ecore_file_dir_get(sd->path);
438    if (dir)
439      {
440         eina_stringshare_replace(&wd->path, dir);
441         free(dir);
442      }
443    else
444      {
445         eina_stringshare_replace(&wd->path, "");
446      }
447
448 end:
449    if (wd->sel_idler)
450      {
451         old_sd = ecore_idler_del(wd->sel_idler);
452         free(old_sd);
453      }
454    wd->sel_idler = ecore_idler_add(_sel_do, sd);
455 }
456
457 static void
458 _up(void            *data,
459     Evas_Object *obj __UNUSED__,
460     void *event_info __UNUSED__)
461 {
462    Evas_Object *fs = data;
463    char *parent;
464
465    Widget_Data *wd = elm_widget_data_get(fs);
466    if (!wd) return;
467    parent = ecore_file_dir_get(wd->path);
468    _populate(fs, parent, NULL);
469    free(parent);
470 }
471
472 static void
473 _home(void            *data,
474       Evas_Object *obj __UNUSED__,
475       void *event_info __UNUSED__)
476 {
477    Evas_Object *fs = data;
478    _populate(fs, getenv("HOME"), NULL);
479 }
480
481 static void
482 _ok(void            *data,
483     Evas_Object *obj __UNUSED__,
484     void *event_info __UNUSED__)
485 {
486    Evas_Object *fs = data;
487    evas_object_smart_callback_call(fs, SIG_DONE,
488                                    (void *)elm_fileselector_selected_get(fs));
489 }
490
491 static void
492 _canc(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, NULL);
498 }
499
500 static void
501 _anchor_clicked(void            *data,
502                 Evas_Object *obj __UNUSED__,
503                 void            *event_info)
504 {
505    Evas_Object *fs = data;
506    Widget_Data *wd = elm_widget_data_get(fs);
507    Elm_Entry_Anchor_Info *info = event_info;
508    const char *p;
509    if (!wd) return;
510    // keep a ref to path 'couse it will be destroyed by _populate
511    p = eina_stringshare_add(info->name);
512    _populate(fs, p, NULL);
513    evas_object_smart_callback_call(data, SIG_SELECTED, (void *)p);
514    eina_stringshare_del(p);
515 }
516
517 static void
518 _do_anchors(Evas_Object *obj,
519             const char  *path)
520 {
521    Widget_Data *wd = elm_widget_data_get(obj);
522    char **tok, buf[PATH_MAX * 3];
523    int i, j;
524    if (!wd) return;
525    buf[0] = '\0';
526    tok = eina_str_split(path, "/", 0);
527    eina_strlcat(buf, "<a href=/>root</a>", sizeof(buf));
528    for (i = 0; tok[i]; i++)
529      {
530         if ((!tok[i]) || (!tok[i][0])) continue;
531         eina_strlcat(buf, wd->path_separator, sizeof(buf));
532         eina_strlcat(buf, "<a href=", sizeof(buf));
533         for (j = 0; j <= i; j++)
534           {
535              if (strlen(tok[j]) < 1) continue;
536              eina_strlcat(buf, "/", sizeof(buf));
537              eina_strlcat(buf, tok[j], sizeof(buf));
538           }
539         eina_strlcat(buf, ">", sizeof(buf));
540         eina_strlcat(buf, tok[i], sizeof(buf));
541         eina_strlcat(buf, "</a>", sizeof(buf));
542      }
543    free(tok[0]);
544    free(tok);
545
546    elm_object_text_set(wd->path_entry, buf);
547 }
548
549 #ifdef HAVE_EIO
550 static Eina_Bool
551 _filter_cb(void *data __UNUSED__, Eio_File *handler, const Eina_File_Direct_Info *info)
552 {
553    const char *filename;
554
555    if (info->path[info->name_start] == '.')
556      return EINA_FALSE;
557
558    filename = eina_stringshare_add(info->path);
559    eio_file_associate_direct_add(handler, "filename", filename, EINA_FREE_CB(eina_stringshare_del));
560
561    if (info->type == EINA_FILE_DIR)
562      {
563         eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_DIRECTORY], NULL);
564         eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_DIRECTORY], NULL);
565      }
566    else
567      {
568         if (evas_object_image_extension_can_load_get(info->path + info->name_start))
569           {
570              eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_FILE_IMAGE], NULL);
571              eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_FILE_IMAGE], NULL);
572           }
573         else
574           {
575              eio_file_associate_direct_add(handler, "type/grid", &grid_itc[ELM_FILE_UNKNOW], NULL);
576              eio_file_associate_direct_add(handler, "type/list", &list_itc[ELM_FILE_UNKNOW], NULL);
577           }
578      }
579
580    return EINA_TRUE;
581 }
582
583 static int
584 _file_grid_cmp(const void *a, const void *b)
585 {
586    const Elm_Object_Item *ga = a;
587    const Elm_Object_Item *gb = b;
588    const Elm_Gengrid_Item_Class *ca = elm_gengrid_item_item_class_get(ga);
589    const Elm_Gengrid_Item_Class *cb = elm_gengrid_item_item_class_get(gb);
590
591    if (ca == &grid_itc[ELM_DIRECTORY])
592      {
593         if (cb != &grid_itc[ELM_DIRECTORY])
594           return -1;
595      }
596    else if (cb == &grid_itc[ELM_DIRECTORY])
597      {
598         return 1;
599      }
600
601    return strcoll(elm_object_item_data_get(ga), elm_object_item_data_get(gb));
602 }
603
604 static int
605 _file_list_cmp(const void *a, const void *b)
606 {
607    const Elm_Object_Item *la = a;
608    const Elm_Object_Item *lb = b;
609    const Elm_Genlist_Item_Class *ca = elm_genlist_item_item_class_get(la);
610    const Elm_Genlist_Item_Class *cb = elm_genlist_item_item_class_get(lb);
611
612    if (ca == &list_itc[ELM_DIRECTORY])
613      {
614         if (cb != &list_itc[ELM_DIRECTORY])
615           return -1;
616      }
617    else if (cb == &list_itc[ELM_DIRECTORY])
618      {
619         return 1;
620      }
621
622    return strcoll(elm_object_item_data_get(la), elm_object_item_data_get(lb));
623 }
624
625 static void
626 _signal_first(Widget_Request *wr)
627 {
628    if (!wr->first) return ;
629    evas_object_smart_callback_call(wr->obj, SIG_DIRECTORY_OPEN, (void *)wr->path);
630    if (!wr->parent)
631      {
632         elm_genlist_clear(wr->wd->files_list);
633         elm_gengrid_clear(wr->wd->files_grid);
634         eina_stringshare_replace(&wr->wd->path, wr->path);
635         _do_anchors(wr->obj, wr->path);
636      }
637
638    if (wr->wd->filename_entry) elm_object_text_set(wr->wd->filename_entry, "");
639
640    wr->first = EINA_FALSE;
641 }
642
643 static void
644 _main_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info *info __UNUSED__)
645 {
646    Widget_Request *wr = data;
647
648    if (eio_file_check(handler))
649      return ;
650    if (!wr->wd->files_list || !wr->wd->files_grid || wr->wd->current != handler)
651      {
652         eio_file_cancel(handler);
653         return ;
654      }
655
656    _signal_first(wr);
657
658    if (wr->wd->mode == ELM_FILESELECTOR_LIST)
659      {
660         Eina_Bool is_dir = (eio_file_associate_find(handler, "type/list") == &list_itc[ELM_DIRECTORY]);
661
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, wr->wd->expand && is_dir ? ELM_GENLIST_ITEM_SUBITEMS : ELM_GENLIST_ITEM_NONE,
665                                               _file_list_cmp, NULL, NULL);
666      }
667    else if (wr->wd->mode == ELM_FILESELECTOR_GRID)
668      elm_gengrid_item_direct_sorted_insert(wr->wd->files_grid, eio_file_associate_find(handler, "type/grid"),
669                                            eina_stringshare_ref(eio_file_associate_find(handler, "filename")),
670                                            _file_grid_cmp, NULL, NULL);
671 }
672
673 static void
674 _widget_request_cleanup(Widget_Request *wr)
675 {
676    EINA_REFCOUNT_UNREF(wr->wd)
677      _widget_data_free(wr->wd);
678
679    eina_stringshare_del(wr->path);
680    free(wr);
681 }
682
683 static void
684 _done_cb(void *data, Eio_File *handler __UNUSED__)
685 {
686    Widget_Request *wr = data;
687
688    _signal_first(wr);
689
690    wr->wd->current = NULL;
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    if (wr->wd->current == handler)
700      wr->wd->current = NULL;
701    _widget_request_cleanup(wr);
702 }
703
704 #endif
705
706 static void
707 _populate(Evas_Object      *obj,
708           const char       *path,
709           Elm_Object_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_object_text_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->expand && wd->current) return ;
786    if (wd->current)
787      eio_file_cancel(wd->current);
788    wr = malloc(sizeof (Widget_Request));
789    if (!wr) return ;
790    wr->wd = wd;
791    EINA_REFCOUNT_REF(wr->wd);
792    wr->parent = parent; /* FIXME: should we refcount the parent ? */
793    wr->obj = obj;
794    wr->path = eina_stringshare_add(path);
795    wr->first = EINA_TRUE;
796
797    wd->current = eio_file_stat_ls(path,
798                                   _filter_cb,
799                                   _main_cb,
800                                   _done_cb,
801                                   _error_cb,
802                                   wr);
803 #endif
804 }
805
806 /***  API  ***/
807
808 EAPI Evas_Object *
809 elm_fileselector_add(Evas_Object *parent)
810 {
811    Evas *e;
812    Evas_Object *obj, *ic, *bt, *li, *en, *grid;
813    Widget_Data *wd;
814    unsigned int i;
815    int s;
816
817    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
818
819    EINA_REFCOUNT_INIT(wd);
820
821    ELM_SET_WIDTYPE(widtype, "fileselector");
822    elm_widget_type_set(obj, "fileselector");
823    elm_widget_sub_object_add(parent, obj);
824    elm_widget_data_set(obj, wd);
825    elm_widget_del_hook_set(obj, _del_hook);
826    elm_widget_theme_hook_set(obj, _theme_hook);
827    elm_widget_can_focus_set(obj, EINA_FALSE);
828
829    wd->expand = !!_elm_config->fileselector_expand_enable;
830
831    wd->edje = edje_object_add(e);
832    _elm_theme_object_set(obj, wd->edje, "fileselector", "base", "default");
833    elm_widget_resize_object_set(obj, wd->edje);
834
835    // up btn
836    ic = elm_icon_add(parent);
837    elm_icon_standard_set(ic, "arrow_up");
838    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
839    bt = elm_button_add(parent);
840    elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
841    elm_object_part_content_set(bt, "icon", ic);
842    elm_object_domain_translatable_text_set(bt, PACKAGE, N_("Up"));
843    evas_object_size_hint_align_set(bt, 0.0, 0.0);
844
845    evas_object_smart_callback_add(bt, "clicked", _up, obj);
846
847    elm_widget_sub_object_add(obj, bt);
848    wd->up_button = bt;
849
850    // home btn
851    ic = elm_icon_add(parent);
852    elm_icon_standard_set(ic, "home");
853    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
854    bt = elm_button_add(parent);
855    elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
856    elm_object_part_content_set(bt, "icon", ic);
857    elm_object_domain_translatable_text_set(bt, PACKAGE, N_("Home"));
858    evas_object_size_hint_align_set(bt, 0.0, 0.0);
859
860    evas_object_smart_callback_add(bt, "clicked", _home, obj);
861
862    elm_widget_sub_object_add(obj, bt);
863    wd->home_button = bt;
864
865    list_itc[ELM_DIRECTORY].func.content_get = grid_itc[ELM_DIRECTORY].func.content_get = _itc_icon_folder_get;
866    list_itc[ELM_FILE_IMAGE].func.content_get = grid_itc[ELM_FILE_IMAGE].func.content_get = _itc_icon_image_get;
867    list_itc[ELM_FILE_UNKNOW].func.content_get = grid_itc[ELM_FILE_UNKNOW].func.content_get = _itc_icon_file_get;
868
869    for (i = 0; i < ELM_FILE_LAST; ++i)
870      {
871         list_itc[i].func.text_get = grid_itc[i].func.text_get = _itc_text_get;
872         list_itc[i].func.state_get = grid_itc[i].func.state_get = _itc_state_get;
873         list_itc[i].func.del = grid_itc[i].func.del = _itc_del;
874      }
875
876    li = elm_genlist_add(parent);
877    elm_widget_mirrored_automatic_set(li, EINA_FALSE);
878    evas_object_size_hint_align_set(li, EVAS_HINT_FILL, EVAS_HINT_FILL);
879    evas_object_size_hint_weight_set(li, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
880    evas_object_size_hint_min_set(li, 100, 100);
881
882    grid = elm_gengrid_add(parent);
883    elm_widget_mirrored_automatic_set(grid, EINA_FALSE);
884    evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
885    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
886
887    s = elm_finger_size_get() * 2;
888    elm_gengrid_item_size_set(grid, s, s);
889    elm_gengrid_align_set(grid, 0.0, 0.0);
890
891    evas_object_smart_callback_add(li, "selected", _sel, obj);
892    evas_object_smart_callback_add(li, "expand,request", _expand_req, obj);
893    evas_object_smart_callback_add(li, "contract,request", _contract_req, obj);
894    evas_object_smart_callback_add(li, "expanded", _expand_done, obj);
895    evas_object_smart_callback_add(li, "contracted", _contract_done, obj);
896
897    evas_object_smart_callback_add(grid, "selected", _sel, obj);
898
899    elm_widget_sub_object_add(obj, li);
900    elm_widget_sub_object_add(obj, grid);
901    wd->files_list = li;
902    wd->files_grid = grid;
903
904    // path entry
905    en = elm_entry_add(parent);
906    elm_entry_scrollable_set(en, EINA_TRUE);
907    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
908    elm_entry_editable_set(en, EINA_FALSE);
909    elm_entry_single_line_set(en, EINA_TRUE);
910    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
911    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
912    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
913
914    evas_object_smart_callback_add(en, "anchor,clicked", _anchor_clicked, obj);
915
916    elm_widget_sub_object_add(obj, en);
917    wd->path_entry = en;
918
919    // filename entry
920    en = elm_entry_add(parent);
921    elm_entry_scrollable_set(en, EINA_TRUE);
922    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
923    elm_entry_editable_set(en, EINA_TRUE);
924    elm_entry_single_line_set(en, EINA_TRUE);
925    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
926    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
927    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
928
929    elm_widget_sub_object_add(obj, en);
930    wd->filename_entry = en;
931
932    elm_fileselector_buttons_ok_cancel_set(obj, EINA_TRUE);
933    elm_fileselector_is_save_set(obj, EINA_FALSE);
934
935    _theme_hook(obj);
936
937    evas_object_smart_callbacks_descriptions_set(obj, _signals);
938    return obj;
939 }
940
941 EAPI void
942 elm_fileselector_is_save_set(Evas_Object *obj,
943                              Eina_Bool    is_save)
944 {
945    ELM_CHECK_WIDTYPE(obj, widtype);
946    Widget_Data *wd = elm_widget_data_get(obj);
947    if (!wd) return;
948
949    elm_object_disabled_set(wd->filename_entry, !is_save);
950
951    if (is_save)
952      edje_object_signal_emit(wd->edje, "elm,state,save,on", "elm");
953    else
954      edje_object_signal_emit(wd->edje, "elm,state,save,off", "elm");
955 }
956
957 EAPI Eina_Bool
958 elm_fileselector_is_save_get(const Evas_Object *obj)
959 {
960    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
961    Widget_Data *wd = elm_widget_data_get(obj);
962    if (!wd) return EINA_FALSE;
963    return elm_object_disabled_get(wd->filename_entry);
964 }
965
966 EAPI void
967 elm_fileselector_folder_only_set(Evas_Object *obj,
968                                  Eina_Bool    only)
969 {
970    ELM_CHECK_WIDTYPE(obj, widtype);
971    Widget_Data *wd = elm_widget_data_get(obj);
972    if (!wd) return;
973    if (wd->only_folder == only) return;
974    wd->only_folder = !!only;
975    if (wd->path) _populate(obj, wd->path, NULL);
976 }
977
978 EAPI Eina_Bool
979 elm_fileselector_folder_only_get(const Evas_Object *obj)
980 {
981    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
982    Widget_Data *wd = elm_widget_data_get(obj);
983    if (!wd) return EINA_FALSE;
984    return wd->only_folder;
985 }
986
987 EAPI void
988 elm_fileselector_buttons_ok_cancel_set(Evas_Object *obj,
989                                        Eina_Bool    visible)
990 {
991    ELM_CHECK_WIDTYPE(obj, widtype);
992    Widget_Data *wd = elm_widget_data_get(obj);
993    Evas_Object *bt;
994    if (!wd) return;
995
996    if (visible)
997      {
998         // cancel btn
999         bt = elm_button_add(obj);
1000         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1001         elm_object_domain_translatable_text_set(bt, PACKAGE, N_("Cancel"));
1002
1003         evas_object_smart_callback_add(bt, "clicked", _canc, obj);
1004
1005         elm_widget_sub_object_add(obj, bt);
1006         wd->cancel_button = bt;
1007
1008         // ok btn
1009         bt = elm_button_add(obj);
1010         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1011         elm_object_domain_translatable_text_set(bt, PACKAGE, N_("OK"));
1012
1013         evas_object_smart_callback_add(bt, "clicked", _ok, obj);
1014
1015         elm_widget_sub_object_add(obj, bt);
1016         wd->ok_button = bt;
1017
1018         _theme_hook(obj);
1019      }
1020    else
1021      {
1022         evas_object_del(wd->cancel_button);
1023         wd->cancel_button = NULL;
1024         evas_object_del(wd->ok_button);
1025         wd->ok_button = NULL;
1026      }
1027 }
1028
1029 EAPI Eina_Bool
1030 elm_fileselector_buttons_ok_cancel_get(const Evas_Object *obj)
1031 {
1032    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1033    Widget_Data *wd = elm_widget_data_get(obj);
1034    if (!wd) return EINA_FALSE;
1035    return wd->ok_button ? EINA_TRUE : EINA_FALSE;
1036 }
1037
1038 EAPI void
1039 elm_fileselector_expandable_set(Evas_Object *obj,
1040                                 Eina_Bool    expand)
1041 {
1042    ELM_CHECK_WIDTYPE(obj, widtype);
1043    Widget_Data *wd;
1044
1045    wd = elm_widget_data_get(obj);
1046    if (!wd) return;
1047
1048    wd->expand = !!expand;
1049
1050    if (wd->path) _populate(obj, wd->path, NULL);
1051 }
1052
1053 EAPI Eina_Bool
1054 elm_fileselector_expandable_get(const Evas_Object *obj)
1055 {
1056    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1057    Widget_Data *wd = elm_widget_data_get(obj);
1058    if (!wd) return EINA_FALSE;
1059    return wd->expand;
1060 }
1061
1062 EAPI void
1063 elm_fileselector_path_set(Evas_Object *obj,
1064                           const char  *path)
1065 {
1066    ELM_CHECK_WIDTYPE(obj, widtype);
1067    _populate(obj, path, NULL);
1068 }
1069
1070 EAPI const char *
1071 elm_fileselector_path_get(const Evas_Object *obj)
1072 {
1073    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1074    Widget_Data *wd = elm_widget_data_get(obj);
1075    if (!wd) return NULL;
1076    return wd->path;
1077 }
1078
1079 EAPI void
1080 elm_fileselector_mode_set(Evas_Object          *obj,
1081                           Elm_Fileselector_Mode mode)
1082 {
1083    ELM_CHECK_WIDTYPE(obj, widtype);
1084
1085    Widget_Data *wd = elm_widget_data_get(obj);
1086    if (!wd) return;
1087
1088    if (mode == wd->mode) return;
1089
1090    if (mode == ELM_FILESELECTOR_LIST)
1091      {
1092         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1093                                      wd->files_list))
1094           {
1095              evas_object_show(wd->files_list);
1096              evas_object_hide(wd->files_grid);
1097           }
1098         else
1099           evas_object_hide(wd->files_list);
1100      }
1101    else
1102      {
1103         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1104                                      wd->files_grid))
1105           {
1106              evas_object_show(wd->files_grid);
1107              evas_object_hide(wd->files_list);
1108           }
1109         else
1110           evas_object_hide(wd->files_grid);
1111      }
1112
1113    wd->mode = mode;
1114
1115    _populate(obj, wd->path, NULL);
1116 }
1117
1118 EAPI Elm_Fileselector_Mode
1119 elm_fileselector_mode_get(const Evas_Object *obj)
1120 {
1121    ELM_CHECK_WIDTYPE(obj, widtype) ELM_FILESELECTOR_LAST;
1122
1123    Widget_Data *wd = elm_widget_data_get(obj);
1124    if (!wd) return ELM_FILESELECTOR_LAST;
1125
1126    return wd->mode;
1127 }
1128
1129 EAPI const char *
1130 elm_fileselector_selected_get(const Evas_Object *obj)
1131 {
1132    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1133    Widget_Data *wd = elm_widget_data_get(obj);
1134    if (!wd) return NULL;
1135
1136    if (wd->filename_entry)
1137      {
1138         const char *name;
1139         char buf[PATH_MAX];
1140         char *dir;
1141
1142         dir = wd->only_folder ? ecore_file_dir_get(wd->path) : strdup(wd->path);
1143         name = elm_object_text_get(wd->filename_entry);
1144         snprintf(buf, sizeof(buf), "%s/%s",
1145                  dir, name);
1146         if (wd->only_folder && !ecore_file_is_dir(buf))
1147           eina_stringshare_replace(&wd->selection, ecore_file_dir_get(buf));
1148         else
1149           eina_stringshare_replace(&wd->selection, buf);
1150         if (dir) free(dir);
1151         return wd->selection;
1152      }
1153
1154    if (wd->mode == ELM_FILESELECTOR_LIST)
1155      {
1156         Elm_Object_Item *gg_it = elm_genlist_selected_item_get(wd->files_list);
1157         if (gg_it) return elm_object_item_data_get(gg_it);
1158      }
1159    else
1160      {
1161         Elm_Object_Item *gg_it = elm_gengrid_selected_item_get(wd->files_grid);
1162         if (gg_it) return elm_object_item_data_get(gg_it);
1163      }
1164
1165    return wd->path;
1166 }
1167
1168 EAPI Eina_Bool
1169 elm_fileselector_selected_set(Evas_Object *obj,
1170                               const char  *path)
1171 {
1172    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1173    Widget_Data *wd = elm_widget_data_get(obj);
1174    if (!wd) return EINA_FALSE;
1175
1176    if (ecore_file_is_dir(path))
1177      _populate(obj, path, NULL);
1178    else
1179      {
1180         if (!ecore_file_exists(path))
1181           return EINA_FALSE;
1182
1183         _populate(obj, ecore_file_dir_get(path), NULL);
1184         if (wd->filename_entry)
1185           {
1186              elm_object_text_set(wd->filename_entry,
1187                                           ecore_file_file_get(path));
1188              eina_stringshare_replace(&wd->selection, path);
1189           }
1190      }
1191
1192    return EINA_TRUE;
1193 }
1194