aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/elementary/src/lib/elc_fileselector.c
blob: 148c73d60ecd8d86c09b073c1f9df8597e173c23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
/*
 * TODO:
 *  - child elements focusing support
 *  - user defined icon/label cb
 *  - show/hide/add buttons ???
 *  - show/hide hidden files
 *  - double click to choose a file
 *  - multi-selection
 *  - make variable/function names that are sensible
 *  - Filter support
 */

#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#ifdef HAVE_EIO
# include <Eio.h>
#endif

#include <Elementary.h>
#include "elm_priv.h"

typedef struct _Widget_Data Widget_Data;

struct _Widget_Data
{
   EINA_REFCOUNT;

   Evas_Object *edje;
   Evas_Object *filename_entry;
   Evas_Object *path_entry;
   Evas_Object *files_list;
   Evas_Object *files_grid;
   Evas_Object *up_button;
   Evas_Object *home_button;

   Evas_Object *ok_button;
   Evas_Object *cancel_button;

   const char  *path;
   const char  *selection;
   Ecore_Idler *sel_idler;

   const char  *path_separator;

#ifdef HAVE_EIO
   Eio_File    *current;
#endif

   Elm_Fileselector_Mode mode;

   Eina_Bool    only_folder : 1;
   Eina_Bool    expand : 1;
};

struct sel_data
{
   Evas_Object *fs;
   const char  *path;
};

typedef struct _Widget_Request Widget_Request;
struct _Widget_Request
{
   Widget_Data *wd;
   Elm_Object_Item *parent;

   Evas_Object *obj;
   const char *path;
   Eina_Bool first : 1;
};

typedef enum {
  ELM_DIRECTORY = 0,
  ELM_FILE_IMAGE = 1,
  ELM_FILE_UNKNOW = 2,
  ELM_FILE_LAST
} Elm_Fileselector_Type;

static Elm_Genlist_Item_Class *list_itc[ELM_FILE_LAST];
static Elm_Gengrid_Item_Class *grid_itc[ELM_FILE_LAST];

static const char *widtype = NULL;

static const char SIG_DIRECTORY_OPEN[] = "directory,open";
static const char SIG_DONE[] = "done";
static const char SIG_SELECTED[] = "selected";
static const Evas_Smart_Cb_Description _signals[] = {
   {SIG_DIRECTORY_OPEN, "s"},
   {SIG_DONE, "s"},
   {SIG_SELECTED, "s"},
   {NULL, NULL}
};

static void _populate(Evas_Object      *obj,
                      const char       *path,
                      Elm_Object_Item  *parent);
static void _do_anchors(Evas_Object *obj,
                        const char  *path);

/***  ELEMENTARY WIDGET  ***/
static void
_widget_data_free(Widget_Data *wd)
{
   if (wd->path) eina_stringshare_del(wd->path);
   if (wd->selection) eina_stringshare_del(wd->selection);
   if (wd->sel_idler)
     {
        void *sd;

        sd = ecore_idler_del(wd->sel_idler);
        free(sd);
     }
   free(wd);
}

static void
_del_hook(Evas_Object *obj)
{
   Widget_Data *wd;
   int i;

   wd = elm_widget_data_get(obj);
   if (!wd) return;

   for (i = 0; i < ELM_FILE_LAST; ++i)
     {
        elm_genlist_item_class_free(list_itc[i]);
        elm_gengrid_item_class_free(grid_itc[i]);
     }

#ifdef HAVE_EIO
   if (wd->current)
     eio_file_cancel(wd->current);
#endif

   wd->files_list = NULL;
   wd->files_grid = NULL;

   EINA_REFCOUNT_UNREF(wd)
     _widget_data_free(wd);
}

static void
_sizing_eval(Evas_Object *obj)
{
   Widget_Data *wd = elm_widget_data_get(obj);
   Evas_Coord minw = -1, minh = -1;
   if (!wd) return;
   elm_coords_finger_size_adjust(1, &minw, 1, &minh);
   edje_object_size_min_restricted_calc(wd->edje, &minw, &minh, minw, minh);
   evas_object_size_hint_min_set(obj, minw, minh);
}

static void
_mirrored_set(Evas_Object *obj, Eina_Bool rtl)
{
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return;
   elm_widget_mirrored_set(wd->cancel_button, rtl);
   elm_widget_mirrored_set(wd->ok_button, rtl);
   elm_widget_mirrored_set(wd->files_list, rtl);
   elm_widget_mirrored_set(wd->up_button, rtl);
   elm_widget_mirrored_set(wd->home_button, rtl);
   edje_object_mirrored_set(wd->edje, rtl);
}

