1. Changed license year
[apps/home/mobileprint.git] / mobileprint / app / pts_print_search.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <fcntl.h>
21 #include <glib.h>
22 #include <app.h>
23 #include <appsvc.h>
24 #include <ui-gadget.h>
25 #include <notification.h>
26 #include <vconf.h>
27 #include <vconf-keys.h>
28
29 #include <paper_size.h>
30 #include <previewgen.h>
31 #include "pt_api.h"
32
33 #include "mobileprint.h"
34 #include "pts_common.h"
35 #include "pts_main_view.h"
36 #include "pts_setting_view.h"
37 #include "preview_content.h"
38 #include "preview_util.h"
39
40 static char *WiFi_Type_String[] = {
41         "Wi-Fi", "Wi-Fi Direct"
42 };
43
44 typedef struct {
45         pt_printer_mgr_t *pt;
46         int radio_index;
47         Evas_Object *radio_group;
48 } radio_print_list_data_t;
49
50 static Elm_Genlist_Item_Class list_itc;
51 //static Elm_Genlist_Item_Class refresh_itc;
52 static Elm_Genlist_Item_Class wifi_list_itc;
53
54 static Evas_Object *printer_list_radio_main = NULL;
55
56 void  pt_post_print_search(pts_appdata_t *ad);
57 Evas_Object *pts_create_no_found_popup(Evas_Object *parent, void *refresh_cb, void *cancel_cb);
58
59 static char *_pts_wifi_text_get(void *data, Evas_Object *obj, const char *part)
60 {
61         int index = (int)data;
62
63         if (!strcmp(part, "elm.text")) {
64                 return strdup(WiFi_Type_String[index]);
65         } else {
66                 return strdup("Unknown index");
67         }
68 }
69
70 static void _wifi_direct_ug_layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
71 {
72         PTS_TRACE_BEGIN;
73         PTS_RET_IF(ug == NULL, "ug is NULL");
74
75         Evas_Object *base, *win;
76
77         base = ug_get_layout(ug);
78         if (!base) {
79                 ug_destroy(ug);
80                 return;
81         }
82
83         win = ug_get_window();
84
85         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
86         elm_win_resize_object_add(win, base);
87
88         ug_disable_effect(ug);
89         evas_object_show(base);
90         PTS_TRACE_END;
91 }
92
93 static void _wifi_direct_ug_result_cb(ui_gadget_h ug, service_h result, void *priv)
94 {
95         PTS_TRACE_BEGIN;
96         pts_appdata_t *ad = (pts_appdata_t *)priv;
97         PTS_RET_IF(ad == NULL, "ad is NULL");
98
99         char *ip_addr = NULL;
100         service_get_extra_data(result, "ip_address", &ip_addr);
101         if (ip_addr) {
102                 /* Check IP address of connected peer */
103                 // TODO - Add active printer set routine to CUPS using this address
104                 PTS_DEBUG("IP address of WiFi Direct Printer : %s", ip_addr);
105                 PTS_IF_FREE_MEM(ad->search_info.ip_address);
106                 ad->search_info.ip_address = strdup(ip_addr);
107                 PTS_RET_IF(ad->search_info.ip_address == NULL, "ad->search_info.ip_address is NULL");
108         } else {
109                 PTS_DEBUG("Not found ip_address");
110         }
111
112         PTS_TRACE_END;
113 }
114
115 static void _wifi_direct_ug_destroy_cb(ui_gadget_h ug, void *priv)
116 {
117         PTS_TRACE_BEGIN;
118         pts_appdata_t *ad = (pts_appdata_t *)priv;
119         PTS_RET_IF(ug == NULL || ad == NULL, "Invalid argument");
120
121         pts_search_printer(ad);
122
123         PTS_TRACE_END;
124 }
125
126 static int pts_launch_wifi_qs(void)
127 {
128         PTS_TRACE_BEGIN;
129         int tpid = 0;
130         int ret = 0;
131
132         tpid = sysman_get_pid("wifi-qs");
133         if (tpid > 0) {
134                 PTS_DEBUG("wifi-qs existed !\n");
135                 ret = -1;
136         } else {
137                 bundle *b = NULL;
138                 b = bundle_create();
139                 if (b == NULL) {
140                         ret = -1;
141                 } else {
142                         ret = syspopup_launch("wifi-qs", b);
143                         if (ret < 0) {
144                                 PTS_DEBUG("syspopup is failed. [%d]\n", ret);
145                         }
146
147                         bundle_free(b);
148                 }
149         }
150
151         PTS_TRACE_END;
152         return ret;
153 }
154
155 void _wifi_qs_monitoring_cb(keynode_t *key, void *data)
156 {
157         PTS_TRACE_BEGIN;
158         int wifi_qs_exit = -1;
159         int error_code = vconf_get_int(VCONFKEY_WIFI_QS_EXIT, &wifi_qs_exit);
160
161         PTS_RET_IF(error_code == -1, "Error when check wifi qs exit status");
162
163         vconf_ignore_key_changed(VCONFKEY_WIFI_QS_EXIT, _wifi_qs_monitoring_cb);
164
165         pts_appdata_t *ad = (pts_appdata_t *)data;
166
167         pts_search_printer(ad);
168         PTS_TRACE_END;
169 }
170
171 static void pts_launch_wifi_direct_ug(pts_appdata_t *ad)
172 {
173         PTS_TRACE_BEGIN;
174
175         int ret = 0;
176         struct ug_cbs cbs = {0, };
177         service_h service = NULL;
178
179         memset(&cbs, 0x00, sizeof(struct ug_cbs));
180         cbs.layout_cb = _wifi_direct_ug_layout_cb;
181         cbs.result_cb = _wifi_direct_ug_result_cb;
182         cbs.destroy_cb = _wifi_direct_ug_destroy_cb;
183         cbs.priv = ad;
184
185         service_create(&service);
186         ret = service_add_extra_data(service, "device_filter", "printer");
187         if (ret != SERVICE_ERROR_NONE) { goto ERROR; }
188         ret= service_add_extra_data(service, "auto_exit", "on");
189         if (ret != SERVICE_ERROR_NONE) { goto ERROR; }
190         ret = service_add_extra_data(service, "multi_connect", "off");
191         if (ret != SERVICE_ERROR_NONE) { goto ERROR; }
192         ret= service_add_extra_data(service, "title_string", _("IDS_COM_OPT1_WI_FI_DIRECT"));
193         if (ret != SERVICE_ERROR_NONE) { goto ERROR; }
194
195         ug_create(NULL, "setting-wifidirect-efl", UG_MODE_FULLVIEW, service, &cbs);
196         service_destroy(service);
197
198         PTS_TRACE_END;
199         return ret;
200
201 ERROR:
202         PTS_DEBUG("Failed to create wifi direct ug(%d)", ret);
203         service_destroy(service);
204         PTS_TRACE_END;
205         return ret;
206
207 }
208
209 static void _pts_wifi_list_sel(void *data, Evas_Object *obj, void *event_info)
210 {
211         PTS_TRACE_BEGIN;
212         pts_appdata_t *ad = pts_get_appdata();
213         int index = 0;
214         int ret = -1;
215         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
216
217         if (item == NULL) {
218                 PTS_IF_DEL_OBJ(ad->select_info.wifi_type_popup);
219                 return;
220         }
221
222         index = (int)elm_object_item_data_get(item);
223         if (index == 0) { // Wi-Fi
224                 ret = pts_launch_wifi_qs();
225                 if (ret > 0) {
226                         // Currently ret value is procces id of wifi_qs.
227                         PTS_DEBUG("Now register vocnf cb for VCONFKEY_WIFI_QS_EXIT");
228                         vconf_notify_key_changed(VCONFKEY_WIFI_QS_EXIT, _wifi_qs_monitoring_cb, ad);
229                 } else {
230                         PTS_DEBUG("Failed to launch wifi-qs(%d)",ret);
231                 }
232         } else if (index == 1) { // Wi-Fi Direct
233                 pts_launch_wifi_direct_ug(ad);
234         } else {
235                 PTS_DEBUG("Unknown index");
236         }
237         PTS_IF_DEL_OBJ(ad->select_info.wifi_type_popup);
238
239         PTS_TRACE_END;
240 }
241
242 static void _pts_refresh_cb(void *data, Evas_Object *obj, void *event_info)
243 {
244         PTS_TRACE_BEGIN;
245         pts_appdata_t *ad = pts_get_appdata();
246         PTS_RET_IF(ad == NULL, "ad is NULL");
247
248         PTS_IF_DEL_OBJ(ad->list_info.no_found_popup);
249         PTS_IF_DEL_OBJ(ad->list_info.printer_list_popup);
250
251         pts_search_printer(ad);
252
253         PTS_TRACE_END;
254 }
255
256
257 static void _pts_cancel_cb(void *data, Evas_Object *obj, void *event_info)
258 {
259         PTS_TRACE_BEGIN;
260         pts_appdata_t *ad = pts_get_appdata();
261         PTS_RET_IF(ad == NULL, "ad is NULL");
262
263         if (ad->search_info.print_search_mode ==  PTS_SEARCH_INITIAL) {
264                 PTS_IF_DEL_OBJ(ad->list_info.printer_list_popup);
265                 PTS_IF_DEL_OBJ(obj);
266                 pt_deinit();
267                 elm_exit();
268         } else {
269                 PTS_IF_DEL_OBJ(ad->list_info.no_found_popup);
270                 PTS_IF_DEL_OBJ(ad->select_info.wifi_type_popup);
271                 PTS_IF_DEL_OBJ(obj);
272
273                 if (ad->list_info.active_printer == NULL) {
274                         PTS_DEBUG("No active printer. So remove all option");
275                         elm_genlist_item_update(ad->setting_info.printer_item);
276                         elm_object_item_del(ad->setting_info.header_separator_item);
277                         elm_object_item_del(ad->setting_info.item_paper_size);
278                         elm_object_item_del(ad->setting_info.item_paper_copies);
279                         elm_object_item_del(ad->setting_info.item_page_range);
280                         elm_object_item_del(ad->setting_info.item_paper_quality);
281                         elm_object_item_del(ad->setting_info.item_paper_type);
282                         elm_object_item_del(ad->setting_info.item_grayscale);
283                         elm_object_item_del(ad->setting_info.footer_separator_item);
284                 }
285         }
286         PTS_TRACE_END;
287 }
288
289 static int pts_save_selected_printer(pts_appdata_t *ad, pt_printer_mgr_t *pt)
290 {
291         PTS_TRACE_BEGIN;
292         PTS_RETV_IF(pt == NULL, PT_ERR_INVALID_PARAM, "printer_info is NULL.");
293
294         int ret = 0;
295         PTS_DEBUG("pt name is %s", pt->name);
296         PTS_DEBUG("pt address %x", pt);
297
298         /* Check whether the printer is already activated or not. */
299         if (NULL != ad->list_info.active_printer) {
300                 if ((strcmp(ad->list_info.active_printer->name, pt->name) == 0) &&
301                                 (strcmp(ad->list_info.active_printer->address, pt->address) == 0)) {
302                         // if same printer, then nothing to do...
303                         PTS_DEBUG("Same printer is selected. No update required. %x", pt);
304                         return PT_ERR_NONE;
305                 }
306         }
307
308         ret = pt_set_active_printer(pt);
309         PTS_RETV_IF(ret != PT_ERR_NONE, PT_ERR_FAIL, "Can not set active printer.");
310
311         ret = pt_set_default_printer(pt);
312         PTS_RETV_IF(ret != PT_ERR_NONE, PT_ERR_FAIL, "Can not set default printer.");
313
314         if (NULL == ad->list_info.active_printer) {
315                 ad->list_info.active_printer = (pt_printer_mgr_t *)malloc(sizeof(pt_printer_mgr_t));
316                 PTS_RETV_IF(ad->list_info.active_printer == NULL, PT_ERR_NO_MEMORY, "Can allocate memory");
317         }
318         memset(ad->list_info.active_printer, 0, sizeof(pt_printer_mgr_t));
319         memcpy(ad->list_info.active_printer, pt, sizeof(pt_printer_mgr_t));
320
321         ad->setting_info.paper_size = (Elm_Object_Item *)realloc(ad->setting_info.paper_size, pt_get_print_option_papersize_num() * sizeof(Elm_Object_Item *));
322         memset(ad->setting_info.paper_size, 0, pt_get_print_option_papersize_num() * sizeof(Elm_Object_Item *));
323
324         return PT_ERR_NONE;
325 }
326
327 static void _pts_printer_list_cancel_cb(void *data, Evas_Object *obj, void *event_info)
328 {
329         PTS_TRACE_BEGIN;
330         struct appdata *ad = pts_get_appdata();
331
332         PTS_IF_DEL_OBJ(data);
333
334         if (ad->search_info.print_search_mode == PTS_SEARCH_INITIAL) {
335                 PTS_TRACE_END;
336                 pt_deinit();
337                 elm_exit();
338         } else {
339                 //Don't terminate mobileprint. Previous screen is setting view.
340         }
341
342         PTS_TRACE_END;
343 }
344
345 static void _pts_printer_list_ok_cb(void *data, Evas_Object *obj, void *event_info)
346 {
347         PTS_TRACE_BEGIN;
348         pts_appdata_t *ad = (pts_appdata_t *)data;
349         PTS_RET_IF(ad == NULL, "ad is NULL");
350
351         PTS_RET_IF(ad->list_info.select_printer == NULL, "No supported printer");
352         PTS_IF_DEL_OBJ(printer_list_radio_main);
353         PTS_IF_DEL_OBJ(ad->list_info.printer_list_popup);
354
355         pts_save_selected_printer(ad, ad->list_info.select_printer);
356
357         if (ad->search_info.selection_done_cb != NULL) {
358                 ad->search_info.selection_done_cb(ad->search_info.printer_search_user_data);
359         }
360
361         PTS_TRACE_END;
362         return;
363 }
364
365 /*
366 * select the printer from the searched printers list
367 */
368 static void __pts_search_print_list_sel(void *data, Evas_Object *obj, void *event_info)
369 {
370         PTS_TRACE_BEGIN;
371         PTS_RET_IF(data == NULL, "data is NULL");
372         PTS_RET_IF(event_info == NULL, "event_info is NULL");
373         pts_appdata_t *ad = (pts_appdata_t *)data;
374         Elm_Object_Item *item = event_info;
375
376         pt_printer_mgr_t *pt = NULL;
377         radio_print_list_data_t *core = NULL;
378
379         elm_genlist_item_selected_set(item, EINA_FALSE);
380
381         core  = (radio_print_list_data_t *)elm_object_item_data_get(item);
382         PTS_RET_IF(core == NULL, "core is NULL");
383         pt = core->pt;
384         PTS_RET_IF(pt == NULL, "pt is NULL");
385
386         PTS_DEBUG("Selected printer information: index=[%d]", core->radio_index);
387         PTS_DEBUG("pt->name : %s", pt->name);
388         PTS_DEBUG("pt->address : %s", pt->address);
389
390         ad->list_info.select_printer = pt;
391         elm_radio_value_set(core->radio_group, core->radio_index);
392
393         // Now call real event trigger to update radio button when selected.
394         elm_genlist_item_update(item);
395
396         PTS_TRACE_END;
397 }
398
399 /*
400 * get the value of the label
401 */
402 static char *__pts_list_view_label_get(void *data, Evas_Object *obj, const char *part)
403 {
404         PTS_TRACE_BEGIN;
405         radio_print_list_data_t *core = (radio_print_list_data_t *) data;
406         pt_printer_mgr_t *item = core->pt;
407         PTS_RETV_IF(item == NULL, NULL, "item is NULL");
408
409         if (strcmp(part, "elm.text.1") == 0) {
410                 if (item != NULL) {
411                         char *printer_label = strdup(item->name);
412                         pts_unstandardization(printer_label);
413                         return printer_label;
414                 }
415         } else if (strcmp(part, "elm.text.2") == 0) {
416                 if (item != NULL) {
417                         if (item->is_ppd_exist == FALSE) {
418                                 return strdup(_("IDS_COM_POP_UNSUPPORTED"));
419                         } else {
420                                 return pts_parse_protocol(item->address);
421                         }
422                 }
423         }
424
425         PTS_TRACE_END;
426         return NULL;
427 }
428
429 /*
430 * set the icon of the obj
431 */
432 static Evas_Object *__pts_list_view_icon_get(void *data, Evas_Object *obj, const char *part)
433 {
434         PTS_TRACE_BEGIN;
435
436         radio_print_list_data_t *core = (radio_print_list_data_t *) data;
437         pts_appdata_t *ad  = pts_get_appdata();
438         PTS_RETV_IF(core == NULL || ad == NULL, NULL, "Invalid argument");
439
440         pt_printer_mgr_t *pt = core->pt;
441         Evas_Object *radio = NULL;
442
443         if (!strcmp(part, "elm.icon") || !strcmp(part, "elm.swallow.icon")) {
444
445                 radio = elm_radio_add(obj);
446                 elm_radio_state_value_set(radio, core->radio_index);
447                 elm_radio_group_add(radio, core->radio_group);
448                 if (ad->list_info.active_printer != NULL) {
449                         if (strcmp(ad->list_info.active_printer->name, pt->name) ==0 &&
450                                         strcmp(ad->list_info.active_printer->address, pt->address) ==0) {
451                                 elm_radio_value_set(radio, core->radio_index);
452                                 ad->list_info.select_printer = pt;
453
454                                 PTS_DEBUG("Printer[%d] name=[%s], address=[%s] -> Active", core->radio_index, pt->name, pt->address);
455                         } else {
456                                 PTS_DEBUG("Printer[%d] name=[%s], address=[%s] -> x", core->radio_index, pt->name, pt->address);
457                         }
458                 }
459
460                 evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
461                 evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
462
463                 return radio;
464         }
465
466         PTS_TRACE_END;
467         return NULL;
468 }
469
470 #if 0
471 static Evas_Object *__pts_refresh_icon_get(void *data, Evas_Object *obj, const char *part)
472 {
473         PTS_TRACE_BEGIN;
474
475         Evas_Object *btn1;
476         pts_appdata_t *ad  = (pts_appdata_t *)data;
477         PTS_RETV_IF(ad == NULL, NULL, "Invalid argument");
478
479         if (!strcmp(part, "elm.icon")) {
480                 btn1 = elm_button_add(obj);
481                 elm_object_text_set(btn1, _("IDS_COM_SK_REFRESH"));
482                 //elm_object_part_content_set(obj, "button1", btn1);
483                 evas_object_smart_callback_add(btn1, "clicked", _pts_refresh_cb, ad);
484                 PTS_TRACE_END;
485                 return btn1;
486         } else {
487                 PTS_DEBUG("Unknwon part : %s", part);
488         }
489
490         PTS_TRACE_END;
491         return NULL;
492 }
493 #endif
494
495 /*
496 * popup when machine is unsupported
497 */
498 static Eina_Bool __pts_exit_cb(void *data)
499 {
500         PTS_TRACE_BEGIN;
501         pts_appdata_t *ad = (pts_appdata_t *) data;
502         PTS_RETV_IF(ad == NULL, ECORE_CALLBACK_CANCEL, "ad is NULL");
503
504         PTS_IF_FREE_MEM(ad);
505         elm_exit();
506
507         PTS_TRACE_END;
508         return ECORE_CALLBACK_CANCEL;
509 }
510
511 /*
512 * popup error when searching printers failed
513 */
514 static Eina_Bool __pts_list_view_popup_error(void *data)
515 {
516         PTS_TRACE_BEGIN;
517         pts_appdata_t *ad = (pts_appdata_t *) data;
518         PTS_RETV_IF(ad == NULL, ECORE_CALLBACK_CANCEL, "ad is NULL");
519
520         ad->search_info.is_process = 0;/*process is complete*/
521
522         PTS_IF_DEL_OBJ(ad->search_info.popup);
523
524         PTS_DEBUG("search printer failed");
525         pts_create_popup(ad->search_info.print_search_popup_parent, _("IDS_ASEML_POP_SEARCH_FAILED"), 1.0);
526         ecore_timer_add(1, (Ecore_Task_Cb)__pts_exit_cb, (void *)ad);
527
528         PTS_IF_FREE_MEM(ad);
529
530         PTS_TRACE_END;
531         return ECORE_CALLBACK_CANCEL;
532 }
533
534 // pts_create_wifi_popup
535 Evas_Object *pts_create_wifi_type_selector_popup(Evas_Object *parent)
536 {
537         PTS_TRACE_BEGIN;
538
539         Evas_Object *popup = elm_popup_add(parent);
540         elm_object_style_set(popup,"min_menustyle");
541         elm_object_part_text_set(popup, "title,text", _("IDS_PRT_BODY_CONNECT_TO_WI_FI_NETWORK_TO_PRINT"));
542         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
543
544         /* accessability - screen reader support */
545         Evas_Object *po, *eo, *ao;
546         eo = elm_layout_edje_get(popup);
547         po = edje_object_part_object_get(eo, "access.title");
548         ao = elm_access_object_get(po);
549         elm_access_info_set(ao, ELM_ACCESS_CONTEXT_INFO, _("IDS_PRT_BODY_CONNECT_TO_WI_FI_NETWORK_TO_PRINT"));
550
551         Evas_Object *cancel_button = elm_button_add(popup);
552         elm_object_style_set(cancel_button,"popup_button/default");
553         elm_object_text_set(cancel_button, _("IDS_COM_SK_CANCEL"));
554         elm_access_info_set(cancel_button, ELM_ACCESS_CONTEXT_INFO, IDS_SCRREAD_CANCEL_CONNECT_WIFI);
555         elm_object_part_content_set(popup, "button1", cancel_button);
556         evas_object_smart_callback_add(cancel_button, "clicked", _pts_cancel_cb, NULL);
557
558         wifi_list_itc.item_style = "1text";
559         wifi_list_itc.func.text_get = _pts_wifi_text_get;
560         wifi_list_itc.func.content_get = NULL;
561         wifi_list_itc.func.state_get = NULL;
562         wifi_list_itc.func.del = NULL;
563
564         Evas_Object *genlist = elm_genlist_add(popup);
565         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
566         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
567
568         int index = 0;
569         for (index = 0; index < 2; index++) {
570                 elm_genlist_item_append(genlist, &wifi_list_itc, (void *) index, NULL,
571                                                                 ELM_GENLIST_ITEM_NONE, _pts_wifi_list_sel, NULL);
572         }
573
574         Evas_Object *box = elm_box_add(popup);
575         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
576
577         elm_box_pack_end(box, genlist);
578         evas_object_size_hint_min_set(box, 400 * elm_config_scale_get(), elm_genlist_items_count(genlist) * 114 * elm_config_scale_get());
579         elm_object_content_set(popup, box);
580
581         evas_object_show(genlist);
582         evas_object_show(popup);
583         evas_object_show(parent);
584
585         PTS_TRACE_END;
586         return popup;
587 }
588
589 Evas_Object *pts_create_no_found_popup(Evas_Object *parent, void *refresh_cb, void *cancel_cb)
590 {
591         PTS_TRACE_BEGIN;
592
593         Evas_Object *popup = elm_popup_add(parent);
594         //elm_object_style_set(popup,"menustyle");
595         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
596         elm_object_text_set(popup, _("IDS_PRT_BODY_NO_PRINTERS_AVAILABLE"));
597 //      evas_object_smart_callback_add(popup, "block,clicked", _block_clicked_cb,
598 //              NULL);
599
600         Evas_Object *refresh_button = elm_button_add(popup);
601         //elm_object_style_set(refresh_button,"popup_button/default");
602         elm_object_text_set(refresh_button, _("IDS_COM_SK_REFRESH"));
603         elm_object_part_content_set(popup, "button1", refresh_button);
604         evas_object_smart_callback_add(refresh_button, "clicked", refresh_cb, NULL);
605
606         Evas_Object *cancel_button = elm_button_add(popup);
607         //elm_object_style_set(cancel_button,"popup_button/default");
608         elm_object_text_set(cancel_button, _("IDS_COM_SK_CANCEL"));
609         elm_object_part_content_set(popup, "button2", cancel_button);
610         evas_object_smart_callback_add(cancel_button, "clicked", cancel_cb, NULL);
611
612         evas_object_show(popup);
613         evas_object_show(parent);
614
615         PTS_TRACE_END;
616         return popup;
617 }
618
619 /*
620 * create the content of the list view
621 */
622 Evas_Object *pts_create_content_printer_list(Evas_Object *parent, pts_appdata_t *ad, int *print_counter)
623 {
624         PTS_TRACE_BEGIN;
625         PTS_RETV_IF(ad == NULL, NULL, "ad is NULL");
626
627         Evas_Object *genlist = NULL;
628         Elm_Object_Item *git;
629         genlist = elm_genlist_add(parent);
630
631         // ToDo: Make sure that printer_list_radio_main always is NULL at here..
632         //if (!printer_list_radio_main) {
633         printer_list_radio_main = elm_radio_add(genlist);
634         elm_radio_state_value_set(printer_list_radio_main, 0);
635         elm_radio_value_set(printer_list_radio_main, 1);
636         //}
637
638         list_itc.item_style = "2text.1icon.4"; // Left Icon, Right Main/Sub Text
639         list_itc.func.text_get = __pts_list_view_label_get;
640         list_itc.func.content_get = __pts_list_view_icon_get;
641         list_itc.func.state_get = NULL;
642         list_itc.func.del = NULL;
643
644 #if 0
645         refresh_itc.item_style = "1icon"; // Text Button
646         refresh_itc.func.text_get = NULL;
647         refresh_itc.func.content_get = __pts_refresh_icon_get;
648         refresh_itc.func.state_get = NULL;
649         refresh_itc.func.del = NULL;
650 #endif
651
652         pt_printer_mgr_t *it = NULL;
653         Eina_List *cursor = NULL;
654
655         // printer_list_radio_main is index zero.
656         // Thus radio index starts from 1.
657         int radio_index = 1;
658         radio_print_list_data_t *core = NULL;
659
660         ad->list_info.select_printer = NULL;
661
662         EINA_LIST_FOREACH(ad->list_info.printer_list, cursor, it) {
663
664                 if (ad->list_info.select_printer == NULL && it->is_ppd_exist == TRUE)   {
665                         // set the first printer as default one.
666                         ad->list_info.select_printer = it;
667                 }
668                 PTS_DEBUG("printer %d\n", radio_index);
669                 core = (radio_print_list_data_t *)malloc(sizeof(radio_print_list_data_t));
670                 core->radio_index = radio_index;
671                 core->pt = it;
672                 core->radio_group = printer_list_radio_main;
673                 //core->ugd = ld->ugd;
674
675                 git = elm_genlist_item_append(
676                         genlist,
677                         &list_itc,
678                         (void *)core,
679                         NULL,
680                         ELM_GENLIST_ITEM_NONE,
681                         __pts_search_print_list_sel,
682                         ad);
683
684                 if (it->is_ppd_exist == FALSE) {
685                         elm_object_item_disabled_set(git, EINA_TRUE);
686                 }
687                 radio_index ++;
688         }
689
690         *print_counter = radio_index-1;
691         PTS_DEBUG("printer num = %d\n", *print_counter);
692
693         evas_object_show(genlist);
694         PTS_TRACE_END;
695         return genlist;
696 }
697
698 Evas_Object *pts_create_printer_list_popup(Evas_Object *parent, pts_appdata_t *ad)
699 {
700         PTS_TRACE_BEGIN;
701         Evas_Object *popup;
702         Evas_Object *genlist;
703         Evas_Object *ok_button;
704         Evas_Object *refresh_button;
705         Evas_Object *cancel_button;
706         int printer_num = 0;
707
708 // XXX
709 //  Currently min_menustyle does not support "2text.1icon.4" genlist.
710 //  When min_menustyle supports it, change this.
711 //  To avoid this limitation, applied box object.
712         popup = elm_popup_add(parent);
713         //elm_object_style_set(popup,"menustyle");
714         elm_object_style_set(popup,"min_menustyle");
715         elm_object_part_text_set(popup, "title,text", _("IDS_PRT_BODY_SELECT_PRINTER"));
716         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
717         elm_access_info_set(popup, ELM_ACCESS_CONTEXT_INFO, _("IDS_PRT_BODY_SELECT_PRINTER")); //FIXME: should be set to title, not popup itself
718
719         /* accessability - screen reader support */
720         Evas_Object *po, *eo, *ao;
721         eo = elm_layout_edje_get(popup);
722         po = edje_object_part_object_get(eo, "access.title");
723         ao = elm_access_object_get(po);
724         elm_access_info_set(ao, ELM_ACCESS_INFO, _("IDS_PRT_BODY_SELECT_PRINTER"));
725         elm_access_info_set(ao, ELM_ACCESS_CONTEXT_INFO, IDS_SCRREAD_PRINTERS_LIST_IS_BELOW);
726
727         ok_button = elm_button_add(popup);
728         elm_object_style_set(ok_button,"popup_button/default");
729         elm_object_text_set(ok_button, _("IDS_COM_SK_OK"));
730         elm_access_info_set(ok_button, ELM_ACCESS_CONTEXT_INFO, IDS_SCRREAD_CONFIRM_SELECTED_PRINTER);
731         elm_object_part_content_set(popup, "button1", ok_button);
732         evas_object_smart_callback_add(ok_button, "clicked", _pts_printer_list_ok_cb, ad);
733
734         refresh_button = elm_button_add(popup);
735         elm_object_style_set(refresh_button,"popup_button/default");
736         elm_object_text_set(refresh_button, _("IDS_COM_SK_REFRESH"));
737         elm_access_info_set(ok_button, ELM_ACCESS_CONTEXT_INFO, IDS_SCRREAD_REFRESH_SEARCHING_PRINTERS);
738         elm_object_part_content_set(popup, "button2", refresh_button);
739         evas_object_smart_callback_add(refresh_button, "clicked", _pts_refresh_cb, ad);
740
741         cancel_button = elm_button_add(popup);
742         elm_object_style_set(cancel_button,"popup_button/default");
743         elm_object_text_set(cancel_button, _("IDS_COM_SK_CANCEL"));
744         elm_access_info_set(cancel_button, ELM_ACCESS_CONTEXT_INFO, IDS_SCRREAD_CANCEL_PRINTING);
745         elm_object_part_content_set(popup, "button3", cancel_button);
746         evas_object_smart_callback_add(cancel_button, "clicked", _pts_printer_list_cancel_cb, popup);
747
748         genlist = pts_create_content_printer_list(parent, ad, &printer_num);
749         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
750         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
751
752         Evas_Object *box = elm_box_add(popup);
753         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
754
755         elm_box_pack_end(box, genlist);
756
757         /*
758          * XXX - below 143 value is just for entry height of 2text.1icon.4 style in genlist
759          */
760
761         int rotate_angle = 0;
762         if (ad->win) {
763                 rotate_angle = elm_win_rotation_get(ad->win);
764                 PTS_DEBUG("rotate_angle: %d", rotate_angle);
765         } else {
766                 PTS_DEBUG("No ld->ugd->root_win");
767         }
768
769         ad->list_info.printer_list_popup_portrait_height        = MIN(PRINTER_LIST_POPUP_H, elm_genlist_items_count(genlist) * 143) * elm_config_scale_get();
770         ad->list_info.printer_list_popup_landscape_height       = MIN(PRINTER_LIST_POPUP_LN_H, elm_genlist_items_count(genlist) * 143) * elm_config_scale_get();
771
772         if (0 == rotate_angle || 180 == rotate_angle)
773                 evas_object_size_hint_min_set(box,
774                 PRINTER_LIST_POPUP_W * elm_config_scale_get(),
775                 ad->list_info.printer_list_popup_portrait_height);
776         else
777                 evas_object_size_hint_min_set(box,
778                 PRINTER_LIST_POPUP_LN_W * elm_config_scale_get(),
779                 ad->list_info.printer_list_popup_landscape_height);
780
781         ad->list_info.printer_list_popup_box = box;
782         ad->list_info.printer_list_popup = popup;
783
784         elm_object_content_set(popup, box);
785         evas_object_show(genlist);
786         evas_object_show(box);
787         evas_object_show(popup);
788         evas_object_show(parent);
789
790         PTS_TRACE_END;
791         return popup;
792 }
793
794 void pts_search_printer_select(pts_appdata_t *ad)
795 {
796 //      if (ugd->print_search_mode == PTS_SEARCH_INITIAL)
797         pts_create_printer_list_popup(ad->search_info.print_search_popup_parent, ad);
798 //      else
799 //              pts_create_printer_list_view(ugd->print_search_popup_parent, ld);
800
801         return;
802 }
803
804 /*
805 * the response callback when search the printer
806 */
807 static void __pts_get_printer_cb(pt_response_data_t *cb)
808 {
809         PTS_TRACE_BEGIN;
810         PTS_RET_IF(cb == NULL, "response data is NULL");
811
812         pts_appdata_t *ad = cb->userdata;
813         PTS_RET_IF(ad == NULL, "ad is NULL");
814
815         ad->list_info.printer_list = cb->printerlist;
816         ad->search_info.is_process = 0; /*process is complete*/
817
818         /* destroy popup progress bar */
819         PTS_IF_DEL_OBJ(ad->search_info.popup);
820
821         /*
822          * Not found any printer
823          */
824         if (NULL == ad->list_info.printer_list) {
825                 /*
826                  * destroy popup progress bar
827                  */
828                 PTS_DEBUG("search printer failed");
829                 PTS_IF_FREE_MEM(ad->list_info.active_printer);
830                 ad->list_info.no_found_popup = pts_create_no_found_popup(ad->search_info.print_search_popup_parent,
831                                                                                                 _pts_refresh_cb,
832                                                                                                 _pts_cancel_cb);
833                 return;
834         }
835
836         int pt_count = 0;
837         /* get printer list count */
838         pt_count = eina_list_count(ad->list_info.printer_list);
839         PTS_DEBUG("Printer count is %d", pt_count);
840
841         if (pt_count > 0) {
842                 pts_search_printer_select(ad);
843         } else {
844                 // No Printer found.
845                 // Control can't be reach here...
846         }
847         PTS_TRACE_END;
848 }
849
850 /**
851  *      This function let the app create the print searching popup
852  *      @return  void
853  *      @param[in] ld the pointer to the ug list structure
854  */
855 Evas_Object *pts_create_print_search_popup(Evas_Object *parent)
856 {
857         PTS_TRACE_BEGIN;
858         PTS_RETV_IF(parent == NULL, NULL, "parent is NULL");
859
860         int ret = 0;
861         Evas_Object *layout = NULL;
862         Evas_Object *progressbar = NULL;
863         Evas_Object *btn = NULL;
864         Evas_Object *popup = NULL;
865
866         popup = elm_popup_add(parent);
867         layout = pts_create_base_layout(popup, "popup_search");
868         progressbar = elm_progressbar_add(popup);
869         elm_object_style_set(progressbar, "list_process");
870         elm_progressbar_horizontal_set(progressbar, EINA_TRUE);
871         evas_object_size_hint_align_set(progressbar, EVAS_HINT_FILL, EVAS_HINT_FILL);
872         evas_object_size_hint_weight_set(progressbar, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
873         elm_progressbar_pulse(progressbar, EINA_TRUE);
874         elm_access_info_set(progressbar, ELM_ACCESS_INFO, _("IDS_PRT_BODY_SEARCHING_FOR_PRINTERS_ING")); //FIXME Screen reader doesn't pronounce it
875         evas_object_show(progressbar);
876
877         elm_object_part_content_set(layout, "elm.swallow.content", progressbar);
878         elm_object_part_text_set(layout, "elm.text", _("IDS_PRT_BODY_SEARCHING_FOR_PRINTERS_ING"));
879
880         /* accessability - screen reader support */
881         Evas_Object *po, *ao;
882         po = edje_object_part_object_get(layout, "elm.swallow.content");
883         ao = elm_access_object_get(po);
884         elm_access_info_set(ao, ELM_ACCESS_INFO, _("IDS_PRT_BODY_SEARCHING_FOR_PRINTERS_ING"));
885
886         elm_object_content_set(popup, layout);
887         btn = elm_button_add(popup);
888         elm_object_text_set(btn, _("IDS_COM_SK_CANCEL"));
889         elm_access_info_set(btn, ELM_ACCESS_CONTEXT_INFO, IDS_SCRREAD_CANCEL_SEARCHING_PRINTERS);
890         elm_object_part_content_set(popup, "button1", btn);
891         evas_object_smart_callback_add(btn, "clicked", _pts_cancel_cb, NULL);
892
893         evas_object_show(layout);
894         evas_object_show(popup);
895         evas_object_show(parent);
896
897         PTS_TRACE_END;
898         return popup;
899 }
900
901 static gboolean __is_wifi_direct_connected(int net_info)
902 {
903         /** Connected with peer as GC */
904         if ((net_info&PT_CONNECTION_WIFI_DIRECT)>0) {
905                 return TRUE;
906         }
907         return FALSE;
908 }
909
910 static gboolean __is_wifi_connected(int net_info)
911 {
912         if ((net_info&PT_CONNECTION_WIFI)>0) {
913                 return TRUE;
914         }
915         return FALSE;
916 }
917
918 static gboolean __is_usb_host_connected(int net_info)
919 {
920         if ((net_info&PT_CONNECTION_USB)>0) {
921                 return TRUE;
922         }
923         return FALSE;
924 }
925
926 static gboolean __is_connected(int net_info)
927 {
928         if (net_info == 0) {
929                 return FALSE;
930         }
931         return TRUE;
932 }
933
934 static gboolean __is_valid_ip_address(const char *ip_address)
935 {
936         PTS_RETV_IF(ip_address == NULL, FALSE, "ip_address is NULL");
937         return FALSE;
938 }
939
940 /*
941 * start to search printer
942 */
943 void pts_search_printer(pts_appdata_t *ad)
944 {
945         PTS_TRACE_BEGIN;
946         PTS_RET_IF(ad == NULL, "ad is NULL");
947         int ret = 0;
948         int net_type = 0;
949         ret = pt_get_connection_status(&net_type);
950         PTS_RET_IF(ret != PT_ERR_NONE, "Failed to get connection status(%d)", ret);
951
952         if (__is_connected(net_type)) {
953                 if (__is_wifi_direct_connected(net_type)) {
954                         ad->search_info.popup = pts_create_print_search_popup(ad->search_info.print_search_popup_parent);
955                         ad->search_info.is_process = 1; /* in process */
956                         PTS_IF_DEL_TIMER(ad->search_info.search_err_timer);
957
958                         ret = pt_get_printers(__pts_get_printer_cb, ad);
959                         if (PT_ERR_NONE != ret) {
960                                 ad->search_info.search_err_timer = ecore_timer_add(1.0, (Ecore_Task_Cb)__pts_list_view_popup_error, (void *)ad);
961                         }
962                 } else if (__is_wifi_connected(net_type) || __is_usb_host_connected(net_type)) {
963                         ad->search_info.popup = pts_create_print_search_popup(ad->search_info.print_search_popup_parent);
964                         ad->search_info.is_process = 1; /* in process */
965                         PTS_IF_DEL_TIMER(ad->search_info.search_err_timer);
966                         ret = pt_get_printers(__pts_get_printer_cb, ad);
967                         if (PT_ERR_NONE != ret) {
968                                 ad->search_info.search_err_timer = ecore_timer_add(1.0, (Ecore_Task_Cb)__pts_list_view_popup_error, (void *)ad);
969                         }
970                 } else {
971                         PTS_DEBUG("Undefined connection status");
972                         return;
973                 }
974         } else {
975                 ad->select_info.wifi_type_popup = pts_create_wifi_type_selector_popup(ad->search_info.print_search_popup_parent);
976                 return;
977         }
978         PTS_TRACE_END;
979 }