Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-container / examples / tizen / ContainerServerApp / src / rcmain.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "rcmain.h"
22 #include <tizen.h>
23
24 typedef struct appdata
25 {
26     Evas_Object *win;
27     Evas_Object *conform;
28     Evas_Object *layout;
29     Evas_Object *nf;
30     Evas_Object *findButton;
31     Evas_Object *logtext;
32     Evas_Object *listview;
33 } appdata_s;
34
35 static void
36 win_delete_request_cb(void *data , Evas_Object *obj , void *event_info)
37 {
38     ui_app_exit();
39 }
40
41 static void
42 list_selected_cb(void *data, Evas_Object *obj, void *event_info)
43 {
44     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
45     elm_list_item_selected_set(it, EINA_FALSE);
46 }
47
48 static Eina_Bool
49 naviframe_pop_cb(void *data, Elm_Object_Item *it)
50 {
51     ui_app_exit();
52     return EINA_FALSE;
53 }
54
55 static void
56 create_list_view(appdata_s *ad)
57 {
58     Evas_Object *list;
59     Evas_Object *btn;
60     Evas_Object *nf = ad->nf;
61     Elm_Object_Item *nf_it;
62
63     // List
64     list = elm_list_add(nf);
65     elm_list_mode_set(list, ELM_LIST_COMPRESS);
66     evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
67
68     // Main Menu Items Here
69     elm_list_item_append(list, "Resource Container", NULL, NULL, containerCreateUI, nf);
70
71     elm_list_go(list);
72
73     // This button is set for devices which doesn't have H/W back key.
74     btn = elm_button_add(nf);
75     elm_object_style_set(btn, "naviframe/end_btn/default");
76     nf_it = elm_naviframe_item_push(nf, "Resource Encapsulation", btn, NULL, list, NULL);
77     elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, ad->win);
78 }
79
80
81 static void
82 create_base_gui(appdata_s *ad)
83 {
84     // Window
85     ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
86     elm_win_conformant_set(ad->win, EINA_TRUE);
87     elm_win_autodel_set(ad->win, EINA_TRUE);
88
89     if (elm_win_wm_rotation_supported_get(ad->win))
90     {
91         int rots[4] = { 0, 90, 180, 270 };
92         elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
93     }
94
95     evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
96
97     // Conformant
98     ad->conform = elm_conformant_add(ad->win);
99     evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
100     elm_win_resize_object_add(ad->win, ad->conform);
101     evas_object_show(ad->conform);
102
103     // Base Layout
104     ad->layout = elm_layout_add(ad->conform);
105     evas_object_size_hint_weight_set(ad->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
106     elm_layout_theme_set(ad->layout, "layout", "application", "default");
107     evas_object_show(ad->layout);
108
109     elm_object_content_set(ad->conform, ad->layout);
110
111     // Naviframe
112     ad->nf = elm_naviframe_add(ad->layout);
113     create_list_view(ad);
114     elm_object_part_content_set(ad->layout, "elm.swallow.content", ad->nf);
115     eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_BACK, eext_naviframe_back_cb, NULL);
116     eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_MORE, eext_naviframe_more_cb, NULL);
117
118     // Show window after base gui is set up
119     evas_object_show(ad->win);
120 }
121
122 // Configures the OCPlatform
123 static void
124 configure_platform()
125 {
126     try
127     {
128         PlatformConfig config
129         { OC::ServiceType::InProc, ModeType::Server, "0.0.0.0", 0, OC::QualityOfService::LowQos };
130
131         OCPlatform::Configure(config);
132
133         dlog_print(DLOG_INFO, LOG_TAG, "#### Platform configuration done!!!!");
134     }
135     catch (OCException &e)
136     {
137         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
138     }
139 }
140
141 static bool
142 app_create(void *data)
143 {
144     /* Hook to take necessary actions before main event loop starts
145        Initialize UI resources and application's data
146        If this function returns true, the main loop of application starts
147        If this function returns false, the application is terminated */
148     appdata_s *ad = (appdata_s *)data;
149
150     elm_app_base_scale_set(1.8);
151
152     create_base_gui(ad);
153
154     configure_platform();
155
156     return true;
157 }
158
159 static void
160 app_control(app_control_h app_control, void *data)
161 {
162     // Handle the launch request.
163 }
164
165 static void
166 app_pause(void *data)
167 {
168     // Take necessary actions when application becomes invisible.
169 }
170
171 static void
172 app_resume(void *data)
173 {
174     // Take necessary actions when application becomes visible.
175 }
176
177 static void
178 app_terminate(void *data)
179 {
180     // Release all resources.
181 }
182
183 static void
184 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
185 {
186     // APP_EVENT_LANGUAGE_CHANGED
187     char *locale = NULL;
188     system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
189     elm_language_set(locale);
190     free(locale);
191     return;
192 }
193
194 static void
195 ui_app_orient_changed(app_event_info_h event_info, void *user_data)
196 {
197     // APP_EVENT_DEVICE_ORIENTATION_CHANGED
198     return;
199 }
200
201 static void
202 ui_app_region_changed(app_event_info_h event_info, void *user_data)
203 {
204     // APP_EVENT_REGION_FORMAT_CHANGED
205 }
206
207 static void
208 ui_app_low_battery(app_event_info_h event_info, void *user_data)
209 {
210     // APP_EVENT_LOW_BATTERY
211 }
212
213 static void
214 ui_app_low_memory(app_event_info_h event_info, void *user_data)
215 {
216     // APP_EVENT_LOW_MEMORY
217 }
218
219 int
220 main(int argc, char *argv[])
221 {
222     appdata_s ad = {0,};
223     int ret = 0;
224
225     ui_app_lifecycle_callback_s event_callback = {0,};
226     app_event_handler_h handlers[5] = {NULL, };
227
228     event_callback.create = app_create;
229     event_callback.terminate = app_terminate;
230     event_callback.pause = app_pause;
231     event_callback.resume = app_resume;
232     event_callback.app_control = app_control;
233
234     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY,
235                              ui_app_low_battery, &ad);
236     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY,
237                              ui_app_low_memory, &ad);
238     ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED],
239                              APP_EVENT_DEVICE_ORIENTATION_CHANGED,
240                              ui_app_orient_changed, &ad);
241     ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
242                              APP_EVENT_LANGUAGE_CHANGED,
243                              ui_app_lang_changed, &ad);
244     ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
245                              APP_EVENT_REGION_FORMAT_CHANGED,
246                              ui_app_region_changed, &ad);
247     ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
248
249     ret = ui_app_main(argc, argv, &event_callback, &ad);
250     if (APP_ERROR_NONE != ret)
251     {
252         dlog_print(DLOG_ERROR, LOG_TAG, "app_main() is failed. err = %d", ret);
253     }
254     return ret;
255 }