static void
_theme_hook(Evas_Object *obj)
{
   Widget_Data *wd = elm_widget_data_get(obj);
   const char *style = elm_widget_style_get(obj);
   const char *data;
   char buf[1024];

   if (!wd) return;
   _elm_widget_mirrored_reload(obj);

   _elm_theme_object_set(obj, wd->edje, "fileselector", "base", style);

   if (elm_object_disabled_get(obj))
     edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");

   data = edje_object_data_get(wd->edje, "path_separator");
   if (data)
     wd->path_separator = data;
   else
     wd->path_separator = "/";

   if (!style) style = "default";
   snprintf(buf, sizeof(buf), "fileselector/%s", style);

#define SWALLOW(part_name, object_ptn)                                \
  if (object_ptn)                                                     \
    {                                                                 \
       elm_widget_style_set(object_ptn, buf);                         \
       if (edje_object_part_swallow(wd->edje, part_name, object_ptn)) \
         evas_object_show(object_ptn);                                \
       else                                                           \
         evas_object_hide(object_ptn);                                \
    }
   SWALLOW("elm.swallow.up", wd->up_button);
   SWALLOW("elm.swallow.home", wd->home_button);

   if (wd->mode == ELM_FILESELECTOR_LIST)
     {
        if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
                                     wd->files_list))
          {
             evas_object_show(wd->files_list);
             evas_object_hide(wd->files_grid);
          }
        else
          evas_object_hide(wd->files_list);
     }
   else
     {
        if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
                                     wd->files_grid))
          {
             evas_object_show(wd->files_grid);
             evas_object_hide(wd->files_list);
          }
        else
          evas_object_hide(wd->files_grid);
     }

   SWALLOW("elm.swallow.filename", wd->filename_entry);
   SWALLOW("elm.swallow.path", wd->path_entry);

   snprintf(buf, sizeof(buf), "fileselector/actions/%s", style);
   SWALLOW("elm.swallow.cancel", wd->cancel_button);
   SWALLOW("elm.swallow.ok", wd->ok_button);
#undef SWALLOW

   edje_object_message_signal_process(wd->edje);
   _mirrored_set(obj, elm_widget_mirrored_get(obj));
   edje_object_scale_set
     (wd->edje, elm_widget_scale_get(obj) * _elm_config->scale);
   _sizing_eval(obj);
}

/***  GENLIST "MODEL"  ***/
static char *
_itc_text_get(void              *data,
               Evas_Object *obj   __UNUSED__,
               const char *source __UNUSED__)
{
   return elm_entry_utf8_to_markup(ecore_file_file_get(data)); /* NOTE this will be
                                                                * free() by the
                                                                * caller */
}

static Evas_Object *
_itc_icon_folder_get(void        *data __UNUSED__,
                     Evas_Object *obj,
                     const char  *source)
{
   Evas_Object *ic;

   if (strcmp(source, "elm.swallow.icon")) return NULL;

   ic = elm_icon_add(obj);
   elm_icon_standard_set(ic, "folder");

   evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
                                    1, 1);
   return ic;
}

static Evas_Object *
_itc_icon_image_get(void        *data,
                    Evas_Object *obj,
                    const char  *source)
{
   const char *filename = data;
   Evas_Object *ic;

   if (strcmp(source, "elm.swallow.icon")) return NULL;

   ic = elm_icon_add(obj);
   elm_icon_standard_set(ic, "image");
   elm_icon_thumb_set(ic, filename, NULL);

   evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
                                    1, 1);
   return ic;
}

static Evas_Object *
_itc_icon_file_get(void        *data __UNUSED__,
                   Evas_Object *obj,
                   const char  *source)
{
   Evas_Object *ic;

   if (strcmp(source, "elm.swallow.icon")) return NULL;

   ic = elm_icon_add(obj);
   elm_icon_standard_set(ic, "file");

   evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL,
                                    1, 1);
   return ic;
}

static Eina_Bool
_itc_state_get(void *data         __UNUSED__,
               Evas_Object *obj   __UNUSED__,
               const char *source __UNUSED__)
{
   return EINA_FALSE;
}

static void
_itc_del(void            *data,
         Evas_Object *obj __UNUSED__)
{
   eina_stringshare_del(data);
}

static void
_expand_done(void            *data,
             Evas_Object *obj __UNUSED__,
             void            *event_info)
{
   Elm_Object_Item *it = event_info;
   const char *path = elm_object_item_data_get(it);
   _populate(data, path, it);
}

static void
_contract_done(void *data       __UNUSED__,
               Evas_Object *obj __UNUSED__,
               void            *event_info)
{
   Elm_Object_Item *it = event_info;
   elm_genlist_item_subitems_clear(it);
}

