2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Janusz Kozerski (j.kozerski@samsung.com)
23 * This is just a example pop-up that shows how to use a new C-API in
25 * This pop-up should be re-implemented by members of NGWAP.
35 #include <Elementary.h>
37 #include <dpl/log/log.h>
38 #include <dpl/serialization.h>
39 #include <ace_api_common.h>
40 #include <ace_api_popup_validation.h>
42 #include "popup-runner.h"
45 namespace { // anonymous
46 static void set_validity_from_checkbox(struct ace_popup_data *pdp)
49 LogError("pdp is NULL; return");
54 LogDebug("Remember ALWAYS");
55 pdp->remember_choice = ACE_ALWAYS;
58 if (pdp->per_session) {
59 LogDebug("Remember PER_SESSION");
60 pdp->remember_choice = ACE_PER_SESSION;
63 LogDebug("Remember ONCE");
64 pdp->remember_choice = ACE_ONCE;
68 static void on_done(void)
70 // Quit the efl-mainloop
71 LogDebug("elm_exit()");
75 static void grant_answer(void *data,
76 Evas_Object * /* obj */,
77 void * /* event_info */)
79 LogDebug("grant_answer");
81 LogError("data is NULL; return");
84 struct ace_popup_data *pdp = static_cast <struct ace_popup_data *>(data);
86 set_validity_from_checkbox(pdp);
88 ace_popup_validation_initialize();
89 pdp->validation_return = ace_validate_answer(
96 &(pdp->validation_result));
97 ace_popup_validation_shutdown();
102 static void deny_answer(void *data,
103 Evas_Object * /* obj */,
104 void * /* event_info */)
106 LogDebug("deny_answer");
108 LogError("data is NULL; return");
111 struct ace_popup_data *pdp = static_cast <struct ace_popup_data *>(data);
113 set_validity_from_checkbox(pdp);
115 ace_popup_validation_initialize();
116 pdp->validation_return = ace_validate_answer(
118 pdp->remember_choice,
123 &(pdp->validation_result));
124 ace_popup_validation_shutdown();
129 static int _ace_params_deserializer(ace_param_list_t* ace_param_list,
130 Wrt::Popup::BinaryStream *stream)
132 LogDebug("_ace_params_deserializer");
134 if (NULL == ace_param_list || NULL == stream) {
135 LogError("ace_param_list or stream is NULL; return -1");
139 // deserialize ace_param_list->count;
141 DPL::Deserialization::Deserialize(*stream, count);
142 ace_param_list->count = count;
143 LogDebug("count : " << count);
148 ace_param_list->items = NULL;
151 ace_param_list->items =
152 static_cast <ace_param_t *>(malloc(count * sizeof(ace_param_t)));
153 memset(ace_param_list->items, 0, count * sizeof(ace_param_t));
155 for (size_t i = 0; i < count; ++i) {
156 DPL::Deserialization::Deserialize(*stream, name);
157 ace_param_list->items[i].name =
158 strdup(const_cast <char *>(name.c_str()));
160 DPL::Deserialization::Deserialize(*stream, value);
161 ace_param_list->items[i].value =
162 strdup(const_cast <char *>(value.c_str()));
167 static void _ace_params_finalize(ace_param_list_t* ace_param_list)
169 if (!ace_param_list || !ace_param_list->items) {
170 LogDebug("List is null, nothing to do");
173 for (size_t i = 0; i < ace_param_list->count; ++i) {
174 free(ace_param_list->items[i].name);
175 free(ace_param_list->items[i].value);
177 free(ace_param_list->items);
178 ace_param_list->items = NULL;
179 ace_param_list->count = 0;
182 static void show_popup(struct ace_popup_data *pdp)
184 LogDebug("show_popup()");
187 LogError("pdp is NULL; return");
191 const char *resource_type = static_cast <char *>(pdp->resource_name);
193 Evas_Object *win = NULL;
194 Evas_Object *cb_session = NULL;
195 Evas_Object *cb_always = NULL;
196 Evas_Object *box = NULL;
197 Evas_Object *label = NULL;
198 Evas_Object *grant_button = NULL;
199 Evas_Object *deny_button = NULL;
200 pdp->per_session = EINA_FALSE;
201 pdp->always = EINA_FALSE;
203 win = elm_win_add(NULL, "The wrt-client popup", ELM_WIN_NOTIFICATION);
204 elm_win_autodel_set(win, EINA_TRUE);
205 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
206 evas_object_show(win);
207 elm_win_indicator_opacity_set(win, ELM_WIN_INDICATOR_TRANSLUCENT);
209 pdp->popup = elm_popup_add(win);
211 box = elm_box_add(pdp->popup);
212 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, 0);
213 evas_object_size_hint_align_set(box, EVAS_HINT_FILL, 0.0);
215 label = elm_label_add(pdp->popup);
216 elm_object_style_set(label, "popup_description/default");
217 elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
220 asprintf(&buff, "Application need an access to %s.<br>Grant or deny?",
223 LogError("asprintf failed - returned -1");
224 evas_object_del(label);
225 evas_object_del(pdp->popup);
226 evas_object_del(win);
229 elm_object_text_set(label, buff);
231 evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
232 evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
233 evas_object_show(label);
234 elm_box_pack_end(box, label);
236 LogDebug("popup_type == " << pdp->popup_type);
237 if (pdp->popup_type == ACE_SESSION || pdp->popup_type == ACE_BLANKET) {
238 LogDebug("popup_type == ACE_SESSION || ACE_BLANKET");
239 cb_session = elm_check_add(pdp->popup);
240 elm_object_text_set(cb_session, "Remember choice for this session");
241 elm_check_state_pointer_set(cb_session, &(pdp->per_session));
242 evas_object_smart_callback_add(cb_session, "changed", NULL, NULL);
243 evas_object_size_hint_align_set(cb_session,
246 evas_object_size_hint_weight_set(cb_session,
249 evas_object_show(cb_session);
250 elm_box_pack_end(box, cb_session);
252 if (pdp->popup_type == ACE_BLANKET) {
253 LogDebug("popup_type == ACE_BLANKET");
254 cb_always = elm_check_add(pdp->popup);
255 elm_object_text_set(cb_always, "Remember choice forever");
256 elm_check_state_pointer_set(cb_always, &(pdp->always));
257 evas_object_smart_callback_add(cb_always, "changed", NULL, NULL);
258 evas_object_size_hint_align_set(cb_always,
261 evas_object_size_hint_weight_set(cb_always,
264 evas_object_show(cb_always);
265 elm_box_pack_end(box, cb_always);
269 elm_object_part_content_set(pdp->popup, "default", box);
271 grant_button = elm_button_add(pdp->popup);
272 elm_object_text_set(grant_button, "Grant");
273 elm_object_part_content_set(pdp->popup, "button1", grant_button);
274 evas_object_smart_callback_add(grant_button, "clicked", grant_answer, pdp);
276 deny_button = elm_button_add(pdp->popup);
277 elm_object_text_set(deny_button, "Deny");
278 elm_object_part_content_set(pdp->popup, "button2", deny_button);
279 evas_object_smart_callback_add(deny_button, "clicked", deny_answer, pdp);
281 evas_object_show(pdp->popup);
283 // Showing the popup window
284 evas_object_show(win);
286 // Run the efl mainloop
292 elm_main(int argc, char ** argv)
294 UNHANDLED_EXCEPTION_HANDLER_BEGIN
296 // int pipe_in and int pipe_out should be passed to Popup via args.
298 // These parameters should be passed to Popup via pipe:
299 // ace_popup_t popup_type
300 // const ace_resource_t resource_name
301 // const ace_session_id_t session_id
302 // ace_widget_handle_t handle
303 // const ace_param_list_t param_list
306 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-POPUP-BIN");
307 } Catch(DPL::Exception) {
309 return ACE_INTERNAL_ERROR;
312 "############################ popup binary ################################");
316 "To few args passed in exec to popup-bin - should be at least 3:");
317 LogError("(binary-name, pipe_in, pipe_out)");
318 LogError("return ACE_INTERNAL_ERROR");
319 return ACE_INTERNAL_ERROR;
321 LogDebug("Passed args: " << argv[0] << ", " << argv[1] << ", " <<
327 // Parsing args (pipe_in, pipe_out)
328 if (0 == sscanf(argv[1], "%d", &pipe_in) ) {
329 LogError("Error while parsing pipe_in; return ACE_INTERNAL_ERROR");
330 return ACE_INTERNAL_ERROR;
332 if (0 == sscanf(argv[2], "%d", &pipe_out) ) {
333 LogError("Error while parsing pipe_out; return ACE_INTERNAL_ERROR");
334 return ACE_INTERNAL_ERROR;
336 LogDebug("Parsed pipes: IN: " << pipe_in << ", OUT: " << pipe_out);
338 int buff_size = 1024;
339 char line[buff_size];
341 struct ace_popup_data pd;
342 struct ace_popup_data *pdp = &pd;
348 time(&time_start); // now
349 double timeout = 10.0;
350 // try to read parameters from pipe_in
351 // timeout is set for 10 seconds
353 count = TEMP_FAILURE_RETRY(read(pipe_in, line, buff_size));
354 if (timeout < difftime(time(NULL), time_start) ) {
355 LogError("Timeout reached! Exit popup - ACE_INTERNAL_ERROR");
358 return ACE_INTERNAL_ERROR;
360 } while (0 == count);
364 LogError("read returned a negative value (" << count << ")");
365 LogError("errno: " << strerror(errno) );
366 LogError("Exit popup - ACE_INTERNAL_ERROR");
367 return ACE_INTERNAL_ERROR;
369 LogDebug("Read bytes : " << count << " (in " <<
370 difftime(time_start, time(NULL)) << " seconds)");
371 close(pipe_in); // cleanup
373 Wrt::Popup::BinaryStream stream;
374 stream.Write(count, static_cast <void *>(line));
375 int popup_type = ACE_ONESHOT;
376 std::string resource_name_str;
377 std::string session_id_str;
380 LogDebug("------- Deserialization -------");
381 // Deserialization order:
382 // popup_type, resource_name, session_id, handle, param_list
384 DPL::Deserialization::Deserialize(stream, popup_type);
385 LogDebug("popup_type_int : " << popup_type);
386 pdp->popup_type = static_cast <ace_popup_t>(popup_type);
388 DPL::Deserialization::Deserialize(stream, resource_name_str);
389 LogDebug("resource_name_char : " << resource_name_str.c_str());
391 strdup(const_cast <ace_resource_t>(resource_name_str.c_str()));
393 DPL::Deserialization::Deserialize(stream, session_id_str);
394 LogDebug("session_id_char : " << session_id_str.c_str());
396 strdup(const_cast <ace_session_id_t>(session_id_str.c_str()));
398 DPL::Deserialization::Deserialize(stream, handle);
399 LogDebug("handle_int : " << handle);
400 pdp->handle = static_cast <ace_widget_handle_t>(handle);
402 if (_ace_params_deserializer(&(pdp->param_list), &stream)) {
403 return ACE_INTERNAL_ERROR;
406 pdp->per_session = EINA_FALSE;
407 pdp->always = EINA_FALSE;
408 pdp->validation_return = ACE_ACE_UNKNOWN_ERROR;
410 show_popup(pdp); // Showing popup
412 // sending validation_result to popup-runner
413 Wrt::Popup::BinaryStream stream_out;
415 LogDebug("pdp->validation_result : " << pdp->validation_result);
416 int validation_result_int = (int) pdp->validation_result;
417 LogDebug("validation_result_int : " << validation_result_int);
418 DPL::Serialization::Serialize(stream_out, validation_result_int);
420 TEMP_FAILURE_RETRY(write(pipe_out, stream_out.char_pointer(),
423 LogError("Write to pipe failed!");
425 return ACE_INTERNAL_ERROR;
429 if (pdp->validation_return == ACE_OK) {
431 } else if (pdp->validation_return == ACE_INVALID_ARGUMENTS) {
432 LogError("ACE_INVALID_ARGUMENTS");
433 } else if (pdp->validation_return == ACE_INTERNAL_ERROR) {
434 LogError("ACE_INTERNAL_ERROR");
435 } else if (pdp->validation_return == ACE_ACE_UNKNOWN_ERROR) {
436 LogError("ACE_ACE_UNKNOWN_ERROR");
438 LogError("Really unknown error!!!");
442 "############################ /popup binary ################################");
444 // Shutdown elementary
445 //LogDebug("elm_shutdown()");
447 // This is commented because, it causes that popup exits with 139 code
448 // (Segmentatation violation).
449 // Not calling elm_shutdown() should not have any negative consequences
450 // because this binary ends
451 // in next line, and system should clear the memory.
453 _ace_params_finalize(&(pdp->param_list));
454 return pdp->validation_return;
456 UNHANDLED_EXCEPTION_HANDLER_END