Change string for sd card
[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
23 #define SETTING_MMC_ENCRYPTION_UG  "setting-mmc-encryption-efl"
24
25 #define DD_BUS_NAME             "org.tizen.system.deviced"
26 #define DD_OBJECT_PATH_ODE      "/Org/Tizen/System/DeviceD/Ode"
27 #define DD_INTERFACE_NAME_ODE   DD_BUS_NAME".ode"
28 #define DD_SIGNAL_GENERAL_MOUNT "RequestGeneralMount"
29 #define DD_SIGNAL_ODE_MOUNT     "RequestOdeMount"
30 #define DD_SIGNAL_REMOVE_MMC    "RemoveMmc"
31
32 static const struct popup_ops mount_error_ops;
33 static const struct popup_ops unmount_error_ops;
34 static const struct popup_ops mount_read_only_ops;
35 static const struct popup_ops check_smack_ops;
36 static const struct popup_ops ode_encrypt_ops;
37 static const struct popup_ops ode_decrypt_ops;
38
39 static void remove_other_mmc_popups(const struct popup_ops *ops)
40 {
41         if (ops != &mount_error_ops)
42                 unload_simple_popup(&mount_error_ops);
43
44         if (ops != &unmount_error_ops)
45                 unload_simple_popup(&unmount_error_ops);
46
47         if (ops != &mount_read_only_ops)
48                 unload_simple_popup(&mount_read_only_ops);
49
50         if (ops != &check_smack_ops)
51                 unload_simple_popup(&check_smack_ops);
52
53         if (ops != &ode_encrypt_ops)
54                 unload_simple_popup(&ode_encrypt_ops);
55
56         if (ops != &ode_decrypt_ops)
57                 unload_simple_popup(&ode_decrypt_ops);
58 }
59
60 static bool mmc_inserted(void)
61 {
62         int val;
63         if (vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &val) == 0
64                         && val != VCONFKEY_SYSMAN_MMC_REMOVED)
65                 return true;
66         return false;
67 }
68
69 static bool mmc_mounted(void)
70 {
71         int val;
72         if (vconf_get_int(VCONFKEY_SYSMAN_MMC_MOUNT, &val) == 0
73                         && val == VCONFKEY_SYSMAN_MMC_MOUNT_FAILED)
74                 return false;
75         return true;
76 }
77
78 static bool mmc_unmounted(void)
79 {
80         int val;
81
82         if (vconf_get_int(VCONFKEY_SYSMAN_MMC_UNMOUNT, &val) == 0
83                         && val == VCONFKEY_SYSMAN_MMC_UNMOUNT_FAILED)
84                 return false;
85         return true;
86 }
87
88 static void send_mount_signal(char *signal)
89 {
90         int ret;
91
92         if (!signal)
93                 return;
94
95         ret = broadcast_dbus_signal(
96                         DD_OBJECT_PATH_ODE,
97                         DD_INTERFACE_NAME_ODE,
98                         signal,
99                         NULL, NULL);
100         if (ret < 0)
101                 _E("FAIL: broadcast_dbus_signal(%s:%d)", signal, ret);
102 }
103
104 static void launch_app(char *appname)
105 {
106         bundle *b;
107         int ret;
108
109         if (!appname)
110                 return;
111
112         b = bundle_create();
113         if (b) {
114                 ret = aul_launch_app(appname, b);
115                 if (ret < 0)
116                         _E("FAIL: aul_launch_app(%d)", ret);
117                 if (bundle_free(b) != 0)
118                         _E("FAIL: bundle_free(b);");
119         } else {
120                 _E("Failed to create bundle");
121         }
122 }
123
124 static void send_general_mount_signal(const struct popup_ops *ops)
125 {
126         unload_simple_popup(ops);
127         send_mount_signal(DD_SIGNAL_GENERAL_MOUNT);
128         terminate_if_no_popup();
129 }
130
131 static void send_ode_mount_signal(const struct popup_ops *ops)
132 {
133         unload_simple_popup(ops);
134         send_mount_signal(DD_SIGNAL_ODE_MOUNT);
135         terminate_if_no_popup();
136 }
137
138 static void ode_launch_settings(const struct popup_ops *ops)
139 {
140         unload_simple_popup(ops);
141         launch_app(SETTING_MMC_ENCRYPTION_UG);
142         terminate_if_no_popup();
143 }
144
145 static bool skip_mount_error_popup(bundle *b, const struct popup_ops *ops)
146 {
147         return mmc_mounted();
148 }
149
150 static bool skip_unmount_error_popup(bundle *b, const struct popup_ops *ops)
151 {
152         return mmc_unmounted();
153 }
154
155 static bool skip_ode_popup(bundle *b, const struct popup_ops *ops)
156 {
157         return !mmc_inserted();
158 }
159
160 static E_DBus_Signal_Handler *mmc_removed_handler= NULL;
161
162 static void unregister_ode_handler(const struct popup_ops *ops)
163 {
164         if (mmc_removed_handler) {
165                 unregister_dbus_signal_handler(mmc_removed_handler);
166                 mmc_removed_handler = NULL;
167         }
168 }
169
170 static void mmc_removed(void *data, DBusMessage *msg)
171 {
172         const struct popup_ops *ops = data;
173
174         unregister_ode_handler(ops);
175         unload_simple_popup(ops);
176         terminate_if_no_popup();
177 }
178
179 static void register_ode_handler(const struct popup_ops *ops)
180 {
181         int ret;
182
183         unregister_ode_handler(ops);
184
185         ret = register_dbus_signal_handler(&mmc_removed_handler,
186                         DD_OBJECT_PATH_ODE,
187                         DD_INTERFACE_NAME_ODE,
188                         DD_SIGNAL_REMOVE_MMC,
189                         mmc_removed,
190                         (void*)ops);
191         if (ret < 0)
192                 _E("Failed to register mmc removed signal handler(%d)", ret);
193 }
194
195 static void mmc_mount_status_changed(keynode_t *in_key, void *data)
196 {
197         const struct popup_ops *ops = data;
198
199         if (vconf_keynode_get_int(in_key) == VCONFKEY_SYSMAN_MMC_MOUNT_FAILED)
200                 return;
201
202         unload_simple_popup(ops);
203         terminate_if_no_popup();
204 }
205
206 static void mmc_unmount_status_changed(keynode_t *in_key, void *data)
207 {
208         const struct popup_ops *ops = data;
209
210         if (vconf_keynode_get_int(in_key) == VCONFKEY_SYSMAN_MMC_UNMOUNT_FAILED)
211                 return;
212
213         unload_simple_popup(ops);
214         terminate_if_no_popup();
215 }
216
217 static void register_mmc_mount_handler(const struct popup_ops *ops)
218 {
219         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
220                                 mmc_mount_status_changed, (void *)ops) != 0)
221                 _E("Failed to register mmc mount handler");
222 }
223
224 static void unregister_mmc_mount_handler(const struct popup_ops *ops)
225 {
226         vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
227                         mmc_mount_status_changed);
228 }
229
230 static void register_mmc_unmount_handler(const struct popup_ops *ops)
231 {
232         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
233                                 mmc_unmount_status_changed, (void *)ops) != 0)
234                 _E("Failed to register mmc mount handler");
235 }
236
237 static void unregister_mmc_unmount_handler(const struct popup_ops *ops)
238 {
239         vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
240                         mmc_unmount_status_changed);
241 }
242
243 static int launch_mmc_popup(bundle *b, const struct popup_ops *ops)
244 {
245         remove_other_mmc_popups(ops);
246         unregister_ode_handler(ops);
247
248         if (ops == &mount_error_ops)
249                 register_mmc_mount_handler(ops);
250
251         if (ops == &unmount_error_ops)
252                 register_mmc_unmount_handler(ops);
253
254         if (ops == &ode_encrypt_ops ||
255                 ops == &ode_decrypt_ops)
256                 register_ode_handler(ops);
257
258         return 0;
259 }
260
261 static void terminate_mmc_popup(const struct popup_ops *ops)
262 {
263         unregister_mmc_mount_handler(ops);
264         unregister_mmc_unmount_handler(ops);
265         unregister_ode_handler(ops);
266 }
267
268 static const struct popup_ops mount_error_ops = {
269         .name           = "mounterr",//"mmc_mount_error",
270         .show           = load_simple_popup,
271         .title          = "IDS_IDLE_HEADER_COULDNT_MOUNT_SD_CARD_ABB",
272         .content        = "IDS_IDLE_POP_REMOVE_AND_REINSERT_OR_FORMAT_YOUR_SD_CARD_THEN_TRY_AGAIN",
273         .left_text      = "IDS_COM_SK_OK",
274         .skip           = skip_mount_error_popup,
275         .pre            = launch_mmc_popup,
276 };
277
278 static const struct popup_ops unmount_error_ops = {
279         .name           = "unmounterr",//"mmc_unmount_error",
280         .show           = load_simple_popup,
281         .title          = "IDS_IDLE_HEADER_COULDNT_UNMOUNT_SD_CARD_ABB",
282         .content        = "IDS_IDLE_POP_THE_SD_CARD_MAY_BE_IN_USE_TRY_AGAIN_LATER",
283         .left_text      = "IDS_COM_SK_OK",
284         .skip           = skip_unmount_error_popup,
285         .pre            = launch_mmc_popup,
286 };
287
288 static const struct popup_ops mount_read_only_ops = {
289         .name           = "mountrdonly",//"mmc_mount_read_only",
290         .show           = load_simple_popup,
291         .content        = "IDS_ST_BODY_SD_CARD_MOUNTED_READ_ONLY",
292         .left_text      = "IDS_COM_SK_OK",
293         .pre            = launch_mmc_popup,
294 };
295
296 static const struct popup_ops check_smack_ops = {
297         .name           = "checksmack",//"mmc_check_smack",
298         .show           = load_simple_popup,
299         .content        = "IDS_MF_BODY_MMC_DATA_IS_INITIALIZING_ING",
300         .left_text      = "IDS_COM_SK_OK",
301         .pre            = launch_mmc_popup,
302 };
303
304 static const struct popup_ops ode_encrypt_ops = {
305         .name           = "odeencrypt",//"mmc_ode_encrypt",
306         .show           = load_simple_popup,
307         .title          = "IDS_DN_BODY_ENCRYPT_SD_CARD",
308         .content        = "IDS_ST_POP_TO_USE_YOUR_SD_CARD_IT_MUST_BE_ENCRYPTED_ENCRYPT_SD_CARD_OR_DISABLE_DEVICE_ENCRYPTION_Q",
309         .left_text      = "IDS_ST_BUTTON_ENCRYPT_SD_CARD_ABB",
310         .left           = ode_launch_settings,
311         .right_text     = "IDS_ST_BUTTON_DISABLE_ENCRYPTION_ABB",
312         .right          = send_general_mount_signal,
313         .skip           = skip_ode_popup,
314         .pre            = launch_mmc_popup,
315         .terminate      = terminate_mmc_popup,
316 };
317
318 static const struct popup_ops ode_decrypt_ops = {
319         .name           = "odedecrypt",//"mmc_ode_decrypt",
320         .show           = load_simple_popup,
321         .title          = "IDS_DN_BODY_DECRYPT_SD_CARD",
322         .content        = "IDS_ST_POP_TO_USE_YOUR_SD_CARD_IT_MUST_BE_DECRYPTED_DECRYPT_SD_CARD_OR_ENABLE_DEVICE_ENCRYPTION_Q",
323         .left_text      = "IDS_ST_BUTTON_DECRYPT_SD_CARD_ABB",
324         .left           = ode_launch_settings,
325         .right_text     = "IDS_ST_BUTTON_ENABLE_ENCRYPTION_ABB",
326         .right          = send_ode_mount_signal,
327         .skip           = skip_ode_popup,
328         .pre            = launch_mmc_popup,
329         .terminate      = terminate_mmc_popup,
330 };
331
332 /* Constructor to register mmc popup */
333 static __attribute__ ((constructor)) void register_mmc_popup(void)
334 {
335         register_popup(&mount_error_ops);
336         register_popup(&unmount_error_ops);
337         register_popup(&mount_read_only_ops);
338         register_popup(&check_smack_ops);
339         register_popup(&ode_encrypt_ops);
340         register_popup(&ode_decrypt_ops);
341 }