static void
_expand_req(void *data       __UNUSED__,
            Evas_Object *obj __UNUSED__,
            void            *event_info)
{
   Elm_Object_Item *it = event_info;
   elm_genlist_item_expanded_set(it, EINA_TRUE);
}

static void
_contract_req(void *data       __UNUSED__,
              Evas_Object *obj __UNUSED__,
              void            *event_info)
{
   Elm_Object_Item *it = event_info;
   elm_genlist_item_expanded_set(it, EINA_FALSE);
}

/***  PRIVATES  ***/
static Eina_Bool
_sel_do(void *data)
{
   struct sel_data *sd;
   const char *path;
   Widget_Data *wd;
   const char *p;

   sd = data;
   wd = elm_widget_data_get(sd->fs);
   path = sd->path;

   if ((!wd->only_folder) && ecore_file_is_dir(path))
     {
        if (wd->expand && wd->mode == ELM_FILESELECTOR_LIST)
          {
             _do_anchors(sd->fs, path);
             elm_object_text_set(wd->filename_entry, "");
          }
        else
          {
             /* keep a ref to path 'couse it will be destroyed by _populate */
             p = eina_stringshare_add(path);
             _populate(sd->fs, p, NULL);
             eina_stringshare_del(p);
          }
        goto end;
     }
   else /* navigating through folders only or file is not a dir. */
     {
        char *s;
        
        if (wd->expand && wd->mode == ELM_FILESELECTOR_LIST)
          _do_anchors(sd->fs, path);
        else if (wd->only_folder)
          {
             /* keep a ref to path 'couse it will be destroyed by _populate */
             p = eina_stringshare_add(path);
             _populate(sd->fs, p, NULL);
             eina_stringshare_del(p);
          }
        s = elm_entry_utf8_to_markup(ecore_file_file_get(path));
        if (s)
          {
             elm_object_text_set(wd->filename_entry, s);
             free(s);
          }
        else
          elm_object_text_set(wd->filename_entry, "");
     }

   evas_object_smart_callback_call(sd->fs, SIG_SELECTED, (void *)path);

end:
   wd->sel_idler = NULL;
   free(sd);
   return ECORE_CALLBACK_CANCEL;
}

static void
_sel(void            *data,
     Evas_Object *obj __UNUSED__,
     void            *event_info)
{
   struct sel_data *sd;
   Widget_Data *wd;
   void *old_sd;
   char *dir;
   //This event_info could be a list or gengrid item
   Elm_Object_Item *it = event_info;

   wd = elm_widget_data_get(data);
   if (!wd) return;

   sd = malloc(sizeof(*sd));
   sd->fs = data;
   sd->path = elm_object_item_data_get(it);

   if (!sd->path)
     {
        eina_stringshare_replace(&wd->path, "");
        goto end;
     }

   dir = wd->only_folder ? strdup(sd->path) : ecore_file_dir_get(sd->path);
   if (dir)
     {
        eina_stringshare_replace(&wd->path, dir);
        free(dir);
     }
   else
     {
        eina_stringshare_replace(&wd->path, "");
     }

end:
   if (wd->sel_idler)
     {
        old_sd = ecore_idler_del(wd->sel_idler);
        free(old_sd);
     }
   wd->sel_idler = ecore_idler_add(_sel_do, sd);
}

static void
_up(void            *data,
    Evas_Object *obj __UNUSED__,
    void *event_info __UNUSED__)
{
   Evas_Object *fs = data;
   char *parent;

   Widget_Data *wd = elm_widget_data_get(fs);
   if (!wd) return;
   parent = ecore_file_dir_get(wd->path);
   _populate(fs, parent, NULL);
   free(parent);
}

static void
_home(void            *data,
      Evas_Object *obj __UNUSED__,
      void *event_info __UNUSED__)
{
   Evas_Object *fs = data;
   _populate(fs, getenv("HOME"), NULL);
}

static void
_ok(void            *data,
    Evas_Object *obj __UNUSED__,
    void *event_info __UNUSED__)
{
   Evas_Object *fs = data;
   evas_object_smart_callback_call(fs, SIG_DONE,
                                   (void *)elm_fileselector_selected_get(fs));
}

static void
_canc(void            *data,
      Evas_Object *obj __UNUSED__,
      void *event_info __UNUSED__)
{
   Evas_Object *fs = data;
   evas_object_smart_callback_call(fs, SIG_DONE, NULL);
}

static void
_anchor_clicked(void            *data,
                Evas_Object *obj __UNUSED__,
                void            *event_info)
{
   Evas_Object *fs = data;
   Widget_Data *wd = elm_widget_data_get(fs);
   Elm_Entry_Anchor_Info *info = event_info;
   const char *p;
   if (!wd) return;
   // keep a ref to path 'couse it will be destroyed by _populate
   p = eina_stringshare_add(info->name);
   _populate(fs, p, NULL);
   evas_object_smart_callback_call(data, SIG_SELECTED, (void *)p);
   eina_stringshare_del(p);
}

