Imported Upstream version 1.0.1
[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     elm_list_go(list);
69
70     // This button is set for devices which doesn't have H/W back key.
71     btn = elm_button_add(nf);
72     elm_object_style_set(btn, "naviframe/end_btn/default");
73     nf_it = elm_naviframe_item_push(nf, "Container Server", btn, NULL, list, NULL);
74     elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, ad->win);
75
76     containerCreateUI(nf);
77 }
78
79
80 static void
81 create_base_gui(appdata_s *ad)
82 {
83     // Window
84     ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
85     elm_win_conformant_set(ad->win, EINA_TRUE);
86     elm_win_autodel_set(ad->win, EINA_TRUE);
87
88     if (elm_win_wm_rotation_supported_get(ad->win))
89     {
90         int rots[4] = { 0, 90, 180, 270 };
91         elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
92     }
93
94     evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
95
96     // Conformant
97     ad->conform = elm_conformant_add(ad->win);
98     evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
99     elm_win_resize_object_add(ad->win, ad->conform);
100     evas_object_show(ad->conform);
101
102     // Base Layout
103     ad->layout = elm_layout_add(ad->conform);
104     evas_object_size_hint_weight_set(ad->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
105     elm_layout_theme_set(ad->layout, "layout", "application", "default");
106     evas_object_show(ad->layout);
107
108     elm_object_content_set(ad->conform, ad->layout);
109
110     // Naviframe
111     ad->nf = elm_naviframe_add(ad->layout);
112     create_list_view(ad);
113     elm_object_part_content_set(ad->layout, "elm.swallow.content", ad->nf);
114     eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_BACK, eext_naviframe_back_cb, NULL);
115     eext_object_event_callback_add(ad->nf, EEXT_CALLBACK_MORE, eext_naviframe_more_cb, NULL);
116
117     // Show window after base gui is set up
118     evas_object_show(ad->win);
119 }
120
121 // Configures the OCPlatform
122 static void
123 configure_platform()
124 {
125     try
126     {
127         PlatformConfig config
128         { OC::ServiceType::InProc, ModeType::Server, "0.0.0.0", 0, OC::QualityOfService::LowQos };
129
130         OCPlatform::Configure(config);
131
132         dlog_print(DLOG_INFO, LOG_TAG, "#### Platform configuration done!!!!");
133     }
134     catch (OCException &e)
135     {
136         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
137     }
138 }
139
140 static bool
141 app_create(void *data)
142 {
143     /* Hook to take necessary actions before main event loop starts
144        Initialize UI resources and application's data
145        If this function returns true, the main loop of application starts
146        If this function returns false, the application is terminated */
147     appdata_s *ad = (appdata_s *)data;
148
149     elm_app_base_scale_set(1.8);
150
151     create_base_gui(ad);
152
153     configure_platform();
154
155     return true;
156 }
157
158 static void
159 app_control(app_control_h app_control, void *data)
160 {
161     // Handle the launch request.
162 }
163
164 static void
165 app_pause(void *data)
166 {
167     // Take necessary actions when application becomes invisible.
168 }
169
170 static void
171 app_resume(void *data)
172 {
173     // Take necessary actions when application becomes visible.
174 }
175
176 static void
177 app_terminate(void *data)
178 {
179     // Release all resources.
180 }
181
182 static void
183 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
184 {
185     // APP_EVENT_LANGUAGE_CHANGED
186     char *locale = NULL;
187     system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
188     elm_language_set(locale);
189     free(locale);
190     return;
191 }
192
193 static void
194 ui_app_orient_changed(app_event_info_h event_info, void *user_data)
195 {
196     // APP_EVENT_DEVICE_ORIENTATION_CHANGED
197     return;
198 }
199
200 static void
201 ui_app_region_changed(app_event_info_h event_info, void *user_data)
202 {
203     // APP_EVENT_REGION_FORMAT_CHANGED
204 }
205
206 static void
207 ui_app_low_battery(app_event_info_h event_info, void *user_data)
208 {
209     // APP_EVENT_LOW_BATTERY
210 }
211
212 static void
213 ui_app_low_memory(app_event_info_h event_info, void *user_data)
214 {
215     // APP_EVENT_LOW_MEMORY
216 }
217
218 int
219 main(int argc, char *argv[])
220 {
221     appdata_s ad = {0,};
222     int ret = 0;
223
224     ui_app_lifecycle_callback_s event_callback = {0,};
225     app_event_handler_h handlers[5] = {NULL, };
226
227     event_callback.create = app_create;
228     event_callback.terminate = app_terminate;
229     event_callback.pause = app_pause;
230     event_callback.resume = app_resume;
231     event_callback.app_control = app_control;
232
233     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY,
234                              ui_app_low_battery, &ad);
235     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY,
236                              ui_app_low_memory, &ad);
237     ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED],
238                              APP_EVENT_DEVICE_ORIENTATION_CHANGED,
239                              ui_app_orient_changed, &ad);
240     ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
241                              APP_EVENT_LANGUAGE_CHANGED,
242                              ui_app_lang_changed, &ad);
243     ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
244                              APP_EVENT_REGION_FORMAT_CHANGED,
245                              ui_app_region_changed, &ad);
246     ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
247
248     ret = ui_app_main(argc, argv, &event_callback, &ad);
249     if (APP_ERROR_NONE != ret)
250     {
251         dlog_print(DLOG_ERROR, LOG_TAG, "app_main() is failed. err = %d", ret);
252     }
253     return ret;
254 }