3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <stdio.h> /*for debugging */
23 #include "account/service.h"
26 #include "utility/sync_util.h"
27 #include "utility/fw_file.h"
29 #include "plugin/account_interface.h"
32 #define EXPORT_API __attribute__ ((visibility("default")))
35 #ifndef SYNC_AGENT_LOG
37 #define LOG_TAG "PLUGIN_ACCOUNT"
41 * Key : DEFAULT_ACCOUNT_PLUGIN_FOLDER/.[Agent Name]_Service_[Service_Type]_[FW AccountID]
42 * Value : Service Account ID
44 #define ACCOUNT_ID_SERVICE_ACCOUNT_ID ".%s_service_%d_%d"
47 * KEY : DEFAULT_ACCOUNT_PLUGIN_FOLDER/.[Agent Name]_FW_[Service_Type]_[Service AccountID]_[Index]
48 * Value : FW Account ID
50 #define ACCOUNT_ID_FW_ACCOUNT_ID ".%s_fw_%d_%d_%d"
53 * Key : DEFAULT_ACCOUNT_PLUGIN_FOLDER/.[Agent Name]_FW_Count_[Service_Type]_[Service AccountID]
54 * Value : FW Account Count Included in one service Account
56 #define ACCOUNT_ID_FW_ACCOUNT_COUNT ".%s_fw_count_%d_%d"
61 #define FW_CONTENT_COUNT 3
63 #define MAX_ACCOUNT_COUNT 20
65 static const char *agent_name = 0;
67 static void _set_account_id_service(int service_type, int fw_account_id, int service_account_id);
69 static void _arrange_account_id_fw(int service_type, int service_account_id, int index, int fw_account_id);
71 static void _set_account_id_fw(int service_type, int service_account_id, int count, int fw_account_id);
73 static int _increase_account_count_fw(int service_type, int service_account_id);
75 static int _get_account_count_fw(int service_type, int service_account_id);
77 static void _unset_account_id_service(int service_type, int fw_account_id);
79 static void _unset_account_id_fw(int service_type, int service_account_id, int index);
81 static int _decrease_account_count_fw(int service_type, int service_account_id);
83 static int _get_account_info(int service_account_id, sync_agent_service_account_info_s ** account_info);
85 EXPORT_API void sync_agent_plugin_set_account_repository_name(const char *input_agent_name)
89 agent_name = input_agent_name;
94 EXPORT_API void sync_agent_plugin_add_platform_account(int fw_account_id)
100 int slp_contact_id = sync_agent_plugin_get_service_account_id(FW_CONTACT, fw_account_id);
101 int slp_calendar_id = sync_agent_plugin_get_service_account_id(FW_CALENDAR, fw_account_id);
102 int slp_memo_id = sync_agent_plugin_get_service_account_id(FW_MEMO, fw_account_id);
107 if (slp_contact_id == -100) {
108 _set_account_id_service(FW_CONTACT, fw_account_id, slp_id);
109 int contact_index = _increase_account_count_fw(FW_CONTACT, slp_id);
110 if (contact_index < 1 || contact_index > MAX_ACCOUNT_COUNT) {
111 _DEBUG_ERROR("[account_plugIn] _increase_account_count_fw() returned OUT OF BOUND int !!!");
114 _set_account_id_fw(FW_CONTACT, slp_id, contact_index, fw_account_id);
117 if (slp_calendar_id == -100) {
118 _set_account_id_service(FW_CALENDAR, fw_account_id, slp_id);
119 int calendar_index = _increase_account_count_fw(FW_CALENDAR, slp_id);
120 if (calendar_index < 1 || calendar_index > MAX_ACCOUNT_COUNT) {
121 _DEBUG_ERROR("[account_plugIn] _increase_account_count_fw() returned OUT OF BOUND int !!!");
124 _set_account_id_fw(FW_CALENDAR, slp_id, calendar_index, fw_account_id);
127 if (slp_memo_id == -100) {
128 _set_account_id_service(FW_MEMO, fw_account_id, slp_id);
129 int memo_index = _increase_account_count_fw(FW_MEMO, slp_id);
130 if (memo_index < 1 || memo_index > MAX_ACCOUNT_COUNT) {
131 _DEBUG_ERROR("[account_plugIn] _increase_account_count_fw() returned OUT OF BOUND int !!!");
134 _set_account_id_fw(FW_MEMO, slp_id, memo_index, fw_account_id);
140 EXPORT_API void sync_agent_plugin_set_platform_account_service(int fw_account_id, int service_type, int service_account_id)
144 _set_account_id_service(service_type, fw_account_id, service_account_id);
146 int service_index = _increase_account_count_fw(service_type, service_account_id);
147 if (service_index < 1 || service_index > MAX_ACCOUNT_COUNT) {
148 _DEBUG_ERROR("[account_plugIn] _increase_account_count_fw() returned OUT OF BOUND int !!!");
151 _set_account_id_fw(service_type, service_account_id, service_index, fw_account_id);
156 EXPORT_API void sync_agent_plugin_delete_platform_account(int fw_account_id)
160 _DEBUG_INFO("fw_account_id = %d", fw_account_id);
164 for (; i < FW_CONTENT_COUNT; i++) {
167 int service_account_id = sync_agent_plugin_get_service_account_id(i, fw_account_id);
168 _DEBUG_INFO("service_account_id =%d", service_account_id);
170 _unset_account_id_service(i, fw_account_id);
172 int count = _get_account_count_fw(i, service_account_id);
173 if (count < 0 || count > MAX_ACCOUNT_COUNT) {
174 _DEBUG_ERROR("[account_plugIn] _get_account_count_fw() returned OUT OF BOUND int !!!");
178 _DEBUG_INFO("count =%d", count);
180 for (; k < count; k++) {
181 int target_fw_account_id = sync_agent_plugin_get_fw_account_id(i, service_account_id, k);
182 _DEBUG_INFO("target_fw_account_id =%d", target_fw_account_id);
183 if (target_fw_account_id == fw_account_id) {
184 _unset_account_id_fw(i, service_account_id, k);
185 _decrease_account_count_fw(i, service_account_id);
186 _DEBUG_INFO("deleted !!!");
190 _arrange_account_id_fw(i, service_account_id, k, target_fw_account_id);
198 EXPORT_API void sync_agent_plugin_delete_platform_account_service(int fw_account_id, int service_type)
202 _unset_account_id_service(service_type, fw_account_id);
207 EXPORT_API int sync_agent_plugin_has_platform_account_service(int fw_account_id, int service_type)
211 int result = sync_agent_plugin_get_service_account_id(service_type, fw_account_id);
212 if (result == -100) {
221 EXPORT_API int sync_agent_plugin_get_service_account_id(int service_type, int fw_account_id)
225 int service_account_id;
226 _DEBUG_INFO("[account_plugIn] service_type : %d\n", service_type);
227 _DEBUG_INFO("[account_plugIn] fw_account_id : %d\n", fw_account_id);
230 snprintf(path, sizeof(path), ACCOUNT_ID_SERVICE_ACCOUNT_ID, agent_name, service_type, fw_account_id);
232 if (!sync_agent_get_int_from_file(path, &service_account_id)) {
233 _DEBUG_ERROR("[account_plugIn] __get_int_from_file FAIL\n");
237 _DEBUG_INFO("[account_plugIn] path : %s\n", path);
238 _DEBUG_INFO("[account_plugIn] service_account_id : %d\n", service_account_id);
242 return service_account_id;
245 EXPORT_API int sync_agent_plugin_get_fw_account_id(int service_type, int service_account_id, int index)
250 snprintf(path, sizeof(path), ACCOUNT_ID_FW_ACCOUNT_ID, agent_name, service_type, service_account_id, index);
253 if (!sync_agent_get_int_from_file(path, &fw_account_id)) {
254 _DEBUG_ERROR("[account_plugIn] __get_int_from_file FAIL\n");
260 return fw_account_id;
263 EXPORT_API int sync_agent_plugin_get_service_account_info(int fw_account_id, int service_type, sync_agent_service_account_info_s ** account_info)
267 retvm_if(*account_info == NULL, 0, "*account_info is NULL. FAIL !!!");
269 int service_account_id = sync_agent_plugin_get_service_account_id(service_type, fw_account_id);
270 _DEBUG_INFO("[account_plugIn] service_account_id : %d\n", service_account_id);
271 if (service_account_id == -100) {
275 int result = _get_account_info(service_account_id, account_info);
278 _DEBUG_INFO("[account_plugIn] account_info->type : %d\n", (*account_info)->type);
279 _DEBUG_INFO("[account_plugIn] account_info->email : %s\n", (*account_info)->email);
281 _DEBUG_ERROR("[account_plugIn] account_info is empty\n");
290 /******************************************* Impl static function ********************************************/
292 static void _set_account_id_service(int service_type, int fw_account_id, int service_account_id)
297 snprintf(path, sizeof(path), ACCOUNT_ID_SERVICE_ACCOUNT_ID, agent_name, service_type, fw_account_id);
299 if (!sync_agent_set_int_into_file(path, service_account_id))
300 _DEBUG_ERROR("[account_plugIn] __set_int_into_file FAIL\n");
302 _DEBUG_TRACE("\n[account_plugIn] Sucess __set_int_into_file\n");
303 _DEBUG_TRACE("[account_plugIn] service_type : %d, fw_account_id : %d, service_account_id : %d\n\n", service_type, fw_account_id, service_account_id);
308 static void _arrange_account_id_fw(int service_type, int service_account_id, int index, int fw_account_id)
312 retm_if(index < 0, "index = %d", index);
313 _DEBUG_INFO("service_type = %d", service_type);
314 _DEBUG_INFO("service_account_id = %d", service_account_id);
315 _DEBUG_INFO("index = %d", index);
316 _DEBUG_INFO("fw_account_id = %d", fw_account_id);
319 snprintf(path, sizeof(path), ACCOUNT_ID_FW_ACCOUNT_ID, agent_name, service_type, service_account_id, index - 1);
321 if (!sync_agent_set_int_into_file(path, fw_account_id)) {
322 _DEBUG_ERROR("[account_plugIn] __set_int_into_file FAIL\n");
326 _unset_account_id_fw(service_type, service_account_id, index);
331 static void _set_account_id_fw(int service_type, int service_account_id, int count, int fw_account_id)
335 _DEBUG_TRACE("count : %d", count);
337 char path[100] = { 0 };
340 for (; index < count; index++) {
342 for (; i < 100; i++) {
347 snprintf(path, sizeof(path), ACCOUNT_ID_FW_ACCOUNT_ID, agent_name, service_type, service_account_id, index);
348 _DEBUG_TRACE("================== 01 [%s]\n", path);
349 if (!sync_agent_get_int_from_file(path, &data)) {
350 _DEBUG_TRACE("================== 02\n");
355 if (!sync_agent_set_int_into_file(path, fw_account_id)) {
356 _DEBUG_ERROR("[account_plugIn] __set_int_into_file FAIL\n");
360 _DEBUG_TRACE("\n[account_plugIn] Sucess __set_int_into_file\n");
361 _DEBUG_TRACE("[account_plugIn] service_type : %d, service_account_id : %d, fw_account_id[%d] : %d\n\n", service_type, service_account_id, index, fw_account_id);
366 static int _increase_account_count_fw(int service_type, int service_account_id)
371 snprintf(path, sizeof(path), ACCOUNT_ID_FW_ACCOUNT_COUNT, agent_name, service_type, service_account_id);
373 int fw_account_count;
374 if (!sync_agent_get_int_from_file(path, &fw_account_count)) {
375 _DEBUG_TRACE("[account_plugIn] first added\n");
376 if (!sync_agent_set_int_into_file(path, 1))
377 _DEBUG_ERROR("[account_plugIn] __set_int_into_file FAIL\n");
382 if (fw_account_count < 1 || fw_account_count > MAX_ACCOUNT_COUNT) {
383 _DEBUG_ERROR("[account_plugIn] Out of MAX_ACCOUNT_COUNT range !!!");
387 if (!sync_agent_set_int_into_file(path, fw_account_count + 1))
388 _DEBUG_ERROR("[account_plugIn] __set_int_into_file FAIL\n");
392 return fw_account_count + 1;
396 static int _get_account_count_fw(int service_type, int service_account_id)
401 snprintf(path, sizeof(path), ACCOUNT_ID_FW_ACCOUNT_COUNT, agent_name, service_type, service_account_id);
403 int fw_account_count;
404 if (!sync_agent_get_int_from_file(path, &fw_account_count)) {
405 _DEBUG_ERROR("[account_plugIn] empty\n");
412 return fw_account_count;
415 static void _unset_account_id_service(int service_type, int fw_account_id)
420 snprintf(path, sizeof(path), ACCOUNT_ID_SERVICE_ACCOUNT_ID, agent_name, service_type, fw_account_id);
422 if (!sync_agent_unset_file(path))
423 _DEBUG_ERROR("[account_plugIn] __unset_file FAIL\n");
428 static void _unset_account_id_fw(int service_type, int service_account_id, int index)
433 snprintf(path, sizeof(path), ACCOUNT_ID_FW_ACCOUNT_ID, agent_name, service_type, service_account_id, index);
435 if (!sync_agent_unset_file(path)) {
436 _DEBUG_ERROR("[account_plugIn] __unset_file FAIL : %s\n", path);
442 static int _decrease_account_count_fw(int service_type, int service_account_id)
447 snprintf(path, sizeof(path), ACCOUNT_ID_FW_ACCOUNT_COUNT, agent_name, service_type, service_account_id);
449 int fw_account_count = 0;
450 if (!sync_agent_get_int_from_file(path, &fw_account_count)) {
451 _DEBUG_ERROR("[account_plugIn] attempt delete when empty\n");
455 if (fw_account_count < 1 || fw_account_count > MAX_ACCOUNT_COUNT) {
456 _DEBUG_ERROR("[account_plugIn] Out of MAX_ACCOUNT_COUNT range !!!");
460 fw_account_count = fw_account_count - 1;
462 if (fw_account_count == 0) {
463 if (!sync_agent_unset_file(path)) {
464 _DEBUG_ERROR("[account_plugIn] __unset_file FAIL : %s\n", path);
469 if (!sync_agent_set_int_into_file(path, fw_account_count))
470 _DEBUG_ERROR("[account_plugIn] vconf_set_id FAIL\n");
474 return fw_account_count;
477 static int _get_account_info(int service_account_id, sync_agent_service_account_info_s ** account_info)
481 retvm_if(*account_info == NULL, 0, "*account_info is NULL. FAIL !!!");
483 if (service_account_id == -1) {
484 (*account_info)->email = 0;
485 (*account_info)->type = SYNC_AGENT_ACC_TYPE_LOCAL;
490 char *domain_name = 0;
492 account_h account_handle = 0;
494 int error_code = account_connect();
495 if (error_code != ACCOUNT_ERROR_NONE) {
496 _DEBUG_ERROR("Failed to call account_connect()");
500 account_create(&account_handle);
501 error_code = account_query_account_by_account_id(service_account_id, &account_handle);
502 if (error_code != ACCOUNT_ERROR_NONE) {
503 _DEBUG_ERROR("Failed to call account_query_account_by_account_id()");
507 error_code = account_get_domain_name(account_handle, &domain_name);
508 if (error_code != ACCOUNT_ERROR_NONE) {
509 _DEBUG_ERROR("Failed to call account_get_domain_name()");
513 if (!strcmp(domain_name, "Gmail")) {
514 (*account_info)->type = SYNC_AGENT_ACC_TYPE_GOOGLE;
515 } else if (!strcmp(domain_name, "exchange")) {
516 (*account_info)->type = SYNC_AGENT_ACC_TYPE_EAS;
518 (*account_info)->type = SYNC_AGENT_ACC_TYPE_NONE;
521 error_code = account_get_email_address(account_handle, &email);
522 if (error_code != ACCOUNT_ERROR_NONE) {
523 _DEBUG_ERROR("Failed to call account_get_email_address()");
528 (*account_info)->email = strdup(email);
531 error_code = account_destroy(account_handle);
532 if (error_code != ACCOUNT_ERROR_NONE) {
533 _DEBUG_ERROR("Failed to call account_destroy() = %d", error_code);
535 error_code = account_disconnect();
536 if (error_code != ACCOUNT_ERROR_NONE) {
537 _DEBUG_ERROR("Failed to call account_disconnect() = %d", error_code);
540 // if (domain_name != NULL)
551 error_code = account_destroy(account_handle);
552 if (error_code != ACCOUNT_ERROR_NONE) {
553 _DEBUG_ERROR("Failed to call account_destroy() = %d", error_code);
555 error_code = account_disconnect();
556 if (error_code != ACCOUNT_ERROR_NONE) {
557 _DEBUG_ERROR("Failed to call account_disconnect() = %d", error_code);
562 if (domain_name != NULL)
567 _DEBUG_ERROR("Returning through ERROR_PART");