static void
_do_anchors(Evas_Object *obj,
            const char  *path)
{
   Widget_Data *wd = elm_widget_data_get(obj);
   char **tok, buf[PATH_MAX * 3], *s;
   int i, j;
   
   if (!wd) return;
   s = elm_entry_utf8_to_markup(path);
   if (!s) return;
   buf[0] = '\0';
   tok = eina_str_split(s, "/", 0);
   free(s);
   eina_strlcat(buf, "<a href=/>root</a>", sizeof(buf));
   for (i = 0; tok[i]; i++)
     {
        if ((!tok[i]) || (!tok[i][0])) continue;
        eina_strlcat(buf, wd->path_separator, sizeof(buf));
        eina_strlcat(buf, "<a href=", sizeof(buf));
        for (j = 0; j <= i; j++)
          {
             if (strlen(tok[j]) < 1) continue;
             eina_strlcat(buf, "/", sizeof(buf));
             eina_strlcat(buf, tok[j], sizeof(buf));
          }
        eina_strlcat(buf, ">", sizeof(buf));
        eina_strlcat(buf, tok[i], sizeof(buf));
        eina_strlcat(buf, "</a>", sizeof(buf));
     }
   free(tok[0]);
   free(tok);

   elm_object_text_set(wd->path_entry, buf);
}

#ifdef HAVE_EIO
static Eina_Bool
_filter_cb(void *data __UNUSED__, Eio_File *handler, const Eina_File_Direct_Info *info)
{
   const char *filename;

   if (info->path[info->name_start] == '.')
     return EINA_FALSE;

   filename = eina_stringshare_add(info->path);
   eio_file_associate_direct_add(handler, "filename", filename, EINA_FREE_CB(eina_stringshare_del));

   if (info->type == EINA_FILE_DIR)
     {
        eio_file_associate_direct_add(handler, "type/grid", grid_itc[ELM_DIRECTORY], NULL);
        eio_file_associate_direct_add(handler, "type/list", list_itc[ELM_DIRECTORY], NULL);
     }
   else
     {
        if (evas_object_image_extension_can_load_get(info->path + info->name_start))
          {
             eio_file_associate_direct_add(handler, "type/grid", grid_itc[ELM_FILE_IMAGE], NULL);
             eio_file_associate_direct_add(handler, "type/list", list_itc[ELM_FILE_IMAGE], NULL);
          }
        else
          {
             eio_file_associate_direct_add(handler, "type/grid", grid_itc[ELM_FILE_UNKNOW], NULL);
             eio_file_associate_direct_add(handler, "type/list", list_itc[ELM_FILE_UNKNOW], NULL);
          }
     }

   return EINA_TRUE;
}

static int
_file_grid_cmp(const void *a, const void *b)
{
   const Elm_Object_Item *ga = a;
   const Elm_Object_Item *gb = b;
   const Elm_Gengrid_Item_Class *ca = elm_gengrid_item_item_class_get(ga);
   const Elm_Gengrid_Item_Class *cb = elm_gengrid_item_item_class_get(gb);

   if (ca == grid_itc[ELM_DIRECTORY])
     {
        if (cb != grid_itc[ELM_DIRECTORY])
          return -1;
     }
   else if (cb == grid_itc[ELM_DIRECTORY])
     {
        return 1;
     }

   return strcoll(elm_object_item_data_get(ga), elm_object_item_data_get(gb));
}

static int
_file_list_cmp(const void *a, const void *b)
{
   const Elm_Object_Item *la = a;
   const Elm_Object_Item *lb = b;
   const Elm_Genlist_Item_Class *ca = elm_genlist_item_item_class_get(la);
   const Elm_Genlist_Item_Class *cb = elm_genlist_item_item_class_get(lb);

   if (ca == list_itc[ELM_DIRECTORY])
     {
        if (cb != list_itc[ELM_DIRECTORY])
          return -1;
     }
   else if (cb == list_itc[ELM_DIRECTORY])
     {
        return 1;
     }

   return strcoll(elm_object_item_data_get(la), elm_object_item_data_get(lb));
}

static void
_signal_first(Widget_Request *wr)
{
   if (!wr->first) return ;
   evas_object_smart_callback_call(wr->obj, SIG_DIRECTORY_OPEN, (void *)wr->path);
   if (!wr->parent)
     {
        elm_genlist_clear(wr->wd->files_list);
        elm_gengrid_clear(wr->wd->files_grid);
        eina_stringshare_replace(&wr->wd->path, wr->path);
        _do_anchors(wr->obj, wr->path);
     }

   if (wr->wd->filename_entry) elm_object_text_set(wr->wd->filename_entry, "");

   wr->first = EINA_FALSE;
}

