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