elementary/genlist - killed elm_genlist_item_sorted_insert.
[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
84 static const char *widtype = NULL;
85
86 static const char SIG_DIRECTORY_OPEN[] = "directory,open";
87 static const char SIG_DONE[] = "done";
88 static const char SIG_SELECTED[] = "selected";
89 static const Evas_Smart_Cb_Description _signals[] = {
90    {SIG_DIRECTORY_OPEN, "s"},
91    {SIG_DONE, "s"},
92    {SIG_SELECTED, "s"},
93    {NULL, NULL}
94 };
95
96 static void _populate(Evas_Object      *obj,
97                       const char       *path,
98                       Elm_Object_Item  *parent);
99 static void _do_anchors(Evas_Object *obj,
100                         const char  *path);
101
102 /***  ELEMENTARY WIDGET  ***/
103 static void
104 _widget_data_free(Widget_Data *wd)
105 {
106    if (wd->path) eina_stringshare_del(wd->path);
107    if (wd->selection) eina_stringshare_del(wd->selection);
108    if (wd->sel_idler)
109      {
110         void *sd;
111
112         sd = ecore_idler_del(wd->sel_idler);
113         free(sd);
114      }
115    free(wd);
116 }
117
118 static void
119 _del_hook(Evas_Object *obj)
120 {
121    Widget_Data *wd;
122    int i;
123
124    wd = elm_widget_data_get(obj);
125    if (!wd) return;
126
127    for (i = 0; i < ELM_FILE_LAST; ++i)
128      {
129         elm_genlist_item_class_free(list_itc[i]);
130         elm_gengrid_item_class_free(grid_itc[i]);
131      }
132
133 #ifdef HAVE_EIO
134    if (wd->current)
135      eio_file_cancel(wd->current);
136 #endif
137
138    wd->files_list = NULL;
139    wd->files_grid = NULL;
140
141    EINA_REFCOUNT_UNREF(wd)
142      _widget_data_free(wd);
143 }
144
145 static void
146 _sizing_eval(Evas_Object *obj)
147 {
148    Widget_Data *wd = elm_widget_data_get(obj);
149    Evas_Coord minw = -1, minh = -1;
150    if (!wd) return;
151    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
152    edje_object_size_min_restricted_calc(wd->edje, &minw, &minh, minw, minh);
153    evas_object_size_hint_min_set(obj, minw, minh);
154 }
155
156 static void
157 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
158 {
159    Widget_Data *wd = elm_widget_data_get(obj);
160    if (!wd) return;
161    elm_widget_mirrored_set(wd->cancel_button, rtl);
162    elm_widget_mirrored_set(wd->ok_button, rtl);
163    elm_widget_mirrored_set(wd->files_list, rtl);
164    elm_widget_mirrored_set(wd->up_button, rtl);
165    elm_widget_mirrored_set(wd->home_button, rtl);
166    edje_object_mirrored_set(wd->edje, rtl);
167 }
168
169 static void
170 _theme_hook(Evas_Object *obj)
171 {
172    Widget_Data *wd = elm_widget_data_get(obj);
173    const char *style = elm_widget_style_get(obj);
174    const char *data;
175    char buf[1024];
176
177    if (!wd) return;
178    _elm_widget_mirrored_reload(obj);
179
180    _elm_theme_object_set(obj, wd->edje, "fileselector", "base", style);
181
182    if (elm_object_disabled_get(obj))
183      edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
184
185    data = edje_object_data_get(wd->edje, "path_separator");
186    if (data)
187      wd->path_separator = data;
188    else
189      wd->path_separator = "/";
190
191    if (!style) style = "default";
192    snprintf(buf, sizeof(buf), "fileselector/%s", style);
193
194 #define SWALLOW(part_name, object_ptn)                                \
195   if (object_ptn)                                                     \
196     {                                                                 \
197        elm_widget_style_set(object_ptn, buf);                         \
198        if (edje_object_part_swallow(wd->edje, part_name, object_ptn)) \
199          evas_object_show(object_ptn);                                \
200        else                                                           \
201          evas_object_hide(object_ptn);                                \
202     }
203    SWALLOW("elm.swallow.up", wd->up_button);
204    SWALLOW("elm.swallow.home", wd->home_button);
205
206    if (wd->mode == ELM_FILESELECTOR_LIST)
207      {
208         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
209                                      wd->files_list))
210           {
211              evas_object_show(wd->files_list);
212              evas_object_hide(wd->files_grid);
213           }
214         else
215           evas_object_hide(wd->files_list);
216      }
217    else
218      {
219         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
220                                      wd->files_grid))
221           {
222              evas_object_show(wd->files_grid);
223              evas_object_hide(wd->files_list);
224           }
225         else
226           evas_object_hide(wd->files_grid);
227      }
228
229    SWALLOW("elm.swallow.filename", wd->filename_entry);
230    SWALLOW("elm.swallow.path", wd->path_entry);
231
232    snprintf(buf, sizeof(buf), "fileselector/actions/%s", style);
233    SWALLOW("elm.swallow.cancel", wd->cancel_button);
234    SWALLOW("elm.swallow.ok", wd->ok_button);
235 #undef SWALLOW
236
237    edje_object_message_signal_process(wd->edje);
238    _mirrored_set(obj, elm_widget_mirrored_get(obj));
239    edje_object_scale_set
240      (wd->edje, elm_widget_scale_get(obj) * _elm_config->scale);
241    _sizing_eval(obj);
242 }
243
244 /***  GENLIST "MODEL"  ***/
245 static char *
246 _itc_text_get(void              *data,
247                Evas_Object *obj   __UNUSED__,
248                const char *source __UNUSED__)
249 {
250    return strdup(ecore_file_file_get(data)); /* NOTE this will be
251                                               * free() by the
252                                               * caller */
253 }
254
255 static Evas_Object *
256 _itc_icon_folder_get(void        *data __UNUSED__,
257                      Evas_Object *obj,
258                      const char  *source)
259 {
260    Evas_Object *ic;
261
262    if (strcmp(source, "elm.swallow.icon")) return NULL;
263
264    ic = elm_icon_add(obj);
265    elm_icon_standard_set(ic, "folder");
266
267    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
268                                     1, 1);
269    return ic;
270 }
271
272 static Evas_Object *
273 _itc_icon_image_get(void        *data,
274                     Evas_Object *obj,
275                     const char  *source)
276 {
277    const char *filename = data;
278    Evas_Object *ic;
279
280    if (strcmp(source, "elm.swallow.icon")) return NULL;
281
282    ic = elm_icon_add(obj);
283    elm_icon_standard_set(ic, "image");
284    elm_icon_thumb_set(ic, filename, NULL);
285
286    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
287                                     1, 1);
288    return ic;
289 }
290
291 static Evas_Object *
292 _itc_icon_file_get(void        *data __UNUSED__,
293                    Evas_Object *obj,
294                    const char  *source)
295 {
296    Evas_Object *ic;
297
298    if (strcmp(source, "elm.swallow.icon")) return NULL;
299
300    ic = elm_icon_add(obj);
301    elm_icon_standard_set(ic, "file");
302
303    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
304                                     1, 1);
305    return ic;
306 }
307
308 static Eina_Bool
309 _itc_state_get(void *data         __UNUSED__,
310                Evas_Object *obj   __UNUSED__,
311                const char *source __UNUSED__)
312 {
313    return EINA_FALSE;
314 }
315
316 static void
317 _itc_del(void            *data,
318          Evas_Object *obj __UNUSED__)
319 {
320    eina_stringshare_del(data);
321 }
322
323 static void
324 _expand_done(void            *data,
325              Evas_Object *obj __UNUSED__,
326              void            *event_info)
327 {
328    Elm_Object_Item *it = event_info;
329    const char *path = elm_object_item_data_get(it);
330    _populate(data, path, it);
331 }
332
333 static void
334 _contract_done(void *data       __UNUSED__,
335                Evas_Object *obj __UNUSED__,
336                void            *event_info)
337 {
338    Elm_Object_Item *it = event_info;
339    elm_genlist_item_subitems_clear(it);
340 }
341
342 static void
343 _expand_req(void *data       __UNUSED__,
344             Evas_Object *obj __UNUSED__,
345             void            *event_info)
346 {
347    Elm_Object_Item *it = event_info;
348    elm_genlist_item_expanded_set(it, EINA_TRUE);
349 }
350
351 static void
352 _contract_req(void *data       __UNUSED__,
353               Evas_Object *obj __UNUSED__,
354               void            *event_info)
355 {
356    Elm_Object_Item *it = event_info;
357    elm_genlist_item_expanded_set(it, EINA_FALSE);
358 }
359
360 /***  PRIVATES  ***/
361 static Eina_Bool
362 _sel_do(void *data)
363 {
364    struct sel_data *sd;
365    const char *path;
366    Widget_Data *wd;
367    const char *p;
368
369    sd = data;
370    wd = elm_widget_data_get(sd->fs);
371    path = sd->path;
372
373    if ((!wd->only_folder) && ecore_file_is_dir(path))
374      {
375         if (wd->expand && wd->mode == ELM_FILESELECTOR_LIST)
376           {
377              _do_anchors(sd->fs, path);
378              elm_object_text_set(wd->filename_entry, "");
379           }
380         else
381           {
382              /* keep a ref to path 'couse it will be destroyed by _populate */
383              p = eina_stringshare_add(path);
384              _populate(sd->fs, p, NULL);
385              eina_stringshare_del(p);
386           }
387         goto end;
388      }
389    else /* navigating through folders only or file is not a dir. */
390      {
391         if (wd->expand && wd->mode == ELM_FILESELECTOR_LIST)
392           _do_anchors(sd->fs, path);
393         else if (wd->only_folder)
394           {
395              /* keep a ref to path 'couse it will be destroyed by _populate */
396              p = eina_stringshare_add(path);
397              _populate(sd->fs, p, NULL);
398              eina_stringshare_del(p);
399           }
400         elm_object_text_set(wd->filename_entry,
401                                      ecore_file_file_get(path));
402      }
403
404    evas_object_smart_callback_call(sd->fs, SIG_SELECTED, (void *)path);
405
406 end:
407    wd->sel_idler = NULL;
408    free(sd);
409    return ECORE_CALLBACK_CANCEL;
410 }
411
412 static void
413 _sel(void            *data,
414      Evas_Object *obj __UNUSED__,
415      void            *event_info)
416 {
417    struct sel_data *sd;
418    Widget_Data *wd;
419    void *old_sd;
420    char *dir;
421    //This event_info could be a list or gengrid item
422    Elm_Object_Item *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 = elm_object_item_data_get(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_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    for (i = 0; i < ELM_FILE_LAST; ++i)
866      {
867         list_itc[i] = elm_genlist_item_class_new();
868         grid_itc[i] = elm_gengrid_item_class_new();
869
870         list_itc[i]->item_style = "default";
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    list_itc[ELM_DIRECTORY]->func.content_get =
877      grid_itc[ELM_DIRECTORY]->func.content_get = _itc_icon_folder_get;
878    list_itc[ELM_FILE_IMAGE]->func.content_get =
879      grid_itc[ELM_FILE_IMAGE]->func.content_get = _itc_icon_image_get;
880    list_itc[ELM_FILE_UNKNOW]->func.content_get =
881      grid_itc[ELM_FILE_UNKNOW]->func.content_get = _itc_icon_file_get;
882
883    li = elm_genlist_add(parent);
884    elm_widget_mirrored_automatic_set(li, EINA_FALSE);
885    evas_object_size_hint_align_set(li, EVAS_HINT_FILL, EVAS_HINT_FILL);
886    evas_object_size_hint_weight_set(li, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
887    evas_object_size_hint_min_set(li, 100, 100);
888
889    grid = elm_gengrid_add(parent);
890    elm_widget_mirrored_automatic_set(grid, EINA_FALSE);
891    evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
892    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
893
894    s = elm_finger_size_get() * 2;
895    elm_gengrid_item_size_set(grid, s, s);
896    elm_gengrid_align_set(grid, 0.0, 0.0);
897
898    evas_object_smart_callback_add(li, "selected", _sel, obj);
899    evas_object_smart_callback_add(li, "expand,request", _expand_req, obj);
900    evas_object_smart_callback_add(li, "contract,request", _contract_req, obj);
901    evas_object_smart_callback_add(li, "expanded", _expand_done, obj);
902    evas_object_smart_callback_add(li, "contracted", _contract_done, obj);
903
904    evas_object_smart_callback_add(grid, "selected", _sel, obj);
905
906    elm_widget_sub_object_add(obj, li);
907    elm_widget_sub_object_add(obj, grid);
908    wd->files_list = li;
909    wd->files_grid = grid;
910
911    // path entry
912    en = elm_entry_add(parent);
913    elm_entry_scrollable_set(en, EINA_TRUE);
914    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
915    elm_entry_editable_set(en, EINA_FALSE);
916    elm_entry_single_line_set(en, EINA_TRUE);
917    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
918    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
919    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
920
921    evas_object_smart_callback_add(en, "anchor,clicked", _anchor_clicked, obj);
922
923    elm_widget_sub_object_add(obj, en);
924    wd->path_entry = en;
925
926    // filename entry
927    en = elm_entry_add(parent);
928    elm_entry_scrollable_set(en, EINA_TRUE);
929    elm_widget_mirrored_automatic_set(en, EINA_FALSE);
930    elm_entry_editable_set(en, EINA_TRUE);
931    elm_entry_single_line_set(en, EINA_TRUE);
932    elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
933    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
934    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
935
936    elm_widget_sub_object_add(obj, en);
937    wd->filename_entry = en;
938
939    elm_fileselector_buttons_ok_cancel_set(obj, EINA_TRUE);
940    elm_fileselector_is_save_set(obj, EINA_FALSE);
941
942    _theme_hook(obj);
943
944    evas_object_smart_callbacks_descriptions_set(obj, _signals);
945    return obj;
946 }
947
948 EAPI void
949 elm_fileselector_is_save_set(Evas_Object *obj,
950                              Eina_Bool    is_save)
951 {
952    ELM_CHECK_WIDTYPE(obj, widtype);
953    Widget_Data *wd = elm_widget_data_get(obj);
954    if (!wd) return;
955
956    elm_object_disabled_set(wd->filename_entry, !is_save);
957
958    if (is_save)
959      edje_object_signal_emit(wd->edje, "elm,state,save,on", "elm");
960    else
961      edje_object_signal_emit(wd->edje, "elm,state,save,off", "elm");
962 }
963
964 EAPI Eina_Bool
965 elm_fileselector_is_save_get(const Evas_Object *obj)
966 {
967    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
968    Widget_Data *wd = elm_widget_data_get(obj);
969    if (!wd) return EINA_FALSE;
970    return !elm_object_disabled_get(wd->filename_entry);
971 }
972
973 EAPI void
974 elm_fileselector_folder_only_set(Evas_Object *obj,
975                                  Eina_Bool    only)
976 {
977    ELM_CHECK_WIDTYPE(obj, widtype);
978    Widget_Data *wd = elm_widget_data_get(obj);
979    if (!wd) return;
980    if (wd->only_folder == only) return;
981    wd->only_folder = !!only;
982    if (wd->path) _populate(obj, wd->path, NULL);
983 }
984
985 EAPI Eina_Bool
986 elm_fileselector_folder_only_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 wd->only_folder;
992 }
993
994 EAPI void
995 elm_fileselector_buttons_ok_cancel_set(Evas_Object *obj,
996                                        Eina_Bool    visible)
997 {
998    ELM_CHECK_WIDTYPE(obj, widtype);
999    Widget_Data *wd = elm_widget_data_get(obj);
1000    Evas_Object *bt;
1001    if (!wd) return;
1002
1003    if (visible)
1004      {
1005         // cancel btn
1006         bt = elm_button_add(obj);
1007         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1008         elm_object_domain_translatable_text_set(bt, PACKAGE, N_("Cancel"));
1009
1010         evas_object_smart_callback_add(bt, "clicked", _canc, obj);
1011
1012         elm_widget_sub_object_add(obj, bt);
1013         wd->cancel_button = bt;
1014
1015         // ok btn
1016         bt = elm_button_add(obj);
1017         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
1018         elm_object_domain_translatable_text_set(bt, PACKAGE, N_("OK"));
1019
1020         evas_object_smart_callback_add(bt, "clicked", _ok, obj);
1021
1022         elm_widget_sub_object_add(obj, bt);
1023         wd->ok_button = bt;
1024
1025         _theme_hook(obj);
1026      }
1027    else
1028      {
1029         evas_object_del(wd->cancel_button);
1030         wd->cancel_button = NULL;
1031         evas_object_del(wd->ok_button);
1032         wd->ok_button = NULL;
1033      }
1034 }
1035
1036 EAPI Eina_Bool
1037 elm_fileselector_buttons_ok_cancel_get(const Evas_Object *obj)
1038 {
1039    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1040    Widget_Data *wd = elm_widget_data_get(obj);
1041    if (!wd) return EINA_FALSE;
1042    return wd->ok_button ? EINA_TRUE : EINA_FALSE;
1043 }
1044
1045 EAPI void
1046 elm_fileselector_expandable_set(Evas_Object *obj,
1047                                 Eina_Bool    expand)
1048 {
1049    ELM_CHECK_WIDTYPE(obj, widtype);
1050    Widget_Data *wd;
1051
1052    wd = elm_widget_data_get(obj);
1053    if (!wd) return;
1054
1055    wd->expand = !!expand;
1056
1057    if (wd->path) _populate(obj, wd->path, NULL);
1058 }
1059
1060 EAPI Eina_Bool
1061 elm_fileselector_expandable_get(const Evas_Object *obj)
1062 {
1063    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1064    Widget_Data *wd = elm_widget_data_get(obj);
1065    if (!wd) return EINA_FALSE;
1066    return wd->expand;
1067 }
1068
1069 EAPI void
1070 elm_fileselector_path_set(Evas_Object *obj,
1071                           const char  *_path)
1072 {
1073    ELM_CHECK_WIDTYPE(obj, widtype);
1074    char *path;
1075    path = ecore_file_realpath(_path);
1076    _populate(obj, path, NULL);
1077    free(path);
1078 }
1079
1080 EAPI const char *
1081 elm_fileselector_path_get(const Evas_Object *obj)
1082 {
1083    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1084    Widget_Data *wd = elm_widget_data_get(obj);
1085    if (!wd) return NULL;
1086    return wd->path;
1087 }
1088
1089 EAPI void
1090 elm_fileselector_mode_set(Evas_Object          *obj,
1091                           Elm_Fileselector_Mode mode)
1092 {
1093    ELM_CHECK_WIDTYPE(obj, widtype);
1094
1095    Widget_Data *wd = elm_widget_data_get(obj);
1096    if (!wd) return;
1097
1098    if (mode == wd->mode) return;
1099
1100    if (mode == ELM_FILESELECTOR_LIST)
1101      {
1102         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1103                                      wd->files_list))
1104           {
1105              evas_object_show(wd->files_list);
1106              evas_object_hide(wd->files_grid);
1107           }
1108         else
1109           evas_object_hide(wd->files_list);
1110      }
1111    else
1112      {
1113         if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
1114                                      wd->files_grid))
1115           {
1116              evas_object_show(wd->files_grid);
1117              evas_object_hide(wd->files_list);
1118           }
1119         else
1120           evas_object_hide(wd->files_grid);
1121      }
1122
1123    wd->mode = mode;
1124
1125    _populate(obj, wd->path, NULL);
1126 }
1127
1128 EAPI Elm_Fileselector_Mode
1129 elm_fileselector_mode_get(const Evas_Object *obj)
1130 {
1131    ELM_CHECK_WIDTYPE(obj, widtype) ELM_FILESELECTOR_LAST;
1132
1133    Widget_Data *wd = elm_widget_data_get(obj);
1134    if (!wd) return ELM_FILESELECTOR_LAST;
1135
1136    return wd->mode;
1137 }
1138
1139 EAPI const char *
1140 elm_fileselector_selected_get(const Evas_Object *obj)
1141 {
1142    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1143    Widget_Data *wd = elm_widget_data_get(obj);
1144    if (!wd) return NULL;
1145
1146    if (wd->filename_entry)
1147      {
1148         const char *name;
1149         char buf[PATH_MAX];
1150         char *dir;
1151
1152         dir = wd->only_folder ? ecore_file_dir_get(wd->path) : strdup(wd->path);
1153         name = elm_object_text_get(wd->filename_entry);
1154         snprintf(buf, sizeof(buf), "%s/%s",
1155                  dir, name);
1156         if (wd->only_folder && !ecore_file_is_dir(buf))
1157           eina_stringshare_replace(&wd->selection, ecore_file_dir_get(buf));
1158         else
1159           eina_stringshare_replace(&wd->selection, buf);
1160         if (dir) free(dir);
1161         return wd->selection;
1162      }
1163
1164    if (wd->mode == ELM_FILESELECTOR_LIST)
1165      {
1166         Elm_Object_Item *gl_it = elm_genlist_selected_item_get(wd->files_list);
1167         if (gl_it) return elm_object_item_data_get(gl_it);
1168      }
1169    else
1170      {
1171         Elm_Object_Item *gg_it = elm_gengrid_selected_item_get(wd->files_grid);
1172         if (gg_it) return elm_object_item_data_get(gg_it);
1173      }
1174
1175    return wd->path;
1176 }
1177
1178 EAPI Eina_Bool
1179 elm_fileselector_selected_set(Evas_Object *obj,
1180                               const char  *_path)
1181 {
1182    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1183    Widget_Data *wd = elm_widget_data_get(obj);
1184    if (!wd) return EINA_FALSE;
1185
1186    Eina_Bool ret = EINA_TRUE;
1187    char *path;
1188    path = ecore_file_realpath(_path);
1189
1190    if (ecore_file_is_dir(path))
1191      _populate(obj, path, NULL);
1192    else
1193      {
1194         if (!ecore_file_exists(path))
1195           {
1196              ret = EINA_FALSE;
1197              goto clean_up;
1198           }
1199
1200         _populate(obj, ecore_file_dir_get(path), NULL);
1201         if (wd->filename_entry)
1202           {
1203              elm_object_text_set(wd->filename_entry,
1204                                           ecore_file_file_get(path));
1205              eina_stringshare_replace(&wd->selection, path);
1206           }
1207      }
1208
1209 clean_up:
1210    free(path);
1211    return ret;
1212 }
1213