static void
_main_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info *info __UNUSED__)
{
   Widget_Request *wr = data;

   if (eio_file_check(handler))
     return ;
   if (!wr->wd->files_list || !wr->wd->files_grid || wr->wd->current != handler)
     {
        eio_file_cancel(handler);
        return ;
     }

   _signal_first(wr);

   if (wr->wd->mode == ELM_FILESELECTOR_LIST)
     {
        Eina_Bool is_dir = (eio_file_associate_find(handler, "type/list") == list_itc[ELM_DIRECTORY]);

        elm_genlist_item_sorted_insert(wr->wd->files_list, eio_file_associate_find(handler, "type/list"),
                                       eina_stringshare_ref(eio_file_associate_find(handler, "filename")),
                                       wr->parent, wr->wd->expand && is_dir ? ELM_GENLIST_ITEM_TREE : ELM_GENLIST_ITEM_NONE,
                                       _file_list_cmp, NULL, NULL);
     }
   else if (wr->wd->mode == ELM_FILESELECTOR_GRID)
     elm_gengrid_item_sorted_insert(wr->wd->files_grid, eio_file_associate_find(handler, "type/grid"),
                                           eina_stringshare_ref(eio_file_associate_find(handler, "filename")),
                                           _file_grid_cmp, NULL, NULL);
}

static void
_widget_request_cleanup(Widget_Request *wr)
{
   EINA_REFCOUNT_UNREF(wr->wd)
     _widget_data_free(wr->wd);

   eina_stringshare_del(wr->path);
   free(wr);
}

static void
_done_cb(void *data, Eio_File *handler __UNUSED__)
{
   Widget_Request *wr = data;

   _signal_first(wr);

   wr->wd->current = NULL;
   _widget_request_cleanup(wr);
}

static void
_error_cb(void *data, Eio_File *handler, int error __UNUSED__)
{
   Widget_Request *wr = data;

   if (wr->wd->current == handler)
     wr->wd->current = NULL;
   _widget_request_cleanup(wr);
}

#endif

static void
_populate(Evas_Object      *obj,
          const char       *path,
          Elm_Object_Item  *parent)
{
   Widget_Data *wd = elm_widget_data_get(obj);
#ifdef HAVE_EIO
   Widget_Request *wr;
#else
   Eina_File_Direct_Info *file;
   Eina_Iterator *it;
   const char *real;
   Eina_List *files = NULL, *dirs = NULL;
#endif

   if (!wd) return;
#ifndef HAVE_EIO
   if (!ecore_file_is_dir(path)) return ;
   it = eina_file_stat_ls(path);
   if (!it) return ;
   evas_object_smart_callback_call(obj, SIG_DIRECTORY_OPEN, (void *)path);
   if (!parent)
     {
        elm_genlist_clear(wd->files_list);
        elm_gengrid_clear(wd->files_grid);
        eina_stringshare_replace(&wd->path, path);
        _do_anchors(obj, path);
     }

   if (wd->filename_entry) elm_object_text_set(wd->filename_entry, "");
   EINA_ITERATOR_FOREACH(it, file)
     {
        const char *filename;

        if (file->path[file->name_start] == '.')
          continue ;

        filename = eina_stringshare_add(file->path);
        if (file->type == EINA_FILE_DIR)
          dirs = eina_list_append(dirs, filename);
        else if (!wd->only_folder)
          files = eina_list_append(files, filename);
     }
   eina_iterator_free(it);

   files = eina_list_sort(files, eina_list_count(files),
                          EINA_COMPARE_CB(strcoll));
   dirs = eina_list_sort(dirs, eina_list_count(dirs), EINA_COMPARE_CB(strcoll));
   EINA_LIST_FREE(dirs, real)
     {
        if (wd->mode == ELM_FILESELECTOR_LIST)
          elm_genlist_item_append(wd->files_list, list_itc[ELM_DIRECTORY],
                                  real, /* item data */
                                  parent,
                                  wd->expand ? ELM_GENLIST_ITEM_TREE :
                                  ELM_GENLIST_ITEM_NONE,
                                  NULL, NULL);
        else if (wd->mode == ELM_FILESELECTOR_GRID)
          elm_gengrid_item_append(wd->files_grid, grid_itc[ELM_DIRECTORY],
                                  real, /* item data */
                                  NULL, NULL);
     }

   EINA_LIST_FREE(files, real)
     {
        Elm_Fileselector_Type type = evas_object_image_extension_can_load_fast_get(real) ?
          ELM_FILE_IMAGE : ELM_FILE_UNKNOW;

        if (wd->mode == ELM_FILESELECTOR_LIST)
          elm_genlist_item_append(wd->files_list, list_itc[type],
                                  real, /* item data */
                                  parent, ELM_GENLIST_ITEM_NONE,
                                  NULL, NULL);
        else if (wd->mode == ELM_FILESELECTOR_GRID)
          elm_gengrid_item_append(wd->files_grid, grid_itc[type],
                                  real, /* item data */
                                  NULL, NULL);
     }
#else
   if (wd->expand && wd->current) return ;
   if (wd->current)
     eio_file_cancel(wd->current);
   wr = malloc(sizeof (Widget_Request));
   if (!wr) return ;
   wr->wd = wd;
   EINA_REFCOUNT_REF(wr->wd);
   wr->parent = parent; /* FIXME: should we refcount the parent ? */
   wr->obj = obj;
   wr->path = eina_stringshare_add(path);
   wr->first = EINA_TRUE;

   wd->current = eio_file_stat_ls(path,
                                  _filter_cb,
                                  _main_cb,
                                  _done_cb,
                                  _error_cb,
                                  wr);
#endif
}

