e167748ce1d5986de6213914d9be4f1c6f633e12
[platform/core/system/system-popup.git] / src / mmc / mmc-mobile.c
1 /*
2  *  system-popup
3  *
4  * Copyright (c) 2014 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 mount_read_only_ops;
34 static const struct popup_ops check_smack_ops;
35 static const struct popup_ops ode_encrypt_ops;
36 static const struct popup_ops ode_decrypt_ops;
37
38 static void remove_other_mmc_popups(const struct popup_ops *ops)
39 {
40         if (ops != &mount_error_ops)
41                 unload_simple_popup(&mount_error_ops);
42
43         if (ops != &mount_read_only_ops)
44                 unload_simple_popup(&mount_read_only_ops);
45
46         if (ops != &check_smack_ops)
47                 unload_simple_popup(&check_smack_ops);
48
49         if (ops != &ode_encrypt_ops)
50                 unload_simple_popup(&ode_encrypt_ops);
51
52         if (ops != &ode_decrypt_ops)
53                 unload_simple_popup(&ode_decrypt_ops);
54 }
55
56 static bool mmc_inserted(void)
57 {
58         int val;
59         if (vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &val) == 0
60                         && val != VCONFKEY_SYSMAN_MMC_REMOVED)
61                 return true;
62         return false;
63 }
64
65 static bool mmc_mounted(void)
66 {
67         int val;
68         if (vconf_get_int(VCONFKEY_SYSMAN_MMC_MOUNT, &val) == 0
69                         && val == VCONFKEY_SYSMAN_MMC_MOUNT_FAILED)
70                 return false;
71         return true;
72 }
73
74 static void send_mount_signal(char *signal)
75 {
76         int ret;
77
78         if (!signal)
79                 return;
80
81         ret = broadcast_dbus_signal(
82                         DD_OBJECT_PATH_ODE,
83                         DD_INTERFACE_NAME_ODE,
84                         signal,
85                         NULL, NULL);
86         if (ret < 0)
87                 _E("FAIL: broadcast_dbus_signal(%s:%d)", signal, ret);
88 }
89
90 static void launch_app(char *appname)
91 {
92         bundle *b;
93         int ret;
94
95         if (!appname)
96                 return;
97
98         b = bundle_create();
99         if (b) {
100                 ret = aul_launch_app(appname, b);
101                 if (ret < 0)
102                         _E("FAIL: aul_launch_app(%d)", ret);
103                 if (bundle_free(b) != 0)
104                         _E("FAIL: bundle_free(b);");
105         } else {
106                 _E("Failed to create bundle");
107         }
108 }
109
110 static void send_general_mount_signal(const struct popup_ops *ops)
111 {
112         unload_simple_popup(ops);
113         send_mount_signal(DD_SIGNAL_GENERAL_MOUNT);
114         terminate_if_no_popup();
115 }
116
117 static void send_ode_mount_signal(const struct popup_ops *ops)
118 {
119         unload_simple_popup(ops);
120         send_mount_signal(DD_SIGNAL_ODE_MOUNT);
121         terminate_if_no_popup();
122 }
123
124 static void ode_launch_settings(const struct popup_ops *ops)
125 {
126         unload_simple_popup(ops);
127         launch_app(SETTING_MMC_ENCRYPTION_UG);
128         terminate_if_no_popup();
129 }
130
131 static bool skip_mount_error_popup(bundle *b, const struct popup_ops *ops)
132 {
133         return mmc_mounted();
134 }
135
136 static bool skip_ode_popup(bundle *b, const struct popup_ops *ops)
137 {
138         return !mmc_inserted();
139 }
140
141 static E_DBus_Signal_Handler *mmc_removed_handler= NULL;
142
143 static void unregister_ode_handler(const struct popup_ops *ops)
144 {
145         if (mmc_removed_handler) {
146                 unregister_dbus_signal_handler(mmc_removed_handler);
147                 mmc_removed_handler = NULL;
148         }
149 }
150
151 static void mmc_removed(void *data, DBusMessage *msg)
152 {
153         const struct popup_ops *ops = data;
154
155         unregister_ode_handler(ops);
156         unload_simple_popup(ops);
157         terminate_if_no_popup();
158 }
159
160 static void register_ode_handler(const struct popup_ops *ops)
161 {
162         int ret;
163
164         unregister_ode_handler(ops);
165
166         ret = register_dbus_signal_handler(&mmc_removed_handler,
167                         DD_OBJECT_PATH_ODE,
168                         DD_INTERFACE_NAME_ODE,
169                         DD_SIGNAL_REMOVE_MMC,
170                         mmc_removed,
171                         (void*)ops);
172         if (ret < 0)
173                 _E("Failed to register mmc removed signal handler(%d)", ret);
174 }
175
176 static void mmc_mount_status_changed(keynode_t *in_key, void *data)
177 {
178         const struct popup_ops *ops = data;
179
180         if (vconf_keynode_get_int(in_key) == VCONFKEY_SYSMAN_MMC_MOUNT_FAILED)
181                 return;
182
183         unload_simple_popup(ops);
184         terminate_if_no_popup();
185 }
186
187 static void register_mmc_mount_handler(const struct popup_ops *ops)
188 {
189         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
190                                 mmc_mount_status_changed, (void *)ops) != 0)
191                 _E("Failed to register mmc mount handler");
192 }
193
194 static void unregister_mmc_mount_handler(const struct popup_ops *ops)
195 {
196         vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
197                         mmc_mount_status_changed);
198 }
199
200 static int launch_mmc_popup(bundle *b, const struct popup_ops *ops)
201 {
202         remove_other_mmc_popups(ops);
203         unregister_ode_handler(ops);
204
205         if (ops == &mount_error_ops)
206                 register_mmc_mount_handler(ops);
207
208         if (ops == &ode_encrypt_ops ||
209                 ops == &ode_decrypt_ops)
210                 register_ode_handler(ops);
211
212         return 0;
213 }
214
215 static void terminate_mmc_popup(const struct popup_ops *ops)
216 {
217         unregister_mmc_mount_handler(ops);
218         unregister_ode_handler(ops);
219 }
220
221 static const struct popup_ops mount_error_ops = {
222         .name           = "mounterr",//"mmc_mount_error",
223         .show           = load_simple_popup,
224         .content        = "IDS_DN_POP_FAILED_TO_MOUNT_SD_CARD_REINSERT_OR_FORMAT_SD_CARD",
225         .left_text      = "IDS_COM_SK_OK",
226         .skip           = skip_mount_error_popup,
227         .pre            = launch_mmc_popup,
228 };
229
230 static const struct popup_ops mount_read_only_ops = {
231         .name           = "mountrdonly",//"mmc_mount_read_only",
232         .show           = load_simple_popup,
233         .content        = "IDS_ST_BODY_SD_CARD_MOUNTED_READ_ONLY",
234         .left_text      = "IDS_COM_SK_OK",
235         .pre            = launch_mmc_popup,
236 };
237
238 static const struct popup_ops check_smack_ops = {
239         .name           = "checksmack",//"mmc_check_smack",
240         .show           = load_simple_popup,
241         .content        = "IDS_MF_BODY_MMC_DATA_IS_INITIALIZING_ING",
242         .left_text      = "IDS_COM_SK_OK",
243         .pre            = launch_mmc_popup,
244 };
245
246 static const struct popup_ops ode_encrypt_ops = {
247         .name           = "odeencrypt",//"mmc_ode_encrypt",
248         .show           = load_simple_popup,
249         .title          = "IDS_DN_BODY_ENCRYPT_SD_CARD",
250         .content        = "IDS_ST_POP_TO_USE_YOUR_SD_CARD_IT_MUST_BE_ENCRYPTED_ENCRYPT_SD_CARD_OR_DISABLE_DEVICE_ENCRYPTION_Q",
251         .left_text      = "IDS_ST_BUTTON_ENCRYPT_SD_CARD_ABB",
252         .left           = ode_launch_settings,
253         .right_text     = "IDS_ST_BUTTON_DISABLE_ENCRYPTION_ABB",
254         .right          = send_general_mount_signal,
255         .skip           = skip_ode_popup,
256         .pre            = launch_mmc_popup,
257         .terminate      = terminate_mmc_popup,
258 };
259
260 static const struct popup_ops ode_decrypt_ops = {
261         .name           = "odedecrypt",//"mmc_ode_decrypt",
262         .show           = load_simple_popup,
263         .title          = "IDS_DN_BODY_DECRYPT_SD_CARD",
264         .content        = "IDS_ST_POP_TO_USE_YOUR_SD_CARD_IT_MUST_BE_DECRYPTED_DECRYPT_SD_CARD_OR_ENABLE_DEVICE_ENCRYPTION_Q",
265         .left_text      = "IDS_ST_BUTTON_DECRYPT_SD_CARD_ABB",
266         .left           = ode_launch_settings,
267         .right_text     = "IDS_ST_BUTTON_ENABLE_ENCRYPTION_ABB",
268         .right          = send_ode_mount_signal,
269         .skip           = skip_ode_popup,
270         .pre            = launch_mmc_popup,
271         .terminate      = terminate_mmc_popup,
272 };
273
274 /* Constructor to register mmc popup */
275 static __attribute__ ((constructor)) void register_mmc_popup(void)
276 {
277         register_popup(&mount_error_ops);
278         register_popup(&mount_read_only_ops);
279         register_popup(&check_smack_ops);
280         register_popup(&ode_encrypt_ops);
281         register_popup(&ode_decrypt_ops);
282 }