Basic routine : type -> select
[apps/native/sample/adventure.git] / src / item.c
1 #include <Elementary.h>
2 #include <system_settings.h>
3 #include <time.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <app.h>
7
8 #include "log.h"
9 #include "util.h"
10 #include "city.h"
11 #include "item.h"
12 #include "main_view.h"
13 #include "scroller.h"
14
15 #define _EDJ(o) elm_layout_edje_get(o)
16 const char *const DATA_KEY_RESULT = "result";
17 const char *const DATA_KEY_CITY = "city";
18
19 struct _result_info_s {
20         Evas_Object *rect1;
21         Evas_Object *rect2;
22         int start;
23         int end;
24 };
25 typedef struct _result_info_s result_info_s;
26
27 const char *const ITEM_EDJE = "item.edj";
28
29 void _text_set_here_time(Evas_Object *item, const char *part)
30 {
31         char *timezone_str = NULL;
32         struct tm *local_time = NULL;
33
34         int ret = SYSTEM_SETTINGS_ERROR_NONE;
35         time_t t = 0;
36         char time_result[PATH_LEN] = {0, };
37
38         ret_if(!item);
39         ret_if(!part);
40
41         local_time = localtime(&t);
42         ret_if(!local_time);
43
44         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONE, &timezone_str);
45         if (ret != SYSTEM_SETTINGS_ERROR_NONE)
46                 _E("cannot get the timezone string");
47
48         /* Timezone
49            Asia/Seoul
50         */
51         if (local_time->tm_gmtoff >= 0)
52                 snprintf(time_result, sizeof(time_result), "%s, +%ld", timezone_str, local_time->tm_gmtoff / 3600);
53         else
54                 snprintf(time_result, sizeof(time_result), "%s, %ld", timezone_str, local_time->tm_gmtoff / 3600);
55
56         free(timezone_str);
57
58         elm_object_part_text_set(item, part, timezone_str);
59 }
60
61 static void _item_typing_down_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
62 {
63         _D("An item is down");
64 }
65
66 static void _item_typing_up_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
67 {
68         Evas_Object *item = obj;
69         Evas_Object *selecting_item = NULL;
70         main_view_s *main_view_info = data;
71         int city = 0;
72
73         ret_if(!item);
74         ret_if(!main_view_info);
75         ret_if(!main_view_info->selected_scroller);
76
77         _D("An item is selected");
78
79         city = (int) evas_object_data_get(item, DATA_KEY_CITY);
80         selecting_item = item_create_selecting(main_view_info->selected_scroller, city, 0);
81         ret_if(!selecting_item);
82
83         scroller_append_item(main_view_info->selected_scroller, selecting_item);
84 }
85
86 Evas_Object *item_create_typing(Evas_Object *parent, int city, main_view_s *main_view_info)
87 {
88         Evas_Object *item = NULL;
89         city_s *city_info = NULL;
90
91         char *path = NULL;
92         char full_path[PATH_LEN] = {0, };
93         char city_str[PATH_LEN] = {0, };
94
95         retv_if(!parent, NULL);
96
97         path = app_get_resource_path();
98         retv_if(!path, NULL);
99
100         snprintf(full_path, sizeof(full_path), "%s/edje/%s", path, ITEM_EDJE);
101         free(path);
102
103         item = elm_layout_add(parent);
104         retv_if(!item, NULL);
105
106         elm_layout_file_set(item, full_path, "item_typing");
107
108         evas_object_size_hint_weight_set(item, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
109         evas_object_size_hint_align_set(item, EVAS_HINT_FILL, EVAS_HINT_FILL);
110         evas_object_data_set(item, DATA_KEY_CITY, (void *) city);
111         evas_object_show(item);
112
113         city_info = city_get();
114         if (!city_info) {
115                 _E("Critical, no city information");
116                 evas_object_del(item);
117                 return NULL;
118         }
119         
120         snprintf(city_str, sizeof(city_str), "%s, %s", city_info[city].name, city_info[city].nation);
121         elm_object_part_text_set(item, "city", city_str);
122
123         elm_object_signal_callback_add(item, "down", "item", _item_typing_down_cb, NULL);
124         elm_object_signal_callback_add(item, "up", "item", _item_typing_up_cb, main_view_info);
125
126         return item;
127 }
128
129 void item_destroy_typing(Evas_Object *item)
130 {
131         ret_if(!item);
132
133         elm_object_signal_callback_del(item, "down", "item", _item_typing_down_cb);
134         elm_object_signal_callback_del(item, "up", "item", _item_typing_up_cb);
135
136         evas_object_data_del(item, DATA_KEY_CITY);
137         evas_object_del(item);
138 }
139
140 static void _delete_down_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
141 {
142         _D("Delete button is down");
143 }
144
145 static void _delete_up_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
146 {
147         Evas_Object *item= obj;
148
149         ret_if(!item);
150
151         item_destroy_selecting(item);
152 }
153
154 Evas_Object *item_create_selecting(Evas_Object *parent, int city, int color)
155 {
156         Evas_Object *item = NULL;
157         city_s *city_info = NULL;
158
159         char *path = NULL;
160         char full_path[PATH_LEN] = {0, };
161         char city_str[PATH_LEN] = {0, };
162         char color_str[16] = {0, };
163
164         retv_if(!parent, NULL);
165
166         _D("HELLO, city:%d, color:%d", city, color);
167         path = app_get_resource_path();
168         retv_if(!path, NULL);
169
170         snprintf(full_path, sizeof(full_path), "%s/edje/%s", path, ITEM_EDJE);
171         free(path);
172
173         item = elm_layout_add(parent);
174         retv_if(!item, NULL);
175
176         elm_layout_file_set(item, full_path, "item_selecting");
177
178         evas_object_size_hint_weight_set(item, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
179         evas_object_size_hint_align_set(item, EVAS_HINT_FILL, EVAS_HINT_FILL);
180         evas_object_show(item);
181
182         city_info = city_get();
183         if (!city_info) {
184                 _E("Critical, no city information");
185                 evas_object_del(item);
186                 return NULL;
187         }
188         
189         snprintf(city_str, sizeof(city_str), "%s, %s", city_info[city].name, city_info[city].nation);
190         elm_object_part_text_set(item, "city", city_str);
191
192         if (color) {
193                 snprintf(color_str, sizeof(color_str), "%d", color);
194                 elm_object_signal_emit(item, color_str, "mark");
195         }
196
197         elm_object_signal_callback_add(item, "delete,down", "item", _delete_down_cb, NULL);
198         elm_object_signal_callback_add(item, "delete,up", "item", _delete_up_cb, NULL);
199
200         return item;
201 }
202
203 void item_destroy_selecting(Evas_Object *item)
204 {
205         ret_if(!item);
206
207         elm_object_signal_callback_del(item, "delete,down", "item", _delete_down_cb);
208         elm_object_signal_callback_del(item, "delete,up", "item", _delete_up_cb);
209
210         evas_object_del(item);
211 }
212
213 void _text_set_local_time(Evas_Object *item, const char *part, int city, struct tm *global_time)
214 {
215         city_s *city_info = NULL;
216         int hour = global_time->tm_hour;
217         int minute = global_time->tm_min;
218
219         char time_result[PATH_LEN] = {0, };
220
221         ret_if(!item);
222         ret_if(!part);
223
224         city_info = city_get();
225         ret_if(!city_info);
226
227         if (((double) ((int) city_info[city].timezone)) < city_info[city].timezone) {
228                 minute += 30;
229                 if (minute > 60) {
230                         hour++;
231                         minute -= 60;
232                 }
233         }
234
235         snprintf(time_result, sizeof(time_result), "%d:%d", hour, minute);
236         elm_object_part_text_set(item, part, time_result);
237 }
238
239 static void _resize_graph_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
240 {
241         result_info_s *result_info = NULL;
242         int x, y, w, h;
243         int start_x = 0;
244
245         result_info = evas_object_data_get(obj, DATA_KEY_RESULT);
246         ret_if(!result_info);
247
248         edje_object_part_geometry_get(_EDJ(obj), "graph_base", &x, &y, &w, &h);
249
250         if (result_info->start > result_info->end) {
251                 evas_object_move(result_info->rect2, x, y);
252                 evas_object_resize(result_info->rect2, w / 10.0, h);
253                 evas_object_color_set(result_info->rect2, 0, 0, 0, 255);
254                 evas_object_show(result_info->rect2);
255         } else {
256                 evas_object_hide(result_info->rect2);
257         }
258
259         start_x = (result_info->start / 24.0f) * w + x;
260
261         evas_object_move(result_info->rect1, start_x, y);
262         evas_object_resize(result_info->rect1, x + w - start_x, h);
263         evas_object_color_set(result_info->rect1, 0, 0, 0, 255);
264         evas_object_show(result_info->rect1);
265 }
266
267 Evas_Object *item_create_result(Evas_Object *parent, int city, int color, int start, int end, struct tm *global_time)
268 {
269         Evas_Object *item = NULL;
270         city_s *city_info = NULL;
271         result_info_s *result_info = NULL;
272         Evas *e = NULL;
273
274         char *path = NULL;
275         char full_path[PATH_LEN] = {0, };
276         char city_str[PATH_LEN] = {0, };
277         char color_str[16] = {0, };
278
279         retv_if(!parent, NULL);
280
281         path = app_get_resource_path();
282         retv_if(!path, NULL);
283
284         snprintf(full_path, sizeof(full_path), "%s/edje/%s", path, ITEM_EDJE);
285         free(path);
286
287         result_info = calloc(1, sizeof(result_info_s));
288         retv_if(!result_info, NULL);
289
290         item = elm_layout_add(parent);
291         goto_if(!item, error);
292
293         elm_layout_file_set(item, full_path, "item_result");
294
295         evas_object_size_hint_weight_set(item, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
296         evas_object_size_hint_align_set(item, EVAS_HINT_FILL, EVAS_HINT_FILL);
297         evas_object_data_set(item, DATA_KEY_RESULT, result_info);
298         evas_object_show(item);
299
300         city_info = city_get();
301         goto_if(!city_info, error);
302         
303         snprintf(city_str, sizeof(city_str), "%s, %s", city_info[city].name, city_info[city].nation);
304         elm_object_part_text_set(item, "city", city_str);
305
306         snprintf(color_str, sizeof(color_str), "%d", color);
307         elm_object_signal_emit(item, color_str, "mark");
308
309         _text_set_local_time(item, "time", city, global_time);
310
311         e = evas_object_evas_get(item);
312         goto_if(!e, error);
313
314         result_info->rect1 = evas_object_rectangle_add(e);
315         goto_if(!result_info->rect1, error);
316         
317         result_info->rect2 = evas_object_rectangle_add(e);
318         goto_if(!result_info->rect2, error);
319
320         result_info->start = start;
321         result_info->end = end;
322
323         evas_object_event_callback_add(item, EVAS_CALLBACK_RESIZE, _resize_graph_cb, result_info);
324         evas_object_event_callback_add(item, EVAS_CALLBACK_MOVE, _resize_graph_cb, result_info); 
325
326         return item;
327
328 error:
329         if (result_info) {
330                 if (result_info->rect1)
331                         evas_object_del(result_info->rect1);
332                 if (result_info->rect2)
333                         evas_object_del(result_info->rect2);
334                 free(result_info);
335         }
336
337         if (item)
338                 evas_object_del(item);
339
340         free(result_info);
341         return NULL;
342 }
343
344 void item_destroy_result(Evas_Object *item)
345 {
346         result_info_s *result_info = NULL;
347
348         ret_if(!item);
349
350         elm_object_signal_callback_del(item, "delete,down", "item", _delete_down_cb);
351         elm_object_signal_callback_del(item, "delete,up", "item", _delete_up_cb);
352
353         result_info = evas_object_data_del(item, DATA_KEY_RESULT);
354         if (result_info) {
355                 if (result_info->rect1)
356                         evas_object_del(result_info->rect1);
357                 if (result_info->rect2)
358                         evas_object_del(result_info->rect2);
359                 free(result_info);
360         }
361
362         evas_object_del(item);
363 }