/***  API  ***/

EAPI Evas_Object *
elm_fileselector_add(Evas_Object *parent)
{
   Evas *e;
   Evas_Object *obj, *ic, *bt, *li, *en, *grid;
   Widget_Data *wd;
   unsigned int i;
   int s;

   ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);

   EINA_REFCOUNT_INIT(wd);

   ELM_SET_WIDTYPE(widtype, "fileselector");
   elm_widget_type_set(obj, "fileselector");
   elm_widget_sub_object_add(parent, obj);
   elm_widget_data_set(obj, wd);
   elm_widget_del_hook_set(obj, _del_hook);
   elm_widget_theme_hook_set(obj, _theme_hook);
   elm_widget_can_focus_set(obj, EINA_FALSE);

   wd->expand = !!_elm_config->fileselector_expand_enable;

   wd->edje = edje_object_add(e);
   _elm_theme_object_set(obj, wd->edje, "fileselector", "base", "default");
   elm_widget_resize_object_set(obj, wd->edje);

   // up btn
   ic = elm_icon_add(parent);
   elm_icon_standard_set(ic, "arrow_up");
   evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
   bt = elm_button_add(parent);
   elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
   elm_object_part_content_set(bt, "icon", ic);
   elm_object_domain_translatable_text_set(bt, PACKAGE, N_("Up"));
   evas_object_size_hint_align_set(bt, 0.0, 0.0);

   evas_object_smart_callback_add(bt, "clicked", _up, obj);

   elm_widget_sub_object_add(obj, bt);
   wd->up_button = bt;

   // home btn
   ic = elm_icon_add(parent);
   elm_icon_standard_set(ic, "home");
   evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
   bt = elm_button_add(parent);
   elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
   elm_object_part_content_set(bt, "icon", ic);
   elm_object_domain_translatable_text_set(bt, PACKAGE, N_("Home"));
   evas_object_size_hint_align_set(bt, 0.0, 0.0);

   evas_object_smart_callback_add(bt, "clicked", _home, obj);

   elm_widget_sub_object_add(obj, bt);
   wd->home_button = bt;

   for (i = 0; i < ELM_FILE_LAST; ++i)
     {
        list_itc[i] = elm_genlist_item_class_new();
        grid_itc[i] = elm_gengrid_item_class_new();

        list_itc[i]->item_style = "default";
        list_itc[i]->func.text_get = grid_itc[i]->func.text_get = _itc_text_get;
        list_itc[i]->func.state_get = grid_itc[i]->func.state_get = _itc_state_get;
        list_itc[i]->func.del = grid_itc[i]->func.del = _itc_del;
     }

   list_itc[ELM_DIRECTORY]->func.content_get =
     grid_itc[ELM_DIRECTORY]->func.content_get = _itc_icon_folder_get;
   list_itc[ELM_FILE_IMAGE]->func.content_get =
     grid_itc[ELM_FILE_IMAGE]->func.content_get = _itc_icon_image_get;
   list_itc[ELM_FILE_UNKNOW]->func.content_get =
     grid_itc[ELM_FILE_UNKNOW]->func.content_get = _itc_icon_file_get;

   li = elm_genlist_add(parent);
   elm_widget_mirrored_automatic_set(li, EINA_FALSE);
   evas_object_size_hint_align_set(li, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_size_hint_weight_set(li, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_min_set(li, 100, 100);

   grid = elm_gengrid_add(parent);
   elm_widget_mirrored_automatic_set(grid, EINA_FALSE);
   evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

   // XXX: will fail for dynamic finger size changing
   s = _elm_config->finger_size * 2;
   elm_gengrid_item_size_set(grid, s, s);
   elm_gengrid_align_set(grid, 0.0, 0.0);

   evas_object_smart_callback_add(li, "selected", _sel, obj);
   evas_object_smart_callback_add(li, "expand,request", _expand_req, obj);
   evas_object_smart_callback_add(li, "contract,request", _contract_req, obj);
   evas_object_smart_callback_add(li, "expanded", _expand_done, obj);
   evas_object_smart_callback_add(li, "contracted", _contract_done, obj);

   evas_object_smart_callback_add(grid, "selected", _sel, obj);

   elm_widget_sub_object_add(obj, li);
   elm_widget_sub_object_add(obj, grid);
   wd->files_list = li;
   wd->files_grid = grid;

   // path entry
   en = elm_entry_add(parent);
   elm_entry_scrollable_set(en, EINA_TRUE);
   elm_widget_mirrored_automatic_set(en, EINA_FALSE);
   elm_entry_editable_set(en, EINA_FALSE);
   elm_entry_single_line_set(en, EINA_TRUE);
   elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
   evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);

   evas_object_smart_callback_add(en, "anchor,clicked", _anchor_clicked, obj);

   elm_widget_sub_object_add(obj, en);
   wd->path_entry = en;

   // filename entry
   en = elm_entry_add(parent);
   elm_entry_scrollable_set(en, EINA_TRUE);
   elm_widget_mirrored_automatic_set(en, EINA_FALSE);
   elm_entry_editable_set(en, EINA_TRUE);
   elm_entry_single_line_set(en, EINA_TRUE);
   elm_entry_line_wrap_set(en, ELM_WRAP_CHAR);
   evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);

   elm_widget_sub_object_add(obj, en);
   wd->filename_entry = en;

   elm_fileselector_buttons_ok_cancel_set(obj, EINA_TRUE);
   elm_fileselector_is_save_set(obj, EINA_FALSE);

   _theme_hook(obj);

   evas_object_smart_callbacks_descriptions_set(obj, _signals);
   return obj;
}

