Remove executable flag from non-executable files
[platform/core/system/system-popup.git] / src / mmc / mmc-mobile.c
1 /*
2  *  system-popup
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 "popup-common.h"
21 #include <aul.h>
22 #include <appsvc.h>
23 #include <app_control.h>
24
25 #define SETTING_MMC_ENCRYPTION_UG  "setting-mmc-encryption-efl"
26 #define EXTENDED_SD_APPNAME             "org.tizen.extended-sd"
27 #define ODE_APPNAME                     "org.tizen.ode"
28 #define SDCARD_KEY                      "_SDCARD_NUM_"
29 #define VIEWTYPE_KEY                    "viewtype"
30 #define DEVPATH_KEY                     "dev_path"
31 #define MAPPINGNODE_KEY                 "mapping_node"
32
33 #define DD_BUS_NAME             "org.tizen.system.deviced"
34 #define DD_OBJECT_PATH_ODE      "/Org/Tizen/System/DeviceD/Ode"
35 #define DD_INTERFACE_NAME_ODE   DD_BUS_NAME".ode"
36 #define DD_SIGNAL_GENERAL_MOUNT "RequestGeneralMount"
37 #define DD_SIGNAL_ODE_MOUNT     "RequestOdeMount"
38 #define DD_SIGNAL_REMOVE_MMC    "RemoveMmc"
39
40 enum ext_app {
41         EXT_SETTING_MMC_UG,
42         EXT_SD_SETTING,
43         EXT_ODE
44 };
45
46 static const struct popup_ops mount_error_ops;
47 static const struct popup_ops unmount_error_ops;
48 static const struct popup_ops mount_read_only_ops;
49 static const struct popup_ops check_smack_ops;
50 static const struct popup_ops ode_encrypt_ops;
51 static const struct popup_ops ode_decrypt_ops;
52 static const struct popup_ops sdcard_setup_ops;
53 static const struct popup_ops unlock_extendedsd_ops;
54
55 static char *Sdcard_ID = NULL;
56 static char *View_Type = NULL;
57 static char *Dev_Path = NULL;
58 static char *Mapping_Node = NULL;
59
60 static void remove_other_mmc_popups(const struct popup_ops *ops)
61 {
62         if (ops != &sdcard_setup_ops)
63                 unload_simple_popup(&sdcard_setup_ops);
64
65         if (ops != &mount_error_ops)
66                 unload_simple_popup(&mount_error_ops);
67
68         if (ops != &unmount_error_ops)
69                 unload_simple_popup(&unmount_error_ops);
70
71         if (ops != &mount_read_only_ops)
72                 unload_simple_popup(&mount_read_only_ops);
73
74         if (ops != &check_smack_ops)
75                 unload_simple_popup(&check_smack_ops);
76
77         if (ops != &ode_encrypt_ops)
78                 unload_simple_popup(&ode_encrypt_ops);
79
80         if (ops != &ode_decrypt_ops)
81                 unload_simple_popup(&ode_decrypt_ops);
82 }
83
84 static bool mmc_inserted(void)
85 {
86         int val;
87         if (vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &val) == 0
88                         && val != VCONFKEY_SYSMAN_MMC_REMOVED)
89                 return true;
90         return false;
91 }
92
93 static bool mmc_mounted(void)
94 {
95         int val;
96         if (vconf_get_int(VCONFKEY_SYSMAN_MMC_MOUNT, &val) == 0
97                         && val == VCONFKEY_SYSMAN_MMC_MOUNT_FAILED)
98                 return false;
99         return true;
100 }
101
102 static bool mmc_unmounted(void)
103 {
104         int val;
105
106         if (vconf_get_int(VCONFKEY_SYSMAN_MMC_UNMOUNT, &val) == 0
107                         && val == VCONFKEY_SYSMAN_MMC_UNMOUNT_FAILED)
108                 return false;
109         return true;
110 }
111
112 static void send_mount_signal(char *signal)
113 {
114         int ret;
115
116         if (!signal)
117                 return;
118
119         ret = broadcast_dbus_signal(
120                         DD_OBJECT_PATH_ODE,
121                         DD_INTERFACE_NAME_ODE,
122                         signal,
123                         NULL, NULL);
124         if (ret < 0)
125                 _E("FAIL: broadcast_dbus_signal(%s:%d)", signal, ret);
126 }
127
128 static void launch_app(int type)
129 {
130         app_control_h app_control = NULL;
131         int ret = -1;
132
133         ret = app_control_create(&app_control);
134         if (ret != APP_CONTROL_ERROR_NONE)
135                 return;
136
137         if (type == EXT_SETTING_MMC_UG)
138                 ret = app_control_set_app_id(app_control, SETTING_MMC_ENCRYPTION_UG);
139         else if (type == EXT_SD_SETTING) {
140                 app_control_add_extra_data(app_control, SDCARD_KEY, Sdcard_ID);
141                 ret = app_control_set_app_id(app_control, EXTENDED_SD_APPNAME);
142         } else if (type == EXT_ODE) {
143                 app_control_add_extra_data(app_control, VIEWTYPE_KEY, View_Type);
144                 app_control_add_extra_data(app_control, DEVPATH_KEY, Dev_Path);
145                 app_control_add_extra_data(app_control, MAPPINGNODE_KEY, Mapping_Node);
146                 ret = app_control_set_app_id(app_control, ODE_APPNAME);
147         } else {
148                 _E("No matched type(%d)", type);
149                 goto out;
150         }
151
152         if (ret != APP_CONTROL_ERROR_NONE) {
153                 _E("Fail to set app id");
154                 goto out;
155         }
156
157         ret = app_control_send_launch_request(app_control, NULL, NULL);
158         if (ret != APP_CONTROL_ERROR_NONE)
159                 _E("Fail to send launch request");
160
161 out:
162         if (app_control) {
163                 (void)app_control_destroy(app_control);
164                 app_control = NULL;
165         }
166 }
167
168 static void send_general_mount_signal(const struct popup_ops *ops)
169 {
170         unload_simple_popup(ops);
171         send_mount_signal(DD_SIGNAL_GENERAL_MOUNT);
172         terminate_if_no_popup();
173 }
174
175 static void send_ode_mount_signal(const struct popup_ops *ops)
176 {
177         unload_simple_popup(ops);
178         send_mount_signal(DD_SIGNAL_ODE_MOUNT);
179         terminate_if_no_popup();
180 }
181
182 static void ode_launch_settings(const struct popup_ops *ops)
183 {
184         unload_simple_popup(ops);
185         launch_app(EXT_SETTING_MMC_UG);
186         terminate_if_no_popup();
187 }
188
189 static bool skip_mount_error_popup(bundle *b, const struct popup_ops *ops)
190 {
191         return mmc_mounted();
192 }
193
194 static bool skip_unmount_error_popup(bundle *b, const struct popup_ops *ops)
195 {
196         return mmc_unmounted();
197 }
198
199 static bool skip_ode_popup(bundle *b, const struct popup_ops *ops)
200 {
201         return !mmc_inserted();
202 }
203
204 static E_DBus_Signal_Handler *mmc_removed_handler = NULL;
205
206 static void unregister_ode_handler(const struct popup_ops *ops)
207 {
208         if (mmc_removed_handler) {
209                 unregister_dbus_signal_handler(mmc_removed_handler);
210                 mmc_removed_handler = NULL;
211         }
212 }
213
214 static void mmc_removed(void *data, DBusMessage *msg)
215 {
216         const struct popup_ops *ops = data;
217
218         unregister_ode_handler(ops);
219         unload_simple_popup(ops);
220         terminate_if_no_popup();
221 }
222
223 static void register_ode_handler(const struct popup_ops *ops)
224 {
225         int ret;
226
227         unregister_ode_handler(ops);
228
229         ret = register_dbus_signal_handler(&mmc_removed_handler,
230                         DD_OBJECT_PATH_ODE,
231                         DD_INTERFACE_NAME_ODE,
232                         DD_SIGNAL_REMOVE_MMC,
233                         mmc_removed,
234                         (void*)ops);
235         if (ret < 0)
236                 _E("Failed to register mmc removed signal handler(%d)", ret);
237 }
238
239 static void mmc_mount_status_changed(keynode_t *in_key, void *data)
240 {
241         const struct popup_ops *ops = data;
242
243         if (vconf_keynode_get_int(in_key) == VCONFKEY_SYSMAN_MMC_MOUNT_FAILED)
244                 return;
245
246         unload_simple_popup(ops);
247         terminate_if_no_popup();
248 }
249
250 static void mmc_unmount_status_changed(keynode_t *in_key, void *data)
251 {
252         const struct popup_ops *ops = data;
253
254         if (vconf_keynode_get_int(in_key) == VCONFKEY_SYSMAN_MMC_UNMOUNT_FAILED)
255                 return;
256
257         unload_simple_popup(ops);
258         terminate_if_no_popup();
259 }
260
261 static void register_mmc_mount_handler(const struct popup_ops *ops)
262 {
263         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
264                                 mmc_mount_status_changed, (void *)ops) != 0)
265                 _E("Failed to register mmc mount handler");
266 }
267
268 static void unregister_mmc_mount_handler(const struct popup_ops *ops)
269 {
270         vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
271                         mmc_mount_status_changed);
272 }
273
274 static void register_mmc_unmount_handler(const struct popup_ops *ops)
275 {
276         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
277                                 mmc_unmount_status_changed, (void *)ops) != 0)
278                 _E("Failed to register mmc mount handler");
279 }
280
281 static void unregister_mmc_unmount_handler(const struct popup_ops *ops)
282 {
283         vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
284                         mmc_unmount_status_changed);
285 }
286
287 static int launch_mmc_popup(bundle *b, const struct popup_ops *ops)
288 {
289         remove_other_mmc_popups(ops);
290         unregister_ode_handler(ops);
291
292         if (ops == &mount_error_ops)
293                 register_mmc_mount_handler(ops);
294
295         if (ops == &unmount_error_ops)
296                 register_mmc_unmount_handler(ops);
297
298         if (ops == &ode_encrypt_ops ||
299                 ops == &ode_decrypt_ops)
300                 register_ode_handler(ops);
301
302         return 0;
303 }
304
305 static void terminate_mmc_popup(const struct popup_ops *ops)
306 {
307         unregister_mmc_mount_handler(ops);
308         unregister_mmc_unmount_handler(ops);
309         unregister_ode_handler(ops);
310 }
311
312 static void launch_sdcard_setup(const struct popup_ops *ops)
313 {
314         unload_simple_popup(ops);
315         launch_app(EXT_SD_SETTING);
316         terminate_if_no_popup();
317 }
318
319 static int launch_ode(bundle *b, const struct popup_ops *ops)
320 {
321         if (!b || !ops)
322                 return -EINVAL;
323
324         View_Type = (char *)bundle_get_val(b, VIEWTYPE_KEY);
325         Dev_Path = (char *)bundle_get_val(b, DEVPATH_KEY);
326         Mapping_Node = (char *)bundle_get_val(b, MAPPINGNODE_KEY);
327
328         if (!View_Type || !Dev_Path || !Mapping_Node) {
329                 _E("Failed to get data for ode");
330                 return -ENOENT;
331         }
332
333         launch_app(EXT_ODE);
334         return 0;
335 }
336
337 static int sdcard_setup_get_content(const struct popup_ops *ops, char *content, unsigned int len)
338 {
339         int ret;
340         char *text;
341         struct object_ops *obj;
342
343         if (!ops || !content)
344                 return -EINVAL;
345
346         ret = get_object_by_ops(ops, &obj);
347         if (ret < 0) {
348                 _E("Failed to get object (%d)", ret);
349                 return -ENOENT;
350         }
351
352         Sdcard_ID = (char *)bundle_get_val(obj->b, SDCARD_KEY);
353         if (!Sdcard_ID) {
354                 _E("Failed to get SDCARD ID");
355                 return -ENOENT;
356         }
357
358         text = _("IDS_ST_BODY_THERE_IS_ALREADY_DATA_FROM_ANOTHER_PHONE_STORED_ON_THIS_SD_CARD_TO_USE_THIS_SD_CARD_ON_THIS_PHONE_TAP_SET_UP_TO_FORMAT_MSG");
359         snprintf(content, len, "%s", text);
360
361         return 0;
362 }
363
364 static const struct popup_ops mount_error_ops = {
365         .name           = "mounterr",//"mmc_mount_error",
366         .pattern        = FEEDBACK_PATTERN_LOWBATT,
367         .show           = load_simple_popup,
368         .title          = "IDS_IDLE_HEADER_COULDNT_MOUNT_SD_CARD_ABB",
369         .content        = "IDS_IDLE_POP_REMOVE_AND_REINSERT_YOUR_SD_CARD_OR_FORMAT_IT_THEN_TRY_AGAIN",
370         .left_text      = "IDS_COM_SK_OK",
371         .skip           = skip_mount_error_popup,
372         .pre            = launch_mmc_popup,
373 };
374
375 static const struct popup_ops unmount_error_ops = {
376         .name           = "unmounterr",//"mmc_unmount_error",
377         .pattern        = FEEDBACK_PATTERN_LOWBATT,
378         .show           = load_simple_popup,
379         .title          = "IDS_IDLE_HEADER_COULDNT_UNMOUNT_SD_CARD_ABB",
380         .content        = "IDS_IDLE_POP_THE_SD_CARD_MAY_BE_IN_USE_TRY_AGAIN_LATER",
381         .left_text      = "IDS_COM_SK_OK",
382         .skip           = skip_unmount_error_popup,
383         .pre            = launch_mmc_popup,
384 };
385
386 static const struct popup_ops mount_read_only_ops = {
387         .name           = "mountrdonly",//"mmc_mount_read_only",
388         .pattern        = FEEDBACK_PATTERN_LOWBATT,
389         .show           = load_simple_popup,
390         .content        = "IDS_ST_BODY_SD_CARD_MOUNTED_READ_ONLY",
391         .left_text      = "IDS_COM_SK_OK",
392         .pre            = launch_mmc_popup,
393 };
394
395 static const struct popup_ops check_smack_ops = {
396         .name           = "checksmack",//"mmc_check_smack",
397         .pattern        = FEEDBACK_PATTERN_LOWBATT,
398         .show           = load_simple_popup,
399         .content        = "IDS_MF_BODY_MMC_DATA_IS_INITIALIZING_ING",
400         .left_text      = "IDS_COM_SK_OK",
401         .pre            = launch_mmc_popup,
402 };
403
404 static const struct popup_ops ode_encrypt_ops = {
405         .name           = "odeencrypt",//"mmc_ode_encrypt",
406         .pattern        = FEEDBACK_PATTERN_LOWBATT,
407         .show           = load_simple_popup,
408         .title          = "IDS_DN_BODY_ENCRYPT_SD_CARD",
409         .content        = "IDS_ST_POP_TO_USE_YOUR_SD_CARD_IT_MUST_BE_ENCRYPTED_ENCRYPT_SD_CARD_OR_DISABLE_DEVICE_ENCRYPTION_Q",
410         .left_text      = "IDS_ST_BUTTON_ENCRYPT_SD_CARD_ABB",
411         .left           = ode_launch_settings,
412         .right_text     = "IDS_ST_BUTTON_DISABLE_ENCRYPTION_ABB",
413         .right          = send_general_mount_signal,
414         .skip           = skip_ode_popup,
415         .pre            = launch_mmc_popup,
416         .terminate      = terminate_mmc_popup,
417 };
418
419 static const struct popup_ops ode_decrypt_ops = {
420         .name           = "odedecrypt",//"mmc_ode_decrypt",
421         .pattern        = FEEDBACK_PATTERN_LOWBATT,
422         .show           = load_simple_popup,
423         .title          = "IDS_DN_BODY_DECRYPT_SD_CARD",
424         .content        = "IDS_ST_POP_TO_USE_YOUR_SD_CARD_IT_MUST_BE_DECRYPTED_DECRYPT_SD_CARD_OR_ENABLE_DEVICE_ENCRYPTION_Q",
425         .left_text      = "IDS_ST_BUTTON_DECRYPT_SD_CARD_ABB",
426         .left           = ode_launch_settings,
427         .right_text     = "IDS_ST_BUTTON_ENABLE_ENCRYPTION_ABB",
428         .right          = send_ode_mount_signal,
429         .skip           = skip_ode_popup,
430         .pre            = launch_mmc_popup,
431         .terminate      = terminate_mmc_popup,
432 };
433
434 static const struct popup_ops sdcard_setup_ops = {
435         .name           = "sdcardsetup",
436         .pattern        = FEEDBACK_PATTERN_LOWBATT,
437         .show           = load_simple_popup,
438         .title          = "IDS_ST_HEADER_COULDNT_USE_SD_CARD",
439         .get_content    = sdcard_setup_get_content,
440         .left_text      = "IDS_COM_SK_CANCEL",
441         .right_text     = "IDS_TPLATFORM_BUTTON_SET_UP_ABB",
442         .right          = launch_sdcard_setup,
443         .pre            = launch_mmc_popup,
444 };
445
446 static const struct popup_ops unlock_extendedsd_ops = {
447         .name           = "unlockextendedsd",
448         .show           = launch_ode,
449 };
450
451 /* Constructor to register mmc popup */
452 static __attribute__ ((constructor)) void register_mmc_popup(void)
453 {
454         register_popup(&mount_error_ops);
455         register_popup(&unmount_error_ops);
456         register_popup(&mount_read_only_ops);
457         register_popup(&check_smack_ops);
458         register_popup(&ode_encrypt_ops);
459         register_popup(&ode_decrypt_ops);
460         register_popup(&sdcard_setup_ops);
461         register_popup(&unlock_extendedsd_ops);
462 }