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