EAPI void
elm_fileselector_is_save_set(Evas_Object *obj,
                             Eina_Bool    is_save)
{
   ELM_CHECK_WIDTYPE(obj, widtype);
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return;

   elm_object_disabled_set(wd->filename_entry, !is_save);

   if (is_save)
     edje_object_signal_emit(wd->edje, "elm,state,save,on", "elm");
   else
     edje_object_signal_emit(wd->edje, "elm,state,save,off", "elm");
}

EAPI Eina_Bool
elm_fileselector_is_save_get(const Evas_Object *obj)
{
   ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return EINA_FALSE;
   return !elm_object_disabled_get(wd->filename_entry);
}

EAPI void
elm_fileselector_folder_only_set(Evas_Object *obj,
                                 Eina_Bool    only)
{
   ELM_CHECK_WIDTYPE(obj, widtype);
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return;
   if (wd->only_folder == only) return;
   wd->only_folder = !!only;
   if (wd->path) _populate(obj, wd->path, NULL);
}

EAPI Eina_Bool
elm_fileselector_folder_only_get(const Evas_Object *obj)
{
   ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return EINA_FALSE;
   return wd->only_folder;
}

EAPI void
elm_fileselector_buttons_ok_cancel_set(Evas_Object *obj,
                                       Eina_Bool    visible)
{
   ELM_CHECK_WIDTYPE(obj, widtype);
   Widget_Data *wd = elm_widget_data_get(obj);
   Evas_Object *bt;
   if (!wd) return;

   if (visible)
     {
        // cancel btn
        bt = elm_button_add(obj);
        elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
        elm_object_domain_translatable_text_set(bt, PACKAGE, N_("Cancel"));

        evas_object_smart_callback_add(bt, "clicked", _canc, obj);

        elm_widget_sub_object_add(obj, bt);
        wd->cancel_button = bt;

        // ok btn
        bt = elm_button_add(obj);
        elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
        elm_object_domain_translatable_text_set(bt, PACKAGE, N_("OK"));

        evas_object_smart_callback_add(bt, "clicked", _ok, obj);

        elm_widget_sub_object_add(obj, bt);
        wd->ok_button = bt;

        _theme_hook(obj);
     }
   else
     {
        evas_object_del(wd->cancel_button);
        wd->cancel_button = NULL;
        evas_object_del(wd->ok_button);
        wd->ok_button = NULL;
     }
}

EAPI Eina_Bool
elm_fileselector_buttons_ok_cancel_get(const Evas_Object *obj)
{
   ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return EINA_FALSE;
   return wd->ok_button ? EINA_TRUE : EINA_FALSE;
}

