tizen 2.3 release
[framework/uifw/elementary.git] / src / bin / test_eio.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4
5 #ifdef HAVE_SYS_TIMES_H
6 # include <sys/times.h>
7 #endif
8
9 #ifdef _WIN32
10 # ifndef WIN32_LEAN_AND_MEAN
11 #  define WIN32_LEAN_AND_MEAN
12 # endif
13 # include <windows.h>
14 # undef WIN32_LEAN_AND_MEAN
15 #endif
16
17 #include <Eio.h>
18
19 #include <Elementary.h>
20 #ifndef ELM_LIB_QUICKLAUNCH
21
22 static Elm_Genlist_Item_Class it_eio;
23
24 #ifdef _WIN32
25 ULONGLONG st_time_kernel;
26 ULONGLONG st_time_user;
27 ULONGLONG en_time_kernel;
28 ULONGLONG en_time_user;
29 #else
30 static clock_t st_time;
31 static clock_t en_time;
32 static struct tms st_cpu;
33 static struct tms en_cpu;
34 #endif
35
36 static void _sel_file(void *data, Evas_Object *obj, void *event_info);
37 static Eina_Bool _ls_filter_cb(void *data, Eio_File *handler, const char *file);
38 static void _ls_main_cb(void *data, Eio_File *handler, const char *file);
39 static void _ls_done_cb(void *data, Eio_File *handler);
40 static void _ls_error_cb(void *data, Eio_File *handler, int error);
41 static void _file_chosen(void *data, Evas_Object *obj, void *event_info);
42 static char *_gl_text_get(void *data, Evas_Object *obj, const char *part);
43 static Evas_Object *_gl_content_get(void *data, Evas_Object *obj, const char *part);
44 static Eina_Bool _gl_state_get(void *data, Evas_Object *obj, const char *part);
45 static void _gl_del(void *data, Evas_Object *obj);
46 static void _test_eio_clear(void *data, Evas_Object *obj, void *event);
47
48 static void
49 _sel_file(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
50 {
51 }
52
53 static Eina_Bool
54 _ls_filter_cb(void *data __UNUSED__, Eio_File *handler __UNUSED__, const char *file __UNUSED__)
55 {
56   return EINA_TRUE;
57 }
58
59 static int
60 _compare_cb(const void *data1, const void *data2)
61 {
62    Elm_Object_Item *it = (Elm_Object_Item *)data1;
63    Elm_Object_Item *it2 = (Elm_Object_Item *)data2;
64    return strcoll(elm_object_item_data_get(it),
65                   elm_object_item_data_get(it2));
66 }
67
68 static void
69 _ls_main_cb(void *data, Eio_File *handler __UNUSED__, const char *file)
70 {
71    elm_genlist_item_sorted_insert(data,
72                                   &it_eio,
73                                   eina_stringshare_add(file),
74                                   NULL,
75                                   ELM_GENLIST_ITEM_NONE,
76                                   _compare_cb,
77                                   _sel_file,
78                                   NULL);
79 }
80
81 static void
82 _ls_done_cb(void *data __UNUSED__, Eio_File *handler __UNUSED__)
83 {
84 #ifdef _WIN32
85    FILETIME tc;
86    FILETIME te;
87    FILETIME tk;
88    FILETIME tu;
89    ULARGE_INTEGER time_kernel;
90    ULARGE_INTEGER time_user;
91
92    if (!GetProcessTimes(GetCurrentProcess(),
93                         &tc, &te, &tk, &tu))
94      return;
95
96    time_kernel.u.LowPart = tk.dwLowDateTime;
97    time_kernel.u.HighPart = tk.dwHighDateTime;
98    en_time_kernel = time_kernel.QuadPart;
99
100    time_user.u.LowPart = tu.dwLowDateTime;
101    time_user.u.HighPart = tu.dwHighDateTime;
102    en_time_user = time_user.QuadPart;
103
104    fprintf(stderr, "ls done\n");
105    fprintf(stderr, "Kernel Time: %lld, User Time: %lld",
106            (en_time_kernel - st_time_kernel),
107            (en_time_user - st_time_user));
108 #else
109    en_time = times(&en_cpu);
110    fprintf(stderr, "ls done\n");
111    fprintf(stderr, "Real Time: %.jd, User Time: %.jd, System Time: %.jd\n",
112            (intmax_t)(en_time - st_time),
113            (intmax_t)(en_cpu.tms_utime - st_cpu.tms_utime),
114            (intmax_t)(en_cpu.tms_stime - st_cpu.tms_stime));
115 #endif
116 }
117
118 static void
119 _ls_error_cb(void *data __UNUSED__, Eio_File *handler __UNUSED__, int error)
120 {
121    fprintf(stderr, "error: [%s]\n", strerror(error));
122 }
123
124 static void
125 _file_chosen(void *data, Evas_Object *obj __UNUSED__, void *event_info)
126 {
127    const char *file = event_info;
128    if (file)
129      {
130 #ifdef _WIN32
131         FILETIME tc;
132         FILETIME te;
133         FILETIME tk;
134         FILETIME tu;
135         ULARGE_INTEGER time_kernel;
136         ULARGE_INTEGER time_user;
137
138         if (!GetProcessTimes(GetCurrentProcess(),
139                              &tc, &te, &tk, &tu))
140           return;
141
142         time_kernel.u.LowPart = tk.dwLowDateTime;
143         time_kernel.u.HighPart = tk.dwHighDateTime;
144         st_time_kernel = time_kernel.QuadPart;
145
146         time_user.u.LowPart = tu.dwLowDateTime;
147         time_user.u.HighPart = tu.dwHighDateTime;
148         st_time_user = time_user.QuadPart;
149 #else
150         st_time = times(&st_cpu);
151 #endif
152         eio_file_ls(file,
153                     _ls_filter_cb,
154                     _ls_main_cb,
155                     _ls_done_cb,
156                     _ls_error_cb,
157                     data);
158      }
159 }
160
161 static char *
162 _gl_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
163 {
164    char buf[PATH_MAX];
165    snprintf(buf, sizeof(buf), "Item # %s", (char*)data);
166    return strdup(buf);
167 }
168
169 static Evas_Object *
170 _gl_content_get(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
171 {
172    return NULL;
173 }
174
175 static Eina_Bool
176 _gl_state_get(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
177 {
178    return EINA_FALSE;
179 }
180
181 static void
182 _gl_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__)
183 {
184 }
185
186 static void
187 _test_eio_clear(void *data, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
188 {
189    elm_genlist_clear(data);
190 }
191
192 void
193 test_eio(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
194 {
195    Evas_Object *win, *vbox, *hbox, *ic, *bt, *fs_bt, *gl;
196
197    it_eio.item_style     = "default";
198    it_eio.func.text_get = _gl_text_get;
199    it_eio.func.content_get  = _gl_content_get;
200    it_eio.func.state_get = _gl_state_get;
201    it_eio.func.del       = _gl_del;
202
203    win = elm_win_util_standard_add("fileselector-button", "File Selector Button");
204    elm_win_autodel_set(win, EINA_TRUE);
205
206    vbox = elm_box_add(win);
207    elm_win_resize_object_add(win, vbox);
208    evas_object_size_hint_weight_set(vbox, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
209    evas_object_show(vbox);
210
211    gl = elm_genlist_add(win);
212    evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
213    evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
214    elm_box_pack_end(vbox, gl);
215    evas_object_show(gl);
216
217    /* file selector button */
218    hbox = elm_box_add(win);
219    elm_box_horizontal_set(hbox, EINA_TRUE);
220    ic = elm_icon_add(win);
221    elm_icon_standard_set(ic, "file");
222    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
223    fs_bt = elm_fileselector_button_add(win);
224    elm_object_text_set(fs_bt, "Select a dir");
225    elm_object_part_content_set(fs_bt, "icon", ic);
226    elm_fileselector_button_inwin_mode_set(fs_bt, EINA_TRUE);
227    elm_fileselector_button_folder_only_set(fs_bt, EINA_TRUE);
228
229    elm_box_pack_end(hbox, fs_bt);
230    elm_box_pack_end(vbox, hbox);
231    evas_object_show(fs_bt);
232    evas_object_show(ic);
233
234    /* attribute setting buttons */
235    bt = elm_button_add(win);
236    elm_object_text_set(bt, "clear");
237    evas_object_smart_callback_add(bt, "clicked", _test_eio_clear, gl);
238    elm_box_pack_end(hbox, bt);
239    evas_object_show(bt);
240    evas_object_show(hbox);
241
242    evas_object_smart_callback_add(fs_bt, "file,chosen", _file_chosen, gl);
243
244    evas_object_resize(win, 300, 500);
245    evas_object_show(win);
246 }
247
248 #endif