Fix the boiler plate codes
[framework/osp/social.git] / pkgmgr_account / src / account.cpp
1 //
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <errno.h>
18 #include <new>
19 #include <unique_ptr.h>
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
22 #include <account.h>
23 #include <account-types.h>
24 #include <FBaseDataType.h>
25 #include <FBaseString.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseUtilStringTokenizer.h>
28 #include <FBaseUtilStringUtil.h>
29
30 #define _SysTryReturn(NID, condition, returnValue, ...) \
31         if (!(condition)) { \
32                 SysLog(NID, __VA_ARGS__); \
33                 return returnValue;     \
34         } \
35         else {;}
36
37 #define _SysTryCatch(NID, condition, expr, ...) \
38         if (!(condition)) { \
39                 SysLog(NID, __VA_ARGS__); \
40                 expr; \
41                 goto CATCH;     \
42         } \
43         else {;}
44
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Utility;
47
48 static const xmlChar _NODE_ACCOUNT_PROVIDER[]                           = "account-provider";
49 static const xmlChar _NODE_ICON[]                                                       = "icon";
50 static const xmlChar _NODE_LABEL[]                                                      = "label";
51 static const xmlChar _NODE_CAPABILITY[]                                         = "capability";
52
53 static const xmlChar _ATTRIBUTE_APP_ID[]                                        = "appid";
54 static const xmlChar _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT[]     = "multiple-accounts-support";
55 static const xmlChar _ATTRIBUTE_SECTION[]                                       = "section";
56 static const xmlChar _ATTRIBUTE_TYPE[]                                          = "type";
57 static const xmlChar _ATTRIBUTE_XML_LANG[]                                      = "xml:lang";
58
59 static const xmlChar _VALUE_TRUE[]                                                      = "true";
60 static const xmlChar _VALUE_ACCOUNT[]                                           = "account";
61 static const xmlChar _VALUE_ACCOUNT_SMALL[]                                     = "account-small";
62 static const xmlChar _VALUE_XHIGH[]                                                     = "Xhigh";
63
64 static const char _DEFAULT_LOCALE[]                                                     = "default";
65
66 extern "C"
67 __attribute__ ((visibility("default")))
68 int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId)
69 {
70         SysLog(NID_SCL, "Registering the Account Provider.");
71
72         int ret = account_connect();
73         _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
74
75         account_type_h accountTypeHandle = null;
76         ret = account_type_create(&accountTypeHandle);
77         if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
78         {
79                 ret = account_disconnect();
80                 _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
81
82                 SysLog(NID_SCL, "Memory allocation failed.");
83                 return -ENOMEM;
84         }
85
86         // Node: <account>
87         xmlNodePtr curPtr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
88         _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the element.");
89
90         SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
91
92         // Get the children nodes
93         curPtr = curPtr->xmlChildrenNode;
94         _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the child element.");
95
96         while(curPtr != null)
97         {
98                 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
99
100                 // Node: <account-provider>
101                 if ((!xmlStrcmp(curPtr->name, _NODE_ACCOUNT_PROVIDER)))
102                 {
103                         // Attribute: appid
104                         xmlChar* pAppIdId = xmlGetProp(curPtr, _ATTRIBUTE_APP_ID);
105                         _SysTryCatch(NID_SCL, pAppIdId != null, ret = -EINVAL, "Failed to get the attribute.");
106
107                         SysSecureLog(NID_SCL, "Attribute: appid - %s", pAppIdId);
108                         ret = account_type_set_app_id(accountTypeHandle, reinterpret_cast<char*> (pAppIdId));
109                         _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "Failed to set the app ID.");
110
111                         // Attribute: multiple-accounts-support
112                         xmlChar* pMultipleAccountsSupport = xmlGetProp(curPtr, _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT);
113                         _SysTryCatch(NID_SCL, pMultipleAccountsSupport != null, ret = -EINVAL, "Failed to get the attribute.");
114
115                         SysSecureLog(NID_SCL, "Attribute: multiple-accounts-support - %s", pMultipleAccountsSupport);
116                         if ((!xmlStrcmp(pMultipleAccountsSupport, _VALUE_TRUE)))
117                         {
118                                 ret = account_type_set_multiple_account_support(accountTypeHandle, true);
119                                 _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "Failed to set the multiple accounts support.");
120                         }
121                         else
122                         {
123                                 ret = account_type_set_multiple_account_support(accountTypeHandle, false);
124                                 _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "Failed to set the multiple accounts support.");
125                         }
126
127                         // Get the children nodes
128                         curPtr = curPtr->xmlChildrenNode;
129                         _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the child element.");
130
131                         while (curPtr != NULL)
132                         {
133                                 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
134
135                                 // Node: <icon>
136                                 if ((!xmlStrcmp(curPtr->name, _NODE_ICON)))
137                                 {
138                                         // Attribute: section
139                                         xmlChar* pSection = xmlGetProp(curPtr, _ATTRIBUTE_SECTION);
140                                         _SysTryCatch(NID_SCL, pSection != null, ret = -EINVAL, "Failed to get the attribute.");
141
142                                         SysSecureLog(NID_SCL, "Attribute: section - %s", pSection);
143
144                                         if ((!xmlStrcmp(pSection, _VALUE_ACCOUNT)))
145                                         {
146                                                 xmlChar* pAccountIcon = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
147                                                 _SysTryCatch(NID_SCL, pAccountIcon != null, ret = -EINVAL, "Failed to get the value.");
148
149                                                 SysSecureLog(NID_SCL, "Node: icon - %s", pAccountIcon);
150                                                 ret = account_type_set_icon_path(accountTypeHandle, reinterpret_cast<char*> (pAccountIcon));
151                                                 _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "Failed to set the icon path.");
152                                         }
153                                         else if ((!xmlStrcmp(pSection, _VALUE_ACCOUNT_SMALL)))
154                                         {
155                                                 xmlChar* pAccountSmallIcon = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
156                                                 _SysTryCatch(NID_SCL, pAccountSmallIcon != null, ret = -EINVAL, "Failed to get the value.");
157
158                                                 SysSecureLog(NID_SCL, "Node: icon (small) - %s",  pAccountSmallIcon);
159                                                 ret = account_type_set_small_icon_path(accountTypeHandle, reinterpret_cast<char*> (pAccountSmallIcon));
160                                                 _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "Failed to set the small icon path.");
161                                         }
162                                 }
163                                 // Node: <label>
164                                 else if ((!xmlStrcmp(curPtr->name, _NODE_LABEL)))
165                                 {
166                                         SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
167
168                                 // Attribute: xml:lang
169                                         xmlChar* pLang = xmlNodeGetLang(curPtr);
170                                         if (pLang != null)
171                                         {
172                                                 SysSecureLog(NID_SCL, "Attribute: xml:lang - %s", pLang);
173
174                                                 String lang(reinterpret_cast<char*> (pLang));
175
176                                                 StringTokenizer strTok(lang, L"-");
177                                                 String token;
178                                                 String convertedLang;
179
180                                                 strTok.GetNextToken(convertedLang);
181                                                 convertedLang.Append(L"_");
182
183                                                 strTok.GetNextToken(token);
184                                                 token.ToUpper();
185                                                 convertedLang.Append(token);
186
187                                                 std::unique_ptr<ByteBuffer> pConvertedLangBuf(StringUtil::StringToUtf8N(convertedLang));
188                                                 _SysTryCatch(NID_SCL, pConvertedLangBuf != null, ret = -1, "Failed to convert String to Utf8N.");
189
190                                                 SysSecureLog(NID_SCL, "Attribute: converted lang - %s", pConvertedLangBuf->GetPointer());
191
192                                                 xmlChar* pLabel = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
193                                                 _SysTryCatch(NID_SCL, pLabel != null, ret = -EINVAL, "Failed to get the value.");
194
195                                                 SysSecureLog(NID_SCL, "Node: label - %s", pLabel);
196                                                 ret = account_type_set_label(accountTypeHandle, reinterpret_cast<char*> (pLabel), reinterpret_cast<char*> (pConvertedLangBuf->GetPointer()));
197                                                 _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "Failed to set the display name.");
198                                         }
199                                         else
200                                         {
201                                                 xmlChar* pLabel = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
202                                                 _SysTryCatch(NID_SCL, pLabel != null, ret = -EINVAL, "Failed to get the value.");
203
204                                                 SysSecureLog(NID_SCL, "Node: label - %s",  pLabel);
205                                                 ret = account_type_set_label(accountTypeHandle, reinterpret_cast<char*> (pLabel), _DEFAULT_LOCALE);
206                                                 _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "Failed to set the display name.");
207                                         }
208                                 }
209                                 // Node: <capability>
210                                 else if ((!xmlStrcmp(curPtr->name, _NODE_CAPABILITY)))
211                                 {
212                                         SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
213
214                                         xmlChar* pCapability = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
215                                         _SysTryCatch(NID_SCL, pCapability != null, ret = -EINVAL, "Failed to get the value.");
216
217                                         SysSecureLog(NID_SCL, "Node: capability - %s",  pCapability);
218                                         ret = account_type_set_provider_feature(accountTypeHandle, reinterpret_cast<char*> (pCapability));
219                                         _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "Failed to set the capability.");
220                                 }
221
222                                 curPtr = curPtr->next;
223                         }
224
225                         break;
226                 }
227
228                 curPtr = curPtr->next;
229         }
230
231         // Insert the account to the account DB
232         {
233                 int accountTypeDbId = 0;
234                 ret = account_type_insert_to_db(accountTypeHandle, &accountTypeDbId);
235                 _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "A system error has occurred.");
236         }
237
238         account_type_destroy(accountTypeHandle);
239         ret = account_disconnect();
240         _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
241
242         return 0;
243
244 CATCH:
245         account_type_destroy(accountTypeHandle);
246         _SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
247
248         return ret;
249 }
250
251 extern "C"
252 __attribute__ ((visibility("default")))
253 int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr docPtr, const char* packageId)
254 {
255         SysLog(NID_SCL, "Unregistering the Account Provider.");
256
257         int ret = account_connect();
258         _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
259
260         // Node: <account>
261         xmlNodePtr curPtr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
262         _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the element.");
263
264         SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
265
266         // Get the children nodes
267         curPtr = curPtr->xmlChildrenNode;
268         _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the child element.");
269
270         while(curPtr != null)
271         {
272                 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
273
274                 // Node: <account-provider>
275                 if ((!xmlStrcmp(curPtr->name, _NODE_ACCOUNT_PROVIDER)))
276                 {
277                         // Attribute: appid
278                         xmlChar* pAppIdId = xmlGetProp(curPtr, _ATTRIBUTE_APP_ID);
279                         _SysTryCatch(NID_SCL, pAppIdId != null, ret = -EINVAL, "Failed to get the attribute.");
280
281                         SysSecureLog(NID_SCL, "Attribute: appid - %s", pAppIdId);
282
283                         ret = account_delete_from_db_by_package_name(reinterpret_cast<char*> (pAppIdId));
284                         _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "A system error has occurred.");
285
286                         ret = account_type_delete_by_app_id(reinterpret_cast<char*> (pAppIdId));
287                         _SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "A system error has occurred.");
288
289                         break;
290                 }
291
292                 curPtr = curPtr->next;
293         }
294
295         ret = account_disconnect();
296         _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
297
298         return 0;
299
300 CATCH:
301         _SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
302
303         return ret;
304 }