EAPI void
elm_fileselector_expandable_set(Evas_Object *obj,
                                Eina_Bool    expand)
{
   ELM_CHECK_WIDTYPE(obj, widtype);
   Widget_Data *wd;

   wd = elm_widget_data_get(obj);
   if (!wd) return;

   wd->expand = !!expand;

   if (wd->path) _populate(obj, wd->path, NULL);
}

EAPI Eina_Bool
elm_fileselector_expandable_get(const Evas_Object *obj)
{
   ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return EINA_FALSE;
   return wd->expand;
}

EAPI void
elm_fileselector_path_set(Evas_Object *obj,
                          const char  *_path)
{
   ELM_CHECK_WIDTYPE(obj, widtype);
   char *path;
   path = ecore_file_realpath(_path);
   _populate(obj, path, NULL);
   free(path);
}

EAPI const char *
elm_fileselector_path_get(const Evas_Object *obj)
{
   ELM_CHECK_WIDTYPE(obj, widtype) NULL;
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return NULL;
   return wd->path;
}

EAPI void
elm_fileselector_mode_set(Evas_Object          *obj,
                          Elm_Fileselector_Mode mode)
{
   ELM_CHECK_WIDTYPE(obj, widtype);

   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return;

   if (mode == wd->mode) return;

   if (mode == ELM_FILESELECTOR_LIST)
     {
        if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
                                     wd->files_list))
          {
             evas_object_show(wd->files_list);
             evas_object_hide(wd->files_grid);
          }
        else
          evas_object_hide(wd->files_list);
     }
   else
     {
        if (edje_object_part_swallow(wd->edje, "elm.swallow.files",
                                     wd->files_grid))
          {
             evas_object_show(wd->files_grid);
             evas_object_hide(wd->files_list);
          }
        else
          evas_object_hide(wd->files_grid);
     }

   wd->mode = mode;

   _populate(obj, wd->path, NULL);
}

EAPI Elm_Fileselector_Mode
elm_fileselector_mode_get(const Evas_Object *obj)
{
   ELM_CHECK_WIDTYPE(obj, widtype) ELM_FILESELECTOR_LAST;

   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return ELM_FILESELECTOR_LAST;

   return wd->mode;
}

EAPI const char *
elm_fileselector_selected_get(const Evas_Object *obj)
{
   ELM_CHECK_WIDTYPE(obj, widtype) NULL;
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return NULL;

   if (!wd->path) return NULL;
   
   if (wd->filename_entry)
     {
        const char *name;
        char buf[PATH_MAX];
        char *dir, *s;

        dir = wd->only_folder ? ecore_file_dir_get(wd->path) : strdup(wd->path);
        name = elm_object_text_get(wd->filename_entry);
        if (name)
          {
             s = elm_entry_markup_to_utf8(name);
             if (s)
               {
                  snprintf(buf, sizeof(buf), "%s/%s", dir, s);
                  free(s);
               }
             else
               snprintf(buf, sizeof(buf), "%s", dir);
          }
        else
          {
             snprintf(buf, sizeof(buf), "%s", dir);
          }
        if (wd->only_folder && !ecore_file_is_dir(buf))
          eina_stringshare_replace(&wd->selection, ecore_file_dir_get(buf));
        else
          eina_stringshare_replace(&wd->selection, buf);
        if (dir) free(dir);
        return wd->selection;
     }

   if (wd->mode == ELM_FILESELECTOR_LIST)
     {
        Elm_Object_Item *gl_it = elm_genlist_selected_item_get(wd->files_list);
        if (gl_it) return elm_object_item_data_get(gl_it);
     }
   else
     {
        Elm_Object_Item *gg_it = elm_gengrid_selected_item_get(wd->files_grid);
        if (gg_it) return elm_object_item_data_get(gg_it);
     }

   return wd->path;
}

EAPI Eina_Bool
elm_fileselector_selected_set(Evas_Object *obj,
                              const char  *_path)
{
   ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
   Widget_Data *wd = elm_widget_data_get(obj);
   if (!wd) return EINA_FALSE;

   Eina_Bool ret = EINA_TRUE;
   char *path;
   path = ecore_file_realpath(_path);

   if (ecore_file_is_dir(path))
     _populate(obj, path, NULL);
   else
     {
        if (!ecore_file_exists(path))
          {
             ret = EINA_FALSE;
             goto clean_up;
          }

        _populate(obj, ecore_file_dir_get(path), NULL);
        if (wd->filename_entry)
          {
             char *s;
             
             s = elm_entry_utf8_to_markup(ecore_file_file_get(path));
             if (s)
               {
                  elm_object_text_set(wd->filename_entry, s);
                  free(s);
               }
             else
               elm_object_text_set(wd->filename_entry, "");
             eina_stringshare_replace(&wd->selection, path);
          }
     }

clean_up:
   free(path);
   return ret;
}