b0c97e66f436c639e0e88b991158a9002bcd08fb
[apps/native/menu-screen.git] / src / popup.c
1 /*
2  * MENU-SCREEN
3  *
4  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Jin Yoon <jinny.yoon@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <Elementary.h>
24 #include <app.h>
25
26 #include "conf.h"
27 #include "item.h"
28 #include "menu_screen.h"
29 #include "pkgmgr.h"
30 #include "util.h"
31 #include "all_apps/shortcut.h"
32
33 #define BUFSZE 1024
34 #define ELLIPSIS "..."
35 #define STRSZE 128
36
37
38
39 static struct {
40         Evas_Object *popup;
41 } popup_info = {
42         .popup = NULL,
43 };
44
45
46
47 HAPI Evas_Object *popup_exist(void)
48 {
49         return popup_info.popup;
50 }
51
52
53
54 HAPI void popup_destroy_all(void)
55 {
56         void (*_destroy_popup)(void *data, Evas_Object *obj, void *event_info);
57
58         if (NULL == popup_info.popup) return;
59
60         _destroy_popup = evas_object_data_get(popup_info.popup, "func_destroy_popup");
61         if (_destroy_popup) _destroy_popup(popup_info.popup, NULL, NULL);
62
63         popup_info.popup = NULL;
64 }
65
66
67
68 static void _response_cb(void *data, Evas_Object *obj, void *event_info)
69 {
70         ret_if(NULL == data);
71
72         Evas_Object *popup = data;
73         evas_object_data_del(popup, "func_destroy_popup");
74         popup_info.popup = NULL;
75
76         evas_object_del(evas_object_data_del(popup, "button"));
77         evas_object_del(popup);
78 }
79
80
81
82 HAPI Evas_Object *popup_create_confirm(Evas_Object *parent, const char *warning)
83 {
84         Evas_Object *popup;
85         Evas_Object *btn;
86
87         retv_if(NULL == warning, NULL);
88
89         popup_destroy_all();
90
91         popup = elm_popup_add(parent);
92         retv_if(NULL == popup, NULL);
93
94         btn = elm_button_add(popup);
95         if (NULL == btn) {
96                 evas_object_del(popup);
97                 return NULL;
98         }
99
100         elm_object_text_set(btn, _("IDS_COM_SK_OK"));
101         evas_object_data_set(popup, "button", btn);
102
103         elm_object_part_content_set(popup, "button1", btn);
104         evas_object_smart_callback_add(btn, "clicked", _response_cb, popup);
105         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
106
107         elm_object_part_text_set(popup, "title,text", _("IDS_COM_POP_WARNING"));
108         elm_object_text_set(popup, warning);
109         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
110         evas_object_show(popup);
111
112         evas_object_data_set(popup, "func_destroy_popup", _response_cb);
113         popup_info.popup = popup;
114
115         return popup;
116 }
117
118
119
120 static void _uninstall_no_cb(void *data, Evas_Object *obj, void *event_info)
121 {
122         ret_if(NULL == data);
123
124         Evas_Object *popup = data;
125         evas_object_data_del(popup, "func_destroy_popup");
126         popup_info.popup = NULL;
127
128         evas_object_del(evas_object_data_del(popup, "button1"));
129         evas_object_del(evas_object_data_del(popup, "button2"));
130         evas_object_data_del(popup, "item");
131         evas_object_del(popup);
132 }
133
134
135
136 static void _uninstall_yes_cb(void *data, Evas_Object *obj, void *event_info)
137 {
138         _D("======== uninstall yes");
139         ret_if(NULL == data);
140
141         Evas_Object *popup = data;
142         evas_object_data_del(popup, "func_destroy_popup");
143         popup_info.popup = NULL;
144
145         Evas_Object *item;
146         item = evas_object_data_del(popup, "item");
147
148         evas_object_del(evas_object_data_del(popup, "button1"));
149         evas_object_del(evas_object_data_del(popup, "button2"));
150         evas_object_del(popup);
151
152         bool is_shortcut = false;
153         is_shortcut = (bool) evas_object_data_get(item, "is_shortcut");
154
155         if (is_shortcut) {
156                 all_apps_shortcut_remove(item);
157         } else {
158                 if (MENU_SCREEN_ERROR_OK != pkgmgr_uninstall(item)) {
159                         _E("Cannot communicate with the pkgmgr-server.");
160                 }
161         }
162 }
163
164
165
166 static char *_popup_set_name(Evas_Object *item)
167 {
168         char *name;
169         char *get_name = item_get_name(item);
170         if(!get_name) get_name = item_get_package(item);
171         retv_if(NULL == get_name, NULL);
172
173         name = calloc(strlen(get_name)+1, sizeof(char));
174         retv_if(NULL == name, NULL);
175         strncpy(name, get_name, strlen(get_name));
176
177         if (strlen(name) > STRSZE) {
178                 char *temp, *ellipsis = ELLIPSIS;
179
180                 name = realloc(name, (STRSZE + strlen(ellipsis) + 1)*sizeof(char));
181                 retv_if(NULL == name, NULL);
182
183                 temp = name + STRSZE;
184
185                 while (*ellipsis) *temp++ = *ellipsis++;
186                 *temp = '\0';
187         }
188         return name;
189 }
190
191
192
193 #define IDS_AT_POP_UNINSTALL_PS_Q "IDS_AT_POP_UNINSTALL_PS_Q"
194 HAPI Evas_Object *popup_create_uninstall(Evas_Object *parent, Evas_Object *item)
195 {
196         Evas_Object *popup;
197         Evas_Object *btn1;
198         Evas_Object *btn2;
199         char warning[BUFSZE];
200
201         popup_destroy_all();
202
203         popup = elm_popup_add(parent);
204         retv_if(NULL == popup, NULL);
205
206         evas_object_data_set(popup, "item", item);
207
208         btn1 = elm_button_add(popup);
209         if (NULL == btn1) {
210                 evas_object_del(popup);
211                 return NULL;
212         }
213         elm_object_style_set(btn1, "popup_button/default");
214         elm_object_text_set(btn1, _("IDS_COM_SK_CANCEL"));
215         evas_object_data_set(popup, "button1", btn1);
216         elm_object_part_content_set(popup, "button1", btn1);
217         evas_object_smart_callback_add(btn1, "clicked", _uninstall_no_cb, popup);
218
219         btn2 = elm_button_add(popup);
220         if (NULL == btn2) {
221                 evas_object_del(popup);
222                 return NULL;
223         }
224         elm_object_style_set(btn2, "popup_button/default");
225         elm_object_text_set(btn2, _("IDS_COM_SK_OK"));
226         evas_object_data_set(popup, "button2", btn2);
227         elm_object_part_content_set(popup, "button2", btn2);
228         evas_object_smart_callback_add(btn2, "clicked", _uninstall_yes_cb, popup);
229
230         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
231         evas_object_data_set(popup, "func_destroy_popup", _uninstall_no_cb);
232         popup_info.popup = popup;
233         evas_object_show(popup);
234         char *name = _popup_set_name(item);
235         retv_if(NULL == name, popup);
236
237         char *markup_name = elm_entry_utf8_to_markup(name);
238         if (NULL == markup_name) {
239                 _E("(NULL == markup_name) -> %s() return", __func__);
240                 free(name);
241                 return popup;
242         }
243
244         snprintf(warning, sizeof(warning), _(IDS_AT_POP_UNINSTALL_PS_Q), markup_name);
245         free(name);
246         free(markup_name);
247
248         elm_object_text_set(popup, warning);
249
250         return popup;
251 }
252
253
254
255 // End of a file