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 *box = NULL;
195 Evas_Object *label = NULL;
196 Evas_Object *grant_button = NULL;
197 Evas_Object *deny_button = NULL;
198 pdp->per_session = EINA_FALSE;
199 pdp->always = EINA_FALSE;
201 win = elm_win_add(NULL, "The wrt-client popup", ELM_WIN_NOTIFICATION);
202 elm_win_autodel_set(win, EINA_TRUE);
203 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
204 evas_object_show(win);
205 elm_win_indicator_opacity_set(win, ELM_WIN_INDICATOR_TRANSLUCENT);
207 pdp->popup = elm_popup_add(win);
209 box = elm_box_add(pdp->popup);
210 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, 0);
211 evas_object_size_hint_align_set(box, EVAS_HINT_FILL, 0.0);
213 label = elm_label_add(pdp->popup);
214 elm_object_style_set(label, "popup_description/default");
215 elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
218 asprintf(&buff, "Application need an access to %s.<br>Grant or deny?",
221 LogError("asprintf failed - returned -1");
222 evas_object_del(label);
223 evas_object_del(pdp->popup);
224 evas_object_del(win);
227 elm_object_text_set(label, buff);
229 evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
230 evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
231 evas_object_show(label);
232 elm_box_pack_end(box, label);
234 LogDebug("popup_type == " << pdp->popup_type);
235 if (pdp->popup_type == ACE_SESSION || pdp->popup_type == ACE_BLANKET) {
236 LogDebug("popup_type == ACE_SESSION || ACE_BLANKET");
237 Evas_Object *cb_session = elm_check_add(pdp->popup);
238 elm_object_text_set(cb_session, "Remember choice for this session");
239 elm_check_state_pointer_set(cb_session, &(pdp->per_session));
240 evas_object_smart_callback_add(cb_session, "changed", NULL, NULL);
241 evas_object_size_hint_align_set(cb_session,
244 evas_object_size_hint_weight_set(cb_session,
247 evas_object_show(cb_session);
248 elm_box_pack_end(box, cb_session);
250 if (pdp->popup_type == ACE_BLANKET) {
251 LogDebug("popup_type == ACE_BLANKET");
252 Evas_Object *cb_always = elm_check_add(pdp->popup);
253 elm_object_text_set(cb_always, "Remember choice forever");
254 elm_check_state_pointer_set(cb_always, &(pdp->always));
255 evas_object_smart_callback_add(cb_always, "changed", NULL, NULL);
256 evas_object_size_hint_align_set(cb_always,
259 evas_object_size_hint_weight_set(cb_always,
262 evas_object_show(cb_always);
263 elm_box_pack_end(box, cb_always);
267 elm_object_part_content_set(pdp->popup, "default", box);
269 grant_button = elm_button_add(pdp->popup);
270 elm_object_text_set(grant_button, "Grant");
271 elm_object_part_content_set(pdp->popup, "button1", grant_button);
272 evas_object_smart_callback_add(grant_button, "clicked", grant_answer, pdp);
274 deny_button = elm_button_add(pdp->popup);
275 elm_object_text_set(deny_button, "Deny");
276 elm_object_part_content_set(pdp->popup, "button2", deny_button);
277 evas_object_smart_callback_add(deny_button, "clicked", deny_answer, pdp);
279 evas_object_show(pdp->popup);
281 // Showing the popup window
282 evas_object_show(win);
284 // Run the efl mainloop
290 elm_main(int argc, char ** argv)
292 UNHANDLED_EXCEPTION_HANDLER_BEGIN
294 // int pipe_in and int pipe_out should be passed to Popup via args.
296 // These parameters should be passed to Popup via pipe:
297 // ace_popup_t popup_type
298 // const ace_resource_t resource_name
299 // const ace_session_id_t session_id
300 // ace_widget_handle_t handle
301 // const ace_param_list_t param_list
304 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-POPUP-BIN");
305 } Catch(DPL::Exception) {
307 return ACE_INTERNAL_ERROR;
310 "############################ popup binary ################################");
314 "To few args passed in exec to popup-bin - should be at least 3:");
315 LogError("(binary-name, pipe_in, pipe_out)");
316 LogError("return ACE_INTERNAL_ERROR");
317 return ACE_INTERNAL_ERROR;
319 LogDebug("Passed args: " << argv[0] << ", " << argv[1] << ", " <<
324 std::stringstream parsing_stream;
326 // Parsing args (pipe_in, pipe_out)
327 parsing_stream.str(argv[1]);
328 parsing_stream >> pipe_in;
329 if ( parsing_stream.fail() ) {
330 LogError("Error while parsing pipe_in; return ACE_INTERNAL_ERROR");
331 return ACE_INTERNAL_ERROR;
333 parsing_stream.clear();
334 parsing_stream.str(argv[2]);
335 parsing_stream >> pipe_out;
336 if ( parsing_stream.fail() ) {
337 LogError("Error while parsing pipe_out; return ACE_INTERNAL_ERROR");
338 return ACE_INTERNAL_ERROR;
340 parsing_stream.clear();
341 LogDebug("Parsed pipes: IN: " << pipe_in << ", OUT: " << pipe_out);
343 int buff_size = 1024;
344 char line[buff_size];
346 struct ace_popup_data pd;
347 struct ace_popup_data *pdp = &pd;
353 time(&time_start); // now
354 double timeout = 10.0;
355 // try to read parameters from pipe_in
356 // timeout is set for 10 seconds
358 count = TEMP_FAILURE_RETRY(read(pipe_in, line, buff_size));
359 if (timeout < difftime(time(NULL), time_start) ) {
360 LogError("Timeout reached! Exit popup - ACE_INTERNAL_ERROR");
363 return ACE_INTERNAL_ERROR;
365 } while (0 == count);
369 LogError("read returned a negative value (" << count << ")");
370 LogError("errno: " << strerror(errno) );
371 LogError("Exit popup - ACE_INTERNAL_ERROR");
372 return ACE_INTERNAL_ERROR;
374 LogDebug("Read bytes : " << count << " (in " <<
375 difftime(time_start, time(NULL)) << " seconds)");
376 close(pipe_in); // cleanup
378 Wrt::Popup::BinaryStream stream;
379 stream.Write(count, static_cast <void *>(line));
380 int popup_type = ACE_ONESHOT;
381 std::string resource_name_str;
382 std::string session_id_str;
385 LogDebug("------- Deserialization -------");
386 // Deserialization order:
387 // popup_type, resource_name, session_id, handle, param_list
389 DPL::Deserialization::Deserialize(stream, popup_type);
390 LogDebug("popup_type_int : " << popup_type);
391 pdp->popup_type = static_cast <ace_popup_t>(popup_type);
393 DPL::Deserialization::Deserialize(stream, resource_name_str);
394 LogDebug("resource_name_char : " << resource_name_str.c_str());
396 strdup(const_cast <ace_resource_t>(resource_name_str.c_str()));
398 DPL::Deserialization::Deserialize(stream, session_id_str);
399 LogDebug("session_id_char : " << session_id_str.c_str());
401 strdup(const_cast <ace_session_id_t>(session_id_str.c_str()));
403 DPL::Deserialization::Deserialize(stream, handle);
404 LogDebug("handle_int : " << handle);
405 pdp->handle = static_cast <ace_widget_handle_t>(handle);
407 if (_ace_params_deserializer(&(pdp->param_list), &stream)) {
408 return ACE_INTERNAL_ERROR;
411 pdp->per_session = EINA_FALSE;
412 pdp->always = EINA_FALSE;
413 pdp->validation_return = ACE_ACE_UNKNOWN_ERROR;
415 show_popup(pdp); // Showing popup
417 // sending validation_result to popup-runner
418 Wrt::Popup::BinaryStream stream_out;
420 LogDebug("pdp->validation_result : " << pdp->validation_result);
421 int validation_result_int = (int) pdp->validation_result;
422 LogDebug("validation_result_int : " << validation_result_int);
423 DPL::Serialization::Serialize(stream_out, validation_result_int);
425 TEMP_FAILURE_RETRY(write(pipe_out, stream_out.char_pointer(),
428 LogError("Write to pipe failed!");
430 return ACE_INTERNAL_ERROR;
434 if (pdp->validation_return == ACE_OK) {
436 } else if (pdp->validation_return == ACE_INVALID_ARGUMENTS) {
437 LogError("ACE_INVALID_ARGUMENTS");
438 } else if (pdp->validation_return == ACE_INTERNAL_ERROR) {
439 LogError("ACE_INTERNAL_ERROR");
440 } else if (pdp->validation_return == ACE_ACE_UNKNOWN_ERROR) {
441 LogError("ACE_ACE_UNKNOWN_ERROR");
443 LogError("Really unknown error!!!");
447 "############################ /popup binary ################################");
449 // Shutdown elementary
450 //LogDebug("elm_shutdown()");
452 // This is commented because, it causes that popup exits with 139 code
453 // (Segmentatation violation).
454 // Not calling elm_shutdown() should not have any negative consequences
455 // because this binary ends
456 // in next line, and system should clear the memory.
458 _ace_params_finalize(&(pdp->param_list));
459 return pdp->validation_return;
461 UNHANDLED_EXCEPTION_HANDLER_END