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