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