0955215e28883164eed312ca3b421218bd36b57b
[platform/framework/web/livebox-viewer.git] / live.viewer / src / main.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <Elementary.h>
18
19 #include <dlog.h>
20 #include <ail.h>
21 #include <app.h>
22 #include <bundle.h>
23
24 #include <livebox.h>
25 #include <livebox-service.h>
26
27 #include "main.h"
28 #include "util.h"
29 #include "debug.h"
30 #include "scroller.h"
31 #include "lb.h"
32
33 static struct info {
34         Evas_Object *window;
35         Evas_Object *scroller;
36 } s_info = {
37         .window = NULL,
38         .scroller = NULL,
39 };
40
41 Evas_Object *main_get_window(void)
42 {
43         return s_info.window;
44 }
45
46 static void click_cb(void *data, Evas_Object *obj, void *event_info)
47 {
48         Elm_Object_Item *item;
49         const char *label;
50
51         item = elm_list_selected_item_get(obj);
52         if (!item)
53                 return;
54
55         label = elm_object_item_part_text_get(item, NULL);
56         if (!label)
57                 return;
58
59         DbgPrint("Label: %s (%s)\n", label, data);
60         if (lb_add(s_info.scroller, data) < 0)
61                 ErrPrint("Failed to add a new livebox\n");
62 }
63
64 static int append_livebox_cb(const char *appid, const char *lbid, int is_prime, void *data)
65 {
66         char *name;
67
68         DbgPrint("%s - %s\n", appid, lbid);
69
70         name = livebox_service_i18n_name(lbid, NULL);
71         if (!name) {
72                 name = strdup(lbid);
73                 if (!name) {
74                         ErrPrint("Heap: %s\n", strerror(errno));
75                         return 0;
76                 }
77         }
78
79         DbgPrint("Name: %s\n", name);
80         elm_list_item_append(data, name, NULL, NULL, click_cb, strdup(lbid));
81         free(name);
82         return 0;
83 }
84
85 static inline void livebox_list_create(void)
86 {
87         Evas_Object *list;
88
89         list = elm_list_add(s_info.window);
90         if (!list) {
91                 ErrPrint("Failed to create a list\n");
92                 return;
93         }
94
95         evas_object_resize(list, 720, 1280);
96         evas_object_show(list);
97
98         DbgPrint("Get Package list\n");
99         livebox_service_get_pkglist(append_livebox_cb, list);
100         elm_list_go(list);
101
102         scroller_append(s_info.scroller, list);
103 }
104
105 static bool app_create(void *data)
106 {
107         Evas_Object *bg;
108         DbgPrint("create");
109         lb_init();
110
111         s_info.window = elm_win_add(NULL, "Box viewer", ELM_WIN_BASIC);
112         if (!s_info.window) {
113                 ErrPrint("Failed to create a window\n");
114                 return false;
115         }
116         elm_win_autodel_set(s_info.window, EINA_TRUE);
117
118         evas_object_resize(s_info.window, 720, 1280);
119         evas_object_show(s_info.window);
120
121         bg = elm_bg_add(s_info.window);
122         elm_win_resize_object_add(s_info.window, bg);
123         elm_bg_color_set(bg, 0, 0, 255);
124         evas_object_show(bg);
125
126         s_info.scroller = scroller_create(s_info.window);
127         if (!s_info.scroller) {
128                 evas_object_del(s_info.window);
129                 s_info.window = NULL;
130                 ErrPrint("Failed to create a scroller\n");
131                 return false;
132         }
133
134         evas_object_resize(s_info.scroller, 720, 1280);
135         evas_object_show(s_info.scroller);
136
137         livebox_list_create();
138
139         return true;
140 }
141
142 static void app_terminate(void *data)
143 {
144         DbgPrint("terminate");
145         lb_fini();
146         /*!
147          * \TODO
148          * Delete all objects from the scroller.
149          */
150
151         scroller_destroy(s_info.scroller);
152         evas_object_del(s_info.window);
153         s_info.window = NULL;
154 }
155
156 static void app_pause(void *data)
157 {
158         DbgPrint("pause");
159 }
160
161 static void app_resume(void *data)
162 {
163         DbgPrint("resume");
164 }
165
166 static void app_reset(service_h service, void *data)
167 {
168         DbgPrint("reset");
169 }
170
171 int main(int argc, char *argv[])
172 {
173         app_event_callback_s event_callback;
174
175         setenv("ELM_ENGINE", "gl", 0);
176         event_callback.create = app_create;
177         event_callback.terminate = app_terminate;
178         event_callback.pause = app_pause;
179         event_callback.resume = app_resume;
180         event_callback.service = app_reset;
181         event_callback.low_memory = NULL;
182         event_callback.low_battery = NULL;
183         event_callback.device_orientation = NULL;
184         event_callback.language_changed = NULL;
185
186         return app_efl_main(&argc, &argv, &event_callback, NULL);
187 }
188
189 /* End of a file */