2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <unique_ptr.h>
20 #include <libxml/parser.h>
21 #include <libxml/tree.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>
30 #define _SysTryReturn(NID, condition, returnValue, ...) \
32 SysLog(NID, __VA_ARGS__); \
37 #define _SysTryCatch(NID, condition, expr, ...) \
39 SysLog(NID, __VA_ARGS__); \
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Utility;
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";
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";
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";
64 static const char _DEFAULT_LOCALE[] = "default";
67 __attribute__ ((visibility("default")))
68 int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId)
70 SysLog(NID_SCL, "Registering the Account Provider.");
72 int ret = account_connect();
73 _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
75 account_type_h accountTypeHandle = null;
76 ret = account_type_create(&accountTypeHandle);
77 if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
79 ret = account_disconnect();
80 _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
82 SysLog(NID_SCL, "Memory allocation failed.");
87 xmlNodePtr curPtr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
88 _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the element.");
90 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
92 // Get the children nodes
93 curPtr = curPtr->xmlChildrenNode;
94 _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the child element.");
98 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
100 // Node: <account-provider>
101 if ((!xmlStrcmp(curPtr->name, _NODE_ACCOUNT_PROVIDER)))
104 xmlChar* pAppIdId = xmlGetProp(curPtr, _ATTRIBUTE_APP_ID);
105 _SysTryCatch(NID_SCL, pAppIdId != null, ret = -EINVAL, "Failed to get the attribute.");
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.");
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.");
115 SysSecureLog(NID_SCL, "Attribute: multiple-accounts-support - %s", pMultipleAccountsSupport);
116 if ((!xmlStrcmp(pMultipleAccountsSupport, _VALUE_TRUE)))
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.");
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.");
127 // Get the children nodes
128 curPtr = curPtr->xmlChildrenNode;
129 _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the child element.");
131 while (curPtr != NULL)
133 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
136 if ((!xmlStrcmp(curPtr->name, _NODE_ICON)))
138 // Attribute: section
139 xmlChar* pSection = xmlGetProp(curPtr, _ATTRIBUTE_SECTION);
140 _SysTryCatch(NID_SCL, pSection != null, ret = -EINVAL, "Failed to get the attribute.");
142 SysSecureLog(NID_SCL, "Attribute: section - %s", pSection);
144 if ((!xmlStrcmp(pSection, _VALUE_ACCOUNT)))
146 xmlChar* pAccountIcon = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
147 _SysTryCatch(NID_SCL, pAccountIcon != null, ret = -EINVAL, "Failed to get the value.");
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.");
153 else if ((!xmlStrcmp(pSection, _VALUE_ACCOUNT_SMALL)))
155 xmlChar* pAccountSmallIcon = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
156 _SysTryCatch(NID_SCL, pAccountSmallIcon != null, ret = -EINVAL, "Failed to get the value.");
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.");
164 else if ((!xmlStrcmp(curPtr->name, _NODE_LABEL)))
166 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
168 // Attribute: xml:lang
169 xmlChar* pLang = xmlNodeGetLang(curPtr);
172 SysSecureLog(NID_SCL, "Attribute: xml:lang - %s", pLang);
174 String lang(reinterpret_cast<char*> (pLang));
176 StringTokenizer strTok(lang, L"-");
178 String convertedLang;
180 strTok.GetNextToken(convertedLang);
181 convertedLang.Append(L"_");
183 strTok.GetNextToken(token);
185 convertedLang.Append(token);
187 std::unique_ptr<ByteBuffer> pConvertedLangBuf(StringUtil::StringToUtf8N(convertedLang));
188 _SysTryCatch(NID_SCL, pConvertedLangBuf != null, ret = -1, "Failed to convert String to Utf8N.");
190 SysSecureLog(NID_SCL, "Attribute: converted lang - %s", pConvertedLangBuf->GetPointer());
192 xmlChar* pLabel = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
193 _SysTryCatch(NID_SCL, pLabel != null, ret = -EINVAL, "Failed to get the value.");
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.");
201 xmlChar* pLabel = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
202 _SysTryCatch(NID_SCL, pLabel != null, ret = -EINVAL, "Failed to get the value.");
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.");
209 // Node: <capability>
210 else if ((!xmlStrcmp(curPtr->name, _NODE_CAPABILITY)))
212 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
214 xmlChar* pCapability = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
215 _SysTryCatch(NID_SCL, pCapability != null, ret = -EINVAL, "Failed to get the value.");
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.");
222 curPtr = curPtr->next;
228 curPtr = curPtr->next;
231 // Insert the account to the account DB
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.");
238 account_type_destroy(accountTypeHandle);
239 ret = account_disconnect();
240 _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
245 account_type_destroy(accountTypeHandle);
246 _SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
252 __attribute__ ((visibility("default")))
253 int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr docPtr, const char* packageId)
255 SysLog(NID_SCL, "Unregistering the Account Provider.");
257 int ret = account_connect();
258 _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
261 xmlNodePtr curPtr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
262 _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the element.");
264 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
266 // Get the children nodes
267 curPtr = curPtr->xmlChildrenNode;
268 _SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the child element.");
270 while(curPtr != null)
272 SysSecureLog(NID_SCL, "Node: %s", curPtr->name);
274 // Node: <account-provider>
275 if ((!xmlStrcmp(curPtr->name, _NODE_ACCOUNT_PROVIDER)))
278 xmlChar* pAppIdId = xmlGetProp(curPtr, _ATTRIBUTE_APP_ID);
279 _SysTryCatch(NID_SCL, pAppIdId != null, ret = -EINVAL, "Failed to get the attribute.");
281 SysSecureLog(NID_SCL, "Attribute: appid - %s", pAppIdId);
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.");
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.");
292 curPtr = curPtr->next;
295 ret = account_disconnect();
296 _SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");
301 _SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");