4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 /******************************************************************************
24 * File: email-storage.c
25 * Desc: email-service Storage Library on Sqlite3
30 * 2007.03.02 : created
31 *****************************************************************************/
38 #include <sys/types.h>
48 #include <ss_manager.h>
56 #include "email-internal-types.h"
57 #include "email-convert.h"
58 #include "email-core-utils.h"
59 #include "email-utilities.h"
60 #include "email-storage.h"
61 #include "email-debug-log.h"
62 #include "email-types.h"
63 #include "email-convert.h"
64 #include "email-core-signal.h"
66 #define DB_STMT sqlite3_stmt *
68 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
70 #define CONTENT_DATA "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
71 #define CONTENT_TYPE_DATA "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define FLAG_SEEN 0x01
74 #define FLAG_DELETED 0x02
75 #define FLAG_FLAGGED 0x04
76 #define FLAG_ANSWERED 0x08
78 #define FLAG_DRAFT 0x20
82 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL 256
83 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL 128
84 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL 1027
85 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL 256
87 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
88 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
89 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
90 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
91 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
92 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
93 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL 50
94 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
95 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL 30
96 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
97 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
98 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
99 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL 256
100 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL 256
101 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL 128
102 #define ALIAS_LEN_IN_MAIL_BOX_TBL 128
103 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
104 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
105 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
106 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL 256
107 #define VALUE_LEN_IN_MAIL_RULE_TBL 256
108 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL 128
109 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 128
110 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL 256
111 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 256
112 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL 256
113 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
114 #define MAILBOX_LEN_IN_MAIL_TBL 128
115 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL 128
116 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL 128
117 #define FROM_LEN_IN_MAIL_TBL 256
118 #define SENDER_LEN_IN_MAIL_TBL 256
119 #define REPLY_TO_LEN_IN_MAIL_TBL 256
120 #define TO_LEN_IN_MAIL_TBL 3999
121 #define CC_LEN_IN_MAIL_TBL 3999
122 #define BCC_LEN_IN_MAIL_TBL 3999
123 #define RETURN_PATH_LEN_IN_MAIL_TBL 3999
124 #define SUBJECT_LEN_IN_MAIL_TBL 1027
125 #define THREAD_TOPIC_LEN_IN_MAIL_TBL 256
126 #define TEXT_1_LEN_IN_MAIL_TBL 256
127 #define TEXT_2_LEN_IN_MAIL_TBL 256
128 #define MIME_ENTITY_LEN_IN_MAIL_TBL 256
129 #define DATETIME_LEN_IN_MAIL_TBL 128
130 #define MESSAGE_ID_LEN_IN_MAIL_TBL 256
131 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
132 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
133 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
134 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
135 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL 128
136 #define LOCATION_LEN_IN_MAIL_MEETING_TBL 1024
137 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL 512
138 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL 32
139 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL 32
140 #define PREVIEWBODY_LEN_IN_MAIL_TBL 512
141 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
143 /* this define is used for query to change data (delete, insert, update) */
144 #define EMSTORAGE_START_WRITE_TRANSACTION(transaction_flag, error_code) \
145 if (transaction_flag)\
147 _timedlock_shm_mutex(&mapped_for_db_lock, 2);\
148 if (emstorage_begin_transaction(NULL, NULL, &error_code) == false) \
150 EM_DEBUG_EXCEPTION("emstorage_begin_transaction() error[%d]", error_code);\
155 /* this define is used for query to change data (delete, insert, update) */
156 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction_flag, result_code, error_code) \
157 if (transaction_flag)\
159 if (result_code == true)\
161 if (emstorage_commit_transaction(NULL, NULL, NULL) == false)\
163 error_code = EMAIL_ERROR_DB_FAILURE;\
164 result_code = false;\
169 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)\
170 error_code = EMAIL_ERROR_DB_FAILURE;\
172 _unlockshm_mutex(&mapped_for_db_lock);\
175 /* this define is used for query to read (select) */
176 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
177 if (transaction_flag)\
179 /*_timedlock_shm_mutex(&mapped_for_db_lock, 2);*/\
182 /* this define is used for query to read (select) */
183 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
184 if (transaction_flag)\
186 /*_unlockshm_mutex(&mapped_for_db_lock);*/\
189 /* for safety DB operation */
190 static pthread_mutex_t _transactionBeginLock = PTHREAD_MUTEX_INITIALIZER;
191 static pthread_mutex_t _transactionEndLock = PTHREAD_MUTEX_INITIALIZER;
192 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
194 static int _get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type);
196 #define _MULTIPLE_DB_HANDLE
198 #ifdef _MULTIPLE_DB_HANDLE
200 #define _DISCONNECT_DB /* db_util_close(_db_handle); */
208 #define MAX_DB_CLIENT 100
210 /* static int _db_handle_count = 0; */
211 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{0, 0}, };
213 sqlite3 *emstorage_get_db_handle()
215 EM_DEBUG_FUNC_BEGIN();
217 pthread_t current_thread_id = THREAD_SELF();
218 sqlite3 *result_db_handle = NULL;
220 ENTER_CRITICAL_SECTION(_db_handle_lock);
221 for (i = 0; i < MAX_DB_CLIENT; i++) {
222 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
223 EM_DEBUG_LOG("found db handle at [%d]", i);
224 result_db_handle = _db_handle_list[i].db_handle;
228 LEAVE_CRITICAL_SECTION(_db_handle_lock);
230 if (!result_db_handle)
231 EM_DEBUG_EXCEPTION("Can't find proper handle for [%d]", current_thread_id);
234 return result_db_handle;
237 int emstorage_set_db_handle(sqlite3 *db_handle)
239 EM_DEBUG_FUNC_BEGIN();
240 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
241 pthread_t current_thread_id = THREAD_SELF();
243 ENTER_CRITICAL_SECTION(_db_handle_lock);
244 for (i = 0; i < MAX_DB_CLIENT; i++) {
245 if (_db_handle_list[i].thread_id == 0) {
246 _db_handle_list[i].thread_id = current_thread_id;
247 _db_handle_list[i].db_handle = db_handle;
248 EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
249 error_code = EMAIL_ERROR_NONE;
253 LEAVE_CRITICAL_SECTION(_db_handle_lock);
255 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
256 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
258 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
262 int emstorage_remove_db_handle()
264 EM_DEBUG_FUNC_BEGIN();
265 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
266 ENTER_CRITICAL_SECTION(_db_handle_lock);
267 for (i = 0; i < MAX_DB_CLIENT; i++)
269 if (_db_handle_list[i].thread_id == THREAD_SELF())
271 _db_handle_list[i].thread_id = 0;
272 _db_handle_list[i].db_handle = NULL;
273 EM_DEBUG_LOG("index [%d]", i);
274 error_code = EMAIL_ERROR_NONE;
278 LEAVE_CRITICAL_SECTION(_db_handle_lock);
280 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
281 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
283 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
288 int emstorage_reset_db_handle_list()
290 EM_DEBUG_FUNC_BEGIN();
293 ENTER_CRITICAL_SECTION(_db_handle_lock);
294 for (i = 0; i < MAX_DB_CLIENT; i++)
296 _db_handle_list[i].thread_id = 0;
297 _db_handle_list[i].db_handle = NULL;
299 LEAVE_CRITICAL_SECTION(_db_handle_lock)
302 return EMAIL_ERROR_NONE;
305 sqlite3 *emstorage_get_db_connection()
307 sqlite3 *tmp_db_handle = emstorage_get_db_handle();
308 if (NULL == tmp_db_handle)
309 tmp_db_handle = emstorage_db_open(NULL);
310 return tmp_db_handle;
314 #else /* _MULTIPLE_DB_HANDLE */
315 #define _DISCONNECT_DB /* db_util_close(_db_handle); */
317 sqlite3 *_db_handle = NULL;
319 sqlite3 *emstorage_get_db_connection()
321 if (NULL == _db_handle)
322 emstorage_db_open(NULL);
325 #endif /* _MULTIPLE_DB_HANDLE */
327 /* ------------------------------------------------------------------------------ */
328 /* Mutex using shared memory */
331 pthread_mutex_t mutex;
335 mmapped_t *mapped_for_db_lock = NULL;
336 int shm_fd_for_db_lock = 0;
338 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
339 mmapped_t *mapped_for_generating_mail_id = NULL;
340 int shm_fd_for_generating_mail_id = 0;
341 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
343 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
344 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
345 { _timedlock_shm_mutex(&mapped_for_db_lock, 2); return_value = function_call; _unlockshm_mutex(&mapped_for_db_lock); }
346 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
347 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
348 { return_value = function_call; }
349 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
351 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
353 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
356 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
357 return EMAIL_ERROR_INVALID_PARAM;
360 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
364 EM_DEBUG_LOG("** Create SHM FILE **");
365 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
366 EM_DEBUG_EXCEPTION("ftruncate failed: %s", strerror(errno));
367 return EMAIL_ERROR_SYSTEM_FAILURE;
370 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
371 if (m == MAP_FAILED) {
372 EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
373 return EMAIL_ERROR_SYSTEM_FAILURE;
378 pthread_mutexattr_t mattr;
379 pthread_mutexattr_init(&mattr);
380 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
381 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
382 pthread_mutex_init(&(m->mutex), &mattr);
383 pthread_mutexattr_destroy(&mattr);
386 EM_DEBUG_EXCEPTION("shm_open failed: %s", strerror(errno));
387 return EMAIL_ERROR_SYSTEM_FAILURE;
391 return EMAIL_ERROR_NONE;
394 int emstorage_shm_file_destroy(const char *shm_file_name)
396 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
398 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
399 return EMAIL_ERROR_INVALID_PARAM;
402 if (shm_unlink(shm_file_name) != 0)
403 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", strerror(errno));
405 return EMAIL_ERROR_NONE;
408 static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
410 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
412 if(!shm_file_name || !param_shm_fd || !param_mapped) {
413 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
414 return EMAIL_ERROR_INVALID_PARAM;
417 if (!(*param_mapped)) {
418 EM_DEBUG_LOG("** mapping begin **");
420 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
421 *param_shm_fd = shm_open(shm_file_name, O_RDWR, 0);
422 if ((*param_shm_fd) == -1) {
423 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", strerror(errno));
424 return EMAIL_ERROR_SYSTEM_FAILURE;
427 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
429 if (tmp == MAP_FAILED) {
430 EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
431 return EMAIL_ERROR_SYSTEM_FAILURE;
437 return EMAIL_ERROR_NONE;
440 static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
442 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
445 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
446 return EMAIL_ERROR_INVALID_PARAM;
449 struct timespec abs_time;
450 clock_gettime(CLOCK_REALTIME, &abs_time);
451 abs_time.tv_sec += sec;
453 int err = pthread_mutex_timedlock(&((*param_mapped)->mutex), &abs_time);
455 if (err == EOWNERDEAD) {
456 err = pthread_mutex_consistent(&((*param_mapped)->mutex));
457 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(err));
460 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(err));
465 return EMAIL_ERROR_NONE;
468 void _unlockshm_mutex(mmapped_t **param_mapped)
470 EM_DEBUG_FUNC_BEGIN();
471 pthread_mutex_unlock(&((*param_mapped)->mutex));
474 /* ------------------------------------------------------------------------------ */
477 static int _open_counter = 0;
478 static int g_transaction = false;
480 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name);
481 static int _read_password_from_secure_storage(char *file_name, char **password);
483 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
484 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
488 const char *object_name;
489 unsigned int data_flag;
492 static const email_db_object_t _g_db_tables[] =
494 { "mail_read_mail_uid_tbl", 1},
496 { "mail_attachment_tbl", 1},
500 static const email_db_object_t _g_db_indexes[] =
502 { "mail_read_mail_uid_idx1", 1},
504 { "mail_attachment_idx1", 1},
510 CREATE_TABLE_MAIL_ACCOUNT_TBL,
511 CREATE_TABLE_MAIL_BOX_TBL,
512 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
513 CREATE_TABLE_MAIL_RULE_TBL,
514 CREATE_TABLE_MAIL_TBL,
515 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
516 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
517 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
519 CREATE_TABLE_MAIL_MEETING_TBL,
520 #ifdef __FEATURE_LOCAL_ACTIVITY__
521 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
523 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
524 CREATE_TABLE_MAIL_TASK_TBL,
530 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
531 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
532 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
537 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
538 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
539 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
540 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
541 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
542 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
543 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
544 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
545 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
546 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
547 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
558 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
559 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
560 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
561 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
562 ALIAS_IDX_IN_MAIL_BOX_TBL,
563 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
564 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
565 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
566 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
567 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
572 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
573 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
574 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
575 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
576 S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
577 DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL,
578 DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL,
579 FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL,
580 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
585 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
586 RULE_ID_IDX_IN_MAIL_RULE_TBL,
587 TYPE_IDX_IN_MAIL_RULE_TBL,
588 VALUE_IDX_IN_MAIL_RULE_TBL,
589 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
590 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
591 FLAG1_IDX_IN_MAIL_RULE_TBL,
592 FLAG2_IDX_IN_MAIL_RULE_TBL,
597 MAIL_ID_IDX_IN_MAIL_TBL = 0,
598 ACCOUNT_ID_IDX_IN_MAIL_TBL,
599 MAILBOX_ID_IDX_IN_MAIL_TBL,
600 MAILBOX_NAME_IDX_IN_MAIL_TBL,
601 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
602 SUBJECT_IDX_IN_MAIL_TBL,
603 DATETIME_IDX_IN_MAIL_TBL,
604 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
605 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
606 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
607 MESSAGE_ID_IDX_IN_MAIL_TBL,
608 REFERENCE_ID_IDX_IN_MAIL_TBL,
609 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
610 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
611 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
612 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
613 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
614 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
615 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
616 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
617 ALIAS_SENDER_IDX_IN_MAIL_TBL,
618 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
619 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
620 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
621 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
622 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
623 MAIL_SIZE_IDX_IN_MAIL_TBL,
624 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
625 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
626 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
627 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
628 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
629 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
630 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
631 DRM_STATUS_IDX_IN_MAIL_TBL,
632 PRIORITY_IDX_IN_MAIL_TBL,
633 SAVE_STATUS_IDX_IN_MAIL_TBL,
634 LOCK_STATUS_IDX_IN_MAIL_TBL,
635 REPORT_STATUS_IDX_IN_MAIL_TBL,
636 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
637 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
638 THREAD_ID_IDX_IN_MAIL_TBL,
639 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
640 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
641 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
642 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
643 DIGEST_TYPE_IDX_IN_MAIL_TBL,
644 SMIME_TYPE_IDX_IN_MAIL_TBL,
645 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
650 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
651 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
652 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
653 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
654 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
655 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
656 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
657 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
658 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
659 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
660 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
661 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
662 #ifdef __ATTACHMENT_OPTI__
663 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
664 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
669 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
670 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
671 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
672 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
673 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
674 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
676 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
677 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
678 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
679 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
680 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
681 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
682 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
686 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
687 typedef struct _em_mail_id_list {
689 struct _em_mail_id_list *next;
692 static char *g_test_query[] = {
693 /* 1. select mail_account_tbl */
696 " incoming_server_type, "
697 " incoming_server_address, "
698 " user_email_address, "
699 " incoming_server_user_name, "
700 " incoming_server_password, "
702 " incoming_server_port_number, "
703 " incoming_server_secure_connection, "
704 " outgoing_server_type, "
705 " outgoing_server_address, "
706 " outgoing_server_port_number, "
707 " outgoing_server_need_authentication, "
708 " outgoing_server_secure_connection, "
709 " outgoing_server_user_name, "
710 " outgoing_server_password, "
715 " keep_mails_on_pop_server_after_download, "
719 " incoming_server_requires_apop"
721 " is_preset_account, "
725 " req_delivery_receipt, "
726 " req_read_receipt, "
730 " display_name_from, "
732 " forward_with_files, "
736 ", add_my_address_to_bcc"
745 " FROM mail_account_tbl",
746 /* 2. select mail_box_tbl */
756 " total_mail_count_on_server, "
757 " has_archived_mails, "
761 " FROM mail_box_tbl ",
762 /* 3. select mail_read_mail_uid_tbl */
773 " FROM mail_read_mail_uid_tbl ",
774 /* 4. select mail_rule_tbl */
781 " target_mailbox_id, "
784 " FROM mail_rule_tbl ",
785 /* 5. select mail_tbl */
794 " server_mail_status, "
795 " server_mailbox_name, "
798 " reference_mail_id, "
799 " full_address_from, "
800 " full_address_reply, "
803 " full_address_bcc, "
804 " full_address_return, "
805 " email_address_sender, "
806 " email_address_recipient, "
809 " body_download_status, "
812 " file_path_mime_entity, "
814 " flags_seen_field ,"
815 " flags_deleted_field ,"
816 " flags_flagged_field ,"
817 " flags_answered_field ,"
818 " flags_recent_field ,"
819 " flags_draft_field ,"
820 " flags_forwarded_field,"
826 " attachment_count, "
827 " inline_content_count, "
829 " thread_item_count, "
831 " meeting_request_status, "
836 /* 6. select mail_attachment_tbl */
845 " attachment_save_status, "
846 " attachment_drm_type, "
847 " attachment_drm_method, "
848 " attachment_inline_content_status, "
849 " attachment_mime_type "
850 " FROM mail_attachment_tbl ",
852 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
861 " FROM mail_partial_body_activity_tbl ",
868 " meeting_response, "
872 " global_object_id, "
875 " standard_time_start_date, "
878 " daylight_time_start_date, "
880 " FROM mail_meeting_tbl ",
882 #ifdef __FEATURE_LOCAL_ACTIVITY__
891 " FROM mail_local_activity_tbl ",
898 " task_parameter_length, "
901 " FROM mail_task_tbl ",
906 static int _get_table_field_data_char(char **table, char *buf, int index)
908 if ((table == NULL) || (buf == NULL) || (index < 0)) {
909 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
913 if (table[index] != NULL) {
914 *buf = (char)atoi(table[index]);
918 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
924 static int _get_table_field_data_int(char **table, int *buf, int index)
926 if ((table == NULL) || (buf == NULL) || (index < 0)) {
927 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
931 if (table[index] != NULL) {
932 *buf = atoi(table[index]);
936 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
942 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
944 if ((table == NULL) || (buf == NULL) || (index < 0)) {
945 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
949 if (table[index] != NULL) {
950 *buf = (time_t)atol(table[index]);
954 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
960 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
964 if ((table == NULL) || (buf == NULL) || (index < 0)) {
965 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
969 char *pTemp = table[index];
974 sLen = EM_SAFE_STRLEN(pTemp);
976 *buf = (char *) em_malloc(sLen + 1);
978 EM_DEBUG_EXCEPTION("malloc is failed");
981 strncpy(*buf, pTemp, sLen);
986 #ifdef _PRINT_STORAGE_LOG_
988 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
990 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
998 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1000 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1001 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1005 char *pTemp = table[index];
1010 memset(buf, 0, buffer_size);
1011 strncpy(buf, pTemp, buffer_size - 1);
1013 #ifdef _PRINT_STORAGE_LOG_
1015 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1017 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1024 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1026 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1027 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1031 char *temp_buffer = table[index];
1033 if (temp_buffer == NULL)
1036 *buffer = malloc(buffer_size);
1037 if(*buffer == NULL) {
1038 EM_DEBUG_EXCEPTION("allocation failed.");
1041 memset(*buffer, 0, buffer_size);
1042 memcpy(*buffer, temp_buffer, buffer_size);
1044 #ifdef _PRINT_STORAGE_LOG_
1046 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1048 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1055 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1057 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1058 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1062 if (sqlite3_column_text(hStmt, index) != NULL) {
1063 *buf = (char)sqlite3_column_int(hStmt, index);
1064 #ifdef _PRINT_STORAGE_LOG_
1065 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1070 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1075 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1077 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1078 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1082 if (sqlite3_column_text(hStmt, index) != NULL) {
1083 *buf = sqlite3_column_int(hStmt, index);
1084 #ifdef _PRINT_STORAGE_LOG_
1085 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1090 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1095 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1097 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1098 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1102 if (sqlite3_column_text(hStmt, index) != NULL) {
1103 *buf = (time_t)sqlite3_column_int(hStmt, index);
1104 #ifdef _PRINT_STORAGE_LOG_
1105 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1110 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1114 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1116 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1117 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1122 sLen = sqlite3_column_bytes(hStmt, index);
1124 #ifdef _PRINT_STORAGE_LOG_
1125 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1129 *buf = (char *) em_malloc(sLen + 1);
1130 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1135 #ifdef _PRINT_STORAGE_LOG_
1137 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1139 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1145 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1147 if( !hStmt || !buf || (index < 0)) { /*prevent 39618*/
1148 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1153 sLen = sqlite3_column_bytes(hStmt, index);
1155 #ifdef _PRINT_STORAGE_LOG_
1156 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1160 *buf = (char *) em_malloc(sLen);
1161 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1168 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1170 if (!hStmt || !buf || (index < 0)) { /*prevent 39620*/
1171 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1176 sLen = sqlite3_column_bytes(hStmt, index);
1178 #ifdef _PRINT_STORAGE_LOG_
1179 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1183 memset(buf, 0, buffer_size);
1184 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1189 #ifdef _PRINT_STORAGE_LOG_
1190 EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1196 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1198 if ((hStmt == NULL) || (index < 0)) {
1199 EM_DEBUG_EXCEPTION("index[%d]", index);
1203 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1205 if (ret != SQLITE_OK) {
1206 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1213 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1215 if ((hStmt == NULL) || (index < 0)) {
1216 EM_DEBUG_EXCEPTION("index[%d]", index);
1220 int ret = sqlite3_bind_int(hStmt, index+1, value);
1222 if (ret != SQLITE_OK) {
1223 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1230 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1232 if ((hStmt == NULL) || (index < 0)) {
1233 EM_DEBUG_EXCEPTION("index[%d]", index);
1237 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1239 if (ret != SQLITE_OK) {
1240 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1247 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1249 if ((hStmt == NULL) || (index < 0)) {
1250 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1254 #ifdef _PRINT_STORAGE_LOG_
1255 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1260 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1262 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1264 if (ret != SQLITE_OK) {
1265 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1272 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1274 if ((hStmt == NULL) || (index < 0)) {
1275 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1279 #ifdef _PRINT_STORAGE_LOG_
1280 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1285 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1287 ret = sqlite3_bind_null(hStmt, index+1);
1289 if (ret != SQLITE_OK) {
1290 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1297 static int _delete_temp_file(const char *path)
1299 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1302 struct dirent *entry = NULL;
1304 char buf[1024] = {0x00, };
1306 if ((dp = opendir(path)) == NULL) {
1307 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1311 while ((entry = readdir(dp)) != NULL) {
1312 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1317 EM_DEBUG_FUNC_END();
1321 char *cpy_str(char *src)
1326 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1327 EM_DEBUG_EXCEPTION("mailoc failed...");
1330 strncpy(p, src, EM_SAFE_STRLEN(src));
1336 static void _emstorage_close_once(void)
1338 EM_DEBUG_FUNC_BEGIN();
1340 DELETE_CRITICAL_SECTION(_transactionBeginLock);
1341 DELETE_CRITICAL_SECTION(_transactionEndLock);
1343 EM_DEBUG_FUNC_END();
1346 INTERNAL_FUNC int emstorage_close(int *err_code)
1348 EM_DEBUG_FUNC_BEGIN();
1351 int error = EMAIL_ERROR_NONE;
1353 emstorage_db_close(&error);
1355 if (--_open_counter == 0)
1356 _emstorage_close_once();
1360 if (err_code != NULL)
1363 EM_DEBUG_FUNC_END("ret [%d]", ret);
1367 static void *_emstorage_open_once(int *err_code)
1369 EM_DEBUG_FUNC_BEGIN();
1371 int error = EMAIL_ERROR_NONE;
1373 mkdir(USERDATA_PATH, DIRECTORY_PERMISSION);
1374 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1375 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1376 mkdir(MAILHOME, DIRECTORY_PERMISSION);
1377 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1379 _delete_temp_file(MAILTEMP);
1381 g_transaction = false;
1383 if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) {
1384 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1389 if (err_code != NULL)
1396 /* pData : a parameter which is registered when busy handler is registerd */
1397 /* count : retry count */
1398 #define EMAIL_STORAGE_MAX_RETRY_COUNT 20
1399 static int _callback_sqlite_busy_handler(void *pData, int count)
1401 EM_DEBUG_LOG("Busy Handler Called!!: [%d]", count);
1402 usleep(200000); /* sleep time when SQLITE_LOCK */
1404 /* retry will be stopped if busy handler return 0 */
1405 return EMAIL_STORAGE_MAX_RETRY_COUNT - count;
1408 static int _delete_all_files_and_directories(int *err_code)
1410 EM_DEBUG_FUNC_BEGIN();
1412 int error = EMAIL_ERROR_NONE;
1415 if (!emstorage_delete_file(EMAIL_SERVICE_DB_FILE_PATH, &error)) {
1416 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1417 EM_DEBUG_EXCEPTION("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1422 if (!emstorage_delete_dir(MAILHOME, &error)) {
1423 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1432 EM_DEBUG_FUNC_END();
1436 static int _recovery_from_malformed_db_file(int *err_code)
1438 EM_DEBUG_FUNC_BEGIN();
1440 int error = EMAIL_ERROR_NONE;
1443 /* Delete all files and directories */
1444 if (!_delete_all_files_and_directories(&error)) {
1445 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1449 /* Delete all accounts on MyAccount */
1451 /* Delete all managed connection to DB */
1452 emstorage_reset_db_handle_list();
1459 EM_DEBUG_FUNC_END();
1464 INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
1466 EM_DEBUG_FUNC_BEGIN();
1468 int error = EMAIL_ERROR_NONE;
1471 EM_DEBUG_LOG("*sqlite_handle[%p]", *sqlite_handle);
1473 if (*sqlite_handle) { /*prevent 33351*/
1474 EM_DEBUG_LOG(">>>>> DB Already Opened......");
1475 if (err_code != NULL)
1481 EM_DEBUG_LOG("Open DB");
1482 EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1483 if (SQLITE_OK != rc) {
1484 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1485 error = EMAIL_ERROR_DB_FAILURE;
1486 db_util_close(*sqlite_handle);
1487 *sqlite_handle = NULL;
1489 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1490 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1491 if (!_recovery_from_malformed_db_file(&error)) {
1492 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1496 EM_DEBUG_LOG("Open DB again");
1497 EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1498 if (SQLITE_OK != rc) {
1499 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1500 error = EMAIL_ERROR_DB_FAILURE;
1501 db_util_close(*sqlite_handle);
1502 *sqlite_handle = NULL;
1503 goto FINISH_OFF; /*prevent 33351*/
1509 EM_DEBUG_LOG(">>>>> DB Handle : *sqlite_handle[%p]", *sqlite_handle);
1511 /* register busy handler */
1512 EM_DEBUG_LOG(">>>>> Register busy handler.....");
1513 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1514 if (SQLITE_OK != rc) {
1515 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1516 error = EMAIL_ERROR_DB_FAILURE;
1517 db_util_close(*sqlite_handle);
1518 *sqlite_handle = NULL;
1526 if (err_code != NULL)
1529 EM_DEBUG_FUNC_END("ret [%d]", ret);
1533 INTERNAL_FUNC sqlite3* emstorage_db_open(int *err_code)
1535 EM_DEBUG_FUNC_BEGIN();
1536 #ifdef _MULTIPLE_DB_HANDLE
1537 sqlite3 *_db_handle = NULL;
1540 int error = EMAIL_ERROR_NONE;
1542 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1544 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1545 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1546 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1548 if (!em_db_open(&_db_handle, &error)) {
1549 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1553 #ifdef _MULTIPLE_DB_HANDLE
1554 emstorage_set_db_handle(_db_handle);
1558 if (err_code != NULL)
1561 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1565 INTERNAL_FUNC int emstorage_db_close(int *err_code)
1567 EM_DEBUG_FUNC_BEGIN();
1568 #ifdef _MULTIPLE_DB_HANDLE
1569 sqlite3 *_db_handle = emstorage_get_db_handle();
1572 int error = EMAIL_ERROR_NONE;
1575 DELETE_CRITICAL_SECTION(_transactionBeginLock);
1576 DELETE_CRITICAL_SECTION(_transactionEndLock);
1579 ret = db_util_close(_db_handle);
1581 if (ret != SQLITE_OK) {
1582 EM_DEBUG_EXCEPTION(" db_util_close fail - %d", ret);
1583 error = EMAIL_ERROR_DB_FAILURE;
1587 #ifdef _MULTIPLE_DB_HANDLE
1588 emstorage_remove_db_handle();
1596 if (err_code != NULL)
1599 EM_DEBUG_FUNC_END("ret [%d]", ret);
1604 INTERNAL_FUNC int emstorage_open(int *err_code)
1606 EM_DEBUG_FUNC_BEGIN();
1609 int error = EMAIL_ERROR_NONE;
1613 retValue = mkdir(DB_PATH, DIRECTORY_PERMISSION);
1615 EM_DEBUG_LOG("mkdir return- %d", retValue);
1616 EM_DEBUG_LOG("emstorage_open - before db_util_open - pid = %d", getpid());
1618 if (emstorage_db_open(&error) == NULL) {
1619 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
1624 if (_open_counter++ == 0)
1625 _emstorage_open_once(&error);
1630 if (err_code != NULL)
1633 EM_DEBUG_FUNC_END("ret [%d]", ret);
1638 INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *err_code)
1640 EM_DEBUG_FUNC_BEGIN();
1642 int error = EMAIL_ERROR_NONE;
1643 int rc = -1, ret = false;
1644 char sql_query_string[QUERY_SIZE] = {0, };
1645 char *create_table_query[] =
1647 /* 1. create mail_account_tbl */
1648 "CREATE TABLE mail_account_tbl \n"
1650 "account_id INTEGER PRIMARY KEY,\n"
1651 "account_name VARCHAR(51),\n"
1652 "logo_icon_path VARCHAR(256),\n"
1654 "user_data_length INTEGER,\n"
1655 "account_svc_id INTEGER,\n"
1656 "sync_status INTEGER,\n"
1657 "sync_disabled INTEGER,\n"
1658 "default_mail_slot_size INTEGER,\n"
1659 "user_display_name VARCHAR(31),\n"
1660 "user_email_address VARCHAR(129),\n"
1661 "reply_to_address VARCHAR(129),\n"
1662 "return_address VARCHAR(129),\n"
1663 "incoming_server_type INTEGER,\n"
1664 "incoming_server_address VARCHAR(51),\n"
1665 "incoming_server_port_number INTEGER,\n"
1666 "incoming_server_user_name VARCHAR(51),\n"
1667 "incoming_server_password VARCHAR(51),\n"
1668 "incoming_server_secure_connection INTEGER,\n"
1669 "retrieval_mode INTEGER,\n"
1670 "keep_mails_on_pop_server_after_download INTEGER,\n"
1671 "check_interval INTEGER,\n"
1672 "auto_download_size INTEGER,\n"
1673 "outgoing_server_type INTEGER,\n"
1674 "outgoing_server_address VARCHAR(51),\n"
1675 "outgoing_server_port_number INTEGER,\n"
1676 "outgoing_server_user_name VARCHAR(51),\n"
1677 "outgoing_server_password VARCHAR(51),\n"
1678 "outgoing_server_secure_connection INTEGER,\n"
1679 "outgoing_server_need_authentication INTEGER,\n"
1680 "outgoing_server_use_same_authenticator INTEGER,\n"
1681 "priority INTEGER,\n"
1682 "keep_local_copy INTEGER,\n"
1683 "req_delivery_receipt INTEGER,\n"
1684 "req_read_receipt INTEGER,\n"
1685 "download_limit INTEGER,\n"
1686 "block_address INTEGER,\n"
1687 "block_subject INTEGER,\n"
1688 "display_name_from VARCHAR(256),\n"
1689 "reply_with_body INTEGER,\n"
1690 "forward_with_files INTEGER,\n"
1691 "add_myname_card INTEGER,\n"
1692 "add_signature INTEGER,\n"
1693 "signature VARCHAR(256),\n"
1694 "add_my_address_to_bcc INTEGER,\n"
1695 "pop_before_smtp INTEGER,\n"
1696 "incoming_server_requires_apop INTEGER,\n"
1697 "smime_type INTEGER,\n"
1698 "certificate_path VARCHAR(256),\n"
1699 "cipher_type INTEGER,\n"
1700 "digest_type INTEGER\n"
1702 /* 2. create mail_box_tbl */
1703 "CREATE TABLE mail_box_tbl \n"
1705 " mailbox_id INTEGER \n"
1706 ", account_id INTEGER \n"
1707 ", local_yn INTEGER \n"
1708 ", mailbox_name VARCHAR(256) \n"
1709 ", mailbox_type INTEGER \n"
1710 ", alias VARCHAR(256) \n"
1711 ", deleted_flag INTEGER \n"
1712 ", modifiable_yn INTEGER \n"
1713 ", total_mail_count_on_server INTEGER \n"
1714 ", has_archived_mails INTEGER \n"
1715 ", mail_slot_size INTEGER \n"
1716 ", no_select INTEGER \n"
1717 ", last_sync_time DATETIME \n"
1720 /* 3. create mail_read_mail_uid_tbl */
1721 "CREATE TABLE mail_read_mail_uid_tbl \n"
1723 " account_id INTEGER \n"
1724 ", mailbox_id VARCHAR(129) \n"
1725 ", local_uid INTEGER \n"
1726 ", mailbox_name VARCHAR(256) \n"
1727 ", s_uid VARCHAR(129) \n"
1728 ", data1 INTEGER \n"
1729 ", data2 VARCHAR(257) \n"
1731 ", idx_num INTEGER PRIMARY KEY \n"
1733 /* 4. create mail_rule_tbl */
1734 "CREATE TABLE mail_rule_tbl \n"
1736 " account_id INTEGER \n"
1737 ", rule_id INTEGER PRIMARY KEY\n"
1739 ", value VARCHAR(257) \n"
1740 ", action_type INTEGER \n"
1741 ", target_mailbox_id INTEGER \n"
1742 ", flag1 INTEGER \n"
1743 ", flag2 INTEGER \n"
1745 /* 5. create mail_tbl */
1746 "CREATE TABLE mail_tbl \n"
1748 " mail_id INTEGER PRIMARY KEY \n"
1749 ", account_id INTEGER \n"
1750 ", mailbox_id INTEGER \n"
1751 ", mailbox_name VARCHAR(129) \n"
1752 ", mailbox_type INTEGER \n"
1754 ", date_time DATETIME \n"
1755 ", server_mail_status INTEGER \n"
1756 ", server_mailbox_name VARCHAR(129) \n"
1757 ", server_mail_id VARCHAR(129) \n"
1758 ", message_id VARCHAR(257) \n"
1759 ", reference_mail_id INTEGER \n"
1760 ", full_address_from TEXT \n"
1761 ", full_address_reply TEXT \n"
1762 ", full_address_to TEXT \n"
1763 ", full_address_cc TEXT \n"
1764 ", full_address_bcc TEXT \n"
1765 ", full_address_return TEXT \n"
1766 ", email_address_sender TEXT collation user1 \n"
1767 ", email_address_recipient TEXT collation user1 \n"
1768 ", alias_sender TEXT \n"
1769 ", alias_recipient TEXT \n"
1770 ", body_download_status INTEGER \n"
1771 ", file_path_plain VARCHAR(257) \n"
1772 ", file_path_html VARCHAR(257) \n"
1773 ", file_path_mime_entity VARCHAR(257) \n"
1774 ", mail_size INTEGER \n"
1775 ", flags_seen_field BOOLEAN \n"
1776 ", flags_deleted_field BOOLEAN \n"
1777 ", flags_flagged_field BOOLEAN \n"
1778 ", flags_answered_field BOOLEAN \n"
1779 ", flags_recent_field BOOLEAN \n"
1780 ", flags_draft_field BOOLEAN \n"
1781 ", flags_forwarded_field BOOLEAN \n"
1782 ", DRM_status INTEGER \n"
1783 ", priority INTEGER \n"
1784 ", save_status INTEGER \n"
1785 ", lock_status INTEGER \n"
1786 ", report_status INTEGER \n"
1787 ", attachment_count INTEGER \n"
1788 ", inline_content_count INTEGER \n"
1789 ", thread_id INTEGER \n"
1790 ", thread_item_count INTEGER \n"
1791 ", preview_text TEXT \n"
1792 ", meeting_request_status INTEGER \n"
1793 ", message_class INTEGER \n"
1794 ", digest_type INTEGER \n"
1795 ", smime_type INTEGER \n"
1796 ", FOREIGN KEY(account_id) REFERENCES mail_account_tbl(account_id) \n"
1799 /* 6. create mail_attachment_tbl */
1800 "CREATE TABLE mail_attachment_tbl \n"
1802 " attachment_id INTEGER PRIMARY KEY"
1803 ", attachment_name VARCHAR(257) \n"
1804 ", attachment_path VARCHAR(257) \n"
1805 ", attachment_size INTEGER \n"
1806 ", mail_id INTEGER \n"
1807 ", account_id INTEGER \n"
1808 ", mailbox_id INTEGER \n"
1809 ", attachment_save_status INTEGER \n"
1810 ", attachment_drm_type INTEGER \n"
1811 ", attachment_drm_method INTEGER \n"
1812 ", attachment_inline_content_status INTEGER \n"
1813 ", attachment_mime_type VARCHAR(257) \n"
1814 #ifdef __ATTACHMENT_OPTI__
1815 ", encoding INTEGER \n"
1816 ", section varchar(257) \n"
1820 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1822 "CREATE TABLE mail_partial_body_activity_tbl \n"
1824 " account_id INTEGER \n"
1825 ", mail_id INTEGER \n"
1826 ", server_mail_id INTEGER \n"
1827 ", activity_id INTEGER PRIMARY KEY \n"
1828 ", activity_type INTEGER \n"
1829 ", mailbox_id INTEGER \n"
1830 ", mailbox_name VARCHAR(4000) \n"
1834 "CREATE TABLE mail_meeting_tbl \n"
1836 " mail_id INTEGER PRIMARY KEY \n"
1837 ", account_id INTEGER \n"
1838 ", mailbox_id INTEGER \n"
1839 ", meeting_response INTEGER \n"
1840 ", start_time INTEGER \n"
1841 ", end_time INTEGER \n"
1842 ", location TEXT \n"
1843 ", global_object_id TEXT \n"
1844 ", offset INTEGER \n"
1845 ", standard_name TEXT \n"
1846 ", standard_time_start_date INTEGER \n"
1847 ", standard_bias INTEGER \n"
1848 ", daylight_name TEXT \n"
1849 ", daylight_time_start_date INTEGER \n"
1850 ", daylight_bias INTEGER \n"
1853 #ifdef __FEATURE_LOCAL_ACTIVITY__
1854 "CREATE TABLE mail_local_activity_tbl \n"
1856 " activity_id INTEGER \n"
1857 ", account_id INTEGER \n"
1858 ", mail_id INTEGER \n"
1859 ", activity_type INTEGER \n"
1860 ", server_mailid VARCHAR(129) \n"
1861 ", src_mbox VARCHAR(129) \n"
1862 ", dest_mbox VARCHAR(129) \n"
1865 "CREATE TABLE mail_certificate_tbl \n"
1867 " certificate_id INTEGER \n"
1868 ", issue_year INTEGER \n"
1869 ", issue_month INTEGER \n"
1870 ", issue_day INTEGER \n"
1871 ", expiration_year INTEGER \n"
1872 ", expiration_month INTEGER \n"
1873 ", expiration_day INTEGER \n"
1874 ", issue_organization_name VARCHAR(256) \n"
1875 ", email_address VARCHAR(129) \n"
1876 ", subject_str VARCHAR(256) \n"
1877 ", filepath VARCHAR(256) \n"
1878 ", password VARCHAR(51) \n"
1881 "CREATE TABLE mail_task_tbl \n"
1883 "task_id INTEGER PRIMARY KEY, \n"
1884 "task_type INTEGER, \n"
1885 "task_status INTEGER, \n"
1886 "task_priority INTEGER, \n"
1887 "task_parameter_length INTEGER, \n"
1888 "task_parameter BLOB, \n"
1889 "date_time DATETIME \n"
1894 sqlite3 *local_db_handle = emstorage_get_db_connection();
1896 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
1901 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
1902 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1903 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1904 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1906 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
1908 if (atoi(result[1]) < 1) {
1909 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1910 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1911 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1913 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
1915 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1917 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1918 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1920 /* create mail_account_tbl unique index */
1921 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_account_idx1 ON mail_account_tbl (account_id)");
1923 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1924 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1926 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1928 } /* mail_account_tbl */
1929 else if (type == EMAIL_CREATE_DB_CHECK) {
1930 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
1931 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], rc, sqlite3_errmsg(local_db_handle)));
1934 sqlite3_free_table(result);
1937 /* 2. create mail_box_tbl */
1938 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
1940 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1941 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1942 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1944 if (atoi(result[1]) < 1) {
1945 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1946 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1947 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1949 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
1951 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1953 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1954 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1955 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1957 /* create mail_local_mailbox_tbl unique index */
1958 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_box_idx1 ON mail_box_tbl (account_id, local_yn, mailbox_name)");
1960 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1961 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1962 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1964 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1966 } /* mail_box_tbl */
1967 else if (type == EMAIL_CREATE_DB_CHECK) {
1968 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
1969 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1970 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
1972 sqlite3_free_table(result);
1974 /* 3. create mail_read_mail_uid_tbl */
1975 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
1976 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1977 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1978 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1981 if (atoi(result[1]) < 1) {
1982 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1983 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1984 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1986 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
1988 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1990 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1991 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1992 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1994 /* create mail_read_mail_uid_tbl unique index */
1995 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_read_mail_uid_idx1 ON mail_read_mail_uid_tbl (account_id, mailbox_id, local_uid, mailbox_name, s_uid)");
1997 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1998 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1999 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2000 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2002 } /* mail_read_mail_uid_tbl */
2003 else if (type == EMAIL_CREATE_DB_CHECK) {
2004 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2005 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2006 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2008 sqlite3_free_table(result);
2011 /* 4. create mail_rule_tbl */
2012 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2014 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2015 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2016 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2018 if (atoi(result[1]) < 1) {
2019 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2020 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2021 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2023 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2025 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2027 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2028 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2029 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2030 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2032 } /* mail_rule_tbl */
2033 else if (type == EMAIL_CREATE_DB_CHECK) {
2034 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2035 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2036 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2038 sqlite3_free_table(result);
2041 /* 5. create mail_tbl */
2042 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2043 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2044 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2045 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2047 if (atoi(result[1]) < 1) {
2048 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2049 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2050 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2051 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2053 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2055 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2056 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2057 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2059 /* create mail_tbl unique index */
2060 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_idx1 ON mail_tbl (mail_id, account_id)");
2061 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2062 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2063 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2065 /* create mail_tbl index for date_time */
2066 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE INDEX mail_idx_date_time ON mail_tbl (date_time)");
2067 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2068 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2069 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2071 /* create mail_tbl index for thread_item_count */
2072 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE INDEX mail_idx_thread_mail_count ON mail_tbl (thread_item_count)");
2073 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2074 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2075 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2077 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2079 /* just one time call */
2080 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2082 else if (type == EMAIL_CREATE_DB_CHECK) {
2083 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2084 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2085 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2087 sqlite3_free_table(result);
2090 /* 6. create mail_attachment_tbl */
2091 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2092 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2093 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2094 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2096 if (atoi(result[1]) < 1) {
2097 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2098 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2099 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2100 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2102 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2104 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2105 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2106 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2108 /* create mail_attachment_tbl unique index */
2109 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_attachment_idx1 ON mail_attachment_tbl (mail_id, attachment_id) ");
2111 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2112 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2113 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2114 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2116 } /* mail_attachment_tbl */
2117 else if (type == EMAIL_CREATE_DB_CHECK) {
2118 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2119 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2120 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2122 sqlite3_free_table(result);
2124 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2126 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2127 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2128 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2129 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2131 if (atoi(result[1]) < 1) {
2133 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2134 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2135 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2137 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2139 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2141 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2142 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2143 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2144 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2146 } /* mail_rule_tbl */
2147 else if (type == EMAIL_CREATE_DB_CHECK) {
2148 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2149 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2150 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2152 sqlite3_free_table(result);
2154 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2156 /* create mail_meeting_tbl */
2157 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2158 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2159 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2160 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2162 if (atoi(result[1]) < 1) {
2163 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2164 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2165 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2167 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2169 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2171 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2172 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2173 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2174 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_meeting_idx1 ON mail_meeting_tbl (mail_id)");
2176 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2177 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2178 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2179 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2181 } /* mail_contact_sync_tbl */
2182 else if (type == EMAIL_CREATE_DB_CHECK) {
2183 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2184 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2185 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2187 sqlite3_free_table(result);
2189 #ifdef __FEATURE_LOCAL_ACTIVITY__
2191 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2192 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2193 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2194 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2196 if (atoi(result[1]) < 1) {
2198 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2199 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2200 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2202 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2204 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2206 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2207 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2208 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2209 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2211 } /* mail_rule_tbl */
2212 else if (type == EMAIL_CREATE_DB_CHECK) {
2213 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2214 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2215 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2216 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2218 sqlite3_free_table(result);
2220 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2221 /* create mail_certificate_tbl */
2222 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2223 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2224 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2225 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2227 if (atoi(result[1]) < 1) {
2228 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2229 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2231 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2233 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2235 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2236 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2237 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_certificate_idx1 ON mail_certificate_tbl (certificate_id)");
2239 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2240 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2241 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2242 } /* mail_contact_sync_tbl */
2243 else if (type == EMAIL_CREATE_DB_CHECK) {
2244 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2245 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2248 /* create mail_task_tbl */
2249 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2251 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2253 if (atoi(result[1]) < 1) {
2254 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2255 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2257 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2259 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2261 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2262 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2263 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_certificate_idx1 ON mail_certificate_tbl (certificate_id)");
2265 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2266 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2267 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2268 } /* mail_task_tbl */
2269 else if (type == EMAIL_CREATE_DB_CHECK) {
2270 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2271 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TASK_TBL], rc, sqlite3_errmsg(local_db_handle)));
2274 sqlite3_free_table(result);
2280 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2283 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2288 if (err_code != NULL)
2291 EM_DEBUG_FUNC_END("ret [%d]", ret);
2295 /* Query series --------------------------------------------------------------*/
2297 INTERNAL_FUNC int emstorage_query_mail_count(const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
2299 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_transaction[%d], output_total_mail_count[%p], output_unseen_mail_count[%p]", input_conditional_clause, input_transaction, output_total_mail_count, output_unseen_mail_count);
2301 int error = EMAIL_ERROR_NONE;
2302 DB_STMT hStmt = NULL;
2303 char sql_query_string[QUERY_SIZE] = {0, };
2306 sqlite3 *local_db_handle = NULL;
2308 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2309 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2310 return EMAIL_ERROR_INVALID_PARAM;
2313 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
2314 local_db_handle = emstorage_get_db_connection();
2316 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2318 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT COUNT(*) FROM mail_tbl");
2319 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2321 if (output_total_mail_count) {
2322 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2323 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2324 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2326 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2327 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2328 ("sqlite3_step fail:%d", rc));
2329 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2332 if (output_unseen_mail_count) {
2333 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2335 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2336 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2337 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2339 *output_unseen_mail_count = atoi(result[1]);
2340 sqlite3_free_table(result);
2345 if (hStmt != NULL) {
2346 rc = sqlite3_finalize(hStmt);
2347 if (rc != SQLITE_OK) {
2348 EM_DEBUG_LOG("sqlite3_finalize failed [%d] : %s", rc, err_msg);
2349 error = EMAIL_ERROR_DB_FAILURE;
2353 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2356 EM_DEBUG_FUNC_END("error [%d]", error);
2360 INTERNAL_FUNC int emstorage_query_mail_id_list(const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
2362 EM_DEBUG_FUNC_BEGIN("input_conditional_clause [%p], input_transaction [%d], output_mail_id_list [%p], output_mail_id_count [%p]", input_conditional_clause, input_transaction, output_mail_id_list, output_mail_id_count);
2369 int error = EMAIL_ERROR_NONE;
2370 int *result_mail_id_list = NULL;
2371 char **result = NULL;
2372 char sql_query_string[QUERY_SIZE] = {0, };
2373 sqlite3 *local_db_handle = emstorage_get_db_connection();
2375 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2376 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2377 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2379 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2381 /* Composing query */
2382 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl ");
2383 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2385 EM_DEBUG_LOG("query[%s].", sql_query_string);
2387 /* Performing query */
2388 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2389 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2390 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2394 /* to get mail list */
2396 EM_DEBUG_EXCEPTION("No mail found...");
2397 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2401 EM_DEBUG_LOG("There are [%d] mails.", count);
2403 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2404 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2405 error = EMAIL_ERROR_OUT_OF_MEMORY;
2409 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2411 for (i = 0; i < count; i++)
2412 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2414 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2416 *output_mail_id_list = result_mail_id_list;
2417 *output_mail_id_count = count;
2422 sqlite3_free_table(result);
2424 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2427 if(error != EMAIL_ERROR_NONE)
2428 EM_SAFE_FREE(result_mail_id_list);
2430 EM_DEBUG_FUNC_END("error [%d]", error);
2434 INTERNAL_FUNC int emstorage_query_mail_list(const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list, int *result_count, int *err_code)
2436 EM_DEBUG_FUNC_BEGIN();
2437 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2439 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list)?0:1;
2440 int local_inline_content_count = 0, local_attachment_count = 0;
2441 int cur_query = 0, base_count = 0, col_index;
2442 int ret = false, error = EMAIL_ERROR_NONE;
2443 char *date_time_string = NULL;
2444 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2445 char *field_list = "mail_id, account_id, mailbox_id, full_address_from, email_address_sender, full_address_to, subject, body_download_status, flags_seen_field, flags_deleted_field, flags_flagged_field, flags_answered_field, flags_recent_field, flags_draft_field, flags_forwarded_field, DRM_status, priority, save_status, lock_status, attachment_count, inline_content_count, date_time, preview_text, thread_id, thread_item_count, meeting_request_status, message_class, smime_type ";
2446 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2447 sqlite3 *local_db_handle = emstorage_get_db_connection();
2449 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2450 EM_IF_NULL_RETURN_VALUE(result_count, false);
2452 EMSTORAGE_START_READ_TRANSACTION(transaction);
2455 if (to_get_count) /* count only */
2456 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl");
2457 else /* mail list in plain form */
2458 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_tbl ", field_list);
2460 /* cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, conditional_clause); This code caused some crashes.*/
2461 strncat(sql_query_string, conditional_clause, QUERY_SIZE - cur_query);
2463 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
2465 /* performing query */
2466 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2467 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2468 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2469 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2470 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2473 base_count = ({ int i=0; char *tmp = field_list; for (i=0; tmp && *(tmp + 1); tmp = index(tmp + 1, ','), i++); i; });
2475 col_index = base_count;
2477 EM_DEBUG_LOG("base_count [%d]", base_count);
2482 EM_DEBUG_EXCEPTION("No mail found...");
2484 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2487 EM_DEBUG_LOG("There are [%d] mails.", count);
2490 /* to get mail list */
2492 EM_DEBUG_EXCEPTION("No mail found...");
2494 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2498 EM_DEBUG_LOG("There are [%d] mails.", count);
2499 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2500 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2501 error = EMAIL_ERROR_OUT_OF_MEMORY;
2505 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2506 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2507 for (i = 0; i < count; i++) {
2508 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2509 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2510 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2511 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2512 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2513 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2514 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2515 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2516 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2517 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2518 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2519 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2520 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2521 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2522 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2523 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2524 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2525 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2526 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2527 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2528 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2529 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2530 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2531 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2532 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2533 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2534 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2535 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2537 mail_list_item_from_tbl[i].attachment_count = ((local_attachment_count - local_inline_content_count)>0)?1:0;
2539 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2540 EM_PROFILE_END(emstorage_query_mail_list_loop);
2543 sqlite3_free_table(result);
2548 EM_DEBUG_LOG("COUNT [%d]", count);
2551 *result_count = count;
2554 if (result_mail_list)
2555 *result_mail_list = mail_list_item_from_tbl;
2556 *result_count = count;
2559 EM_SAFE_FREE(mail_list_item_from_tbl);
2562 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2564 sqlite3_release_memory(-1);
2568 EM_SAFE_FREE(date_time_string);
2570 if (err_code != NULL)
2573 EM_PROFILE_END(emstorage_query_mail_list_func);
2574 EM_DEBUG_FUNC_END("ret [%d]", ret);
2579 INTERNAL_FUNC int emstorage_query_mail_tbl(const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
2581 EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_tbl, result_count, transaction, err_code);
2583 if (!conditional_clause) {
2584 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2586 if (err_code != NULL)
2587 *err_code = EMAIL_ERROR_INVALID_PARAM;
2591 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2592 int error = EMAIL_ERROR_NONE;
2593 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2594 emstorage_mail_tbl_t* p_data_tbl = NULL;
2595 sqlite3 *local_db_handle = emstorage_get_db_connection();
2597 EMSTORAGE_START_READ_TRANSACTION(transaction);
2599 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2601 EM_DEBUG_LOG("Query [%s]", sql_query_string);
2603 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2604 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2605 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2608 EM_DEBUG_EXCEPTION("No mail found...");
2610 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2614 EM_DEBUG_LOG("There are [%d] mails.", count);
2615 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2616 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2617 error = EMAIL_ERROR_OUT_OF_MEMORY;
2621 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2622 for (i = 0; i < count; i++) {
2623 _get_table_field_data_int (result, &(p_data_tbl[i].mail_id), col_index++);
2624 _get_table_field_data_int (result, &(p_data_tbl[i].account_id), col_index++);
2625 _get_table_field_data_int (result, &(p_data_tbl[i].mailbox_id), col_index++);
2626 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2627 _get_table_field_data_int (result, &(p_data_tbl[i].mailbox_type), col_index++);
2628 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
2629 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
2630 _get_table_field_data_int (result, &(p_data_tbl[i].server_mail_status), col_index++);
2631 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
2632 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
2633 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
2634 _get_table_field_data_int (result, &(p_data_tbl[i].reference_mail_id), col_index++);
2635 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
2636 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
2637 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
2638 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
2639 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
2640 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
2641 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
2642 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
2643 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
2644 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
2645 _get_table_field_data_int (result, &(p_data_tbl[i].body_download_status), col_index++);
2646 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
2647 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
2648 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
2649 _get_table_field_data_int (result, &(p_data_tbl[i].mail_size), col_index++);
2650 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
2651 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
2652 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
2653 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
2654 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
2655 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
2656 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
2657 _get_table_field_data_int (result, &(p_data_tbl[i].DRM_status), col_index++);
2658 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].priority), col_index++);
2659 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].save_status), col_index++);
2660 _get_table_field_data_int (result, &(p_data_tbl[i].lock_status), col_index++);
2661 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].report_status), col_index++);
2662 _get_table_field_data_int (result, &(p_data_tbl[i].attachment_count), col_index++);
2663 _get_table_field_data_int (result, &(p_data_tbl[i].inline_content_count), col_index++);
2664 _get_table_field_data_int (result, &(p_data_tbl[i].thread_id), col_index++);
2665 _get_table_field_data_int (result, &(p_data_tbl[i].thread_item_count), col_index++);
2666 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
2667 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
2668 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].message_class), col_index++);
2669 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].digest_type), col_index++);
2670 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].smime_type), col_index++);
2671 /* check real body file... */
2672 if (p_data_tbl[i].body_download_status) {
2675 if (p_data_tbl[i].file_path_html && EM_SAFE_STRLEN(p_data_tbl[i].file_path_html) > 0) {
2676 if (stat(p_data_tbl[i].file_path_html, &buf) == -1) {
2678 p_data_tbl[i].body_download_status = 0;
2681 else if (p_data_tbl[i].file_path_plain && EM_SAFE_STRLEN(p_data_tbl[i].file_path_plain) > 0) {
2682 if (stat(p_data_tbl[i].file_path_plain, &buf) == -1){
2684 p_data_tbl[i].body_download_status = 0;
2688 p_data_tbl[i].body_download_status = 0;
2696 sqlite3_free_table(result);
2699 if (result_mail_tbl)
2700 *result_mail_tbl = p_data_tbl;
2701 *result_count = count;
2704 EM_SAFE_FREE(p_data_tbl);
2706 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2708 sqlite3_release_memory(-1);
2712 if (err_code != NULL)
2715 EM_DEBUG_FUNC_END("ret [%d]", ret);
2719 INTERNAL_FUNC int emstorage_query_mailbox_tbl(const char *input_conditional_clause, const char *input_ordering_clause, int input_get_mail_count, int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count)
2721 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], input_get_mail_count[%d], input_transaction[%d], output_mailbox_list[%p], output_mailbox_count[%d]", input_conditional_clause, input_ordering_clause, input_get_mail_count, input_transaction, output_mailbox_list, output_mailbox_count);
2727 int error = EMAIL_ERROR_NONE;
2729 int total_count = 0;
2731 char sql_query_string[QUERY_SIZE] = {0, };
2732 char *fields = "MBT.mailbox_id, MBT.account_id, local_yn, MBT.mailbox_name, MBT.mailbox_type, alias, deleted_flag, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, no_select, last_sync_time ";
2733 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
2734 sqlite3 *local_db_handle = emstorage_get_db_connection();
2736 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2738 if (input_get_mail_count == 0) { /* without mail count */
2740 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s", fields, input_conditional_clause);
2742 else { /* with read count and total count */
2744 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_name, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl %s GROUP BY mailbox_name) AS MT ON MBT.mailbox_name = MT.mailbox_name %s %s", fields, input_conditional_clause, input_conditional_clause, input_ordering_clause);
2747 EM_DEBUG_LOG("query[%s]", sql_query_string);
2749 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
2750 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2751 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)))
2753 EM_DEBUG_LOG("result count [%d]", count);
2756 EM_DEBUG_EXCEPTION("Can't find mailbox");
2757 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2761 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
2762 EM_DEBUG_EXCEPTION("em_malloc failed...");
2763 error = EMAIL_ERROR_OUT_OF_MEMORY;
2767 for (i = 0; i < count; i++) {
2768 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
2769 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
2770 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
2771 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2772 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
2773 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
2774 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
2775 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
2776 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
2777 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
2778 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
2779 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
2780 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
2782 if (input_get_mail_count == 1) {
2783 _get_table_field_data_int(result, &(total_count), col_index++);
2784 p_data_tbl[i].total_mail_count_on_local = total_count;
2785 _get_table_field_data_int(result, &(read_count), col_index++);
2786 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
2793 sqlite3_free_table(result);
2795 if (error == EMAIL_ERROR_NONE) {
2796 *output_mailbox_list = p_data_tbl;
2797 *output_mailbox_count = count;
2798 EM_DEBUG_LOG("Mailbox Count [ %d]", count);
2801 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2803 sqlite3_release_memory(-1);
2807 EM_DEBUG_FUNC_END("error [%d]", error);
2811 /* Query series --------------------------------------------------------------*/
2813 INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, int transaction, int *err_code)
2815 EM_DEBUG_FUNC_BEGIN();
2818 if (err_code != NULL)
2819 *err_code = EMAIL_ERROR_INVALID_PARAM;
2823 int rc = -1, ret = false;
2824 int error = EMAIL_ERROR_NONE;
2827 char sql_query_string[QUERY_SIZE] = {0, };
2829 sqlite3 *local_db_handle = emstorage_get_db_connection();
2830 EMSTORAGE_START_READ_TRANSACTION(transaction);
2832 SNPRINTF(sql_query_string, sizeof(sql_query_string),
2833 "SELECT COUNT(*) FROM mail_account_tbl "
2835 " user_email_address = '%s' AND "
2836 " incoming_server_user_name = '%s' AND "
2837 " incoming_server_type = %d AND "
2838 " incoming_server_address = '%s' AND "
2839 " outgoing_server_user_name = '%s' AND "
2840 " outgoing_server_type = %d AND "
2841 " outgoing_server_address = '%s'; ",
2842 account->user_email_address,
2843 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
2844 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
2846 EM_DEBUG_LOG("Query[%s]", sql_query_string);
2847 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2848 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2849 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2851 count = atoi(result[1]);
2852 sqlite3_free_table(result);
2854 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
2856 if (count == 0) { /* not duplicated account */
2858 EM_DEBUG_LOG("NOT duplicated account: user_email_address[%s]", account->user_email_address);
2860 else { /* duplicated account */
2862 EM_DEBUG_LOG("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
2863 error = EMAIL_ERROR_ALREADY_EXISTS;
2868 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2871 if (err_code != NULL)
2874 EM_DEBUG_FUNC_END("ret [%d]", ret);
2879 INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *err_code)
2881 EM_DEBUG_FUNC_BEGIN();
2884 if (err_code != NULL)
2885 *err_code = EMAIL_ERROR_INVALID_PARAM;
2889 int rc = -1, ret = false;
2890 int error = EMAIL_ERROR_NONE;
2892 DB_STMT hStmt = NULL;
2893 char sql_query_string[QUERY_SIZE] = {0, };
2896 sqlite3 *local_db_handle = emstorage_get_db_connection();
2897 EMSTORAGE_START_READ_TRANSACTION(transaction);
2898 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
2899 EM_DEBUG_LOG("SQL STMT [ %s ]", sql_query_string);
2900 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2901 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
2902 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2903 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2905 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2906 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2907 ("sqlite3_step fail:%d", rc));
2909 *count = sqlite3_column_int(hStmt, 0);
2915 if (hStmt != NULL) {
2916 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
2918 rc = sqlite3_finalize(hStmt);
2920 if (rc != SQLITE_OK) {
2921 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d: %s", rc, err_msg);
2922 error = EMAIL_ERROR_DB_FAILURE;
2924 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
2927 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2930 if (err_code != NULL)
2933 EM_DEBUG_FUNC_END("ret [%d]", ret);
2938 INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_tbl_t** account_list, int transaction, int with_password, int *err_code)
2940 EM_DEBUG_FUNC_BEGIN();
2942 int i = 0, count = 0, rc = -1, ret = false;
2943 int field_index = 0;
2944 int error = EMAIL_ERROR_NONE;
2945 emstorage_account_tbl_t *p_data_tbl = NULL;
2946 char sql_query_string[QUERY_SIZE] = {0, };
2947 char *sql = "SELECT count(*) FROM mail_account_tbl;";
2949 sqlite3 *local_db_handle = NULL;
2950 DB_STMT hStmt = NULL;
2952 if (!select_num || !account_list) {
2953 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
2954 if (err_code != NULL)
2955 *err_code = EMAIL_ERROR_INVALID_PARAM;
2959 local_db_handle = emstorage_get_db_connection();
2960 EMSTORAGE_START_READ_TRANSACTION(transaction);
2962 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2963 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2964 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2966 count = atoi(result[1]);
2967 sqlite3_free_table(result);
2969 EM_DEBUG_LOG("count = %d", rc);
2972 EM_DEBUG_EXCEPTION("no account found...");
2973 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
2977 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_account_tbl ORDER BY account_id");
2979 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2981 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
2982 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2983 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2985 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2986 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2987 ("sqlite3_step fail:%d", rc));
2988 if (rc == SQLITE_DONE) {
2989 EM_DEBUG_EXCEPTION("no account found...");
2991 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
2997 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
2998 EM_DEBUG_EXCEPTION("malloc failed...");
2999 error = EMAIL_ERROR_OUT_OF_MEMORY;
3002 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3003 for (i = 0; i < count; i++) {
3006 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3007 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3008 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3009 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3010 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3011 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3012 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3013 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3014 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3015 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3016 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3017 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3018 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3019 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3020 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3021 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3022 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3023 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3024 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3025 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3026 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3027 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3028 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3029 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3030 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3031 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3032 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3033 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3034 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3035 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3036 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3037 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3038 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3039 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3040 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3041 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3042 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3043 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3044 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3045 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3046 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3047 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3048 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3049 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3050 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3051 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3052 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3053 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3054 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3055 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3056 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3058 if (with_password == true) {
3059 /* get password from the secure storage */
3060 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3061 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3063 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3064 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3066 /* get password file name */
3067 if ((error = _get_password_file_name(p_data_tbl[i].account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3068 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3069 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3073 /* read password from secure storage */
3074 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl[i].incoming_server_password))) < 0 ) {
3075 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed. [%d]", error);
3076 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3080 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl[i].outgoing_server_password))) < 0) {
3081 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed. [%d]", error);
3082 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3087 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3088 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3089 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3090 ("sqlite3_step fail:%d", rc));
3097 *account_list = p_data_tbl;
3098 *select_num = count;
3099 EM_DEBUG_LOG("COUNT : %d", count);
3101 else if (p_data_tbl != NULL)
3102 emstorage_free_account(&p_data_tbl, count, NULL);
3104 if (hStmt != NULL) {
3105 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
3107 rc = sqlite3_finalize(hStmt);
3109 if (rc != SQLITE_OK) {
3110 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3111 error = EMAIL_ERROR_DB_FAILURE;
3115 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3118 if (err_code != NULL)
3121 EM_DEBUG_FUNC_END("ret [%d]", ret);
3126 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(int account_id, char *server_mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
3128 EM_DEBUG_FUNC_BEGIN("account_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", account_id, server_mail_id, mail, transaction, err_code);
3130 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3131 char conditional_clause[QUERY_SIZE] = {0, };
3132 emstorage_mail_tbl_t* p_data_tbl = NULL;
3134 if (!server_mail_id || !mail) {
3135 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3136 error = EMAIL_ERROR_INVALID_PARAM;
3140 if (account_id == ALL_ACCOUNT)
3141 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
3143 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND account_id = %d", server_mail_id, account_id);
3145 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3147 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
3148 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3158 if (err_code != NULL)
3161 EM_DEBUG_FUNC_END("ret [%d]", ret);
3166 static int _write_conditional_clause_for_getting_mail_list(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, bool input_except_delete_flagged_mails, char *conditional_clause_string, int buffer_size, int *err_code)
3168 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3170 if (account_id < ALL_ACCOUNT) {
3171 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3172 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3176 if (account_id == ALL_ACCOUNT) {
3177 cur_clause += (conditional_clause_count++ == 0)?
3178 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3179 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3182 cur_clause += (conditional_clause_count++ == 0)?
3183 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
3184 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3187 if (mailbox_id > 0) {
3188 cur_clause += (conditional_clause_count++ == 0)?
3189 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
3190 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3192 else if(account_id != ALL_ACCOUNT) {
3193 cur_clause += (conditional_clause_count++ == 0)?
3194 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3195 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3198 if (thread_id > 0) {
3199 cur_clause += (conditional_clause_count++ == 0)?
3200 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id):
3201 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3203 else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3204 cur_clause += (conditional_clause_count++ == 0)?
3205 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0"):
3206 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3208 else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3209 cur_clause += (conditional_clause_count++ == 0)?
3210 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0"):
3211 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3213 else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3214 cur_clause += (conditional_clause_count++ == 0)?
3215 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0"):
3216 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3219 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3220 if (addr_list && addr_list->address_count > 0) {
3221 if (!addr_list->address_type) {
3222 cur_clause += (conditional_clause_count++ == 0)?
3223 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]):
3224 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3226 for (i = 1; i < addr_list->address_count; i++)
3227 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3229 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3231 cur_clause += (conditional_clause_count++ == 0)?
3232 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE full_address_to IN(\"%s\") or full_address_cc IN(\"%s\"", addr_list->address_list[0], addr_list->address_list[0]):
3233 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND full_address_to IN(\"%s\") or full_address_cc IN(\"%s\"", addr_list->address_list[0], addr_list->address_list[0]);
3235 for (i = 1; i < addr_list->address_count; i++)
3236 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3238 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3242 if (input_except_delete_flagged_mails) {
3243 cur_clause += (conditional_clause_count++ == 0)?
3244 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0"):
3245 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3249 switch (search_type) {
3250 case EMAIL_SEARCH_FILTER_SUBJECT:
3251 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3252 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3254 case EMAIL_SEARCH_FILTER_SENDER:
3255 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3256 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3257 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3259 case EMAIL_SEARCH_FILTER_RECIPIENT:
3260 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3261 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3262 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3263 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3264 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3266 case EMAIL_SEARCH_FILTER_ALL:
3267 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3268 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3269 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3270 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3271 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3272 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3275 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3280 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3281 static char sorting_str[][50] = {
3282 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3283 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3284 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3285 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3286 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3287 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3288 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3289 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3290 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3291 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3292 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3293 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3294 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3295 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3296 " ORDER BY mailbox_name DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_NAME_HIGH: */
3297 " ORDER BY mailbox_name ASC, date_time DESC" /* case EMAIL_SORT_MAILBOX_NAME_LOW: */
3300 if (sorting < EMAIL_SORT_END && sorting >= 0)
3301 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3303 EM_DEBUG_LOG(" Invalid Sorting order ");
3306 if (start_index != -1 && limit_count != -1)
3307 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3314 * emstorage_get_mail_list - Get the mail list information.
3318 INTERNAL_FUNC int emstorage_get_mail_list(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, int transaction, email_mail_list_item_t** mail_list, int *result_count, int *err_code)
3320 EM_DEBUG_FUNC_BEGIN();
3321 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3323 int ret = false, error = EMAIL_ERROR_NONE;
3324 char conditional_clause_string[QUERY_SIZE] = { 0, };
3326 if (account_id < ALL_ACCOUNT) {
3327 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3328 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3330 EM_IF_NULL_RETURN_VALUE(result_count, false);
3332 _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, addr_list, thread_id, start_index, limit_count, search_type, search_value, sorting, true, conditional_clause_string, QUERY_SIZE, &error);
3334 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3336 if(!emstorage_query_mail_list(conditional_clause_string, transaction, mail_list, result_count, &error)) {
3337 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3344 if (err_code != NULL)
3347 EM_PROFILE_END(emstorage_get_mail_list_func);
3348 EM_DEBUG_FUNC_END("ret [%d]", ret);
3354 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3358 INTERNAL_FUNC int emstorage_get_mails(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, email_sort_type_t sorting, int transaction, emstorage_mail_tbl_t** mail_list, int *result_count, int *err_code)
3360 EM_DEBUG_FUNC_BEGIN();
3361 EM_PROFILE_BEGIN(emStorageGetMails);
3363 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3364 emstorage_mail_tbl_t *p_data_tbl = NULL;
3365 char conditional_clause_string[QUERY_SIZE] = {0, };
3367 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3368 EM_IF_NULL_RETURN_VALUE(result_count, false);
3370 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3371 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3372 if (err_code != NULL)
3373 *err_code = EMAIL_ERROR_INVALID_PARAM;
3377 _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, addr_list, thread_id, start_index, limit_count, 0, NULL, sorting, true, conditional_clause_string, QUERY_SIZE, &error);
3379 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3381 if(!emstorage_query_mail_tbl(conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
3382 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3390 *mail_list = p_data_tbl;
3391 *result_count = count;
3392 EM_DEBUG_LOG("COUNT : %d", count);
3394 else if (p_data_tbl != NULL)
3395 emstorage_free_mail(&p_data_tbl, count, NULL);
3397 if (err_code != NULL)
3400 EM_PROFILE_END(emStorageGetMails);
3401 EM_DEBUG_FUNC_END("ret [%d]", ret);
3408 * emstorage_get_searched_mail_list - Get the mail list information after filtering
3412 INTERNAL_FUNC int emstorage_get_searched_mail_list(int account_id, int mailbox_id, int thread_id, int search_type, const char *search_value, int start_index, int limit_count, email_sort_type_t sorting, int transaction, email_mail_list_item_t** mail_list, int *result_count, int *err_code)
3414 EM_DEBUG_FUNC_BEGIN();
3416 int ret = false, error = EMAIL_ERROR_NONE;
3417 char conditional_clause[QUERY_SIZE] = {0, };
3418 char *temp_search_value = NULL;
3419 char *temp_search_value2 = NULL;
3421 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3422 EM_IF_NULL_RETURN_VALUE(result_count, false);
3424 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3425 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3426 error = EMAIL_ERROR_INVALID_PARAM;
3430 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
3431 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
3433 _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, search_type, temp_search_value2, sorting, true, conditional_clause, QUERY_SIZE, &error);
3435 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3437 if(!emstorage_query_mail_list(conditional_clause, transaction, mail_list, result_count, &error)) {
3438 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3445 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3447 if (err_code != NULL)
3450 EM_SAFE_FREE(temp_search_value);
3451 EM_SAFE_FREE(temp_search_value2);
3453 EM_DEBUG_FUNC_END("ret [%d]", ret);
3458 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name)
3460 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
3462 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
3463 EM_DEBUG_EXCEPTION("Invalid parameter");
3464 return EMAIL_ERROR_INVALID_PARAM;
3467 sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
3468 sprintf(send_password_file_name, ".email_account_%d_send", account_id);
3469 EM_DEBUG_FUNC_END();
3470 return EMAIL_ERROR_NONE;
3473 static int _read_password_from_secure_storage(char *file_name, char **password)
3475 EM_DEBUG_FUNC_BEGIN("file_name[%s], password[%p]", file_name, password);
3477 if (!file_name || !password) {
3478 EM_DEBUG_EXCEPTION("Invalid Parameter");
3479 return EMAIL_ERROR_INVALID_PARAM;
3482 size_t buf_len = 0, read_len = 0;
3483 ssm_file_info_t sfi;
3484 char *temp_password = NULL;
3485 int error_code_from_ssm = 0;
3486 int ret = EMAIL_ERROR_NONE;
3488 if ( (error_code_from_ssm = ssm_getinfo(file_name, &sfi, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3489 EM_DEBUG_EXCEPTION("ssm_getinfo() failed. [%d]", error_code_from_ssm);
3490 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3494 buf_len = sfi.originSize;
3495 EM_DEBUG_LOG("password buf_len[%d]", buf_len);
3496 if ((temp_password = (char *)malloc(buf_len + 1)) == NULL) {
3497 EM_DEBUG_EXCEPTION("malloc failed...");
3498 ret = EMAIL_ERROR_OUT_OF_MEMORY;
3501 memset(temp_password, 0x00, buf_len + 1);
3503 if ( (error_code_from_ssm = ssm_read(file_name, temp_password, buf_len, &read_len, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3504 EM_DEBUG_EXCEPTION("ssm_read() failed. [%d]", error_code_from_ssm);
3505 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3509 EM_DEBUG_LOG("password_file_name[%s], password[%s], originSize[%d], read len[%d]", file_name, temp_password, sfi.originSize, read_len);
3511 *password = temp_password;
3512 temp_password = NULL;
3515 EM_SAFE_FREE(temp_password);
3517 EM_DEBUG_FUNC_END("ret [%d]", ret);
3522 INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
3524 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
3527 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
3528 if (err_code != NULL)
3529 *err_code = EMAIL_ERROR_INVALID_PARAM;
3534 int error = EMAIL_ERROR_NONE;
3535 emstorage_account_tbl_t* p_data_tbl = NULL;
3536 DB_STMT hStmt = NULL;
3537 char sql_query_string[QUERY_SIZE] = {0, };
3540 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3541 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3543 sqlite3 *local_db_handle = emstorage_get_db_connection();
3544 EMSTORAGE_START_READ_TRANSACTION(transaction);
3546 /* Make query string */
3547 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3548 sql_len = EM_SAFE_STRLEN(sql_query_string);
3550 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3551 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3552 "incoming_server_type,"
3553 "incoming_server_address,"
3554 "user_email_address,"
3555 "incoming_server_user_name,"
3557 "incoming_server_port_number,"
3558 "incoming_server_secure_connection,"
3559 "outgoing_server_type,"
3560 "outgoing_server_address,"
3561 "outgoing_server_port_number,"
3562 "outgoing_server_need_authentication,"
3563 "outgoing_server_secure_connection,"
3564 "outgoing_server_user_name,"
3565 "user_display_name,"
3569 "keep_mails_on_pop_server_after_download,"
3570 "auto_download_size,"
3571 "outgoing_server_use_same_authenticator,"
3573 "incoming_server_requires_apop,"
3579 sql_len = EM_SAFE_STRLEN(sql_query_string);
3582 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
3583 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
3584 sql_len = EM_SAFE_STRLEN(sql_query_string);
3587 /* get from secure storage, not from db */
3588 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3589 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3592 "req_delivery_receipt,"
3597 "display_name_from,"
3599 "forward_with_files,"
3603 "add_my_address_to_bcc,"
3606 "default_mail_slot_size,"
3612 sql_len = EM_SAFE_STRLEN(sql_query_string);
3615 /* dummy value, FROM WHERE clause */
3616 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
3619 EM_DEBUG_LOG("query = [%s]", sql_query_string);
3621 /* execute a sql and count rows */
3622 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3623 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3624 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3626 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3627 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3628 ("sqlite3_step fail:%d", rc));
3630 if (rc == SQLITE_DONE) {
3631 EM_DEBUG_EXCEPTION("no matched account found...");
3632 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3636 /* Assign query result to structure */
3637 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
3638 EM_DEBUG_EXCEPTION("malloc failed...");
3639 error = EMAIL_ERROR_OUT_OF_MEMORY;
3643 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
3646 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3647 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
3648 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
3649 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
3650 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
3651 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
3652 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
3653 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
3654 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
3655 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
3656 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
3657 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
3658 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
3659 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
3660 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
3661 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
3662 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
3663 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
3664 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
3665 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
3666 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
3667 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
3668 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
3669 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
3670 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
3671 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
3672 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
3673 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
3676 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
3677 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
3679 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
3680 /* get password file name */
3681 if ((error = _get_password_file_name(p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3682 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3686 /* read password from secure storage */
3687 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
3688 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed. [%d]", error);
3692 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
3694 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
3695 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed. [%d]", error);
3698 EM_DEBUG_LOG("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
3701 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3702 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
3703 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
3704 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
3705 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
3706 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
3707 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
3708 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
3709 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
3710 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
3711 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
3712 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
3713 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
3714 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
3715 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
3716 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
3717 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
3718 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
3719 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
3720 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
3721 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
3722 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
3729 *account = p_data_tbl;
3732 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
3734 if (hStmt != NULL) {
3735 rc = sqlite3_finalize(hStmt);
3736 if (rc != SQLITE_OK) {
3737 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3738 error = EMAIL_ERROR_DB_FAILURE;
3742 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3745 if (err_code != NULL)
3748 EM_DEBUG_FUNC_END("ret [%d]", ret);
3752 INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int *password_length, int *err_code)
3754 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
3756 if (account_id <= 0 || password_length == NULL) {
3757 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3758 if (err_code != NULL)
3759 *err_code = EMAIL_ERROR_INVALID_PARAM;
3764 int error = EMAIL_ERROR_NONE;
3765 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3766 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3767 char *temp_password = NULL;
3770 /* get password file name */
3771 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3772 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3776 /* read password from secure storage */
3777 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
3778 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
3782 *password_length = EM_SAFE_STRLEN(temp_password);
3784 EM_DEBUG_LOG("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
3789 EM_SAFE_FREE(temp_password);
3791 if (err_code != NULL)
3794 EM_DEBUG_FUNC_END("ret [%d]", ret);
3798 INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
3800 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
3802 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
3803 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3804 if (err_code != NULL)
3805 *err_code = EMAIL_ERROR_INVALID_PARAM;
3809 int error = EMAIL_ERROR_NONE;
3810 int rc, ret = false;
3812 DB_STMT hStmt = NULL;
3813 char sql_query_string[QUERY_SIZE] = {0, };
3814 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3815 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3817 sqlite3 *local_db_handle = emstorage_get_db_connection();
3818 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
3820 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3821 "UPDATE mail_account_tbl SET"
3823 ", logo_icon_path = ?"
3825 ", user_data_length = ?"
3826 ", account_svc_id = ?"
3828 ", sync_disabled = ?"
3829 ", default_mail_slot_size = ?"
3830 ", user_display_name = ?"
3831 ", user_email_address = ?"
3832 ", reply_to_address = ?"
3833 ", return_address = ?"
3834 ", incoming_server_type = ?"
3835 ", incoming_server_address = ?"
3836 ", incoming_server_port_number = ?"
3837 ", incoming_server_user_name = ?"
3838 ", incoming_server_secure_connection = ?"
3839 ", retrieval_mode = ?"
3840 ", keep_mails_on_pop_server_after_download = ?"
3841 ", check_interval = ?"
3842 ", auto_download_size = ?"
3843 ", outgoing_server_type = ?"
3844 ", outgoing_server_address = ?"
3845 ", outgoing_server_port_number = ?"
3846 ", outgoing_server_user_name = ?"
3847 ", outgoing_server_secure_connection = ?"
3848 ", outgoing_server_need_authentication = ?"
3849 ", outgoing_server_use_same_authenticator = ?"
3851 ", keep_local_copy = ?"
3852 ", req_delivery_receipt = ?"
3853 ", req_read_receipt = ?"
3854 ", download_limit = ?"
3855 ", block_address = ?"
3856 ", block_subject = ?"
3857 ", display_name_from = ?"
3858 ", reply_with_body = ?"
3859 ", forward_with_files = ?"
3860 ", add_myname_card = ?"
3861 ", add_signature = ?"
3863 ", add_my_address_to_bcc = ?"
3864 ", pop_before_smtp = ?"
3865 ", incoming_server_requires_apop = ?"
3867 ", certificate_path = ?"
3870 " WHERE account_id = ?");
3872 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3873 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
3874 EM_DEBUG_LOG("SQL[%s]", sql_query_string);
3875 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3876 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
3880 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3881 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3882 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
3883 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
3884 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
3885 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
3886 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
3887 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
3888 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3889 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3890 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3891 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3892 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
3893 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3894 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
3895 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3896 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
3897 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
3898 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
3899 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
3900 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
3901 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
3902 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3903 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
3904 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
3905 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
3906 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
3907 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
3908 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
3909 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
3910 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
3911 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
3912 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
3913 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
3914 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
3915 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
3916 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
3917 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
3918 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
3919 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
3920 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
3921 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
3922 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
3923 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
3924 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
3925 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3926 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
3927 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
3928 _bind_stmt_field_data_int(hStmt, i++, account_id);
3930 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3931 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
3932 ("sqlite3_step fail:%d", rc));
3933 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3934 ("sqlite3_step fail:%d", rc));
3936 /* validate account existence */
3937 rc = sqlite3_changes(local_db_handle);
3939 EM_DEBUG_EXCEPTION(" no matched account found...");
3941 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3945 /* get password file name */
3946 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3947 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3951 /* save passwords to the secure storage */
3952 EM_DEBUG_LOG("save to the secure storage : recv_file[%s], recv_pass[%s], send_file[%s], send_pass[%s]", recv_password_file_name, account_tbl->incoming_server_password, send_password_file_name, account_tbl->outgoing_server_password);
3953 if (account_tbl->incoming_server_password && (EM_SAFE_STRLEN(account_tbl->incoming_server_password) > 0)) {
3954 if (ssm_write_buffer(account_tbl->incoming_server_password, EM_SAFE_STRLEN(account_tbl->incoming_server_password), recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
3955 EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -recv incoming_server_password : file[%s]", recv_password_file_name);
3956 error = EMAIL_ERROR_SYSTEM_FAILURE;
3961 if (account_tbl->outgoing_server_password && (EM_SAFE_STRLEN(account_tbl->outgoing_server_password) > 0)) {
3962 if (ssm_write_buffer(account_tbl->outgoing_server_password, EM_SAFE_STRLEN(account_tbl->outgoing_server_password), send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
3963 EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -send password : file[%s]", send_password_file_name);
3964 error = EMAIL_ERROR_SYSTEM_FAILURE;
3969 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
3970 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
3975 if (hStmt != NULL) {
3976 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
3978 rc = sqlite3_finalize(hStmt);
3979 if (rc != SQLITE_OK) {
3980 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
3981 error = EMAIL_ERROR_DB_FAILURE;
3985 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
3989 if (err_code != NULL)
3992 EM_DEBUG_FUNC_END("ret [%d]", ret);
3996 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account_id, char *field_name, int value, int transaction)
3998 EM_DEBUG_FUNC_BEGIN("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
3999 int error = EMAIL_ERROR_NONE;
4002 char sql_query_string[QUERY_SIZE] = {0, };
4003 sqlite3 *local_db_handle = NULL;
4005 if (!account_id || !field_name) {
4006 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4007 return EMAIL_ERROR_INVALID_PARAM;
4010 local_db_handle = emstorage_get_db_connection();
4012 /* Write query string */
4013 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4015 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4018 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4019 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4020 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4021 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4022 if (sqlite3_changes(local_db_handle) == 0)
4023 EM_DEBUG_LOG("no mail matched...");
4027 result = (error == EMAIL_ERROR_NONE) ? true : false;
4028 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, error);
4031 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4032 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4034 EM_DEBUG_FUNC_END("error [%d]", error);
4038 INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *result_sync_status,int *err_code)
4040 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4042 if(!result_sync_status) {
4043 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4044 if (err_code != NULL)
4045 *err_code = EMAIL_ERROR_INVALID_PARAM;
4049 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4050 char sql_query_string[QUERY_SIZE] = {0, };
4051 char **result = NULL;
4052 sqlite3 *local_db_handle = emstorage_get_db_connection();
4055 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4057 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4059 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4060 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4061 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4064 EM_DEBUG_EXCEPTION("no matched account found...");
4065 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4070 *result_sync_status = 0;
4072 for(i = 0; i < count; i++) {
4073 _get_table_field_data_int(result, &sync_status, col_index++);
4074 *result_sync_status |= sync_status;
4077 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4079 sqlite3_free_table(result);
4087 if (err_code != NULL)
4090 EM_DEBUG_FUNC_END("ret [%d]", ret);
4094 INTERNAL_FUNC int emstorage_update_sync_status_of_account(int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
4096 EM_DEBUG_FUNC_BEGIN("account_id[%d], set_operator[%d], sync_status [%d], transaction[%d], err_code[%p]", account_id, set_operator, sync_status, transaction, err_code);
4098 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4099 char sql_query_string[QUERY_SIZE] = {0, };
4100 sqlite3 *local_db_handle = emstorage_get_db_connection();
4102 if(set_operator != SET_TYPE_SET && account_id) {
4103 if(!emstorage_get_sync_status_of_account(account_id, &result_sync_status, &error)) {
4104 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4107 switch(set_operator) {
4108 case SET_TYPE_UNION :
4109 set_value = result_sync_status | set_value;
4111 case SET_TYPE_MINUS :
4112 set_value = result_sync_status & (~set_value);
4115 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4116 error = EMAIL_ERROR_NOT_SUPPORTED;
4119 EM_DEBUG_LOG("set_value [%d]", set_value);
4122 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4125 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4127 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4129 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4131 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4132 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4133 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4135 rc = sqlite3_changes(local_db_handle);
4138 EM_DEBUG_EXCEPTION("no matched account found...");
4139 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4143 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, 0, NULL, 0))
4144 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4149 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4153 if (err_code != NULL)
4156 EM_DEBUG_FUNC_END("ret [%d]", ret);
4160 INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
4162 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4165 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4166 if (err_code != NULL)
4167 *err_code = EMAIL_ERROR_INVALID_PARAM;
4171 int rc = -1, ret = false;
4172 int error = EMAIL_ERROR_NONE;
4173 int error_from_ssm = 0;
4174 DB_STMT hStmt = NULL;
4175 char sql_query_string[QUERY_SIZE] = {0, };
4176 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4177 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4179 sqlite3 *local_db_handle = emstorage_get_db_connection();
4181 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4183 char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
4184 char **result = NULL;
4186 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
4187 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4188 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4190 if (NULL==result[1]) rc = 1;
4191 else rc = atoi(result[1])+1;
4192 sqlite3_free_table(result);
4195 account_tbl->account_id = rc;
4197 if ((error = _get_password_file_name(account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4198 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4202 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4203 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4204 "INSERT INTO mail_account_tbl VALUES "
4206 " ? " /* account_id */
4207 " , ? " /* account_name */
4208 " , ? " /* logo_icon_path */
4209 " , ? " /* user_data */
4210 " , ? " /* user_data_length */
4211 " , ? " /* account_svc_id */
4212 " , ? " /* sync_status */
4213 " , ? " /* sync_disabled */
4214 " , ? " /* default_mail_slot_size */
4215 " , ? " /* user_display_name */
4216 " , ? " /* user_email_address */
4217 " , ? " /* reply_to_address */
4218 " , ? " /* return_address */
4219 " , ? " /* incoming_server_type */
4220 " , ? " /* incoming_server_address */
4221 " , ? " /* incoming_server_port_number */
4222 " , ? " /* incoming_server_user_name */
4223 " , ? " /* incoming_server_password */
4224 " , ? " /* incoming_server_secure_connection */
4225 " , ? " /* retrieval_mode */
4226 " , ? " /* keep_mails_on_pop_server_after_download */
4227 " , ? " /* check_interval */
4228 " , ? " /* auto_download_size */
4229 " , ? " /* outgoing_server_type */
4230 " , ? " /* outgoing_server_address */
4231 " , ? " /* outgoing_server_port_number */
4232 " , ? " /* outgoing_server_user_name */
4233 " , ? " /* outgoing_server_password */
4234 " , ? " /* outgoing_server_secure_connection */
4235 " , ? " /* outgoing_server_need_authentication */
4236 " , ? " /* outgoing_server_use_same_authenticator */
4237 " , ? " /* priority */
4238 " , ? " /* keep_local_copy */
4239 " , ? " /* req_delivery_receipt */
4240 " , ? " /* req_read_receipt */
4241 " , ? " /* download_limit */
4242 " , ? " /* block_address */
4243 " , ? " /* block_subject */
4244 " , ? " /* display_name_from */
4245 " , ? " /* reply_with_body */
4246 " , ? " /* forward_with_files */
4247 " , ? " /* add_myname_card */
4248 " , ? " /* add_signature */
4249 " , ? " /* signature */
4250 " , ? " /* add_my_address_to_bcc */
4251 " , ? " /* pop_before_smtp */
4252 " , ? " /* incoming_server_requires_apop */
4253 " , ? " /* smime_type */
4254 " , ? " /* certificate_path */
4255 " , ? " /* cipher_type */
4256 " , ? " /* digest_type */
4259 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4260 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4261 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4263 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
4266 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4267 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4268 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4269 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4270 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4271 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4272 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4273 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4274 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4275 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4276 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4277 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4278 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4279 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4280 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4281 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4282 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4283 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4284 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4285 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4286 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4287 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4288 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4289 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4290 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4291 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4292 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4293 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4294 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4295 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4296 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4297 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4298 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4299 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4300 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4301 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4302 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4303 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4304 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4305 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4306 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4307 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4308 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4309 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4310 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4311 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4312 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4313 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4314 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
4315 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4316 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4318 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4320 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4321 ("sqlite3_step fail:%d", rc));
4322 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4323 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
4326 /* save passwords to the secure storage */
4327 EM_DEBUG_LOG("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
4328 if ( (error_from_ssm = ssm_write_buffer(account_tbl->incoming_server_password, EM_SAFE_STRLEN(account_tbl->incoming_server_password), recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
4329 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - recv password : file[%s]", error_from_ssm, recv_password_file_name);
4330 error = EMAIL_ERROR_SYSTEM_FAILURE;
4333 if ( (error_from_ssm = ssm_write_buffer(account_tbl->outgoing_server_password, EM_SAFE_STRLEN(account_tbl->outgoing_server_password), send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
4334 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - send password : file[%s]", error_from_ssm, send_password_file_name);
4335 error = EMAIL_ERROR_SYSTEM_FAILURE;
4341 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4343 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
4344 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
4348 if (hStmt != NULL) {
4349 rc = sqlite3_finalize(hStmt);
4350 if (rc != SQLITE_OK) {
4351 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
4352 error = EMAIL_ERROR_DB_FAILURE;
4356 EM_DEBUG_LOG("hStmt is NULL!!!");
4360 if (err_code != NULL)
4363 EM_DEBUG_FUNC_END("ret [%d]", ret);
4368 INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int *err_code)
4370 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
4372 if (account_id < FIRST_ACCOUNT_ID) {
4373 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
4375 if (err_code != NULL)
4376 *err_code = EMAIL_ERROR_INVALID_PARAM;
4380 int rc = -1, ret = false;
4381 int error = EMAIL_ERROR_NONE;
4382 sqlite3 *local_db_handle = emstorage_get_db_connection();
4383 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4385 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
4386 DB_STMT hStmt = NULL;
4387 char sql_query_string[QUERY_SIZE] = {0, };
4388 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4389 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4391 /* get password file name */
4392 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4393 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4397 /* delete from db */
4398 memset(sql_query_string, 0x00, sizeof(sql_query_string));
4399 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
4401 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4402 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4403 ("sqlite3_exec fail:%d", rc));
4404 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4405 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4407 /* validate account existence */
4408 rc = sqlite3_changes(local_db_handle);
4410 EM_DEBUG_EXCEPTION(" no matched account found...");
4411 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4415 /* delete from secure storage */
4416 if (ssm_delete_file(recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4417 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -recv password : file[%s]", recv_password_file_name);
4418 error = EMAIL_ERROR_SYSTEM_FAILURE;
4421 if (ssm_delete_file(send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4422 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -send password : file[%s]", send_password_file_name);
4423 error = EMAIL_ERROR_SYSTEM_FAILURE;
4430 if (hStmt != NULL) {
4431 rc = sqlite3_finalize(hStmt);
4432 if (rc != SQLITE_OK) {
4433 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
4434 error = EMAIL_ERROR_DB_FAILURE;
4438 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4441 if (err_code != NULL)
4444 EM_DEBUG_FUNC_END("ret [%d]", ret);
4449 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
4451 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
4454 int error = EMAIL_ERROR_NONE;
4457 if (!account_list || !*account_list) {
4458 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
4459 error = EMAIL_ERROR_INVALID_PARAM;
4463 emstorage_account_tbl_t* p = *account_list;
4466 for (; i < count; i++) {
4467 EM_SAFE_FREE(p[i].account_name);
4468 EM_SAFE_FREE(p[i].incoming_server_address);
4469 EM_SAFE_FREE(p[i].user_email_address);
4470 EM_SAFE_FREE(p[i].user_data);
4471 EM_SAFE_FREE(p[i].incoming_server_user_name);
4472 EM_SAFE_FREE(p[i].incoming_server_password);
4473 EM_SAFE_FREE(p[i].outgoing_server_address);
4474 EM_SAFE_FREE(p[i].outgoing_server_user_name);
4475 EM_SAFE_FREE(p[i].outgoing_server_password);
4476 EM_SAFE_FREE(p[i].user_display_name);
4477 EM_SAFE_FREE(p[i].reply_to_address);
4478 EM_SAFE_FREE(p[i].return_address);
4479 EM_SAFE_FREE(p[i].logo_icon_path);
4480 EM_SAFE_FREE(p[i].options.display_name_from);
4481 EM_SAFE_FREE(p[i].options.signature);
4482 EM_SAFE_FREE(p[i].certificate_path);
4486 *account_list = NULL;
4492 if (err_code != NULL)
4495 EM_DEBUG_FUNC_END("ret [%d]", ret);
4499 INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int *count, int transaction, int *err_code)
4501 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], count[%p], transaction[%d], err_code[%p]", account_id, local_yn, count, transaction, err_code);
4503 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
4504 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
4506 if (err_code != NULL)
4507 *err_code = EMAIL_ERROR_INVALID_PARAM;
4511 int rc = -1, ret = false;
4512 int error = EMAIL_ERROR_NONE;
4513 char sql_query_string[QUERY_SIZE] = {0, };
4515 sqlite3 *local_db_handle = emstorage_get_db_connection();
4516 EMSTORAGE_START_READ_TRANSACTION(transaction);
4518 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_box_tbl WHERE account_id = %d AND local_yn = %d", account_id, local_yn);
4522 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
4523 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4524 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4526 *count = atoi(result[1]);
4527 sqlite3_free_table(result);
4534 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4537 if (err_code != NULL)
4540 EM_DEBUG_FUNC_END("ret [%d]", ret);
4544 INTERNAL_FUNC int emstorage_get_mailbox_list(int account_id, int local_yn, email_mailbox_sort_type_t sort_type, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4546 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
4548 if (!select_num || !mailbox_list) {
4549 EM_DEBUG_EXCEPTION("Invalid parameters");
4551 if (err_code != NULL)
4552 *err_code = EMAIL_ERROR_INVALID_PARAM;
4558 int error = EMAIL_ERROR_NONE;
4559 char conditional_clause_string[QUERY_SIZE] = {0, };
4560 char ordering_clause_string[QUERY_SIZE] = {0, };
4562 if (account_id == ALL_ACCOUNT) {
4563 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4564 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE local_yn = %d ", local_yn);
4567 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
4568 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4569 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " AND local_yn = %d ", local_yn);
4572 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4574 switch (sort_type) {
4575 case EMAIL_MAILBOX_SORT_BY_NAME_ASC :
4576 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
4579 case EMAIL_MAILBOX_SORT_BY_NAME_DSC :
4580 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
4583 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC :
4584 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
4587 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC :
4588 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
4592 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
4594 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4595 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4603 if (err_code != NULL)
4606 EM_DEBUG_FUNC_END("ret [%d]", ret);
4610 INTERNAL_FUNC int emstorage_get_mailbox_list_ex(int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4612 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
4615 int error = EMAIL_ERROR_NONE;
4616 int where_clause_count = 0;
4617 char conditional_clause_string[QUERY_SIZE] = {0, };
4618 char ordering_clause_string[QUERY_SIZE] = {0, };
4620 if (!select_num || !mailbox_list) {
4621 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4622 error = EMAIL_ERROR_INVALID_PARAM;
4626 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
4627 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), "WHERE local_yn = %d ", local_yn);
4628 where_clause_count++;
4631 if (account_id > 0) {
4632 if (where_clause_count == 0) {
4633 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
4634 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " account_id = %d ", account_id);
4637 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " AND account_id = %d ", account_id);
4640 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY MBT.mailbox_name ");
4641 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4642 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
4644 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4645 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4653 if (err_code != NULL)
4656 EM_DEBUG_FUNC_END("ret [%d]", ret);
4660 INTERNAL_FUNC int emstorage_get_child_mailbox_list(int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4662 EM_DEBUG_FUNC_BEGIN("account_id[%d], parent_mailbox_name[%p], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, parent_mailbox_name, select_num, mailbox_list, transaction, err_code);
4663 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
4664 EM_DEBUG_EXCEPTION("account_id[%d], parent_mailbox_name[%p], select_num[%p], mailbox_list[%p]", account_id, parent_mailbox_name, select_num, mailbox_list);
4666 if (err_code != NULL)
4667 *err_code = EMAIL_ERROR_INVALID_PARAM;
4672 int error = EMAIL_ERROR_NONE;
4673 char conditional_clause_string[QUERY_SIZE] = {0, };
4675 if (parent_mailbox_name)
4676 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND UPPER(mailbox_name) LIKE UPPER('%s/%%')", account_id, parent_mailbox_name);
4678 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND (mailbox_name NOT LIKE '%%/%%')", account_id);
4680 EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
4682 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4683 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4691 if (err_code != NULL)
4694 EM_DEBUG_FUNC_END("ret [%d]", ret);
4698 INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4700 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
4701 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
4702 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4704 if (err_code != NULL)
4705 *err_code = EMAIL_ERROR_INVALID_PARAM;
4711 int error = EMAIL_ERROR_NONE;
4712 char conditional_clause_string[QUERY_SIZE] = {0, };
4714 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
4715 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4717 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4718 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4726 if (err_code != NULL)
4729 EM_DEBUG_FUNC_END("ret [%d]", ret);
4733 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
4735 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
4737 int result_code = false;
4738 int error = EMAIL_ERROR_NONE;
4740 time_t current_time = 0;
4741 char sql_query_string[QUERY_SIZE] = {0, };
4742 sqlite3 *local_db_handle = NULL;
4744 if (!input_mailbox_id ) {
4745 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4746 return EMAIL_ERROR_INVALID_PARAM;
4749 time(¤t_time);
4751 local_db_handle = emstorage_get_db_connection();
4753 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
4755 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4756 "UPDATE mail_box_tbl SET"
4757 " last_sync_time = %d"
4758 " WHERE mailbox_id = %d"
4760 , input_mailbox_id);
4762 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4764 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4765 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4766 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4770 if(error == EMAIL_ERROR_NONE)
4773 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
4776 EM_DEBUG_FUNC_END("error [%d]", error);
4780 INTERNAL_FUNC int emstorage_get_mailbox_by_name(int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t** result_mailbox, int transaction, int *err_code)
4782 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, mailbox_name, result_mailbox, transaction, err_code);
4783 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
4785 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
4786 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
4788 if (err_code != NULL)
4789 *err_code = EMAIL_ERROR_INVALID_PARAM;
4794 int error = EMAIL_ERROR_NONE;
4795 int result_count = 0;
4796 char conditional_clause_string[QUERY_SIZE] = {0, };
4798 if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
4799 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
4800 EM_DEBUG_EXCEPTION("malloc failed...");
4801 error = EMAIL_ERROR_OUT_OF_MEMORY;
4805 (*result_mailbox)->mailbox_id = 0;
4806 (*result_mailbox)->account_id = account_id;
4807 (*result_mailbox)->local_yn = 1;
4808 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
4809 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
4810 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
4811 (*result_mailbox)->deleted_flag = 0;
4812 (*result_mailbox)->modifiable_yn = 1;
4813 (*result_mailbox)->total_mail_count_on_server = 1;
4814 (*result_mailbox)->has_archived_mails = 0;
4815 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
4816 (*result_mailbox)->no_select = 0;
4821 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox_name);
4823 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%s'", account_id, local_yn, mailbox_name);
4825 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4827 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4828 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4837 if (err_code != NULL)
4840 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
4841 EM_DEBUG_FUNC_END("ret [%d]", ret);
4845 INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t** mailbox_name, int transaction, int *err_code)
4847 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_name[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_name, transaction, err_code);
4849 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !mailbox_name) {
4851 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4853 if (err_code != NULL)
4854 *err_code = EMAIL_ERROR_INVALID_PARAM;
4858 int rc, ret = false;
4859 int error = EMAIL_ERROR_NONE;
4860 emstorage_mailbox_tbl_t *p_data_tbl = NULL;
4861 emstorage_account_tbl_t *account = NULL;
4862 DB_STMT hStmt = NULL;
4863 char sql_query_string[QUERY_SIZE] = {0,};
4864 char *fields = "mailbox_id, account_id, local_yn, mailbox_name, mailbox_type, alias, deleted_flag, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, no_select, last_sync_time ";
4866 sqlite3 *local_db_handle = emstorage_get_db_connection();
4867 EMSTORAGE_START_READ_TRANSACTION(transaction);
4869 /* validate account */
4870 /* Check whether the account exists. */
4871 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
4872 EM_DEBUG_EXCEPTION(" emstorage_get_account_by_id failed - %d", error);
4877 emstorage_free_account(&account, 1, NULL);
4879 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", fields, account_id, mailbox_type);
4881 EM_DEBUG_LOG("query = [%s]", sql_query_string);
4884 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4885 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4888 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4889 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
4891 if (rc == SQLITE_DONE) {
4892 EM_DEBUG_EXCEPTION(" no matched mailbox_name found...");
4893 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4898 if (!(p_data_tbl = (emstorage_mailbox_tbl_t*)malloc(sizeof(emstorage_mailbox_tbl_t)))) {
4899 EM_DEBUG_EXCEPTION(" malloc failed...");
4900 error = EMAIL_ERROR_OUT_OF_MEMORY;
4904 memset(p_data_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
4908 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
4909 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4910 _get_stmt_field_data_int(hStmt, &(p_data_tbl->local_yn), col_index++);
4911 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, col_index++);
4912 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->mailbox_type), col_index++);
4913 _get_stmt_field_data_string(hStmt, &(p_data_tbl->alias), 0, col_index++);
4914 _get_stmt_field_data_int(hStmt, &(p_data_tbl->deleted_flag), col_index++);
4915 _get_stmt_field_data_int(hStmt, &(p_data_tbl->modifiable_yn), col_index++);
4916 _get_stmt_field_data_int(hStmt, &(p_data_tbl->total_mail_count_on_server), col_index++);
4917 _get_stmt_field_data_int(hStmt, &(p_data_tbl->has_archived_mails), col_index++);
4918 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_slot_size), col_index++);
4919 _get_stmt_field_data_int(hStmt, &(p_data_tbl->no_select), col_index++);
4920 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->last_sync_time), col_index++);
4926 *mailbox_name = p_data_tbl;
4928 if (hStmt != NULL) {
4929 rc = sqlite3_finalize(hStmt);
4930 if (rc != SQLITE_OK) {
4931 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
4932 error = EMAIL_ERROR_DB_FAILURE;
4936 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4939 if (err_code != NULL)
4942 EM_DEBUG_FUNC_END("ret [%d]", ret);
4946 INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
4948 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
4950 if (input_mailbox_id <= 0 || !output_mailbox) {
4951 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4952 return EMAIL_ERROR_INVALID_PARAM;
4955 int ret = EMAIL_ERROR_NONE;
4956 int result_count = 0;
4957 char conditional_clause_string[QUERY_SIZE] = {0, };
4959 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE mailbox_id = %d", input_mailbox_id);
4961 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4963 if( (ret = emstorage_query_mailbox_tbl(conditional_clause_string, "", false, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4964 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
4970 EM_DEBUG_FUNC_END("ret [%d]", ret);
4974 INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code)
4976 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_id[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_id, transaction, err_code);
4977 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
4978 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
4979 if (err_code != NULL)
4980 *err_code = EMAIL_ERROR_INVALID_PARAM;
4984 int rc, ret = false;
4985 int error = EMAIL_ERROR_NONE;
4986 emstorage_account_tbl_t* account = NULL;
4987 DB_STMT hStmt = NULL;
4988 char sql_query_string[QUERY_SIZE] = {0, };
4990 sqlite3 *local_db_handle = emstorage_get_db_connection();
4991 EMSTORAGE_START_READ_TRANSACTION(transaction);
4993 /* Check whether the account exists. */
4994 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
4995 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5000 emstorage_free_account(&account, 1, NULL);
5002 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mailbox_id FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5004 EM_DEBUG_LOG("query = [%s]", sql_query_string);
5007 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5009 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5010 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5013 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5014 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5015 ("sqlite3_step fail:%d", rc));
5017 if (rc == SQLITE_DONE) {
5018 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5019 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5023 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5028 if (hStmt != NULL) {
5029 rc = sqlite3_finalize(hStmt);
5030 if (rc != SQLITE_OK) {
5031 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5032 error = EMAIL_ERROR_DB_FAILURE;
5036 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5039 if (err_code != NULL)
5042 EM_DEBUG_FUNC_END("ret [%d]", ret);
5046 INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
5048 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_name[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_name, transaction, err_code);
5049 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5050 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5051 if (err_code != NULL)
5052 *err_code = EMAIL_ERROR_INVALID_PARAM;
5056 int rc, ret = false;
5057 int error = EMAIL_ERROR_NONE;
5058 emstorage_account_tbl_t* account = NULL;
5059 DB_STMT hStmt = NULL;
5060 char sql_query_string[QUERY_SIZE] = {0, };
5062 sqlite3 *local_db_handle = emstorage_get_db_connection();
5063 EMSTORAGE_START_READ_TRANSACTION(transaction);
5065 /* Check whether the account exists. */
5066 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5067 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5072 emstorage_free_account(&account, 1, NULL);
5074 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mailbox_name FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5076 EM_DEBUG_LOG("query = [%s]", sql_query_string);
5079 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5081 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5082 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5085 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5086 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5087 ("sqlite3_step fail:%d", rc));
5089 if (rc == SQLITE_DONE) {
5090 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5091 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5095 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5100 if (hStmt != NULL) {
5101 rc = sqlite3_finalize(hStmt);
5102 if (rc != SQLITE_OK) {
5103 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5104 error = EMAIL_ERROR_DB_FAILURE;
5108 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5111 if (err_code != NULL)
5114 EM_DEBUG_FUNC_END("ret [%d]", ret);
5118 INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
5120 EM_DEBUG_FUNC_BEGIN("account_id [%d], local_yn [%d], mailbox_name [%p], modifiable_yn [%d], transaction [%d], err_code [%p]", account_id, local_yn, mailbox_name, modifiable_yn, transaction, err_code);
5121 int rc, ret = false;
5122 int error = EMAIL_ERROR_NONE;
5123 char sql_query_string[QUERY_SIZE] = {0, };
5125 sqlite3 *local_db_handle = emstorage_get_db_connection();
5127 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5129 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5130 "UPDATE mail_box_tbl SET"
5131 " modifiable_yn = %d"
5132 " WHERE account_id = %d"
5133 " AND local_yn = %d"
5134 " AND mailbox_name = '%s'"
5139 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5140 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5141 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5146 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5149 if (err_code != NULL)
5152 EM_DEBUG_FUNC_END("ret [%d]", ret);
5158 INTERNAL_FUNC int emstorage_update_mailbox_total_count(int account_id, int input_mailbox_id, int total_count_on_server, int transaction, int *err_code)
5160 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], transaction[%d], err_code[%p]", account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
5161 int rc, ret = false;
5162 int error = EMAIL_ERROR_NONE;
5163 char sql_query_string[QUERY_SIZE] = {0, };
5165 if (account_id <= 0 || input_mailbox_id <= 0) {
5166 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5168 if (err_code != NULL)
5169 *err_code = EMAIL_ERROR_INVALID_PARAM;
5172 sqlite3 *local_db_handle = emstorage_get_db_connection();
5174 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5176 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5177 "UPDATE mail_box_tbl SET"
5178 " total_mail_count_on_server = %d"
5179 " WHERE account_id = %d"
5180 " AND mailbox_id = %d"
5181 , total_count_on_server
5183 , input_mailbox_id);
5184 EM_DEBUG_LOG("query[%s]", sql_query_string);
5185 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5186 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5187 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5192 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5195 if (err_code != NULL)
5198 EM_DEBUG_FUNC_END("ret [%d]", ret);
5204 INTERNAL_FUNC int emstorage_update_mailbox(int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t* result_mailbox, int transaction, int *err_code)
5206 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], input_mailbox_id[%d], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, result_mailbox, transaction, err_code);
5208 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5209 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d], result_mailbox[%p]", account_id, local_yn, input_mailbox_id, result_mailbox);
5211 if (err_code != NULL)
5212 *err_code = EMAIL_ERROR_INVALID_PARAM;
5216 int rc, ret = false;
5217 int error = EMAIL_ERROR_NONE;
5218 char sql_query_string[QUERY_SIZE] = {0, };
5219 DB_STMT hStmt = NULL;
5222 sqlite3 *local_db_handle = emstorage_get_db_connection();
5223 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5225 if (local_yn != -1) {
5226 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5227 "UPDATE mail_box_tbl SET"
5229 ", mailbox_name = ?"
5230 ", mailbox_type = ?"
5232 ", deleted_flag = ?"
5233 ", modifiable_yn= ?"
5234 ", mail_slot_size= ?"
5235 ", total_mail_count_on_server = ?"
5236 " WHERE account_id = %d"
5237 " AND local_yn = %d"
5238 " AND mailbox_id = '%d'"
5241 , input_mailbox_id);
5244 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5245 "UPDATE mail_box_tbl SET"
5247 ", mailbox_name = ?"
5248 ", mailbox_type = ?"
5250 ", deleted_flag = ?"
5251 ", modifiable_yn= ?"
5252 ", mail_slot_size= ?"
5253 ", total_mail_count_on_server = ?"
5254 " WHERE account_id = %d"
5255 " AND mailbox_id = '%d'"
5257 , input_mailbox_id);
5262 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5263 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5264 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5266 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5267 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->mailbox_name ? result_mailbox->mailbox_name : "", 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
5268 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5269 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5270 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5271 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5272 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5273 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5276 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5277 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5278 ("sqlite3_step fail:%d", rc));
5279 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5280 ("sqlite3_step fail:%d", rc));
5285 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5288 if (hStmt != NULL) {
5289 rc = sqlite3_finalize(hStmt);
5290 if (rc != SQLITE_OK) {
5291 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5292 error = EMAIL_ERROR_DB_FAILURE;
5296 if (err_code != NULL)
5299 EM_DEBUG_FUNC_END("ret [%d]", ret);
5304 INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, char *mailbox_name, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
5306 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], mailbox_name[%s], new_mailbox_type[%d], transaction[%d], err_code[%p]", account_id, local_yn, mailbox_name, new_mailbox_type, transaction, err_code);
5308 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name) {
5309 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s]", account_id, local_yn, mailbox_name);
5311 if (err_code != NULL)
5312 *err_code = EMAIL_ERROR_INVALID_PARAM;
5316 int rc, ret = false;
5317 int error = EMAIL_ERROR_NONE;
5318 char sql_query_string[QUERY_SIZE] = {0, };
5320 sqlite3 *local_db_handle = emstorage_get_db_connection();
5321 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5323 EM_DEBUG_LOG("emstorage_update_mailbox_type");
5325 DB_STMT hStmt_box_tbl = NULL;
5326 DB_STMT hStmt_mail_tbl = NULL;
5329 /* Update mail_box_tbl */
5330 if (local_yn != -1) {
5331 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5332 "UPDATE mail_box_tbl SET"
5334 " WHERE account_id = %d"
5335 " AND local_yn = %d"
5336 " AND mailbox_name = '%s'"
5342 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5343 "UPDATE mail_box_tbl SET"
5345 " WHERE account_id = %d"
5346 " AND mailbox_name = '%s'"
5351 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5353 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
5355 if(SQLITE_OK != rc) {
5356 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5357 error = EMAIL_ERROR_DB_FAILURE;
5361 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
5364 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
5366 if(rc == SQLITE_FULL) {
5367 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5368 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5372 if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
5373 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5374 error = EMAIL_ERROR_DB_FAILURE;
5379 /* Update mail_tbl */
5381 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5382 "UPDATE mail_tbl SET"
5384 " WHERE account_id = %d"
5385 " AND mailbox_name = '%s'"
5389 EM_DEBUG_LOG("SQL[%s]", sql_query_string);
5391 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
5392 if(SQLITE_OK != rc) {
5393 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5394 error = EMAIL_ERROR_DB_FAILURE;
5398 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
5400 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
5401 if(rc == SQLITE_FULL) {
5402 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5403 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5407 if(rc != SQLITE_ROW && rc != SQLITE_DONE){
5408 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5409 error = EMAIL_ERROR_DB_FAILURE;
5416 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5419 if (hStmt_box_tbl != NULL) {
5420 rc = sqlite3_finalize(hStmt_box_tbl);
5421 if (rc != SQLITE_OK) {
5422 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5423 error = EMAIL_ERROR_DB_FAILURE;
5427 if (hStmt_mail_tbl != NULL) {
5428 rc = sqlite3_finalize(hStmt_mail_tbl);
5429 if (rc != SQLITE_OK) {
5430 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5431 error = EMAIL_ERROR_DB_FAILURE;
5435 if (err_code != NULL)
5438 EM_DEBUG_FUNC_END("ret [%d]", ret);
5442 INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox, int transaction)
5444 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", input_mailbox_id, input_is_local_mailbox, transaction);
5446 int rc, ret = false;
5447 int error = EMAIL_ERROR_NONE;
5448 char sql_query_string[QUERY_SIZE] = {0, };
5450 if (input_mailbox_id < 0) {
5451 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5452 return EMAIL_ERROR_INVALID_PARAM;
5455 sqlite3 *local_db_handle = emstorage_get_db_connection();
5457 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5459 EM_DEBUG_LOG("emstorage_update_mailbox_type");
5461 DB_STMT hStmt = NULL;
5464 /* Update mail_box_tbl */
5465 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5466 "UPDATE mail_box_tbl SET"
5468 " WHERE mailbox_id = %d"
5469 , input_mailbox_id);
5471 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5473 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5474 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5475 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5477 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
5479 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5480 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5481 ("sqlite3_step fail:%d", rc));
5482 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5483 ("sqlite3_step fail:%d", rc));
5485 if (hStmt != NULL) {
5486 rc = sqlite3_finalize(hStmt);
5487 if (rc != SQLITE_OK) {
5488 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5489 error = EMAIL_ERROR_DB_FAILURE;
5497 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5500 if (hStmt != NULL) {
5501 rc = sqlite3_finalize(hStmt);
5502 if (rc != SQLITE_OK) {
5503 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5504 error = EMAIL_ERROR_DB_FAILURE;
5508 EM_DEBUG_FUNC_END("error [%d]", error);
5512 INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction)
5514 EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_field_name[%p] input_value[%d] err_code[%p]", input_account_id, input_mailbox_id_array, input_mailbox_id_count, input_field_name, input_value, transaction);
5516 int err = EMAIL_ERROR_NONE;
5519 int cur_mailbox_id_string = 0;
5520 int mailbox_id_string_buffer_length = 0;
5521 char sql_query_string[QUERY_SIZE] = {0, };
5522 char *mailbox_id_string_buffer = NULL;
5523 char *parameter_string = NULL;
5524 sqlite3 *local_db_handle = NULL;
5526 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
5527 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5528 return EMAIL_ERROR_INVALID_PARAM;
5531 local_db_handle = emstorage_get_db_connection();
5533 /* Generating mail id list string */
5534 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
5536 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
5538 if(!mailbox_id_string_buffer) {
5539 EM_DEBUG_EXCEPTION("em_malloc failed");
5540 err = EMAIL_ERROR_OUT_OF_MEMORY;
5544 for(i = 0; i < input_mailbox_id_count; i++)
5545 cur_mailbox_id_string += SNPRINTF_OFFSET(mailbox_id_string_buffer, cur_mailbox_id_string, mailbox_id_string_buffer_length, "%d,", input_mailbox_id_array[i]);
5547 if(EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
5548 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
5550 /* Generating notification parameter string */
5551 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
5553 if(!parameter_string) {
5554 EM_DEBUG_EXCEPTION("em_malloc failed");
5555 err = EMAIL_ERROR_OUT_OF_MEMORY;
5559 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
5561 /* Write query string */
5562 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_box_tbl SET %s = %d WHERE mailbox_id in (%s) ", input_field_name, input_value, mailbox_id_string_buffer);
5564 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
5567 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
5568 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5569 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5570 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5571 if (sqlite3_changes(local_db_handle) == 0)
5572 EM_DEBUG_LOG("no mail matched...");
5577 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, err);
5580 if (result && parameter_string && !emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
5581 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", input_field_name, input_value);
5583 EM_SAFE_FREE(mailbox_id_string_buffer);
5584 EM_SAFE_FREE(parameter_string);
5586 EM_DEBUG_FUNC_END("err [%d]", err);
5590 INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
5592 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
5595 if (err_code != NULL)
5596 *err_code = EMAIL_ERROR_INVALID_PARAM;
5600 int rc, ret = false;
5601 int error = EMAIL_ERROR_NONE;
5602 DB_STMT hStmt = NULL;
5603 char sql_query_string[QUERY_SIZE] = {0,};
5604 char **result = NULL;
5605 time_t current_time;
5606 sqlite3 *local_db_handle = emstorage_get_db_connection();
5607 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5609 EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
5611 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5612 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5613 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5615 time(¤t_time);
5617 if (NULL == result[1])
5620 rc = atoi(result[1]) + 1;
5621 sqlite3_free_table(result);
5623 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
5625 mailbox_tbl->mailbox_id = rc;
5627 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5628 "INSERT INTO mail_box_tbl VALUES "
5629 "( ?" /* mailbox_id */
5630 ", ?" /* account_id */
5631 ", ?" /* local_yn */
5632 ", ?" /* mailbox_name */
5633 ", ?" /* mailbox_type */
5635 ", ?" /* deleted_flag */
5636 ", ?" /* modifiable_yn */
5637 ", ?" /* total_mail_count_on_server */
5638 ", ?" /* has_archived_mails */
5639 ", ?" /* mail_slot_size */
5640 ", ?" /* no_select */
5641 ", ? )");/* last_sync_time */
5644 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5645 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
5646 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5647 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5651 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
5652 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
5653 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
5654 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
5655 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
5656 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5657 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
5658 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
5659 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
5660 _bind_stmt_field_data_int(hStmt, col_index++, 0);
5661 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
5662 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
5663 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
5666 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5667 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5668 ("sqlite3_step fail:%dn", rc));
5673 if (hStmt != NULL) {
5674 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5676 rc = sqlite3_finalize(hStmt);
5677 if (rc != SQLITE_OK) {
5678 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
5679 error = EMAIL_ERROR_DB_FAILURE;
5683 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5686 if (error == EMAIL_ERROR_NONE && !emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id, mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
5687 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5689 if (err_code != NULL)
5692 EM_DEBUG_FUNC_END("ret [%d]", ret);
5696 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
5698 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
5700 if (account_id < FIRST_ACCOUNT_ID) {
5702 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
5704 if (err_code != NULL)
5705 *err_code = EMAIL_ERROR_INVALID_PARAM;
5708 int rc, ret = false;
5709 int error = EMAIL_ERROR_NONE;
5710 char sql_query_string[QUERY_SIZE] = {0,};
5712 sqlite3 *local_db_handle = emstorage_get_db_connection();
5713 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5716 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
5718 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5719 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5720 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5722 rc = sqlite3_changes(local_db_handle);
5724 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
5730 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5733 if (err_code != NULL)
5735 EM_DEBUG_FUNC_END("ret [%d]", ret);
5741 INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
5743 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, transaction, err_code);
5745 if (account_id < FIRST_ACCOUNT_ID) {
5746 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5748 if (err_code != NULL)
5749 *err_code = EMAIL_ERROR_INVALID_PARAM;
5753 int rc, ret = false;
5754 int error = EMAIL_ERROR_NONE;
5755 char sql_query_string[QUERY_SIZE] = {0, };
5757 sqlite3 *local_db_handle = emstorage_get_db_connection();
5758 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5761 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
5763 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d AND local_yn = %d ", account_id, local_yn);
5765 if (input_mailbox_id > 0) { /* 0 means all mailbox_name */
5766 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(1+ EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_id = %d", input_mailbox_id);
5769 EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
5770 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5771 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5772 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5774 rc = sqlite3_changes(local_db_handle);
5776 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
5777 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5784 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5787 if(error == EMAIL_ERROR_NONE) {
5788 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
5789 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5792 if (err_code != NULL)
5795 EM_DEBUG_FUNC_END("ret [%d]", ret);
5799 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
5801 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
5804 int error = EMAIL_ERROR_NONE;
5807 if (!mailbox_list || !*mailbox_list) {
5808 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
5810 error = EMAIL_ERROR_INVALID_PARAM;
5814 emstorage_mailbox_tbl_t* p = *mailbox_list;
5817 for (; i < count; i++) {
5818 EM_SAFE_FREE(p[i].mailbox_name);
5819 EM_SAFE_FREE(p[i].alias);
5822 EM_SAFE_FREE(p); *mailbox_list = NULL;
5828 if (err_code != NULL)
5831 EM_DEBUG_FUNC_END("ret [%d]", ret);
5835 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
5837 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], count[%p], transaction[%d], err_code[%p]", account_id, mailbox_name , count, transaction, err_code);
5839 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
5840 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
5842 if (err_code != NULL)
5843 *err_code = EMAIL_ERROR_INVALID_PARAM;
5847 int rc = -1, ret = false;
5848 int error = EMAIL_ERROR_NONE;
5849 char sql_query_string[QUERY_SIZE] = {0, };
5851 sqlite3 *local_db_handle = emstorage_get_db_connection();
5852 EMSTORAGE_START_READ_TRANSACTION(transaction);
5853 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s' ", account_id, mailbox_name);
5854 EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
5858 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5859 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5860 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5862 *count = atoi(result[1]);
5863 sqlite3_free_table(result);
5868 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5871 if (err_code != NULL)
5874 EM_DEBUG_FUNC_END("ret [%d]", ret);
5880 INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
5882 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], uid[%p], exist[%p], transaction[%d], err_code[%p]", account_id, mailbox_name , uid, exist, transaction, err_code);
5884 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
5885 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
5887 if (err_code != NULL)
5888 *err_code = EMAIL_ERROR_INVALID_PARAM;
5892 int rc = -1, ret = false;
5893 int error = EMAIL_ERROR_NONE;
5894 char sql_query_string[QUERY_SIZE] = {0, };
5895 sqlite3 *local_db_handle = emstorage_get_db_connection();
5896 EMSTORAGE_START_READ_TRANSACTION(transaction);
5899 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s' AND s_uid = '%s' ", account_id, mailbox_name, uid);
5902 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND s_uid = '%s' ", account_id, uid);
5907 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5908 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5909 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5911 *exist = atoi(result[1]);
5912 sqlite3_free_table(result);
5922 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5925 if (err_code != NULL)
5928 EM_DEBUG_FUNC_END("ret [%d]", ret);
5932 INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
5934 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
5936 if (!mail || mail_id <= 0) {
5937 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
5938 if (err_code != NULL)
5939 *err_code = EMAIL_ERROR_INVALID_PARAM;
5943 int rc, ret = false, temp_rule;
5944 int error = EMAIL_ERROR_NONE;
5945 DB_STMT hStmt = NULL;
5946 char sql_query_string[QUERY_SIZE] = {0, };
5948 sqlite3 *local_db_handle = emstorage_get_db_connection();
5949 EMSTORAGE_START_READ_TRANSACTION(transaction);
5951 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
5953 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5954 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5956 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5957 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5960 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5961 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5962 ("sqlite3_step fail:%d", rc));
5964 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
5965 if (*mail == NULL) {
5966 error = EMAIL_ERROR_OUT_OF_MEMORY;
5967 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
5971 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
5973 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5974 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5975 _get_stmt_field_data_string(hStmt, &((*mail)->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5976 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5977 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5978 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5979 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5980 _get_stmt_field_data_int(hStmt, &temp_rule, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5982 (*mail)->server_mail_status = 1;
5987 if (hStmt != NULL) {
5988 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5989 rc = sqlite3_finalize(hStmt);
5990 if (rc != SQLITE_OK) {
5991 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5992 error = EMAIL_ERROR_DB_FAILURE;
5996 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5999 if (err_code != NULL)
6002 EM_DEBUG_FUNC_END("ret [%d]", ret);
6006 INTERNAL_FUNC int emstorage_get_downloaded_list(int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t** read_mail_uid, int *count, int transaction, int *err_code)
6008 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6009 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, read_mail_uid, count, transaction, err_code);
6010 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6011 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6013 if (err_code != NULL)
6014 *err_code = EMAIL_ERROR_INVALID_PARAM;
6018 int rc, ret = false;
6019 int error = EMAIL_ERROR_NONE;
6021 DB_STMT hStmt = NULL;
6022 char sql_query_string[QUERY_SIZE] = {0, };
6024 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6027 sqlite3 *local_db_handle = emstorage_get_db_connection();
6028 EMSTORAGE_START_READ_TRANSACTION(transaction);
6031 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
6033 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6035 EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
6039 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6040 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6041 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6042 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6045 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6046 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6047 ("sqlite3_step fail:%d", rc));
6050 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6051 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6052 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6053 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6055 sqlite3_free_table(result);
6057 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6063 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6064 EM_DEBUG_EXCEPTION(" malloc failed...");
6065 error = EMAIL_ERROR_OUT_OF_MEMORY;
6069 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
6071 for (i = 0; i < *count; ++i) {
6072 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6073 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6074 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6075 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6076 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].s_uid), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6077 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].data1), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6078 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].data2), 0, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6079 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag), FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6081 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6082 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6083 ("sqlite3_step fail:%d", rc));
6090 *read_mail_uid = p_data_tbl;
6091 else if (p_data_tbl)
6092 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6094 if (hStmt != NULL) {
6095 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6096 rc = sqlite3_finalize(hStmt);
6097 if (rc != SQLITE_OK) {
6098 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6100 error = EMAIL_ERROR_DB_FAILURE;
6104 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6107 if (err_code != NULL)
6110 EM_PROFILE_END(emStorageGetDownloadList);
6111 EM_DEBUG_FUNC_END("ret [%d]", ret);
6115 INTERNAL_FUNC int emstorage_get_downloaded_mail_size(int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
6117 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p], locacal_uid[%d], mailbox_name[%p], uid[%p], mail_size[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, mail_size, transaction, err_code);
6119 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6120 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%p], locacal_uid[%d], mailbox_name[%p], uid[%p], mail_size[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, mail_size);
6122 if (err_code != NULL)
6123 *err_code = EMAIL_ERROR_INVALID_PARAM;
6127 int rc, ret = false;
6128 int error = EMAIL_ERROR_NONE;
6129 DB_STMT hStmt = NULL;
6130 char sql_query_string[QUERY_SIZE] = {0, };
6133 sqlite3 *local_db_handle = emstorage_get_db_connection();
6134 EMSTORAGE_START_READ_TRANSACTION(transaction);
6137 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6138 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6139 "WHERE account_id = %d "
6140 "AND mailbox_id = '%s' "
6141 "AND local_uid = %d "
6142 "AND mailbox_name = '%s' "
6144 account_id, mailbox_id, local_uid, mailbox_name, uid);
6147 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6148 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6149 "WHERE account_id = %d "
6150 "AND mailbox_id = '%s' "
6151 "AND local_uid = %d "
6153 account_id, mailbox_id, local_uid, uid);
6157 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6158 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6159 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6162 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6163 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6164 ("sqlite3_step fail:%d", rc));
6166 if (rc == SQLITE_DONE) {
6167 EM_DEBUG_EXCEPTION(" no matched mail found....");
6168 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6172 _get_stmt_field_data_int(hStmt, mail_size, 0);
6177 if (hStmt != NULL) {
6178 rc = sqlite3_finalize(hStmt);
6179 if (rc != SQLITE_OK) {
6180 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6182 error = EMAIL_ERROR_DB_FAILURE;
6186 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6189 if (err_code != NULL)
6192 EM_DEBUG_FUNC_END("ret [%d]", ret);
6196 INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6198 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6200 if (!read_mail_uid) {
6201 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6202 if (err_code != NULL)
6203 *err_code = EMAIL_ERROR_INVALID_PARAM;
6207 int rc, rc2, ret = false;
6208 int error = EMAIL_ERROR_NONE;
6209 DB_STMT hStmt = NULL;
6210 char sql_query_string[QUERY_SIZE] = {0, };
6212 sqlite3 *local_db_handle = emstorage_get_db_connection();
6213 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6215 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6216 char **result = NULL;
6219 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6220 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6221 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6223 if (NULL==result[1]) rc = 1;
6224 else rc = atoi(result[1])+1;
6225 sqlite3_free_table(result);
6227 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6228 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6240 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6241 if (rc2 != SQLITE_OK) {
6242 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6243 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6245 error = EMAIL_ERROR_DB_FAILURE;
6249 EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
6250 EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
6251 EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
6252 EM_DEBUG_LOG("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
6253 EM_DEBUG_LOG("s_uid VALUE [%s] ", read_mail_uid->s_uid);
6254 EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->data1);
6255 EM_DEBUG_LOG("data2 VALUE [%s] ", read_mail_uid->data2);
6256 EM_DEBUG_LOG("flag VALUE [%d] ", read_mail_uid->flag);
6257 EM_DEBUG_LOG("rc VALUE [%d] ", rc);
6259 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6260 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6261 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6262 _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6263 _bind_stmt_field_data_string(hStmt, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6264 _bind_stmt_field_data_int(hStmt, DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->data1);
6265 _bind_stmt_field_data_string(hStmt, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6266 _bind_stmt_field_data_int(hStmt, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flag);
6267 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6270 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6271 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6272 ("sqlite3_step fail:%d", rc));
6273 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6274 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6280 if (hStmt != NULL) {
6281 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6283 rc = sqlite3_finalize(hStmt);
6284 if (rc != SQLITE_OK) {
6285 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6286 error = EMAIL_ERROR_DB_FAILURE;
6290 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6293 if (err_code != NULL)
6296 EM_DEBUG_FUNC_END("ret [%d]", ret);
6300 INTERNAL_FUNC int emstorage_change_read_mail_uid(int account_id, int mailbox_id, int local_uid, char *mailbox_name, char *uid, emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6302 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], local_uid[%d], mailbox_name[%p], uid[%p], read_mail_uid[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, read_mail_uid, transaction, err_code);
6304 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
6305 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%d], local_uid[%d], mailbox_name[%p], uid[%p], read_mail_uid[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, read_mail_uid);
6307 if (err_code != NULL)
6308 *err_code = EMAIL_ERROR_INVALID_PARAM;
6312 int rc, ret = false;
6313 int error = EMAIL_ERROR_NONE;
6314 DB_STMT hStmt = NULL;
6315 char sql_query_string[QUERY_SIZE] = {0, };
6317 sqlite3 *local_db_handle = emstorage_get_db_connection();
6319 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6321 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6322 "UPDATE mail_read_mail_uid_tbl SET"
6326 ", mailbox_name = ?"
6331 " WHERE account_id = ?"
6332 " AND mailbox_id = ?"
6333 " AND local_uid = ?"
6334 " AND mailbox_name= ?"
6338 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6339 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6340 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6341 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6346 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
6347 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
6348 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
6349 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6350 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6351 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->data1);
6352 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6353 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flag);
6354 _bind_stmt_field_data_int(hStmt, i++, account_id);
6355 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
6356 _bind_stmt_field_data_int(hStmt, i++, local_uid);
6357 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6358 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6361 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6362 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6363 ("sqlite3_step fail:%d", rc));
6368 if (hStmt != NULL) {
6369 EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
6371 rc = sqlite3_finalize(hStmt);
6372 if (rc != SQLITE_OK) {
6373 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6375 error = EMAIL_ERROR_DB_FAILURE;
6379 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6382 if (err_code != NULL)
6385 EM_DEBUG_FUNC_END("ret [%d]", ret);
6389 INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
6391 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%s], uid[%s], transaction[%d], err_code[%p]", account_id, mailbox_name, uid, transaction, err_code);
6393 if (account_id < FIRST_ACCOUNT_ID) {
6394 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
6396 if (err_code != NULL)
6397 *err_code = EMAIL_ERROR_INVALID_PARAM;
6401 int rc, ret = false;
6402 int error = EMAIL_ERROR_NONE;
6403 char sql_query_string[QUERY_SIZE] = {0, };
6405 sqlite3 *local_db_handle = emstorage_get_db_connection();
6407 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6409 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
6411 if (mailbox_name) { /* NULL means all mailbox_name */
6412 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_name = '%s' ", mailbox_name);
6415 if (uid) { /* NULL means all mail */
6416 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND s_uid='%s' ", uid);
6419 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6420 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6421 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6428 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6431 if (err_code != NULL)
6434 EM_DEBUG_FUNC_END("ret [%d]", ret);
6438 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
6440 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
6443 int error = EMAIL_ERROR_NONE;
6446 if (!read_mail_uid || !*read_mail_uid) {
6447 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
6449 error = EMAIL_ERROR_INVALID_PARAM;
6453 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
6456 for (i = 0; i < count; i++) {
6457 EM_SAFE_FREE(p[i].mailbox_name);
6458 EM_SAFE_FREE(p[i].s_uid);
6459 EM_SAFE_FREE(p[i].data2);
6462 EM_SAFE_FREE(p); *read_mail_uid = NULL;
6468 if (err_code != NULL)
6471 EM_DEBUG_FUNC_END("ret [%d]", ret);
6475 INTERNAL_FUNC int emstorage_get_rule_count(int account_id, int *count, int transaction, int *err_code)
6477 EM_DEBUG_FUNC_BEGIN("account_id[%d], count[%p], transaction[%d], err_code[%p]", account_id, count, transaction, err_code);
6479 if (account_id != ALL_ACCOUNT || !count) { /* only global rule supported. */
6480 EM_DEBUG_EXCEPTION(" account_id[%d], count[%p]", account_id, count);
6482 if (err_code != NULL)
6483 *err_code = EMAIL_ERROR_INVALID_PARAM;
6487 int rc = -1, ret = false;
6488 int error = EMAIL_ERROR_NONE;
6489 char sql_query_string[QUERY_SIZE] = {0, };
6491 sqlite3 *local_db_handle = emstorage_get_db_connection();
6492 EMSTORAGE_START_READ_TRANSACTION(transaction);
6494 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl WHERE account_id = %d", account_id);
6498 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6499 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6500 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6502 *count = atoi(result[1]);
6503 sqlite3_free_table(result);
6508 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6511 if (err_code != NULL)
6514 EM_DEBUG_FUNC_END("ret [%d]", ret);
6518 INTERNAL_FUNC int emstorage_get_rule(int account_id, int type, int start_idx, int *select_num, int *is_completed, emstorage_rule_tbl_t** rule_list, int transaction, int *err_code)
6520 EM_DEBUG_FUNC_BEGIN("account_id[%d], type[%d], start_idx[%d], select_num[%p], is_completed[%p], rule_list[%p], transaction[%d], err_code[%p]", account_id, type, start_idx, select_num, is_completed, rule_list, transaction, err_code);
6522 if (account_id != ALL_ACCOUNT || !select_num || !is_completed || !rule_list) { /* only global rule supported. */
6523 EM_DEBUG_EXCEPTION(" account_id[%d], type[%d], start_idx[%d], select_num[%p], is_completed[%p], rule_list[%p]", account_id, type, start_idx, select_num, is_completed, rule_list);
6525 if (err_code != NULL)
6526 *err_code = EMAIL_ERROR_INVALID_PARAM;
6531 int error = EMAIL_ERROR_NONE;
6533 emstorage_rule_tbl_t* p_data_tbl = NULL;
6534 int i = 0, count = 0;
6535 DB_STMT hStmt = NULL;
6536 char sql_query_string[QUERY_SIZE] = {0, };
6540 sqlite3 *local_db_handle = emstorage_get_db_connection();
6541 EMSTORAGE_START_READ_TRANSACTION(transaction);
6544 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
6546 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
6549 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6550 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6551 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6552 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6555 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6556 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6557 ("sqlite3_step fail:%d", rc));
6560 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
6561 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
6562 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6563 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6565 sqlite3_free_table(result);
6568 EM_DEBUG_LOG(" no matched rule found...");
6574 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
6575 EM_DEBUG_EXCEPTION(" malloc failed...");
6577 error = EMAIL_ERROR_OUT_OF_MEMORY;
6581 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
6583 for (i = 0; i < count; i++) {
6584 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6585 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6586 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
6587 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6588 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6589 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6590 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6591 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6593 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6594 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6595 ("sqlite3_step fail:%d", rc));
6602 EM_DEBUG_LOG("[%d] rules found.", count);
6605 *rule_list = p_data_tbl;
6606 *select_num = count;
6608 else if (p_data_tbl != NULL)
6609 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
6611 if (hStmt != NULL) {
6612 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6614 rc = sqlite3_finalize(hStmt);
6615 if (rc != SQLITE_OK) {
6616 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6617 error = EMAIL_ERROR_DB_FAILURE;
6622 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6625 if (err_code != NULL)
6628 EM_DEBUG_FUNC_END("ret [%d]", ret);
6632 INTERNAL_FUNC int emstorage_get_rule_by_id(int account_id, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
6634 EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], rule[%p], transaction[%d], err_code[%p]", account_id, rule_id, rule, transaction, err_code);
6636 if (account_id != ALL_ACCOUNT || !rule) {
6637 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d], rule[%p]", account_id, rule_id, rule);
6639 if (err_code != NULL)
6640 *err_code = EMAIL_ERROR_INVALID_PARAM;
6644 emstorage_rule_tbl_t* p_data_tbl = NULL;
6645 int rc, ret = false;
6646 int error = EMAIL_ERROR_NONE;
6648 DB_STMT hStmt = NULL;
6649 char sql_query_string[QUERY_SIZE] = {0, };
6650 sqlite3 *local_db_handle = emstorage_get_db_connection();
6651 EMSTORAGE_START_READ_TRANSACTION(transaction);
6653 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND rule_id = %d", account_id, rule_id);
6656 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6657 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6658 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6661 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6662 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6663 ("sqlite3_step fail:%d", rc));
6665 if (rc == SQLITE_DONE) {
6666 EM_DEBUG_EXCEPTION(" no matched rule found...");
6667 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6671 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
6672 EM_DEBUG_EXCEPTION(" malloc failed...");
6673 error = EMAIL_ERROR_OUT_OF_MEMORY;
6677 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
6678 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6679 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6680 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
6681 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6682 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6683 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6684 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6685 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6693 if (hStmt != NULL) {
6694 EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
6696 rc = sqlite3_finalize(hStmt);
6697 if (rc != SQLITE_OK) {
6698 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6700 error = EMAIL_ERROR_DB_FAILURE;
6706 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6709 if (err_code != NULL)
6712 EM_DEBUG_FUNC_END("ret [%d]", ret);
6716 INTERNAL_FUNC int emstorage_change_rule(int account_id, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
6718 EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", account_id, rule_id, new_rule, transaction, err_code);
6720 if (account_id != ALL_ACCOUNT || !new_rule) { /* only global rule supported. */
6721 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d], new_rule[%p]", account_id, rule_id, new_rule);
6723 if (err_code != NULL)
6724 *err_code = EMAIL_ERROR_INVALID_PARAM;
6728 int rc, ret = false;
6729 int error = EMAIL_ERROR_NONE;
6731 DB_STMT hStmt = NULL;
6732 char sql_query_string[QUERY_SIZE] = {0, };
6733 sqlite3 *local_db_handle = emstorage_get_db_connection();
6734 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6736 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6737 "UPDATE mail_rule_tbl SET"
6741 ", target_mailbox_id = ?"
6744 " WHERE account_id = ?"
6745 " AND rule_id = ?");
6748 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6749 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
6750 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6751 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6755 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
6756 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6757 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
6758 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
6759 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
6760 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
6761 _bind_stmt_field_data_int(hStmt, i++, account_id);
6762 _bind_stmt_field_data_int(hStmt, i++, rule_id);
6765 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6766 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6767 ("sqlite3_step fail:%d", rc));
6772 if (hStmt != NULL) {
6773 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6775 rc = sqlite3_finalize(hStmt);
6776 if (rc != SQLITE_OK) {
6777 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6779 error = EMAIL_ERROR_DB_FAILURE;
6783 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6786 if (err_code != NULL)
6789 EM_DEBUG_FUNC_END("ret [%d]", ret);
6793 INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6795 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6797 if (!rule || rule->account_id != ALL_ACCOUNT) { /* only global rule supported. */
6799 EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6801 if (err_code != NULL)
6802 *err_code = EMAIL_ERROR_INVALID_PARAM;
6806 DB_STMT hStmt = NULL;
6807 char sql_query_string[QUERY_SIZE] = {0, };
6809 int error = EMAIL_ERROR_NONE;
6810 int rc, ret = false;
6812 sqlite3 *local_db_handle = emstorage_get_db_connection();
6813 EMSTORAGE_START_READ_TRANSACTION(transaction);
6815 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6816 "SELECT rule_id FROM mail_rule_tbl WHERE type = %d AND UPPER(value) = UPPER('%s')",
6817 rule->type, rule->value);
6820 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6821 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6822 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6825 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6826 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6827 ("sqlite3_step fail:%d", rc));
6829 if (rc == SQLITE_DONE) {
6830 EM_DEBUG_EXCEPTION(" no matched rule found...");
6831 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6839 if (hStmt != NULL) {
6840 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
6842 rc = sqlite3_finalize(hStmt);
6843 if (rc != SQLITE_OK) {
6844 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
6845 error = EMAIL_ERROR_DB_FAILURE;
6850 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6856 EM_DEBUG_FUNC_END("ret [%d]", ret);
6860 INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6862 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6864 if (!rule || rule->account_id != ALL_ACCOUNT) { /* only global rule supported. */
6866 EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6868 if (err_code != NULL)
6869 *err_code = EMAIL_ERROR_INVALID_PARAM;
6873 int rc, rc_2, ret = false;
6874 int error = EMAIL_ERROR_NONE;
6875 DB_STMT hStmt = NULL;
6876 char sql_query_string[QUERY_SIZE] = {0, };
6879 sqlite3 *local_db_handle = emstorage_get_db_connection();
6881 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6885 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
6887 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6888 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6889 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6891 if (NULL==result[1])
6894 rc = atoi(result[1])+1;
6895 sqlite3_free_table(result);
6897 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6898 "INSERT INTO mail_rule_tbl VALUES "
6899 "( ?" /* account id */
6903 ", ?" /* action_type */
6904 ", ?" /* target_mailbox_id */
6906 ", ?)"); /* flag2 */
6908 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
6909 if (rc_2 != SQLITE_OK) {
6910 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
6911 error = EMAIL_ERROR_DB_FAILURE;
6915 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
6916 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rc);
6917 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
6918 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6919 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
6920 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
6921 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
6922 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
6924 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6925 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6926 ("sqlite3_step fail:%d", rc));
6931 if (hStmt != NULL) {
6932 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6934 rc = sqlite3_finalize(hStmt);
6935 if (rc != SQLITE_OK) {
6936 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6938 error = EMAIL_ERROR_DB_FAILURE;
6942 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6945 if (err_code != NULL)
6948 EM_DEBUG_FUNC_END("ret [%d]", ret);
6952 INTERNAL_FUNC int emstorage_delete_rule(int account_id, int rule_id, int transaction, int *err_code)
6954 EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], transaction[%d], err_code[%p]", account_id, rule_id, transaction, err_code);
6956 if (account_id != ALL_ACCOUNT) { /* only global rule supported. */
6957 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d]", account_id, rule_id);
6959 if (err_code != NULL)
6960 *err_code = EMAIL_ERROR_INVALID_PARAM;
6964 int rc, ret = false;
6965 int error = EMAIL_ERROR_NONE;
6966 char sql_query_string[QUERY_SIZE] = {0, };
6968 sqlite3 *local_db_handle = emstorage_get_db_connection();
6969 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6971 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE account_id = %d AND rule_id = %d", account_id, rule_id);
6972 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6973 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6974 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6975 rc = sqlite3_changes(local_db_handle);
6977 EM_DEBUG_EXCEPTION(" no matched rule found...");
6979 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6986 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6989 if (err_code != NULL)
6992 EM_DEBUG_FUNC_END("ret [%d]", ret);
6996 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
6998 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7001 int error = EMAIL_ERROR_NONE;
7004 if (!rule_list || !*rule_list) {
7005 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7007 error = EMAIL_ERROR_INVALID_PARAM;
7011 emstorage_rule_tbl_t* p = *rule_list;
7014 for (; i < count; i++) {
7015 EM_SAFE_FREE(p[i].value);
7018 EM_SAFE_FREE(p); *rule_list = NULL;
7022 if (err_code != NULL)
7025 EM_DEBUG_FUNC_END("ret [%d]", ret);
7029 INTERNAL_FUNC int emstorage_get_mail_count(int account_id, const char *mailbox_name, int *total, int *unseen, int transaction, int *err_code)
7031 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%s], total[%p], unseen[%p], transaction[%d], err_code[%p]", account_id, mailbox_name, total, unseen, transaction, err_code);
7033 if (!total && !unseen) {
7034 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_name[%s], total[%p], unseen[%p]", account_id, mailbox_name, total, unseen);
7035 if (err_code != NULL)
7036 *err_code = EMAIL_ERROR_INVALID_PARAM;
7040 int rc = -1, ret = false;
7041 int error = EMAIL_ERROR_NONE;
7042 DB_STMT hStmt = NULL;
7043 char sql_query_string[QUERY_SIZE] = {0, };
7047 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7048 sqlite3 *local_db_handle = emstorage_get_db_connection();
7049 EMSTORAGE_START_READ_TRANSACTION(transaction);
7052 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7054 if (account_id != ALL_ACCOUNT) {
7055 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
7057 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND UPPER(mailbox_name) = UPPER('%s')", mailbox_name);
7059 else if (mailbox_name)
7060 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE UPPER(mailbox_name) = UPPER('%s')", mailbox_name);
7062 #ifdef USE_GET_RECORD_COUNT_API
7065 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7066 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
7067 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7069 *total = atoi(result[1]);
7070 sqlite3_free_table(result);
7073 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7074 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7075 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7077 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7078 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7079 ("sqlite3_step fail:%d", rc));
7080 _get_stmt_field_data_int(hStmt, total, 0);
7081 #endif /* USE_GET_RECORD_COUNT_API */
7085 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7087 if (account_id != ALL_ACCOUNT)
7088 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7091 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_name = '%s'", mailbox_name);
7093 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7096 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7097 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7098 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7100 *unseen = atoi(result[1]);
7101 sqlite3_free_table(result);
7109 #ifndef USE_PREPARED_QUERY_
7110 if (hStmt != NULL) {
7111 rc = sqlite3_finalize(hStmt);
7112 if (rc != SQLITE_OK) {
7113 EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
7114 error = EMAIL_ERROR_DB_FAILURE;
7119 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7122 if (err_code != NULL)
7125 EM_DEBUG_FUNC_END("ret [%d]", ret);
7129 INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7131 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7133 if (mail_id <= 0 || !mail) {
7134 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7135 if (err_code != NULL)
7136 *err_code = EMAIL_ERROR_INVALID_PARAM;
7140 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7142 if (p_data_tbl == NULL) {
7143 EM_DEBUG_EXCEPTION("malloc failed...");
7144 if (err_code != NULL)
7145 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7149 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7150 DB_STMT hStmt = NULL;
7151 char sql_query_string[QUERY_SIZE] = {0, };
7153 int rc, ret = false;
7154 int error = EMAIL_ERROR_NONE;
7156 sqlite3 *local_db_handle = emstorage_get_db_connection();
7157 EMSTORAGE_START_READ_TRANSACTION(transaction);
7160 case RETRIEVE_SUMMARY:
7161 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7162 "SELECT account_id, mail_id, mailbox_id, mailbox_name, server_mail_status, server_mailbox_name, server_mail_id, file_path_plain, file_path_html, flags_seen_field, save_status, lock_status, thread_id, thread_item_count FROM mail_tbl WHERE mail_id = %d", mail_id);
7165 case RETRIEVE_FIELDS_FOR_DELETE:
7166 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7167 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7170 case RETRIEVE_ACCOUNT:
7171 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7172 "SELECT account_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7176 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7177 "SELECT account_id, mailbox_name, flags_seen_field, thread_id, mailbox_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7181 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7182 error = EMAIL_ERROR_INVALID_PARAM;
7186 EM_DEBUG_LOG("Query [%s]", sql_query_string);
7188 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7189 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7190 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7193 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7194 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7195 ("sqlite3_step fail:%d", rc));
7197 if (rc == SQLITE_DONE) {
7198 EM_DEBUG_EXCEPTION("no matched mail found...");
7199 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7203 case RETRIEVE_SUMMARY:
7204 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7205 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), 1);
7206 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), 2);
7207 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, 3);
7208 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), 4);
7209 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, 5);
7210 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, 6);
7211 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, 7);
7212 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, 8);
7213 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), 9);
7214 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), 10);
7215 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), 11);
7216 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), 12);
7217 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), 13);
7220 case RETRIEVE_FIELDS_FOR_DELETE:
7221 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7222 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), 1);
7223 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), 2);
7224 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, 3);
7225 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, 4);
7228 case RETRIEVE_ACCOUNT:
7229 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7233 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7234 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, 1);
7235 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), 2);
7236 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), 3);
7237 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), 4);
7246 else if (p_data_tbl != NULL)
7247 emstorage_free_mail(&p_data_tbl, 1, NULL);
7249 if (hStmt != NULL) {
7250 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7252 rc = sqlite3_finalize(hStmt);
7253 if (rc != SQLITE_OK) {
7254 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7255 error = EMAIL_ERROR_DB_FAILURE;
7260 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7263 if (err_code != NULL)
7266 EM_DEBUG_FUNC_END("ret [%d]", ret);
7270 INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7272 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_ids, number_of_mails, type, mail, transaction, err_code);
7275 int error = EMAIL_ERROR_NONE;
7276 int query_string_length = 0;
7277 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
7278 char **result = NULL;
7279 char *sql_query_string = NULL;
7280 emstorage_mail_tbl_t* p_data_tbl = NULL;
7281 sqlite3 *local_db_handle = NULL;
7283 if (number_of_mails <= 0 || !mail_ids) {
7284 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7285 if (err_code != NULL)
7286 *err_code = EMAIL_ERROR_INVALID_PARAM;
7290 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
7292 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
7293 sql_query_string = (char*)em_malloc(query_string_length);
7295 if (p_data_tbl == NULL || sql_query_string == NULL) {
7296 EM_DEBUG_EXCEPTION("malloc failed...");
7298 EM_SAFE_FREE(p_data_tbl);
7299 EM_SAFE_FREE(sql_query_string);
7301 if (err_code != NULL)
7302 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7306 local_db_handle = emstorage_get_db_connection();
7308 EMSTORAGE_START_READ_TRANSACTION(transaction);
7311 case RETRIEVE_SUMMARY:
7312 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7313 "SELECT account_id, mail_id, mailbox_name, server_mail_status, server_mailbox_name, server_mail_id, file_path_plain, file_path_html, flags_seen_field, save_status, lock_status, thread_id, thread_item_count FROM mail_tbl WHERE mail_id in (");
7317 case RETRIEVE_FIELDS_FOR_DELETE:
7318 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7319 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id in (");
7323 case RETRIEVE_ACCOUNT:
7324 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7325 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
7330 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7331 "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
7336 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7337 error = EMAIL_ERROR_INVALID_PARAM;
7341 for(i = 0; i < number_of_mails; i++)
7342 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
7343 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
7345 EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
7347 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
7348 if (SQLITE_OK != rc && -1 != rc) {
7349 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7350 error = EMAIL_ERROR_DB_FAILURE;
7354 if (rc == SQLITE_DONE) {
7355 EM_DEBUG_EXCEPTION("no matched mail found...");
7356 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7360 EM_DEBUG_LOG("item_count [%d]", item_count);
7362 if(number_of_mails != item_count) {
7363 EM_DEBUG_EXCEPTION("Can't find all emails");
7364 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7368 col_index = field_count;
7370 for(i = 0; i < item_count; i++) {
7372 case RETRIEVE_SUMMARY:
7373 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7374 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7375 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
7376 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7377 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7378 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7379 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
7380 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
7381 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7382 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
7383 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
7384 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7385 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
7388 case RETRIEVE_FIELDS_FOR_DELETE:
7389 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7390 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7391 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7392 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7393 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7396 case RETRIEVE_ACCOUNT:
7397 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7401 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7402 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
7403 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7404 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7415 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
7418 sqlite3_free_table(result);
7420 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7423 EM_SAFE_FREE(sql_query_string);
7425 if (err_code != NULL)
7428 EM_DEBUG_FUNC_END("ret [%d]", ret);
7432 INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7434 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7436 if (mail_id <= 0 || !mail) {
7437 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7438 if (err_code != NULL)
7439 *err_code = EMAIL_ERROR_INVALID_PARAM;
7443 int ret = false, error = EMAIL_ERROR_NONE, count;
7444 char conditional_clause[QUERY_SIZE] = {0, };
7445 emstorage_mail_tbl_t* p_data_tbl = NULL;
7447 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7448 EM_DEBUG_LOG("query = [%s]", conditional_clause);
7450 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7451 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7460 else if (p_data_tbl != NULL)
7461 emstorage_free_mail(&p_data_tbl, 1, &error);
7463 if (err_code != NULL)
7466 EM_DEBUG_FUNC_END("ret [%d]", ret);
7470 INTERNAL_FUNC int emstorage_mail_search_start(emstorage_search_filter_t* search, int account_id, char *mailbox_name, int sorting, int *search_handle, int *searched, int transaction, int *err_code)
7472 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_name[%p], sorting[%d], search_handle[%p], searched[%p], transaction[%d], err_code[%p]", search, account_id, mailbox_name, sorting, search_handle, searched, transaction, err_code);
7474 if (!search_handle || !searched) {
7475 if (err_code != NULL)
7476 *err_code = EMAIL_ERROR_INVALID_PARAM;
7477 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7478 EM_DEBUG_FUNC_END("false");
7482 emstorage_search_filter_t* p = search;
7483 int error = EMAIL_ERROR_NONE;
7484 DB_STMT hStmt = NULL;
7485 char sql_query_string[QUERY_SIZE] = {0, };
7486 int rc, ret = false;
7487 int and = false, mail_count = 0;
7489 sqlite3 *local_db_handle = emstorage_get_db_connection();
7490 EMSTORAGE_START_READ_TRANSACTION(transaction);
7492 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
7494 if (account_id != ALL_ACCOUNT) {
7495 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
7500 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_name = '%s'", and ? "AND" : "WHERE", mailbox_name);
7507 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
7508 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s subject LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7511 else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
7512 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s full_address_from LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7515 else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
7516 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s full_address_to LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7519 else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
7520 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s email_address_sender = '%s' OR email_address_recipient = '%s'", and ? "AND" : "WHERE", p->key_value, p->key_value);
7528 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " ORDER BY date_time");
7530 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7534 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7535 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7538 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7539 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7540 ("sqlite3_step fail:%d", rc));
7544 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
7545 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7546 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7548 sqlite3_free_table(result);
7554 *search_handle = (int)hStmt;
7555 *searched = mail_count;
7556 EM_DEBUG_LOG("mail_count [%d]", mail_count);
7559 if (hStmt != NULL) {
7560 rc = sqlite3_finalize(hStmt);
7561 if (rc != SQLITE_OK) {
7562 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7563 error = EMAIL_ERROR_DB_FAILURE;
7567 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7571 if (err_code != NULL)
7574 EM_DEBUG_FUNC_END("ret [%d]", ret);
7578 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
7580 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
7582 if (search_handle == 0 || !data) {
7583 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
7585 if (err_code != NULL)
7586 *err_code = EMAIL_ERROR_INVALID_PARAM;
7590 emstorage_mail_tbl_t* p_data_tbl = NULL;
7591 DB_STMT hStmt = (DB_STMT)search_handle;
7592 int rc, ret = false;
7593 int error = EMAIL_ERROR_NONE;
7597 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
7600 case RETRIEVE_ENVELOPE:
7602 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
7603 EM_DEBUG_EXCEPTION(" em_malloc failed...");
7605 error = EMAIL_ERROR_OUT_OF_MEMORY;
7609 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7610 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7611 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_TBL);
7612 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
7613 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7614 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
7615 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
7616 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
7617 _get_stmt_field_data_int (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
7618 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
7619 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7620 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7621 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
7622 _get_stmt_field_data_char (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
7623 _get_stmt_field_data_int (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
7624 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
7625 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
7626 _get_stmt_field_data_int (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
7627 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
7628 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7629 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
7630 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
7631 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
7632 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
7634 if (type == RETRIEVE_ALL) {
7635 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7636 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7637 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
7638 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
7639 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
7640 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
7641 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
7642 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7643 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7644 _get_stmt_field_data_int (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
7645 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7649 if (p_data_tbl->body_download_status) {
7652 if (p_data_tbl->file_path_html) {
7653 if (stat(p_data_tbl->file_path_html, &buf) == -1)
7654 p_data_tbl->body_download_status = 0;
7656 else if (p_data_tbl->file_path_plain) {
7657 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
7658 p_data_tbl->body_download_status = 0;
7661 p_data_tbl->body_download_status = 0;
7664 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7667 case RETRIEVE_SUMMARY:
7668 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
7669 EM_DEBUG_EXCEPTION(" malloc failed...");
7671 error = EMAIL_ERROR_OUT_OF_MEMORY;
7675 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7677 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7678 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7679 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_TBL);
7680 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7681 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7682 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7684 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7687 case RETRIEVE_ADDRESS:
7688 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
7689 EM_DEBUG_EXCEPTION(" malloc failed...");
7690 error = EMAIL_ERROR_OUT_OF_MEMORY;
7694 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7695 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7696 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7697 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7698 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7706 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7707 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7708 ("sqlite3_step fail:%d", rc));
7714 if (err_code != NULL)
7717 EM_DEBUG_FUNC_END("ret [%d]", ret);
7721 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code)
7723 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
7725 int error = EMAIL_ERROR_NONE;
7726 int rc, ret = false;
7728 if (search_handle == 0) {
7729 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
7730 error = EMAIL_ERROR_INVALID_PARAM;
7734 DB_STMT hStmt = (DB_STMT)search_handle;
7736 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7738 rc = sqlite3_finalize(hStmt);
7739 if (rc != SQLITE_OK) {
7740 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
7741 error = EMAIL_ERROR_DB_FAILURE;
7747 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7750 if (err_code != NULL)
7753 EM_DEBUG_FUNC_END("ret [%d]", ret);
7757 INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7759 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7761 if (mail_id <= 0 || !mail) {
7762 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
7764 if (err_code != NULL)
7765 *err_code = EMAIL_ERROR_INVALID_PARAM;
7769 DB_STMT hStmt = NULL;
7770 char sql_query_string[QUERY_SIZE] = {0, };
7773 int error = EMAIL_ERROR_NONE;
7775 sqlite3 *local_db_handle = emstorage_get_db_connection();
7776 char mailbox_id_param_string[10] = {0,};
7778 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7780 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7781 "UPDATE mail_tbl SET"
7785 ", mailbox_name = ?"
7787 ", server_mail_status = ?"
7788 ", server_mailbox_name = ?"
7789 ", server_mail_id = ?"
7790 ", reference_mail_id = ?"
7791 ", full_address_from = ?"
7792 ", full_address_reply = ?" /* 10 */
7793 ", full_address_to = ?"
7794 ", full_address_cc = ?"
7795 ", full_address_bcc = ?"
7796 ", full_address_return = ?"
7798 ", body_download_status = ?"
7799 ", file_path_plain = ?"
7800 ", file_path_html = ?"
7802 ", flags_seen_field = ?"
7803 ", flags_deleted_field = ?"
7804 ", flags_flagged_field = ?"
7805 ", flags_answered_field = ?"
7806 ", flags_recent_field = ?"
7807 ", flags_draft_field = ?"
7808 ", flags_forwarded_field = ?"
7814 ", report_status = ?"
7815 ", preview_text = ?"
7817 " WHERE mail_id = %d AND account_id != 0 "
7821 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7822 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7823 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7825 _bind_stmt_field_data_int (hStmt, i++, mail->mail_id);
7826 _bind_stmt_field_data_int (hStmt, i++, mail->account_id);
7827 _bind_stmt_field_data_int (hStmt, i++, mail->mailbox_id);
7828 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->mailbox_name, 0, MAILBOX_LEN_IN_MAIL_TBL);
7829 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
7830 _bind_stmt_field_data_int (hStmt, i++, mail->server_mail_status);
7831 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
7832 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
7833 _bind_stmt_field_data_int (hStmt, i++, mail->reference_mail_id);
7834 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
7835 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
7836 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
7837 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
7838 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
7839 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
7840 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
7841 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
7842 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
7843 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
7844 _bind_stmt_field_data_int (hStmt, i++, mail->date_time);
7845 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
7846 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
7847 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
7848 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
7849 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
7850 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
7851 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
7852 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
7853 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
7854 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
7855 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
7856 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
7857 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
7858 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
7859 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
7861 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7862 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7863 ("sqlite3_step fail:%d", rc));
7865 rc = sqlite3_changes(local_db_handle);
7867 EM_DEBUG_EXCEPTION(" no matched mail found...");
7868 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7871 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
7872 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
7873 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
7878 if (hStmt != NULL) {
7879 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7880 rc = sqlite3_finalize(hStmt);
7881 if (rc != SQLITE_OK) {
7882 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7883 error = EMAIL_ERROR_DB_FAILURE;
7887 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7890 if (err_code != NULL)
7893 EM_DEBUG_FUNC_END("ret [%d]", ret);
7897 INTERNAL_FUNC int emstorage_modify_mailbox_of_mails(char *old_mailbox_name, char *new_mailbox_name, int transaction, int *err_code)
7899 EM_DEBUG_FUNC_BEGIN("old_mailbox_name[%p], new_mailbox_name[%p], transaction[%d], err_code[%p]", old_mailbox_name, new_mailbox_name, transaction, err_code);
7901 if (!old_mailbox_name && !new_mailbox_name) {
7902 EM_DEBUG_EXCEPTION(" old_mailbox_name[%p], new_mailbox_name[%p]", old_mailbox_name, new_mailbox_name);
7904 if (err_code != NULL)
7905 *err_code = EMAIL_ERROR_INVALID_PARAM;
7911 int error = EMAIL_ERROR_NONE;
7912 char sql_query_string[QUERY_SIZE] = {0, };
7914 sqlite3 *local_db_handle = emstorage_get_db_connection();
7916 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7918 EM_DEBUG_LOG("Old Mailbox Name [ %s ] , New Mailbox name [ %s ] ", old_mailbox_name, new_mailbox_name);
7920 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_name = '%s' WHERE mailbox_name = '%s'", new_mailbox_name, old_mailbox_name);
7922 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7923 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7924 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7926 rc = sqlite3_changes(local_db_handle);
7928 EM_DEBUG_EXCEPTION(" no matched mail found...");
7930 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7934 EM_DEBUG_LOG(" Modification done in mail_read_mail_uid_tbl based on Mailbox name ");
7935 /* Modify the mailbox_name name in mail_read_mail_uid_tbl table */
7936 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s' WHERE mailbox_name = '%s'", new_mailbox_name, old_mailbox_name);
7938 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7939 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7940 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7942 rc = sqlite3_changes(local_db_handle);
7944 EM_DEBUG_EXCEPTION(" no matched mail found...");
7945 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7949 if (!emcore_notify_storage_event(NOTI_MAILBOX_UPDATE, 1, 0, new_mailbox_name, 0))
7950 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_MAILBOX_UPDATE] : Notification Failed >>> ");
7956 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7959 if (err_code != NULL)
7962 EM_DEBUG_FUNC_END("ret [%d]", ret);
7967 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
7971 INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int *err_code)
7973 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
7975 EM_IF_NULL_RETURN_VALUE(err_code, false);
7978 int error = EMAIL_ERROR_NONE;
7980 char sql_query_string[QUERY_SIZE] = {0, };
7981 sqlite3 *local_db_handle = emstorage_get_db_connection();
7983 memset(sql_query_string, 0x00, sizeof(sql_query_string));
7984 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE save_status = %d", save_status, EMAIL_MAIL_STATUS_SENDING);
7985 EM_DEBUG_LOG("Query [%s]", sql_query_string);
7987 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7988 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7989 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7991 rc = sqlite3_changes(local_db_handle);
7993 EM_DEBUG_LOG(" No Matched Mail Exists ");
7994 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8001 if (err_code != NULL)
8004 EM_DEBUG_FUNC_END("ret [%d]", ret);
8008 INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
8010 EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_ids[%p], mail_ids_count[%d], field_name[%s], value[%d], transaction[%d], err_code[%p]", account_id, mail_ids, mail_ids_count, field_name, value, transaction, err_code);
8012 int error = EMAIL_ERROR_NONE;
8015 int cur_mail_id_string = 0;
8016 int mail_id_string_buffer_length = 0;
8017 char sql_query_string[QUERY_SIZE] = {0, };
8018 char *mail_id_string_buffer = NULL;
8019 char *parameter_string = NULL;
8020 sqlite3 *local_db_handle = emstorage_get_db_connection();
8021 email_mail_attribute_type target_mail_attribute_type = 0;
8023 if (!mail_ids || !field_name || account_id == 0) {
8024 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8025 if (err_code != NULL)
8026 *err_code = EMAIL_ERROR_INVALID_PARAM;
8030 if( (error = _get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8031 EM_DEBUG_EXCEPTION("emstorage_get_attribute_type_by_mail_field_name failed [%d]", error);
8032 if (err_code != NULL)
8037 /* Generating mail id list string */
8038 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8040 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8042 if(!mail_id_string_buffer) {
8043 EM_DEBUG_EXCEPTION("em_malloc failed");
8044 error = EMAIL_ERROR_OUT_OF_MEMORY;
8048 for(i = 0; i < mail_ids_count; i++)
8049 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8051 if(EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8052 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8054 /* Generating notification parameter string */
8055 parameter_string = em_malloc(mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2);
8057 if(!parameter_string) {
8058 EM_DEBUG_EXCEPTION("em_malloc failed");
8059 error = EMAIL_ERROR_OUT_OF_MEMORY;
8063 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8065 /* Write query string */
8066 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_tbl SET %s = %d WHERE mail_id in (%s) AND account_id = %d", field_name, value, mail_id_string_buffer, account_id);
8068 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
8071 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8072 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8073 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8074 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8075 if (sqlite3_changes(local_db_handle) == 0)
8076 EM_DEBUG_LOG("no mail matched...");
8081 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8084 if (ret && parameter_string && !emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8085 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8087 EM_SAFE_FREE(mail_id_string_buffer);
8088 EM_SAFE_FREE(parameter_string);
8090 if (err_code != NULL)
8093 EM_DEBUG_FUNC_END("error [%d]", error);
8097 INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
8099 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8101 if (mail_id <= 0 || !mail) {
8102 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
8103 if (err_code != NULL)
8104 *err_code = EMAIL_ERROR_INVALID_PARAM;
8109 int error = EMAIL_ERROR_NONE;
8111 DB_STMT hStmt = NULL;
8112 char sql_query_string[QUERY_SIZE] = {0, };
8117 char *mailbox_name = NULL;
8118 char mailbox_id_param_string[10] = {0,};
8120 sqlite3 *local_db_handle = emstorage_get_db_connection();
8121 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8125 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8126 "UPDATE mail_tbl SET"
8127 " body_download_status = ?"
8128 ", file_path_plain = ?"
8129 ", file_path_html = ?"
8130 ", flags_seen_field = ?"
8131 ", flags_deleted_field = ?"
8132 ", flags_flagged_field = ?"
8133 ", flags_answered_field = ?"
8134 ", flags_recent_field = ?"
8135 ", flags_draft_field = ?"
8136 ", flags_forwarded_field = ?"
8138 ", attachment_count = ?"
8140 ", meeting_request_status = ? "
8141 ", message_class = ? "
8142 ", digest_type = ? "
8144 " WHERE mail_id = %d AND account_id != 0"
8148 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8149 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8150 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8153 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8154 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8155 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8156 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
8157 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
8158 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
8159 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
8160 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
8161 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
8162 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
8163 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8164 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8165 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8166 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
8167 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
8168 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
8169 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8172 case UPDATE_MAILBOX: {
8174 emstorage_mailbox_tbl_t *mailbox_tbl;
8176 if (!emstorage_get_mailbox_by_name(mail->account_id, -1, mail->mailbox_name, &mailbox_tbl, false, &err)) {
8177 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_name failed - %d", err);
8182 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8183 "UPDATE mail_tbl SET"
8184 " mailbox_id = '%d'"
8185 " mailbox_name = '%s'"
8186 ",mailbox_type = '%d'"
8187 " WHERE mail_id = %d AND account_id != 0"
8188 , mailbox_tbl->mailbox_id
8189 , mail->mailbox_name ? mail->mailbox_name : ""
8190 , mailbox_tbl->mailbox_type
8194 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
8195 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8197 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8198 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8199 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8205 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8206 "UPDATE mail_tbl SET"
8207 " flags_seen_field = %d"
8208 ",flags_deleted_field = %d"
8209 ",flags_flagged_field = %d"
8210 ",flags_answered_field = %d"
8211 ",flags_recent_field = %d"
8212 ",flags_draft_field = %d"
8213 ",flags_forwarded_field = %d"
8214 " WHERE mail_id = %d AND account_id != 0"
8215 , mail->flags_seen_field
8216 , mail->flags_deleted_field
8217 , mail->flags_flagged_field
8218 , mail->flags_answered_field
8219 , mail->flags_recent_field
8220 , mail->flags_draft_field
8221 , mail->flags_forwarded_field
8223 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8226 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8227 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8228 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8232 case UPDATE_EXTRA_FLAG:
8233 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8234 "UPDATE mail_tbl SET"
8236 ", save_status = %d"
8237 ", lock_status = %d"
8238 ", report_status = %d"
8240 " WHERE mail_id = %d AND account_id != 0"
8244 , mail->report_status
8247 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8251 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8252 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8255 case UPDATE_STICKY_EXTRA_FLAG:
8256 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8257 "UPDATE mail_tbl SET"
8259 " WHERE mail_id = %d AND account_id != 0"
8262 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8265 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8266 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8267 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8271 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8272 "UPDATE mail_tbl SET"
8273 " full_address_from = ?"
8274 ", full_address_reply = ?"
8275 ", full_address_to = ?"
8276 ", full_address_cc = ?"
8277 ", full_address_bcc = ?"
8278 ", full_address_return = ?"
8280 ", file_path_plain = ?"
8282 ", flags_seen_field = ?"
8283 ", flags_deleted_field = ?"
8284 ", flags_flagged_field = ?"
8285 ", flags_answered_field = ?"
8286 ", flags_recent_field = ?"
8287 ", flags_draft_field = ?"
8288 ", flags_forwarded_field = ?"
8292 ", report_status = ?"
8294 ", file_path_html = ?"
8295 ", file_path_mime_entity = ?"
8297 ", preview_text = ?"
8298 ", body_download_status = ?"
8299 ", attachment_count = ?"
8300 ", inline_content_count = ?"
8301 ", meeting_request_status = ?"
8302 ", message_class = ?"
8305 " WHERE mail_id = %d AND account_id != 0"
8309 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8310 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8311 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8312 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8314 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8315 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8316 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8317 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8318 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8319 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8320 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8321 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8322 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
8323 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
8324 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
8325 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
8326 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
8327 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
8328 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
8329 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
8330 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
8331 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
8332 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
8333 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
8334 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
8335 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8336 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8337 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
8338 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8339 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
8340 _bind_stmt_field_data_int (hStmt, i++, mail->attachment_count);
8341 _bind_stmt_field_data_int (hStmt, i++, mail->inline_content_count);
8342 _bind_stmt_field_data_int (hStmt, i++, mail->meeting_request_status);
8343 _bind_stmt_field_data_int (hStmt, i++, mail->message_class);
8344 _bind_stmt_field_data_int (hStmt, i++, mail->digest_type);
8345 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
8348 case UPDATE_DATETIME: {
8349 time_t now = time(NULL);
8351 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8352 "UPDATE mail_tbl SET"
8354 " WHERE mail_id = %d AND account_id != 0"
8358 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8359 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8360 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8364 case UPDATE_FROM_CONTACT_INFO:
8365 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
8366 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8367 "UPDATE mail_tbl SET"
8368 " email_address_sender = ?,"
8369 " WHERE mail_id = %d",
8374 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8375 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8376 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8378 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8381 case UPDATE_TO_CONTACT_INFO:
8382 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
8383 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8384 "UPDATE mail_tbl SET"
8385 " email_address_recipient = ?,"
8386 " WHERE mail_id = %d",
8391 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8392 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8393 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8395 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8398 case UPDATE_ALL_CONTACT_INFO:
8399 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
8400 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8401 "UPDATE mail_tbl SET"
8402 " email_address_sender = ?,"
8403 " email_address_recipient = ?,"
8404 " WHERE mail_id = %d",
8409 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8410 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8411 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8413 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8414 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8418 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
8419 case UPDATE_PARTIAL_BODY_DOWNLOAD:
8421 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8422 "UPDATE mail_tbl SET"
8423 " body_download_status = ?"
8424 ", file_path_plain = ?"
8425 ", file_path_html = ?"
8426 ", attachment_count = ?"
8427 ", inline_content_count = ?"
8429 " WHERE mail_id = %d"
8433 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8434 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8435 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8438 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8439 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8440 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8441 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8442 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
8443 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8448 case UPDATE_FILE_PATH:
8449 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8450 "UPDATE mail_tbl SET"
8451 ", file_path_plain = ?"
8452 ", file_path_html = ?"
8453 ", file_path_mime_entity = ?"
8454 " WHERE mail_id = %d"
8458 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8459 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8460 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8461 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8463 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8464 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8465 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8469 EM_DEBUG_LOG(" type[%d]", type);
8471 error = EMAIL_ERROR_INVALID_PARAM;
8475 if (hStmt != NULL) {
8477 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8478 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
8479 ("sqlite3_step fail:%d", rc));
8480 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8481 ("sqlite3_step fail:%d", rc));
8482 rc = sqlite3_changes(local_db_handle);
8484 EM_DEBUG_EXCEPTION(" no matched mail found...");
8486 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8491 if (mail->account_id == 0) {
8492 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
8493 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
8494 EM_DEBUG_EXCEPTION(" emstorage_get_mail_field_by_id failed - %d", error);
8498 mail->account_id = mail_for_account_tbl->account_id;
8499 if (mail_for_account_tbl)
8500 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
8507 if (hStmt != NULL) {
8508 rc = sqlite3_finalize(hStmt);
8509 if (rc != SQLITE_OK) {
8510 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8511 error = EMAIL_ERROR_DB_FAILURE;
8516 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8519 /*h.gahlaut@samsung.com: Moving publication of notification after commiting transaction to DB */
8521 if (ret == true && move_flag != 1) {
8522 if (!emstorage_get_mailbox_name_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_name, false, &error))
8523 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_name_by_mailbox_type failed - %d", error);
8525 if (mail->mailbox_name && mailbox_name) {
8526 if (strcmp(mail->mailbox_name, mailbox_name) != 0) {
8527 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8528 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
8529 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8533 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
8534 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
8535 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] ");
8539 EM_SAFE_FREE(mailbox_name);
8541 if (err_code != NULL)
8544 EM_DEBUG_FUNC_END("ret [%d]", ret);
8547 INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
8549 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8551 int rc, ret = false;
8552 int error = EMAIL_ERROR_NONE;
8553 int latest_mail_id = 0;
8554 sqlite3 *local_db_handle = NULL;
8555 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
8556 char **result = NULL;
8558 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8559 _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
8560 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8562 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
8563 if (ret < 0 || latest_mail_id == 0) {
8564 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
8566 local_db_handle = emstorage_get_db_connection();
8568 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8569 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8570 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
8571 if (NULL == result[1])
8574 rc = atoi(result[1]) + 1;
8576 sqlite3_free_table(result);
8577 latest_mail_id = rc;
8582 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
8585 *mail_id = latest_mail_id;
8587 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8588 _unlockshm_mutex(&mapped_for_generating_mail_id);
8589 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8596 if (err_code != NULL)
8599 EM_DEBUG_FUNC_END("ret [%d]", ret);
8604 INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
8606 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
8607 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
8609 if (!mail_tbl_data) {
8610 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
8611 if (err_code != NULL)
8612 *err_code = EMAIL_ERROR_INVALID_PARAM;
8616 int rc, ret = false;
8617 int error = EMAIL_ERROR_NONE;
8618 char sql_query_string[QUERY_SIZE] = {0, };
8619 DB_STMT hStmt = NULL;
8620 sqlite3 *local_db_handle = emstorage_get_db_connection();
8622 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8625 /* increase unique id */
8626 char *sql = "SELECT max(rowid) FROM mail_tbl;";
8629 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8630 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8631 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
8633 if (NULL == result[1])
8636 rc = atoi(result[1])+1;
8638 sqlite3_free_table(result);
8640 mail_tbl_data->mail_id = rc;
8641 mail_tbl_data->thread_id = rc;
8644 if (mail_tbl_data->date_time == 0)
8645 mail_tbl_data->date_time = time(NULL);
8647 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8648 "INSERT INTO mail_tbl VALUES "
8650 ", ?" /* account_id */
8651 ", ?" /* mailbox_id */
8652 ", ?" /* mailbox_name */
8653 ", ?" /* mailbox_type */
8655 ", ?" /* date_time */
8656 ", ?" /* server_mail_status */
8657 ", ?" /* server_mailbox_name */
8658 ", ?" /* server_mail_id */
8659 ", ?" /* message_id */
8660 ", ?" /* reference_mail_id */
8661 ", ?" /* full_address_from */
8662 ", ?" /* full_address_reply */
8663 ", ?" /* full_address_to */
8664 ", ?" /* full_address_cc */
8665 ", ?" /* full_address_bcc */
8666 ", ?" /* full_address_return */
8667 ", ?" /* email_address_sender */
8668 ", ?" /* email_address_recipient */
8669 ", ?" /* alias_sender */
8670 ", ?" /* alias_recipient */
8671 ", ?" /* body_download_status */
8672 ", ?" /* file_path_plain */
8673 ", ?" /* file_path_html */
8674 ", ?" /* file_path_mime_entity */
8675 ", ?" /* mail_size */
8676 ", ?" /* flags_seen_field */
8677 ", ?" /* flags_deleted_field */
8678 ", ?" /* flags_flagged_field */
8679 ", ?" /* flags_answered_field */
8680 ", ?" /* flags_recent_field */
8681 ", ?" /* flags_draft_field */
8682 ", ?" /* flags_forwarded_field */
8683 ", ?" /* DRM_status */
8684 ", ?" /* priority */
8685 ", ?" /* save_status */
8686 ", ?" /* lock_status */
8687 ", ?" /* report_status */
8688 ", ?" /* attachment_count */
8689 ", ?" /* inline_content_count */
8690 ", ?" /* thread_id */
8691 ", ?" /* thread_item_count */
8692 ", ?" /* preview_text */
8693 ", ?" /* meeting_request_status */
8694 ", ?" /* message_class */
8695 ", ?" /* digest_type */
8696 ", ?" /* smime_type */
8699 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8700 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8701 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8703 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
8704 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
8705 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
8706 _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->mailbox_name, 0, MAILBOX_LEN_IN_MAIL_TBL);
8707 _bind_stmt_field_data_int (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
8708 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8709 _bind_stmt_field_data_int (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
8710 _bind_stmt_field_data_int (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
8711 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8712 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8713 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8714 _bind_stmt_field_data_int (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
8715 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8716 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8717 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8718 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8719 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8720 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8721 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8722 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8723 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->alias_sender, 1, FROM_CONTACT_NAME_LEN_IN_MAIL_TBL);
8724 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->alias_recipient, 1, FROM_CONTACT_NAME_LEN_IN_MAIL_TBL);
8725 _bind_stmt_field_data_int (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
8726 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8727 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8728 _bind_stmt_field_data_string(hStmt, FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8729 _bind_stmt_field_data_int (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
8730 _bind_stmt_field_data_int (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
8731 _bind_stmt_field_data_int (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
8732 _bind_stmt_field_data_int (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
8733 _bind_stmt_field_data_int (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
8734 _bind_stmt_field_data_int (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
8735 _bind_stmt_field_data_int (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
8736 _bind_stmt_field_data_int (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
8737 _bind_stmt_field_data_int (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
8738 _bind_stmt_field_data_int (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
8739 _bind_stmt_field_data_int (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
8740 _bind_stmt_field_data_int (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
8741 _bind_stmt_field_data_int (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
8742 _bind_stmt_field_data_int (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
8743 _bind_stmt_field_data_int (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
8744 _bind_stmt_field_data_int (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
8745 _bind_stmt_field_data_int (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
8746 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8747 _bind_stmt_field_data_int (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
8748 _bind_stmt_field_data_int (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
8749 _bind_stmt_field_data_int (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
8750 _bind_stmt_field_data_int (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
8752 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8753 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8754 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8759 if (hStmt != NULL) {
8760 rc = sqlite3_finalize(hStmt);
8761 if (rc != SQLITE_OK) {
8762 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
8763 error = EMAIL_ERROR_DB_FAILURE;
8767 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8771 if (err_code != NULL)
8774 EM_PROFILE_END(profile_emstorage_add_mail);
8775 EM_DEBUG_FUNC_END("ret [%d]", ret);
8779 INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(int input_source_account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
8781 EM_DEBUG_FUNC_BEGIN("input_source_account_id [%d], input_mailbox_id [%d], mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", input_source_account_id, input_mailbox_id, mail_ids, number_of_mails, transaction, err_code);
8783 int rc, ret = false, i, cur_conditional_clause = 0;
8784 int error = EMAIL_ERROR_NONE;
8785 int target_account_id;
8786 char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
8787 emstorage_mailbox_tbl_t *result_mailbox = NULL;
8788 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
8789 char* target_mailbox_name = NULL;
8791 if (!mail_ids || input_mailbox_id <= 0) {
8792 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8793 if (err_code != NULL)
8794 *err_code = EMAIL_ERROR_INVALID_PARAM;
8798 sqlite3 *local_db_handle = emstorage_get_db_connection();
8800 if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
8801 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
8805 target_mailbox_name = EM_SAFE_STRDUP(result_mailbox->mailbox_name);
8806 target_mailbox_type = result_mailbox->mailbox_type;
8807 target_account_id = result_mailbox->account_id;
8808 emstorage_free_mailbox(&result_mailbox, 1, NULL);
8810 cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
8812 for(i = 0; i < number_of_mails; i++)
8813 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8815 conditional_clause[EM_SAFE_STRLEN(conditional_clause) - 1] = ')';
8817 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8819 /* Updating a mail_tbl */
8821 memset(sql_query_string, 0x00, QUERY_SIZE);
8822 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_tbl SET mailbox_name = '%s', mailbox_type = %d, mailbox_id = %d, account_id = %d %s", target_mailbox_name, target_mailbox_type, input_mailbox_id, target_account_id, conditional_clause);
8823 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8825 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8826 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8827 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8829 /* Updating a mail_attachment_tbl */
8831 memset(sql_query_string, 0x00, QUERY_SIZE);
8832 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_attachment_tbl SET mailbox_id = '%d', account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
8833 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8835 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8836 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8837 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8839 /* Updating a mail_meeting_tbl */
8840 memset(sql_query_string, 0x00, QUERY_SIZE);
8841 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_meeting_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
8842 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8844 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8845 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8846 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8848 /* Updating a mail_read_mail_uid_tbl */
8849 memset(conditional_clause, 0x00, QUERY_SIZE);
8850 cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
8852 for(i = 0; i < number_of_mails; i++)
8853 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8856 char *last_comma = rindex(conditional_clause, ',');
8857 *last_comma = ')'; /* replace , with ) */
8859 memset(sql_query_string, 0x00, QUERY_SIZE);
8860 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d, account_id = %d %s", target_mailbox_name, input_mailbox_id, target_account_id, conditional_clause);
8861 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8863 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8864 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8865 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8870 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8873 EM_SAFE_FREE(target_mailbox_name);
8875 if (err_code != NULL)
8878 EM_DEBUG_FUNC_END("ret [%d]", ret);
8882 INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
8884 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8887 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
8888 if (err_code != NULL)
8889 *err_code = EMAIL_ERROR_INVALID_PARAM;
8893 int rc, ret = false;
8894 int error = EMAIL_ERROR_NONE;
8895 char sql_query_string[QUERY_SIZE] = {0, };
8897 sqlite3 *local_db_handle = emstorage_get_db_connection();
8898 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8900 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8902 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
8903 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8906 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8907 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8908 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8913 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8916 if (err_code != NULL)
8919 EM_DEBUG_FUNC_END("ret [%d]", ret);
8923 INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
8925 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
8927 int rc, ret = false, i, cur_sql_query_string = 0;
8928 int error = EMAIL_ERROR_NONE;
8929 char sql_query_string[QUERY_SIZE] = {0, };
8932 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8933 if (err_code != NULL)
8934 *err_code = EMAIL_ERROR_INVALID_PARAM;
8938 sqlite3 *local_db_handle = emstorage_get_db_connection();
8939 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8941 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
8943 for(i = 0; i < number_of_mails; i++)
8944 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
8947 char *last_comma = rindex(sql_query_string, ',');
8948 *last_comma = ')'; /* replace , with ) */
8950 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8952 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8953 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8954 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8959 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8962 if (err_code != NULL)
8965 EM_DEBUG_FUNC_END("ret [%d]", ret);
8969 INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
8971 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
8973 if (account_id < FIRST_ACCOUNT_ID) {
8974 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
8976 if (err_code != NULL)
8977 *err_code = EMAIL_ERROR_INVALID_PARAM;
8981 int rc, ret = false;
8982 int error = EMAIL_ERROR_NONE;
8983 char sql_query_string[QUERY_SIZE] = {0, };
8985 sqlite3 *local_db_handle = emstorage_get_db_connection();
8986 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8988 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
8989 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8991 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8992 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8993 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8995 rc = sqlite3_changes(local_db_handle);
8997 EM_DEBUG_EXCEPTION(" no mail found...");
8998 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9001 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
9002 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
9003 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9005 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9006 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9007 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9009 rc = sqlite3_changes(local_db_handle);
9011 EM_DEBUG_EXCEPTION("no mail found...");
9012 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9015 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
9016 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
9022 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9025 if (err_code != NULL)
9028 EM_DEBUG_FUNC_END("ret [%d]", ret);
9032 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, char *mailbox, int transaction, int *err_code)
9034 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
9036 if (account_id < FIRST_ACCOUNT_ID || !mailbox) {
9037 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox[%p]", account_id, mailbox);
9038 if (err_code != NULL)
9039 *err_code = EMAIL_ERROR_INVALID_PARAM;
9043 int rc, ret = false;
9044 int error = EMAIL_ERROR_NONE;
9045 char sql_query_string[QUERY_SIZE] = {0, };
9047 sqlite3 *local_db_handle = emstorage_get_db_connection();
9049 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9051 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox);
9052 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9054 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9055 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9056 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9059 /* Delete Mails from mail_read_mail_uid_tbl */
9060 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox);
9061 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9063 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9064 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9065 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9067 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, 0 , mailbox, 0))
9068 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
9073 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9076 if (err_code != NULL)
9079 EM_DEBUG_FUNC_END("ret [%d]", ret);
9083 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
9085 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
9088 if ((mail_list == NULL) || (*mail_list == NULL)) {
9089 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
9092 *err_code = EMAIL_ERROR_INVALID_PARAM;
9096 emstorage_mail_tbl_t* p = *mail_list;
9099 for (; i < count; i++, p++) {
9100 EM_SAFE_FREE(p->mailbox_name);
9101 EM_SAFE_FREE(p->server_mailbox_name);
9102 EM_SAFE_FREE(p->server_mail_id);
9103 EM_SAFE_FREE(p->full_address_from);
9104 EM_SAFE_FREE(p->full_address_reply);
9105 EM_SAFE_FREE(p->full_address_to);
9106 EM_SAFE_FREE(p->full_address_cc);
9107 EM_SAFE_FREE(p->full_address_bcc);
9108 EM_SAFE_FREE(p->full_address_return);
9109 EM_SAFE_FREE(p->subject);
9110 EM_SAFE_FREE(p->file_path_plain);
9111 EM_SAFE_FREE(p->file_path_html);
9112 EM_SAFE_FREE(p->file_path_mime_entity);
9113 EM_SAFE_FREE(p->message_id);
9114 EM_SAFE_FREE(p->email_address_sender);
9115 EM_SAFE_FREE(p->email_address_recipient);
9116 EM_SAFE_FREE(p->preview_text);
9117 EM_SAFE_FREE(p->alias_sender);
9118 EM_SAFE_FREE(p->alias_recipient);
9120 EM_SAFE_FREE(*mail_list);
9123 if (err_code != NULL)
9124 *err_code = EMAIL_ERROR_NONE;
9126 EM_DEBUG_FUNC_END();
9130 INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
9132 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
9134 if (mail_id <= 0 || !count) {
9135 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
9136 if (err_code != NULL)
9137 *err_code = EMAIL_ERROR_INVALID_PARAM;
9141 int rc = -1, ret = false;
9142 int error = EMAIL_ERROR_NONE;
9143 char sql_query_string[QUERY_SIZE] = {0, };
9145 sqlite3 *local_db_handle = emstorage_get_db_connection();
9146 EMSTORAGE_START_READ_TRANSACTION(transaction);
9148 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9152 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9153 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9154 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9156 *count = atoi(result[1]);
9157 sqlite3_free_table(result);
9163 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9166 if (err_code != NULL)
9169 EM_DEBUG_FUNC_END("ret [%d]", ret);
9173 INTERNAL_FUNC int emstorage_get_attachment_list(int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
9175 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_transaction[%d], output_attachment_list[%p], output_attachment_count[%p]", input_mail_id, input_transaction, output_attachment_list, output_attachment_count);
9177 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
9178 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9179 return EMAIL_ERROR_INVALID_PARAM;
9182 int error = EMAIL_ERROR_NONE;
9185 char **result = NULL;
9186 char sql_query_string[QUERY_SIZE] = {0, };
9187 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9188 DB_STMT hStmt = NULL;
9189 sqlite3 *local_db_handle = emstorage_get_db_connection();
9191 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
9192 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
9193 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9194 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9195 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9197 *output_attachment_count = atoi(result[1]);
9198 sqlite3_free_table(result);
9200 if(*output_attachment_count == 0) {
9201 error = EMAIL_ERROR_NONE;
9205 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
9208 EM_DEBUG_EXCEPTION("em_malloc failed...");
9209 error = EMAIL_ERROR_OUT_OF_MEMORY;
9213 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
9214 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
9216 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9217 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9219 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9220 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9222 if (rc == SQLITE_DONE) {
9223 EM_DEBUG_EXCEPTION("no matched attachment found...");
9224 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9227 for (i = 0; i < *output_attachment_count; i++) {
9228 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9229 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9230 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9231 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9232 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9233 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9234 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9235 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9236 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9237 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9238 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9239 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9241 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
9242 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9243 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9248 if (error == EMAIL_ERROR_NONE)
9249 *output_attachment_list = p_data_tbl;
9250 else if (p_data_tbl != NULL)
9251 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
9254 rc = sqlite3_finalize(hStmt);
9255 if (rc != SQLITE_OK) {
9256 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
9257 error = EMAIL_ERROR_DB_FAILURE;
9261 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
9265 EM_DEBUG_FUNC_END("error [%d]", error);
9269 INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
9271 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
9273 if (attachment_id <= 0 || !attachment) {
9274 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
9275 if (err_code != NULL)
9276 *err_code = EMAIL_ERROR_INVALID_PARAM;
9280 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9281 int rc, ret = false;
9282 int error = EMAIL_ERROR_NONE;
9283 char sql_query_string[QUERY_SIZE] = {0, };
9285 sqlite3 *local_db_handle = emstorage_get_db_connection();
9286 EMSTORAGE_START_READ_TRANSACTION(transaction);
9288 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
9290 sqlite3_stmt* hStmt = NULL;
9292 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9293 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9295 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9296 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9299 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9300 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9301 ("sqlite3_step fail:%d", rc));
9303 if (rc == SQLITE_DONE) {
9304 EM_DEBUG_EXCEPTION("no matched attachment found...");
9305 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9309 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
9310 EM_DEBUG_EXCEPTION("malloc failed...");
9311 error = EMAIL_ERROR_OUT_OF_MEMORY;
9315 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9316 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9317 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9318 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9319 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9320 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9321 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9322 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9323 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9324 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9325 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9326 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9328 #ifdef __ATTACHMENT_OPTI__
9329 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9330 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
9337 *attachment = p_data_tbl;
9339 if (hStmt != NULL) {
9340 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
9342 rc = sqlite3_finalize(hStmt);
9343 if (rc != SQLITE_OK) {
9344 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9345 error = EMAIL_ERROR_DB_FAILURE;
9349 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9352 if (err_code != NULL)
9355 EM_DEBUG_FUNC_END("ret [%d]", ret);
9359 INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
9361 EM_DEBUG_FUNC_BEGIN("mail_id[%d], nth[%d], attachment_tbl[%p], transaction[%d], err_code[%p]", mail_id, nth, attachment_tbl, transaction, err_code);
9363 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
9364 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
9366 if (err_code != NULL)
9367 *err_code = EMAIL_ERROR_INVALID_PARAM;
9371 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9373 int rc, ret = false;
9374 int error = EMAIL_ERROR_NONE;
9375 char sql_query_string[QUERY_SIZE] = {0, };
9377 sqlite3 *local_db_handle = emstorage_get_db_connection();
9378 EMSTORAGE_START_READ_TRANSACTION(transaction);
9380 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id LIMIT %d, 1", mail_id, (nth - 1));
9381 EM_DEBUG_LOG("query = [%s]", sql_query_string);
9383 DB_STMT hStmt = NULL;
9385 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9386 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9387 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9390 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9391 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9392 ("sqlite3_step fail:%d", rc));
9394 if (rc == SQLITE_DONE) {
9395 EM_DEBUG_EXCEPTION(" no matched attachment found...");
9396 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9400 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
9401 EM_DEBUG_EXCEPTION(" malloc failed...");
9402 error = EMAIL_ERROR_OUT_OF_MEMORY;
9406 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9407 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9408 p_data_tbl->attachment_name = cpy_str(p);
9409 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9410 p_data_tbl->attachment_path = cpy_str(p);
9411 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9412 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9413 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9414 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9415 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9416 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9417 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9418 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9419 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9420 p_data_tbl->attachment_mime_type = cpy_str(p);
9421 #ifdef __ATTACHMENT_OPTI__
9422 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9423 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9424 p_data_tbl->section= cpy_str(p);
9430 *attachment_tbl = p_data_tbl;
9432 if (hStmt != NULL) {
9433 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
9435 rc = sqlite3_finalize(hStmt);
9436 if (rc != SQLITE_OK) {
9437 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9438 error = EMAIL_ERROR_DB_FAILURE;
9442 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9445 if (err_code != NULL)
9448 EM_DEBUG_FUNC_END("ret [%d]", ret);
9452 INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t* attachment, int transaction, int *err_code)
9454 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
9456 if (mail_id <= 0 || !attachment) {
9457 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
9458 if (err_code != NULL)
9459 *err_code = EMAIL_ERROR_INVALID_PARAM;
9463 int rc, ret = false;
9464 int error = EMAIL_ERROR_NONE;
9465 DB_STMT hStmt = NULL;
9466 char sql_query_string[QUERY_SIZE] = {0, };
9470 sqlite3 *local_db_handle = emstorage_get_db_connection();
9472 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9475 case UPDATE_MAILBOX:
9476 EM_DEBUG_LOG("UPDATE_MAILBOX");
9477 if (!attachment->mailbox_id) {
9478 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
9479 error = EMAIL_ERROR_INVALID_PARAM;
9482 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9483 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
9485 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9486 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9487 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9488 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9490 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
9493 case UPDATE_SAVENAME:
9494 EM_DEBUG_LOG("UPDATE_SAVENAME");
9495 if (!attachment->attachment_path) {
9496 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
9497 error = EMAIL_ERROR_INVALID_PARAM;
9501 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9502 "UPDATE mail_attachment_tbl SET"
9503 " attachment_size = ?"
9504 ", attachment_save_status = 1"
9505 ", attachment_path = ?"
9506 " WHERE mail_id = %d"
9507 " AND attachment_id = %d"
9508 , attachment->mail_id
9509 , attachment->attachment_id);
9512 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9513 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9514 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9515 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9517 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
9518 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9522 EM_DEBUG_LOG("type[%d]", type);
9523 error = EMAIL_ERROR_INVALID_PARAM;
9526 EM_DEBUG_LOG("query = [%s]", sql_query_string);
9528 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9529 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9530 ("sqlite3_step fail:%d", rc));
9534 if (hStmt != NULL) {
9535 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
9536 rc = sqlite3_finalize(hStmt);
9537 if (rc != SQLITE_OK) {
9538 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
9539 error = EMAIL_ERROR_DB_FAILURE;
9543 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9546 if (err_code != NULL)
9548 EM_DEBUG_FUNC_END("ret [%d]", ret);
9553 INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, int input_transaction)
9555 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_new_mailbox_name[%p], input_new_mailbox_alias [%p], input_transaction[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_transaction);
9559 int error = EMAIL_ERROR_NONE;
9560 char sql_query_string[QUERY_SIZE] = {0, };
9561 sqlite3 *local_db_handle = NULL;
9563 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
9565 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
9566 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9567 return EMAIL_ERROR_INVALID_PARAM;
9570 local_db_handle = emstorage_get_db_connection();
9572 if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
9573 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
9576 account_id = old_mailbox_data->account_id;
9578 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
9580 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9581 "UPDATE mail_box_tbl SET"
9582 " mailbox_name = '%s'"
9584 " WHERE mailbox_id = %d"
9585 , input_new_mailbox_name
9586 , input_new_mailbox_alias
9587 , input_mailbox_id);
9589 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9591 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9592 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9594 if (sqlite3_changes(local_db_handle) == 0)
9595 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
9597 /* Update mail_tbl */
9598 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9599 "UPDATE mail_tbl SET"
9600 " mailbox_name = '%s'"
9601 " WHERE mailbox_id = %d"
9602 , input_new_mailbox_name
9603 , input_mailbox_id);
9605 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9607 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9608 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9610 if (sqlite3_changes(local_db_handle) == 0)
9611 EM_DEBUG_LOG("no mail matched...");
9617 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
9620 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9621 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
9624 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
9625 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
9628 if (old_mailbox_data)
9629 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
9633 EM_DEBUG_FUNC_END("error [%d]", error);
9637 INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
9639 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
9640 int rc, ret = false;
9641 int error = EMAIL_ERROR_NONE;
9642 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9645 if (!attachment_no) {
9646 EM_DEBUG_EXCEPTION("Invalid attachment");
9647 error = EMAIL_ERROR_INVALID_PARAM;
9651 *attachment_no = -1;
9653 sqlite3 *local_db_handle = emstorage_get_db_connection();
9656 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9657 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9658 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9660 if (NULL == result[1])
9663 rc = atoi(result[1])+1;
9665 sqlite3_free_table(result);
9667 *attachment_no = rc;
9668 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
9674 if (err_code != NULL)
9677 EM_DEBUG_FUNC_END("ret [%d]", ret);
9681 INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
9683 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
9687 int rc, ret = false;
9688 int error = EMAIL_ERROR_NONE;
9689 DB_STMT hStmt = NULL;
9690 char sql_query_string[QUERY_SIZE] = {0, };
9691 sqlite3 *local_db_handle = emstorage_get_db_connection();
9693 if (!attachment_tbl) {
9694 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
9695 if (err_code != NULL)
9696 *err_code = EMAIL_ERROR_INVALID_PARAM;
9700 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9702 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9704 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9705 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9706 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9708 if (NULL==result[1]) rc = 1;
9709 else rc = atoi(result[1]) + 1;
9710 sqlite3_free_table(result);
9712 attachment_tbl->attachment_id = rc;
9714 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9715 "INSERT INTO mail_attachment_tbl VALUES "
9716 "( ?" /* attachment_id */
9717 ", ?" /* attachment_name */
9718 ", ?" /* attachment_path */
9719 ", ?" /* attachment_size */
9721 ", ?" /* account_id */
9722 ", ?" /* mailbox_id */
9723 ", ?" /* attachment_save_status */
9724 ", ?" /* attachment_drm_type */
9725 ", ?" /* attachment_drm_method */
9726 ", ?" /* attachment_inline_content_status */
9727 ", ?" /* attachment_mime_type */
9728 #ifdef __ATTACHMENT_OPTI__
9735 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9736 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9737 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9739 _bind_stmt_field_data_int (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
9740 _bind_stmt_field_data_string(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
9741 _bind_stmt_field_data_string(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9742 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
9743 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
9744 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
9745 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
9746 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
9747 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
9748 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
9749 _bind_stmt_field_data_int (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
9750 _bind_stmt_field_data_string(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
9751 #ifdef __ATTACHMENT_OPTI__
9752 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
9753 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
9757 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9758 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9759 ("sqlite3_step fail:%d", rc));
9760 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9761 ("sqlite3_step fail:%d", rc));
9763 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9764 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
9766 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9768 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9769 ("sqlite3_exec fail:%d", rc));
9770 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9771 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9773 rc = sqlite3_changes(local_db_handle);
9775 EM_DEBUG_EXCEPTION(" no matched mail found...");
9776 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9783 if (hStmt != NULL) {
9784 rc = sqlite3_finalize(hStmt);
9785 if (rc != SQLITE_OK) {
9786 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
9787 error = EMAIL_ERROR_DB_FAILURE;
9791 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9794 if (err_code != NULL)
9797 EM_DEBUG_FUNC_END("ret [%d]", ret);
9801 INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
9803 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
9805 int rc, ret = false, field_idx = 0;
9806 int error = EMAIL_ERROR_NONE;
9807 DB_STMT hStmt = NULL;
9808 char sql_query_string[QUERY_SIZE] = {0, };
9810 if (!attachment_tbl) {
9811 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
9812 error = EMAIL_ERROR_INVALID_PARAM;
9816 sqlite3 *local_db_handle = emstorage_get_db_connection();
9818 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9820 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9821 "UPDATE mail_attachment_tbl SET "
9822 " attachment_name = ?"
9823 ", attachment_path = ?"
9824 ", attachment_size = ?"
9828 ", attachment_save_status = ?"
9829 ", attachment_drm_type = ?"
9830 ", attachment_drm_method = ?"
9831 ", attachment_inline_content_status = ? "
9832 ", attachment_mime_type = ? "
9833 " WHERE attachment_id = ?;");
9836 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9838 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9839 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9841 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
9842 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9843 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_size);
9844 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mail_id);
9845 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->account_id);
9846 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mailbox_id);
9847 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
9848 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
9849 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
9850 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
9851 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
9852 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_id);
9855 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9856 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9857 ("sqlite3_step fail:%d", rc));
9858 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9859 ("sqlite3_step fail:%d", rc));
9861 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9862 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
9864 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9866 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9867 ("sqlite3_exec fail:%d", rc));
9868 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9869 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9871 rc = sqlite3_changes(local_db_handle);
9873 EM_DEBUG_EXCEPTION(" no matched mail found...");
9874 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9881 if (hStmt != NULL) {
9882 rc = sqlite3_finalize(hStmt);
9883 if (rc != SQLITE_OK) {
9884 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
9885 error = EMAIL_ERROR_DB_FAILURE;
9889 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9892 if (err_code != NULL)
9895 EM_DEBUG_FUNC_END("ret [%d]", ret);
9899 INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
9901 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
9903 if (attachment_id < 0) {
9904 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
9905 if (err_code != NULL)
9906 *err_code = EMAIL_ERROR_INVALID_PARAM;
9910 int rc, ret = false;
9911 int error = EMAIL_ERROR_NONE;
9912 char sql_query_string[QUERY_SIZE] = {0, };
9914 sqlite3 *local_db_handle = emstorage_get_db_connection();
9916 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9918 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
9920 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9921 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9922 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9927 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9933 EM_DEBUG_FUNC_END("ret [%d]", ret);
9937 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
9939 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9940 int rc, ret = false;
9941 int error = EMAIL_ERROR_NONE;
9942 char sql_query_string[QUERY_SIZE] = {0, };
9943 sqlite3 *local_db_handle = NULL;
9946 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9947 if (err_code != NULL)
9948 *err_code = EMAIL_ERROR_INVALID_PARAM;
9952 local_db_handle = emstorage_get_db_connection();
9954 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9956 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9958 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9959 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9960 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9965 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9971 EM_DEBUG_FUNC_END("ret [%d]", ret);
9975 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
9977 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
9979 int error = EMAIL_ERROR_NONE;
9980 int rc, ret = false;
9981 char sql_query_string[QUERY_SIZE] = {0, };
9983 sqlite3 *local_db_handle = emstorage_get_db_connection();
9985 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9987 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
9989 if (account_id != ALL_ACCOUNT) /* '0' means all account */
9990 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
9992 if (mailbox) /* NULL means all mailbox_name */
9993 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_name = '%s'", account_id != ALL_ACCOUNT ? "AND" : "WHERE", mailbox);
9995 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9996 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9997 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10002 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10005 if (err_code != NULL)
10008 EM_DEBUG_FUNC_END("ret [%d]", ret);
10012 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
10014 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
10017 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
10018 EM_DEBUG_EXCEPTION(" attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
10019 if (err_code != NULL)
10020 *err_code = EMAIL_ERROR_INVALID_PARAM;
10024 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
10027 for (i = 0; i < count; i++) {
10028 EM_SAFE_FREE(p[i].attachment_name);
10029 EM_SAFE_FREE(p[i].attachment_path);
10030 EM_SAFE_FREE(p[i].attachment_mime_type);
10031 #ifdef __ATTACHMENT_OPTI__
10032 EM_SAFE_FREE(p[i].section);
10037 *attachment_tbl_list = NULL;
10040 if (err_code != NULL)
10041 *err_code = EMAIL_ERROR_NONE;
10042 EM_DEBUG_FUNC_END();
10048 INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
10050 EM_PROFILE_BEGIN(emStorageBeginTransaction);
10053 ENTER_CRITICAL_SECTION(_transactionBeginLock);
10055 /* wait for the trnasaction authority to be changed. */
10056 while (g_transaction) {
10057 EM_DEBUG_LOG(">>>>>>>> Wait for the transaction authority to be changed");
10061 /* take the transaction authority. */
10062 g_transaction = true;
10064 LEAVE_CRITICAL_SECTION(_transactionBeginLock);
10066 sqlite3 *local_db_handle = emstorage_get_db_connection();
10068 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
10069 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10070 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10071 if (ret == false && err_code != NULL)
10072 *err_code = EMAIL_ERROR_DB_FAILURE;
10074 EM_PROFILE_END(emStorageBeginTransaction);
10075 EM_DEBUG_FUNC_END("ret [%d]", ret);
10079 INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
10081 EM_DEBUG_FUNC_BEGIN();
10083 sqlite3 *local_db_handle = emstorage_get_db_connection();
10085 ENTER_CRITICAL_SECTION(_transactionEndLock);
10088 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
10089 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10090 /* release the transaction authority. */
10091 g_transaction = false;
10093 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10094 if (ret == false && err_code != NULL)
10095 *err_code = EMAIL_ERROR_DB_FAILURE;
10097 EM_DEBUG_FUNC_END("ret [%d]", ret);
10101 INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
10103 EM_DEBUG_FUNC_BEGIN();
10105 sqlite3 *local_db_handle = emstorage_get_db_connection();
10108 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
10110 ENTER_CRITICAL_SECTION(_transactionEndLock);
10111 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10112 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10114 /* release the transaction authority. */
10115 g_transaction = false;
10117 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10119 if (ret == false && err_code != NULL)
10120 *err_code = EMAIL_ERROR_DB_FAILURE;
10122 EM_DEBUG_FUNC_END("ret [%d]", ret);
10126 INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
10128 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
10130 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
10132 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
10134 if (err_code != NULL)
10135 *err_code = EMAIL_ERROR_INVALID_PARAM;
10141 int error = EMAIL_ERROR_NONE;
10142 int mail_count = 0;
10144 if (!emstorage_get_mail_count(account_id, mailbox->mailbox_name, &mail_count, NULL, true, &error)) {
10145 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
10150 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
10151 if (mail_count >= mailbox->mail_slot_size)
10162 if (err_code != NULL)
10165 EM_DEBUG_FUNC_END("ret [%d]", ret);
10169 INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
10171 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
10173 int rc, ret = false;
10174 int error = EMAIL_ERROR_NONE;
10175 char sql_query_string[QUERY_SIZE] = {0, };
10177 const email_db_object_t* tables = _g_db_tables;
10178 const email_db_object_t* indexes = _g_db_indexes;
10180 sqlite3 *local_db_handle = emstorage_get_db_connection();
10181 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10183 if (!emstorage_delete_dir(MAILHOME, &error)) {
10184 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
10189 mkdir(MAILHOME, DIRECTORY_PERMISSION);
10190 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10192 /* first clear index. */
10193 while (indexes->object_name) {
10194 if (indexes->data_flag) {
10195 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
10196 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10197 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10198 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10203 while (tables->object_name) {
10204 if (tables->data_flag) {
10205 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
10206 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10207 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10208 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10216 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10219 if (err_code != NULL)
10222 EM_DEBUG_FUNC_END("ret [%d]", ret);
10225 /*======================= DB File Utils =============================================*/
10226 #include <dirent.h>
10227 #include <sys/types.h>
10228 #define DIR_SEPERATOR "/"
10230 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
10232 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
10233 char delims[] = "/";
10234 char *result = NULL;
10236 result = strtok(file_name, delims);
10239 EM_DEBUG_LOG(">>>> Directory_name [ %s ]", result);
10241 EM_DEBUG_LOG(">>>> No Need to create Directory");
10246 INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10248 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], name_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, name_buf, err_code);
10249 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
10252 int error = EMAIL_ERROR_NONE;
10253 char *dir_name = NULL;
10254 char create_dir[1024]={0};
10255 char *temp_file = NULL;
10257 if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
10258 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p]", account_id, mail_id, atch_id, fname, name_buf);
10259 error = EMAIL_ERROR_INVALID_PARAM;
10263 sprintf(name_buf, "%s", MAILHOME);
10264 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, account_id);
10267 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10270 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10273 temp_file = EM_SAFE_STRDUP(fname);
10274 if (strstr(temp_file, "/")) {
10275 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
10280 sprintf(create_dir, "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
10281 EM_DEBUG_LOG(">>>>> DIR PATH [ %s ]", create_dir);
10282 mkdir(create_dir, DIRECTORY_PERMISSION);
10283 EM_SAFE_FREE(temp_file);
10287 EM_DEBUG_LOG(">>>>> fname [ %s ]", fname);
10288 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s", DIR_SEPERATOR, fname);
10291 EM_DEBUG_LOG(">>>>> name_buf [ %s ]", name_buf);
10296 EM_SAFE_FREE(temp_file);
10298 if (err_code != NULL)
10301 EM_PROFILE_END(profile_emstorage_get_save_name);
10302 EM_DEBUG_FUNC_END("ret [%d]", ret);
10306 INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10308 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, name_buf, err_code);
10310 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
10311 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p]", account_id, mail_id, atch_id, fname, name_buf);
10312 if (err_code != NULL)
10313 *err_code = EMAIL_ERROR_INVALID_PARAM;
10317 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10320 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10325 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10330 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
10332 EM_DEBUG_FUNC_END();
10336 INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
10338 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
10339 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
10341 int error = EMAIL_ERROR_NONE;
10345 if (account_id >= FIRST_ACCOUNT_ID) {
10346 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10348 if (stat(buf, &sbuf) == 0) {
10349 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10350 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10351 error = EMAIL_ERROR_SYSTEM_FAILURE;
10356 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10357 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10358 EM_DEBUG_EXCEPTION("mkdir failed l(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10359 error = EMAIL_ERROR_SYSTEM_FAILURE;
10366 if (account_id < FIRST_ACCOUNT_ID) {
10367 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10368 error = EMAIL_ERROR_INVALID_PARAM;
10372 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf), "%s%d", DIR_SEPERATOR, mail_id);
10374 if (stat(buf, &sbuf) == 0) {
10375 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10376 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10378 error = EMAIL_ERROR_SYSTEM_FAILURE;
10383 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10384 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10385 EM_DEBUG_EXCEPTION("mkdir failed l (Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10386 error = EMAIL_ERROR_SYSTEM_FAILURE;
10393 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
10394 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10396 error = EMAIL_ERROR_INVALID_PARAM;
10400 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
10402 if (stat(buf, &sbuf) == 0) {
10403 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10404 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10406 error = EMAIL_ERROR_SYSTEM_FAILURE;
10411 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10412 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10413 error = EMAIL_ERROR_SYSTEM_FAILURE;
10422 if (err_code != NULL)
10425 EM_PROFILE_END(profile_emcore_save_create_dir);
10426 EM_DEBUG_FUNC_END("ret [%d]", ret);
10430 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10432 EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
10433 EM_DEBUG_LOG("Using the fsync function");
10435 int error = EMAIL_ERROR_NONE;
10436 struct stat st_buf;
10445 if (!src_file || !dst_file) {
10446 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10448 error = EMAIL_ERROR_INVALID_PARAM;
10452 if (stat(src_file, &st_buf) < 0) {
10453 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
10455 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
10459 buf_size = st_buf.st_size;
10460 EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
10461 buf = (char *)calloc(1, buf_size+1);
10464 EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
10465 error = EMAIL_ERROR_OUT_OF_MEMORY;
10470 if ((fp_src = open(src_file, O_RDONLY))<0) { /*prevent 24474*/
10471 EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed [ %d ] - Error [ %s ]", src_file, errno, strerror(errno));
10472 error = EMAIL_ERROR_SYSTEM_FAILURE;
10476 if ((fp_dst = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) { /*prevent 24474*/
10477 EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s Failed [ %d ] - Error [ %s ]", dst_file, errno, strerror(errno));
10478 error = EMAIL_ERROR_SYSTEM_FAILURE;
10482 while ((nread = read(fp_src, buf, buf_size)) > 0) {
10483 if (nread > 0 && nread <= buf_size) {
10484 EM_DEBUG_LOG("Nread Value [%d]", nread);
10485 if ((nwritten = write(fp_dst, buf, nread)) != nread) {
10486 EM_DEBUG_EXCEPTION("fwrite failed...[%d] : [%s]", errno, strerror(errno));
10487 error = EMAIL_ERROR_UNKNOWN;
10490 EM_DEBUG_LOG("NWRITTEN [%d]", nwritten);
10498 if (fp_src>0) /*prevent 24474*/
10501 if (fp_dst>0) { /*prevent 24474*/
10503 EM_DEBUG_LOG("Before fsync");
10509 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
10512 if (err_code != NULL)
10514 EM_DEBUG_FUNC_END("ret [%d]", ret);
10517 /* create Directory if user has deleted [deepam.p@samsung.com] */
10518 INTERNAL_FUNC void emstorage_create_dir_if_delete()
10520 EM_DEBUG_FUNC_BEGIN();
10522 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
10523 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
10524 mkdir(MAILHOME, DIRECTORY_PERMISSION);
10525 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10527 EM_DEBUG_FUNC_END();
10529 static int _get_temp_file_name(char **filename, int *err_code)
10531 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
10534 int error = EMAIL_ERROR_NONE;
10536 if (filename == NULL) {
10537 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
10538 error = EMAIL_ERROR_INVALID_PARAM;
10542 char tempname[512] = {0x00, };
10545 gettimeofday(&tv, NULL);
10548 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
10550 char *p = EM_SAFE_STRDUP(tempname);
10552 EM_DEBUG_EXCEPTION(" strdup failed...");
10553 error = EMAIL_ERROR_OUT_OF_MEMORY;
10562 if (err_code != NULL)
10565 EM_DEBUG_FUNC_END("ret [%d]", ret);
10569 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
10571 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
10573 EM_IF_NULL_RETURN_VALUE(file_path, false);
10574 EM_IF_NULL_RETURN_VALUE(char_set, false);
10575 EM_IF_NULL_RETURN_VALUE(err_code, false);
10579 struct stat st_buf;
10581 char *low_char_set = NULL;
10582 char *match_str = NULL;
10585 int error = EMAIL_ERROR_NONE;
10586 int data_count_to_written = 0;
10587 char *temp_file_name = NULL;
10590 FILE* fp_src = NULL;
10591 FILE* fp_dest = NULL;
10595 if (stat(file_path, &st_buf) < 0) {
10596 EM_DEBUG_EXCEPTION(" stat(\"%s\") failed...", file_path);
10598 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
10602 buf_size = st_buf.st_size;
10604 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
10606 buf = (char *)calloc(1, buf_size+1);
10609 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
10613 if (!(fp_src = fopen(file_path, "rb"))) {
10614 EM_DEBUG_EXCEPTION(" file_path fopen failed - %s", file_path);
10616 error = EMAIL_ERROR_SYSTEM_FAILURE;
10620 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
10621 if (nread > 0 && nread <= buf_size) {
10622 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
10625 * 1.Add check for whether content type is there.
10626 * 2. If not based on the character set, Append it in File
10629 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
10631 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
10633 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
10635 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
10637 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
10639 EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
10641 match_str = strstr(buf, CONTENT_TYPE_DATA);
10642 EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
10644 if (match_str == NULL) {
10645 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
10646 if (fp_src !=NULL) {
10647 fclose(fp_src);fp_src = NULL;
10649 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
10650 EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
10651 buf1 = (char *)calloc(1, data_count_to_written);
10652 EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
10655 EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
10656 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
10658 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
10660 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
10662 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
10665 /* 1. Create a temporary file name */
10666 if (!_get_temp_file_name(&temp_file_name, &err)) {
10667 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
10668 if (err_code != NULL) *err_code = err;
10669 EM_SAFE_FREE(temp_file_name);
10672 EM_DEBUG_LOG(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
10674 /* Open the Temp file in Append mode */
10675 if (!(fp_dest = fopen(temp_file_name, "ab"))) {
10676 EM_DEBUG_EXCEPTION(" fopen failed - %s", temp_file_name);
10677 error = EMAIL_ERROR_SYSTEM_FAILURE;
10681 /* 2. write the Latest data */
10682 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
10684 if (nwritten > 0) {
10685 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
10687 /* 3. Append old data */
10688 nwritten = fwrite(buf, nread-1, 1, fp_dest);
10690 if (nwritten <= 0) {
10691 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
10692 error = EMAIL_ERROR_SYSTEM_FAILURE;
10696 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
10698 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
10699 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
10706 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
10707 error = EMAIL_ERROR_SYSTEM_FAILURE;
10714 EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
10724 EM_SAFE_FREE(buf1);
10725 EM_SAFE_FREE(low_char_set);
10727 if (fp_src != NULL) {
10732 if (fp_dest != NULL) {
10736 EM_DEBUG_FUNC_END("ret [%d]", ret);
10741 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10743 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
10746 int error = EMAIL_ERROR_NONE;
10748 if (src_file == NULL || dst_file == NULL) {
10749 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10750 error = EMAIL_ERROR_INVALID_PARAM;
10754 EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
10756 if (strcmp(src_file, dst_file) != 0) {
10757 if (rename(src_file, dst_file) != 0) {
10758 if (errno == EXDEV) { /* oldpath and newpath are not on the same mounted file system. (Linux permits a file system to be mounted at multiple points, but rename() */
10759 /* does not work across different mount points, even if the same file system is mounted on both.) */
10760 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
10761 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
10762 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
10766 EM_DEBUG_LOG("src[%s] removed", src_file);
10770 if (errno == ENOENT) {
10771 struct stat temp_file_stat;
10772 if (stat(src_file, &temp_file_stat) < 0)
10773 EM_DEBUG_EXCEPTION("no src file found [%s]", src_file);
10774 if (stat(dst_file, &temp_file_stat) < 0)
10775 EM_DEBUG_EXCEPTION("no dst file found [%s]", src_file);
10777 EM_DEBUG_EXCEPTION("no file found [%d]", errno);
10778 error = EMAIL_ERROR_FILE_NOT_FOUND;
10783 EM_DEBUG_EXCEPTION("rename failed [%d]", errno);
10784 error = EMAIL_ERROR_SYSTEM_FAILURE;
10791 EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
10797 if (err_code != NULL)
10800 EM_DEBUG_FUNC_END("ret [%d]", ret);
10804 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
10806 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
10809 int error = EMAIL_ERROR_NONE;
10811 if (src_file == NULL) {
10812 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
10814 error = EMAIL_ERROR_INVALID_PARAM;
10818 if (remove(src_file) != 0) {
10819 if (errno != ENOENT) {
10820 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
10822 error = EMAIL_ERROR_SYSTEM_FAILURE;
10826 EM_DEBUG_EXCEPTION(" no file found...");
10828 error = EMAIL_ERROR_FILE_NOT_FOUND;
10835 if (err_code != NULL)
10838 EM_DEBUG_FUNC_END("ret [%d]", ret);
10843 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
10845 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
10847 if (src_dir == NULL) {
10848 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
10850 if (err_code != NULL)
10851 *err_code = EMAIL_ERROR_INVALID_PARAM;
10855 int error = EMAIL_ERROR_NONE;
10862 dirp = opendir(src_dir);
10864 if (dirp == NULL) {
10865 if (errno == ENOENT) {
10866 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
10867 if (err_code != NULL)
10868 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10872 EM_DEBUG_EXCEPTION("opendir failed - %d", errno);
10873 if (err_code != NULL)
10874 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10879 while ((dp=readdir(dirp))) {
10880 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
10883 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
10885 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
10886 /* check directory */
10887 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
10888 /* recursive call */
10889 if (!emstorage_delete_dir(buf, &error)) {
10891 if (err_code != NULL)
10897 if (remove(buf) < 0) {
10898 EM_DEBUG_EXCEPTION("remove failed - %s", buf);
10900 if (err_code != NULL)
10901 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10907 EM_DEBUG_EXCEPTION("content does not exist...");
10912 EM_DEBUG_LOG("remove direcotory [%s]", src_dir);
10914 /* EM_DEBUG_FUNC_BEGIN(); */
10916 if (remove(src_dir) < 0) {
10917 EM_DEBUG_EXCEPTION("remove failed [%s]", src_dir);
10918 if (err_code != NULL)
10919 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10923 if (err_code != NULL)
10929 /* faizan.h@samsung.com */
10930 INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
10932 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
10933 int rc, ret = false;
10934 int error = EMAIL_ERROR_NONE;
10935 char sql_query_string[QUERY_SIZE] = {0, };
10937 int transaction = true;
10939 if (!old_server_uid || !new_server_uid) {
10940 EM_DEBUG_EXCEPTION("Invalid parameters");
10941 error = EMAIL_ERROR_INVALID_PARAM;
10944 sqlite3 *local_db_handle = emstorage_get_db_connection();
10946 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10948 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10949 "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
10950 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10952 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10953 ("sqlite3_exec fail:%d", rc));
10954 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10955 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10960 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10963 if (err_code != NULL)
10966 EM_DEBUG_FUNC_END("ret [%d]", ret);
10971 INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
10973 EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
10975 int rc, ret = false;
10976 int error = EMAIL_ERROR_NONE;
10977 char sql_query_string[QUERY_SIZE] = {0, };
10979 int transaction = true;
10981 if (!mail_id || !new_server_uid || !mbox_name) {
10982 EM_DEBUG_EXCEPTION("Invalid parameters");
10983 error = EMAIL_ERROR_INVALID_PARAM;
10986 sqlite3 *local_db_handle = emstorage_get_db_connection();
10987 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10990 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10991 "UPDATE mail_read_mail_uid_tbl SET s_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
10992 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10993 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10994 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10999 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11002 if (err_code != NULL)
11005 EM_DEBUG_FUNC_END("ret [%d]", ret);
11011 int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
11013 EM_DEBUG_FUNC_BEGIN();
11015 if ((!mail_id) ||(account_id <= 0 && account_id != -1)) {
11016 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
11017 if (err_code != NULL)
11018 *err_code = EMAIL_ERROR_INVALID_PARAM;
11024 int error = EMAIL_ERROR_NONE;
11025 DB_STMT hStmt = NULL;
11028 int transaction = false;
11029 char sql_query_string[QUERY_SIZE] = {0, };
11031 sqlite3 *local_db_handle = emstorage_get_db_connection();
11032 EMSTORAGE_START_READ_TRANSACTION(transaction);
11034 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11036 if (account_id == -1)
11037 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
11039 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 ORDER BY mail_id DESC", account_id);
11041 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11042 EM_DEBUG_LOG(" sqlite3_prepare hStmt = %p", hStmt);
11043 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11044 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11046 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11047 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11048 ("sqlite3_step fail:%d", rc));
11052 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11053 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11054 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11056 sqlite3_free_table(result);
11058 EM_DEBUG_EXCEPTION("no Mails found...");
11060 error= EMAIL_ERROR_MAIL_NOT_FOUND;
11064 _get_stmt_field_data_int(hStmt, &mailid, 0);
11065 EM_DEBUG_LOG("mailid [%d]", mailid);
11070 if (hStmt != NULL) {
11071 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11073 rc = sqlite3_finalize(hStmt);
11074 if (rc != SQLITE_OK) {
11075 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11077 error = EMAIL_ERROR_DB_FAILURE;
11080 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11083 if (mail_id != NULL)
11086 if (err_code != NULL)
11089 EM_DEBUG_FUNC_END("ret [%d]", ret);
11093 int setting_system_command(const char *command)
11096 int pid = 0, status = 0;
11097 char *const environ[] = { NULL };
11112 argv[2] = (char *)command;
11115 execve("/bin/sh", argv, environ);
11119 if (waitpid(pid, &status, 0) == -1) {
11120 if (errno != EINTR)
11132 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
11134 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
11136 if (!total_usage) {
11137 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
11139 if (err_code != NULL)
11140 *err_code = EMAIL_ERROR_INVALID_PARAM;
11145 int error = EMAIL_ERROR_NONE;
11146 char syscmd[256] = {0, };
11147 char line[256] = {0, };
11148 char *line_from_file = NULL;
11150 unsigned long total_diskusage = 0;
11152 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
11153 if (setting_system_command(syscmd) == -1) {
11154 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11156 error = EMAIL_ERROR_SYSTEM_FAILURE;
11160 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
11161 EM_DEBUG_LOG(" cmd : %s", syscmd);
11162 if (setting_system_command(syscmd) == -1) {
11163 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
11165 error = EMAIL_ERROR_SYSTEM_FAILURE;
11169 fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
11171 perror(SETTING_MEMORY_TEMP_FILE_PATH);
11173 error = EMAIL_ERROR_SYSTEM_FAILURE;
11177 line_from_file = fgets(line, sizeof(line), fp);
11179 if(line_from_file == NULL) {
11180 EM_DEBUG_EXCEPTION("fgets failed");
11181 error = EMAIL_ERROR_SYSTEM_FAILURE;
11184 total_diskusage = strtoul(line, NULL, 10);
11186 memset(syscmd, 0, sizeof(syscmd));
11187 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
11188 if (setting_system_command(syscmd) == -1) {
11189 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11190 error = EMAIL_ERROR_SYSTEM_FAILURE;
11194 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
11199 if (err_code != NULL)
11203 *total_usage = total_diskusage;
11207 if(fp) fclose(fp); /* prevent 32730 */
11209 EM_DEBUG_FUNC_END("ret [%d]", ret);
11214 INTERNAL_FUNC int emstorage_test(int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
11216 DB_STMT hStmt = NULL;
11218 int error = EMAIL_ERROR_NONE;
11220 char sql_query_string[QUERY_SIZE] = {0, };
11222 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11223 "INSERT INTO mail_tbl VALUES "
11224 "( ?" /* mail_id */
11225 ", ?" /* account_id */
11226 ", ?" /* mailbox_name */
11227 ", ?" /* mail_size */
11228 ", ?" /* server_mail_status */
11229 ", ?" /* server_mailbox_name */
11230 ", ?" /* server_mail_id */
11231 ", ?" /* reference_mail_id */
11232 ", ?" /* full_address_from */
11233 ", ?" /* full_address_reply */
11234 ", ?" /* full_address_to */
11235 ", ?" /* full_address_cc */
11236 ", ?" /* full_address_bcc */
11237 ", ?" /* full_address_return */
11238 ", ?" /* subject */
11239 ", ?" /* body_download_status */
11240 ", ?" /* file_path_plain */
11241 ", ?" /* file_path_html */
11242 ", ?" /* date_time */
11243 ", ?" /* flags_seen_field */
11244 ", ?" /* flags_deleted_field */
11245 ", ?" /* flags_flagged_field */
11246 ", ?" /* flags_answered_field */
11247 ", ?" /* flags_recent_field */
11248 ", ?" /* flags_draft_field */
11249 ", ?" /* flags_forwarded_field */
11250 ", ?" /* DRM_status */
11251 ", ?" /* priority */
11252 ", ?" /* save_status */
11253 ", ?" /* lock_status */
11254 ", ?" /* message_id */
11255 ", ?" /* report_status */
11256 ", ?" /* email_address_sender */
11257 ", ?" /* email_address_recipient */
11258 ", ?" /* attachment_count */
11259 ", ?" /* inline_content_count */
11260 ", ?" /* preview_text */
11261 ", ?" /* thread_id */
11262 ", ?" /* mailbox_type */
11263 ", ?" /* alias_sender */
11264 ", ?" /* alias_recipient */
11265 ", ?" /* thread_item_count */
11266 ", ?" /* meeting_request_status */
11267 ", ?" /* message_class */
11268 ", ?" /* digest_type */
11269 ", ?" /* smime_type */
11272 int transaction = true;
11273 sqlite3 *local_db_handle = emstorage_get_db_connection();
11275 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11278 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11279 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11280 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11282 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
11283 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
11284 _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_TBL, "OUTBOX", 0, MAILBOX_LEN_IN_MAIL_TBL);
11285 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
11286 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
11287 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
11288 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
11289 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
11290 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
11291 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
11292 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
11293 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
11294 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
11295 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
11296 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
11297 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
11298 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
11299 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, "<sender_name@sender_host.com>", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11300 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, "<recipient_name@recipient_host.com>", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11301 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11302 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11303 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
11304 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, MAILHOME"/7/348/UTF-8", 0, TEXT_1_LEN_IN_MAIL_TBL);
11305 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
11306 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
11307 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
11308 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
11309 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
11310 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
11311 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
11312 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
11313 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
11314 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
11315 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
11316 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
11317 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
11318 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
11319 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11320 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
11321 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11322 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
11323 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
11324 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
11325 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
11326 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
11327 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
11328 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
11330 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11331 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11332 ("sqlite3_step fail:%d", rc));
11333 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11334 ("sqlite3_step fail:%d", rc));
11338 if (hStmt != NULL) {
11339 rc = sqlite3_finalize(hStmt);
11340 if (rc != SQLITE_OK) {
11341 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11342 error = EMAIL_ERROR_DB_FAILURE;
11346 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11349 if (err_code != NULL)
11352 EM_DEBUG_FUNC_END("ret [%d]", ret);
11357 INTERNAL_FUNC int emstorage_get_max_mail_count()
11359 return EMAIL_MAIL_MAX_COUNT;
11362 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
11364 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(emstorage_mail_tbl_t *mail_tbl, int *thread_id, int *result_latest_mail_id_in_thread, int *thread_item_count)
11366 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], result_latest_mail_id_in_thread [%p], thread_item_count [%p]", mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
11367 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
11368 int rc = 0, query_size = 0, query_size_account = 0;
11369 int account_id = 0;
11370 int err_code = EMAIL_ERROR_NONE;
11371 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
11372 time_t latest_date_time = 0;
11373 time_t date_time = 0;
11374 char *mailbox_name = NULL, *subject = NULL;
11375 char *sql_query_string = NULL, *sql_account = NULL;
11376 char *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
11377 char *sql_format_account = " AND account_id = %d ";
11378 char *sql_format_order_by = " ORDER BY date_time DESC ";
11379 char **result = NULL;
11380 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
11381 sqlite3 *local_db_handle = emstorage_get_db_connection();
11383 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
11384 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
11385 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
11386 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
11388 account_id = mail_tbl->account_id;
11389 mailbox_name = mail_tbl->mailbox_name;
11390 subject = mail_tbl->subject;
11391 date_time = mail_tbl->date_time;
11393 EM_DEBUG_LOG("subject : %s", subject);
11395 if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
11396 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
11397 err_code = EMAIL_ERROR_UNKNOWN;
11398 result_thread_id = -1;
11402 if (EM_SAFE_STRLEN(stripped_subject) < 2) {
11403 result_thread_id = -1;
11407 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
11409 if (account_id > 0) {
11410 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
11411 sql_account = malloc(query_size_account);
11412 if (sql_account == NULL) {
11413 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
11414 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
11417 snprintf(sql_account, query_size_account, sql_format_account, account_id);
11420 /* prevent 34362 */
11421 query_size = strlen(sql_format) + strlen(stripped_subject) + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
11423 sql_query_string = malloc(query_size);
11425 if (sql_query_string == NULL) {
11426 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
11427 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
11431 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
11433 if (account_id > 0)
11434 strcat(sql_query_string, sql_account);
11436 strcat(sql_query_string, sql_format_order_by);
11437 strcat(sql_query_string, ";");
11439 EM_DEBUG_LOG("Query : %s", sql_query_string);
11441 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11443 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11444 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11446 EM_DEBUG_LOG("Result rows count : %d", count);
11449 result_thread_id = -1;
11451 _get_table_field_data_int (result, &result_thread_id, 3);
11452 _get_table_field_data_time_t(result, &latest_date_time, 4);
11453 _get_table_field_data_int (result, &latest_mail_id_in_thread, 5);
11455 if (latest_date_time < mail_tbl->date_time)
11456 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
11458 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
11459 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
11463 *thread_id = result_thread_id;
11464 *thread_item_count = count;
11466 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
11467 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
11468 EM_DEBUG_LOG("err_code : %d", err_code);
11470 EM_SAFE_FREE(sql_account);
11471 EM_SAFE_FREE(sql_query_string);
11473 sqlite3_free_table(result);
11475 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
11481 INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
11483 EM_DEBUG_FUNC_BEGIN();
11485 int count = 0, ret = false;
11486 int error = EMAIL_ERROR_NONE;
11487 emstorage_mail_tbl_t *p_data_tbl = NULL;
11488 char conditional_clause[QUERY_SIZE] = {0, };
11490 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
11492 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
11493 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
11495 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
11496 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
11501 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
11507 *mail_tbl = p_data_tbl;
11508 else if (p_data_tbl != NULL)
11509 emstorage_free_mail(&p_data_tbl, 1, NULL);
11511 if (err_code != NULL)
11514 EM_DEBUG_FUNC_END("ret [%d]", ret);
11519 INTERNAL_FUNC int emstorage_get_sender_list(int account_id, const char *mailbox_name, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count, int *err_code)
11521 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_name [%p], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
11522 , account_id , mailbox_name , search_type , search_value , sorting , sender_list, sender_count, err_code);
11524 if ((!sender_list) ||(!sender_count)) {
11525 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11526 if (err_code != NULL)
11527 *err_code = EMAIL_ERROR_INVALID_PARAM;
11531 int rc = -1, ret = false;
11532 int error = EMAIL_ERROR_NONE;
11534 int i, col_index = 0;
11535 int read_count = 0;
11536 email_sender_list_t *p_sender_list = NULL;
11537 char sql_query_string[QUERY_SIZE] = {0, };
11538 char **result = NULL;
11539 sqlite3 *local_db_handle = emstorage_get_db_connection();
11541 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11542 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
11547 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE UPPER(mailbox_name) = UPPER('%s') ", mailbox_name);
11548 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
11549 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_type not in (3, 5, 7, 8) ");
11552 /* '0' (ALL_ACCOUNT) means all account */
11553 if (account_id > ALL_ACCOUNT)
11554 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d ", account_id);
11556 if (search_value) {
11557 switch (search_type) {
11558 case EMAIL_SEARCH_FILTER_SUBJECT:
11559 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11560 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
11562 case EMAIL_SEARCH_FILTER_SENDER:
11563 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11564 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11565 ") ", search_value);
11567 case EMAIL_SEARCH_FILTER_RECIPIENT:
11568 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11569 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11570 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11571 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11572 ") ", search_value, search_value, search_value);
11574 case EMAIL_SEARCH_FILTER_ALL:
11575 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11576 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
11577 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11578 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11579 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11580 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11583 ")", search_value, search_value, search_value, search_value, search_value);
11589 /* sorting option is not available now. The order of sender list is ascending order by display name */
11590 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
11591 "GROUP BY email_address_sender "
11592 "ORDER BY UPPER(alias_sender) ");
11594 EM_DEBUG_LOG("query[%s]", sql_query_string);
11596 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
11597 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11598 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11600 EM_DEBUG_LOG("Count of Sender [%d]", count);
11602 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
11603 EM_DEBUG_EXCEPTION("em_malloc failed...");
11604 error = EMAIL_ERROR_OUT_OF_MEMORY;
11610 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
11611 for (i = 0; i < count; i++) {
11612 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
11613 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
11614 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
11615 _get_table_field_data_int(result, &(read_count), col_index++);
11616 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
11618 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
11620 sqlite3_free_table(result);
11627 *sender_list = p_sender_list;
11628 *sender_count = count;
11629 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
11634 if (err_code != NULL)
11637 EM_DEBUG_FUNC_END("ret [%d]", ret);
11641 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
11643 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
11645 int err = EMAIL_ERROR_NONE;
11648 if (!sender_list || !*sender_list) {
11649 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
11650 err = EMAIL_ERROR_INVALID_PARAM;
11654 email_sender_list_t* p = *sender_list;
11657 for (; i < count; i++) {
11658 EM_SAFE_FREE(p[i].address);
11659 EM_SAFE_FREE(p[i].display_name);
11663 *sender_list = NULL;
11670 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
11672 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
11674 int err = EMAIL_ERROR_NONE;
11675 email_address_info_t *p_address_info = NULL;
11676 GList *list = NULL;
11677 GList *node = NULL;
11680 if (!address_info_list || !*address_info_list) {
11681 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
11682 err = EMAIL_ERROR_INVALID_PARAM;
11686 /* delete GLists */
11687 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
11689 case EMAIL_ADDRESS_TYPE_FROM:
11690 list = (*address_info_list)->from;
11692 case EMAIL_ADDRESS_TYPE_TO:
11693 list = (*address_info_list)->to;
11695 case EMAIL_ADDRESS_TYPE_CC:
11696 list = (*address_info_list)->cc;
11698 case EMAIL_ADDRESS_TYPE_BCC:
11699 list = (*address_info_list)->bcc;
11703 /* delete dynamic-allocated memory for each item */
11704 node = g_list_first(list);
11705 while (node != NULL) {
11706 p_address_info = (email_address_info_t*)node->data;
11707 EM_SAFE_FREE(p_address_info->address);
11708 EM_SAFE_FREE(p_address_info->display_name);
11709 EM_SAFE_FREE(node->data);
11711 node = g_list_next(node);
11715 EM_SAFE_FREE(*address_info_list);
11716 *address_info_list = NULL;
11718 EM_DEBUG_FUNC_END("err [%d]", err);
11721 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
11723 INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
11725 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
11727 if (!local_activity || !activity_id) {
11728 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
11729 if (err_code != NULL)
11730 *err_code = EMAIL_ERROR_INVALID_PARAM;
11736 int error = EMAIL_ERROR_NONE;
11739 char sql_query_string[QUERY_SIZE] = {0, };
11740 DB_STMT hStmt = NULL;
11741 sqlite3 *local_db_handle = emstorage_get_db_connection();
11742 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11743 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11744 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11745 "INSERT INTO mail_partial_body_activity_tbl VALUES "
11747 "? " /* Account ID */
11748 ",?" /* Local Mail ID */
11749 ",?" /* Server mail ID */
11750 ",?" /* Activity ID */
11751 ",?" /* Activity type*/
11752 ",?" /* Mailbox ID*/
11753 ",?" /* Mailbox name*/
11756 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
11757 char **result = NULL;
11760 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11762 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11763 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11765 if (NULL==result[1]) rc = 1;
11766 else rc = atoi(result[1])+1;
11767 sqlite3_free_table(result);
11770 *activity_id = local_activity->activity_id = rc;
11772 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
11773 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
11775 if (local_activity->mailbox_id)
11776 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
11779 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11780 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11781 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11783 EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
11785 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
11786 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
11787 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
11788 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
11789 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
11790 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
11791 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
11794 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11796 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11797 ("sqlite3_step fail:%d", rc));
11798 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11799 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
11805 if (hStmt != NULL) {
11806 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11808 rc = sqlite3_finalize(hStmt);
11810 if (rc != SQLITE_OK) {
11811 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11812 error = EMAIL_ERROR_DB_FAILURE;
11816 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11818 if (err_code != NULL)
11821 EM_DEBUG_FUNC_END("ret [%d]", ret);
11826 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
11828 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
11830 if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
11831 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
11832 if (err_code != NULL)
11833 *err_code = EMAIL_ERROR_INVALID_PARAM;
11838 int error = EMAIL_ERROR_NONE;
11840 int i = 0, rc = -1;
11841 int *mbox_list = NULL;
11842 DB_STMT hStmt = NULL;
11843 char sql_query_string[QUERY_SIZE] = {0, };
11845 sqlite3 *local_db_handle = emstorage_get_db_connection();
11847 EMSTORAGE_START_READ_TRANSACTION(transaction);
11849 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11850 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
11853 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
11854 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11855 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11857 *count = atoi(result[1]);
11858 sqlite3_free_table(result);
11861 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
11862 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11866 EM_DEBUG_LOG("Mailbox count = %d", *count);
11868 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11870 /* SNPRINTF(g_sql_query, sizeof(g_sql_query), "SELECT distinct mailbox_name FROM mail_partial_body_activity_tbl WHERE account_id = %d order by activity_id", account_id); */
11871 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
11873 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
11876 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11878 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
11879 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11880 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11883 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11884 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11885 ("sqlite3_step fail:%d", rc));
11887 if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
11888 EM_DEBUG_EXCEPTION(" em_malloc failed...");
11889 error = EMAIL_ERROR_OUT_OF_MEMORY;
11893 memset(mbox_list, 0x00, sizeof(int) * (*count));
11895 for (i = 0; i < (*count); i++) {
11896 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
11897 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11898 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
11899 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11900 ("sqlite3_step fail:%d", rc));
11901 EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
11908 *mailbox_list = mbox_list;
11910 EM_SAFE_FREE(mbox_list);
11912 if (hStmt != NULL) {
11913 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11915 rc = sqlite3_finalize(hStmt);
11917 if (rc != SQLITE_OK) {
11918 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
11919 error = EMAIL_ERROR_DB_FAILURE;
11923 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11925 if (err_code != NULL)
11927 EM_DEBUG_FUNC_END("ret [%d]", ret);
11932 INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
11934 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
11936 if (NULL == &account_list || NULL == count) {
11937 EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
11938 if (err_code != NULL)
11939 *err_code = EMAIL_ERROR_INVALID_PARAM;
11944 int error = EMAIL_ERROR_NONE;
11947 int i = 0, rc = -1;
11948 int *result_account_list = NULL;
11949 DB_STMT hStmt = NULL;
11950 char sql_query_string[QUERY_SIZE] = {0, };
11952 sqlite3 *local_db_handle = emstorage_get_db_connection();
11954 EMSTORAGE_START_READ_TRANSACTION(transaction);
11957 sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
11960 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11961 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11962 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11964 *count = atoi(result[1]);
11965 sqlite3_free_table(result);
11968 EM_DEBUG_EXCEPTION("no account found...");
11969 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11974 EM_DEBUG_LOG("Account count [%d]", *count);
11976 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11978 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
11980 EM_DEBUG_LOG("Query [%s]", sql_query_string);
11983 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11985 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
11986 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11987 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11990 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11991 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11992 ("sqlite3_step fail:%d", rc));
11994 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
11995 EM_DEBUG_EXCEPTION(" em_malloc failed...");
11996 error = EMAIL_ERROR_OUT_OF_MEMORY;
12000 memset(result_account_list, 0x00, sizeof(int) * (*count));
12002 for (i = 0; i < (*count); i++) {
12003 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
12005 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12006 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12007 ("sqlite3_step fail:%d", rc));
12008 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
12015 *account_list = result_account_list;
12017 EM_SAFE_FREE(result_account_list);
12019 if (hStmt != NULL) {
12020 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12022 rc = sqlite3_finalize(hStmt);
12024 if (rc != SQLITE_OK) {
12025 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12026 error = EMAIL_ERROR_DB_FAILURE;
12030 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12032 if (err_code != NULL)
12034 EM_DEBUG_FUNC_END("ret [%d]", ret);
12040 INTERNAL_FUNC int emstorage_get_pbd_activity_data(int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
12042 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
12044 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
12045 EM_DEBUG_EXCEPTION("account_id[%d], email_event_partial_body_thd[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
12047 if (err_code != NULL)
12048 *err_code = EMAIL_ERROR_INVALID_PARAM;
12055 int error = EMAIL_ERROR_NONE;
12057 DB_STMT hStmt = NULL;
12058 email_event_partial_body_thd* event_list = NULL;
12059 char sql_query_string[QUERY_SIZE] = {0, };
12061 sqlite3 *local_db_handle = emstorage_get_db_connection();
12063 EMSTORAGE_START_READ_TRANSACTION(transaction);
12065 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12066 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
12069 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12070 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12071 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12073 *count = atoi(result[1]);
12074 sqlite3_free_table(result);
12076 EM_DEBUG_LOG("Query = [%s]", sql_query_string);
12079 EM_DEBUG_EXCEPTION("No matched activity found in mail_partial_body_activity_tbl");
12081 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12085 EM_DEBUG_LOG("Activity Count = %d", *count);
12087 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12088 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
12090 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12093 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12095 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12096 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12097 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12100 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12101 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12102 ("sqlite3_step fail:%d", rc));
12104 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
12105 EM_DEBUG_EXCEPTION("Malloc failed");
12107 error = EMAIL_ERROR_OUT_OF_MEMORY;
12110 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
12112 for (i=0; i < (*count); i++) {
12113 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12114 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12115 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12116 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12117 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12118 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12119 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12121 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12122 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
12123 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12124 ("sqlite3_step fail:%d", rc));
12126 event_list[i].event_type = 0;
12133 *event_start = event_list;
12135 EM_SAFE_FREE(event_list);
12138 if (hStmt != NULL) {
12139 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12141 rc = sqlite3_finalize(hStmt);
12143 if (rc != SQLITE_OK) {
12144 EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
12145 error = EMAIL_ERROR_DB_FAILURE;
12149 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12151 if (err_code != NULL)
12154 EM_DEBUG_FUNC_END("ret [%d]", ret);
12160 INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
12162 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
12165 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
12166 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
12168 if (err_code != NULL)
12169 *err_code = EMAIL_ERROR_INVALID_PARAM;
12175 int error = EMAIL_ERROR_NONE;
12176 char sql_query_string[QUERY_SIZE] = {0, };
12178 sqlite3 *local_db_handle = emstorage_get_db_connection();
12180 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12181 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12183 if (activity_id == 0)
12184 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
12186 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
12188 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12190 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12191 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12192 ("sqlite3_exec fail:%d", rc));
12193 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12194 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12196 /* validate activity existence */
12197 rc = sqlite3_changes(local_db_handle);
12199 EM_DEBUG_EXCEPTION("No matching activity found");
12200 error = EMAIL_ERROR_DATA_NOT_FOUND;
12209 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12212 if (err_code != NULL)
12215 EM_DEBUG_FUNC_END("ret [%d]", ret);
12219 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
12221 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12223 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
12224 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12225 if (err_code != NULL)
12226 *err_code = EMAIL_ERROR_INVALID_PARAM;
12231 int error = EMAIL_ERROR_NONE;
12232 char sql_query_string[QUERY_SIZE] = {0, };
12234 DB_STMT hStmt = NULL;
12236 sqlite3 *local_db_handle = emstorage_get_db_connection();
12238 EMSTORAGE_START_READ_TRANSACTION(transaction);
12239 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12241 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
12243 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12246 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12247 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
12248 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12249 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12252 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12253 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12254 ("sqlite3_step fail:%d", rc));
12256 _get_stmt_field_data_int(hStmt, activity_count, 0);
12258 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12264 if (hStmt != NULL) {
12265 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12266 rc = sqlite3_finalize(hStmt);
12268 if (rc != SQLITE_OK) {
12269 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12270 error = EMAIL_ERROR_DB_FAILURE;
12272 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12275 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12278 if (err_code != NULL)
12281 EM_DEBUG_FUNC_END("ret [%d]", ret);
12286 INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
12288 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
12290 if (NULL == activity_count || NULL == err_code) {
12291 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
12292 if (err_code != NULL)
12293 *err_code = EMAIL_ERROR_INVALID_PARAM;
12298 int error = EMAIL_ERROR_NONE;
12299 DB_STMT hStmt = NULL;
12300 char sql_query_string[QUERY_SIZE] = {0, };
12302 sqlite3 *local_db_handle = emstorage_get_db_connection();
12304 EMSTORAGE_START_READ_TRANSACTION(transaction);
12305 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12307 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
12309 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12312 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12313 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
12314 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12315 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12318 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12319 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12320 ("sqlite3_step fail:%d", rc));
12322 _get_stmt_field_data_int(hStmt, activity_count, 0);
12324 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12331 if (hStmt != NULL) {
12332 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12334 rc = sqlite3_finalize(hStmt);
12336 if (rc != SQLITE_OK) {
12337 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12338 error = EMAIL_ERROR_DB_FAILURE;
12340 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12343 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12345 if (err_code != NULL)
12348 EM_DEBUG_FUNC_END("ret [%d]", ret);
12352 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
12354 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12355 if (account_id < FIRST_ACCOUNT_ID) {
12356 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12357 if (err_code != NULL)
12358 *err_code = EMAIL_ERROR_INVALID_PARAM;
12364 int error = EMAIL_ERROR_NONE;
12365 char sql_query_string[QUERY_SIZE] = {0, };
12367 sqlite3 *local_db_handle = emstorage_get_db_connection();
12368 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12369 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12370 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
12372 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12374 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12375 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12376 ("sqlite3_exec fail:%d", rc));
12377 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12378 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12381 rc = sqlite3_changes(local_db_handle);
12383 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
12384 error = EMAIL_ERROR_DATA_NOT_FOUND;
12393 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12395 if (err_code != NULL)
12398 EM_DEBUG_FUNC_END("ret [%d]", ret);
12402 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
12403 if a mail is moved before its partial body is downloaded.Currently not used but should be used if mail move from server is enabled*/
12405 INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12407 EM_DEBUG_FUNC_BEGIN("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12409 int rc = -1, ret = false;
12410 int error = EMAIL_ERROR_NONE;
12411 char sql_query_string[QUERY_SIZE] = {0, };
12413 int transaction = true;
12415 if (!old_server_uid || !new_server_uid || !mbox_name) {
12416 EM_DEBUG_EXCEPTION("Invalid parameters");
12417 error = EMAIL_ERROR_INVALID_PARAM;
12421 sqlite3 *local_db_handle = emstorage_get_db_connection();
12422 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12423 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12424 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12425 "UPDATE mail_partial_body_activity_tbl SET server_mail_id = %s , mailbox_name=\'%s\' WHERE server_mail_id = %s ", new_server_uid, mbox_name, old_server_uid);
12427 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12429 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12430 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12431 ("sqlite3_exec fail:%d", rc));
12432 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12433 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12435 rc = sqlite3_changes(local_db_handle);
12437 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12438 error = EMAIL_ERROR_DATA_NOT_FOUND;
12446 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12448 if (err_code != NULL)
12451 EM_DEBUG_FUNC_END("ret [%d]", ret);
12456 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
12458 EM_DEBUG_FUNC_BEGIN("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
12461 int error = EMAIL_ERROR_NONE;
12463 FILE* fp_dst = NULL;
12465 if (!data_string || !dst_file_name) {
12466 EM_DEBUG_EXCEPTION("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
12467 error = EMAIL_ERROR_INVALID_PARAM;
12471 fp_dst = fopen(dst_file_name, "w");
12474 EM_DEBUG_EXCEPTION("fopen failed - %s", dst_file_name);
12476 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
12478 error = EMAIL_ERROR_SYSTEM_FAILURE;
12482 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
12483 EM_DEBUG_EXCEPTION("fwrite failed...");
12484 error = EMAIL_ERROR_UNKNOWN;
12492 if (fp_dst != NULL)
12495 if (err_code != NULL)
12498 EM_DEBUG_FUNC_END("ret [%d]", ret);
12502 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
12506 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
12508 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12510 EM_DEBUG_FUNC_BEGIN();
12513 int error = EMAIL_ERROR_NONE;
12514 char sql_query_string[QUERY_SIZE] = {0, };
12516 int transaction = true;
12518 if (!old_server_uid || !new_server_uid || !mbox_name) {
12519 EM_DEBUG_EXCEPTION("Invalid parameters");
12520 error = EMAIL_ERROR_INVALID_PARAM;
12524 EM_DEBUG_LOG("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12526 sqlite3 *local_db_handle = emstorage_get_db_connection();
12527 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12530 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12531 "UPDATE mail_read_mail_uid_tbl SET s_uid=\'%s\' , mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE s_uid=%s ", new_server_uid, mbox_name, mbox_name, old_server_uid);
12533 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12535 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12536 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12537 ("sqlite3_exec fail:%d", rc));
12538 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12539 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12542 rc = sqlite3_changes(local_db_handle);
12545 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12546 error = EMAIL_ERROR_DATA_NOT_FOUND;
12555 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12558 if (err_code != NULL)
12561 EM_DEBUG_FUNC_END("ret [%d]", ret);
12568 * @fn emstorage_get_id_set_from_mail_ids(int mail_ids[], int mail_id_count, email_id_set_t** server_uids, int *id_set_count, int *err_code);
12569 * Prepare an array of mail_id and corresponding server mail id.
12571 *@author h.gahlaut@samsung.com
12572 * @param[in] mail_ids Specifies the comma separated string of mail_ids. Maximaum size of string should be less than or equal to (QUERY_SIZE - 88)
12573 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
12574 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
12575 * @param[out] id_set_count Returns the no. of cells in idset array i.e. no. of sets of mail_ids and server_mail_ids
12576 * @param[out] err_code Returns the error code.
12577 * @remarks An Example of Query to be exexuted in this API:
12578 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
12579 * @return This function returns true on success or false on failure.
12582 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_set_t** idset, int *id_set_count, int *err_code)
12584 EM_DEBUG_FUNC_BEGIN();
12585 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
12587 int error = EMAIL_ERROR_NONE;
12589 email_id_set_t* p_id_set = NULL;
12591 const int buf_size = QUERY_SIZE;
12592 char sql_query_string[QUERY_SIZE] = {0, };
12593 int space_left_in_query_buffer = buf_size;
12596 char *server_mail_id = NULL;
12597 char **result = NULL;
12601 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
12602 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]", mail_ids, idset, id_set_count);
12603 error = EMAIL_ERROR_INVALID_PARAM;
12607 sqlite3 *local_db_handle = emstorage_get_db_connection();
12609 SNPRINTF(sql_query_string, space_left_in_query_buffer, "SELECT local_uid, s_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY s_uid", mail_ids);
12611 EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
12613 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12614 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12615 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12616 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12618 EM_DEBUG_LOG(" Count of mails [%d ]", count);
12621 EM_DEBUG_EXCEPTION("Can't find proper mail");
12622 error = EMAIL_ERROR_DATA_NOT_FOUND;
12627 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
12628 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12629 error = EMAIL_ERROR_OUT_OF_MEMORY;
12635 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
12636 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
12637 for (i = 0; i < count; i++) {
12638 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
12639 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
12640 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
12641 EM_SAFE_FREE(server_mail_id);
12643 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
12644 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
12646 sqlite3_free_table(result);
12655 *id_set_count = count;
12656 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
12659 EM_SAFE_FREE(p_id_set);
12663 if (err_code != NULL)
12666 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
12668 EM_DEBUG_FUNC_END("ret [%d]", ret);
12676 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
12678 EM_DEBUG_FUNC_BEGIN();
12679 int rc, ret = true, transaction = true;
12680 int error = EMAIL_ERROR_NONE;
12681 char sql_query_string[QUERY_SIZE] = {0, };
12683 sqlite3 *local_db_handle = emstorage_get_db_connection();
12684 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12686 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
12688 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12689 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12690 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12692 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
12694 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12695 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12696 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12699 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
12701 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12702 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12703 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12709 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12713 EM_DEBUG_FUNC_END("ret [%d]", ret);
12717 INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbox_id, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
12719 EM_DEBUG_FUNC_BEGIN("account_id[%d], dest_mailbox_id[%d] rule[%p], filtered_mail_id_list[%p], count_of_mails[%p], err_code[%p]", account_id, dest_mailbox_id, rule, filtered_mail_id_list, count_of_mails, err_code);
12721 if ((account_id <= 0) || (dest_mailbox_id <= 0) || (!rule) || (!rule->value)|| (!filtered_mail_id_list) || (!count_of_mails)) {
12722 EM_DEBUG_EXCEPTION("Invalid Parameter");
12724 if (err_code != NULL)
12725 *err_code = EMAIL_ERROR_INVALID_PARAM;
12729 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
12730 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
12731 char **result = NULL, *where_pararaph = NULL;
12732 char sql_query_string[QUERY_SIZE] = {0, };
12734 sqlite3 *local_db_handle = emstorage_get_db_connection();
12736 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
12738 EM_DEBUG_LOG("rule->value [%s]", rule->value);
12740 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 100;
12741 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
12743 if (where_pararaph == NULL) {
12744 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
12745 error = EMAIL_ERROR_OUT_OF_MEMORY;
12749 memset(where_pararaph, 0, sizeof(char) * where_pararaph_length);
12751 EM_DEBUG_LOG("rule->type [%d], rule->flag2[%d]", rule->type, rule->flag2);
12753 if (rule->type == EMAIL_FILTER_FROM) {
12754 if (rule->flag2 == RULE_TYPE_INCLUDES)
12755 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND full_address_from like \'%%%s%%\'", account_id, rule->value);
12756 else /* RULE_TYPE_EXACTLY */
12757 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND full_address_from = \'%s\'", account_id, rule->value);
12759 else if (rule->type == EMAIL_FILTER_SUBJECT) {
12760 if (rule->flag2 == RULE_TYPE_INCLUDES)
12761 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject like \'%%%s%%\'", account_id, rule->value);
12762 else /* RULE_TYPE_EXACTLY */
12763 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject = \'%s\'", account_id, rule->value);
12766 error = EMAIL_ERROR_INVALID_PARAM;
12767 EM_DEBUG_EXCEPTION("rule->type is invald");
12771 /* prevent 34361 */
12772 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12773 strcat(sql_query_string, where_pararaph);
12775 EM_DEBUG_LOG("query[%s]", sql_query_string);
12777 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12778 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12779 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12780 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12782 EM_DEBUG_LOG("Count of mails [%d]", count);
12785 mail_list = malloc(sizeof(int) * count);
12786 if (mail_list == NULL) {
12787 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
12788 error = EMAIL_ERROR_OUT_OF_MEMORY;
12794 for (i = 0; i < count; i++)
12795 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
12797 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = 5 ", dest_mailbox_id);
12799 /* prevent 34361 */
12800 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12801 strcat(sql_query_string, where_pararaph);
12803 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12804 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12805 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12813 *filtered_mail_id_list = mail_list;
12815 EM_SAFE_FREE(mail_list);
12817 sqlite3_free_table(result);
12822 EM_SAFE_FREE(where_pararaph);
12824 if (ret && count_of_mails)
12825 *count_of_mails = count;
12827 if (err_code != NULL)
12830 EM_DEBUG_FUNC_END("ret [%d]", ret);
12834 #define EMAIL_SLOT_UNIT 25
12836 INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
12838 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p] new_slot_size[%d], err_code[%p]", account_id, mailbox_id, new_slot_size, err_code);
12839 int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
12840 int where_pararaph_length = 0;
12841 char *where_pararaph = NULL;
12842 char sql_query_string[QUERY_SIZE] = {0, };
12844 sqlite3 *local_db_handle = emstorage_get_db_connection();
12846 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
12848 if (new_slot_size > 0)
12849 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
12850 else if (new_slot_size == 0)
12851 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
12853 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
12857 where_pararaph_length = 80;
12859 where_pararaph_length = 50;
12861 if (new_slot_size == 0)
12862 where_pararaph_length += 70;
12864 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
12865 if (where_pararaph == NULL) {
12866 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
12867 err = EMAIL_ERROR_OUT_OF_MEMORY;
12870 memset(where_pararaph, 0x00, where_pararaph_length);
12872 if (account_id > ALL_ACCOUNT) {
12875 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
12877 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
12880 if (new_slot_size == 0)
12881 SNPRINTF(where_pararaph + EM_SAFE_STRLEN(where_pararaph), where_pararaph_length - EM_SAFE_STRLEN(where_pararaph), " %s total_mail_count_on_server > mail_slot_size ", (and ? "AND" : "WHERE"));
12883 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
12884 strcat(sql_query_string, where_pararaph);
12886 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
12887 err = EMAIL_ERROR_OUT_OF_MEMORY;
12891 EM_DEBUG_LOG("query[%s]", sql_query_string);
12893 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12894 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12895 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12901 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
12905 EM_SAFE_FREE(where_pararaph);
12907 if (err_code != NULL)
12910 EM_DEBUG_FUNC_END("ret [%d]", ret);
12914 INTERNAL_FUNC int emstorage_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
12916 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], transaction[%d], err_code[%p]", account_id, input_mailbox_id, meeting_req, transaction, err_code);
12918 if (!meeting_req || meeting_req->mail_id <= 0) {
12920 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
12922 if (err_code != NULL)
12923 *err_code = EMAIL_ERROR_INVALID_PARAM;
12930 int error = EMAIL_ERROR_NONE;
12931 DB_STMT hStmt = NULL;
12932 char sql_query_string[QUERY_SIZE] = {0, };
12934 time_t temp_unix_time = 0;
12936 sqlite3 *local_db_handle = emstorage_get_db_connection();
12937 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12939 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12940 "INSERT INTO mail_meeting_tbl VALUES "
12941 "( ?" /* mail_id */
12942 ", ?" /* account_id */
12943 ", ?" /* mailbox_id */
12944 ", ?" /* meeting_response */
12945 ", ?" /* start_time */
12946 ", ?" /* end_time */
12947 ", ?" /* location */
12948 ", ?" /* global_object_id */
12950 ", ?" /* standard_name */
12951 ", ?" /* standard_time_start_date */
12952 ", ?" /* standard_biad */
12953 ", ?" /* daylight_name */
12954 ", ?" /* daylight_time_start_date */
12955 ", ?" /* daylight_bias */
12958 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12959 if (rc != SQLITE_OK) {
12960 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
12961 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
12963 error = EMAIL_ERROR_DB_FAILURE;
12969 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
12970 EM_DEBUG_LOG(">>>>> account_id[%d]", account_id);
12971 EM_DEBUG_LOG(">>>>> mailbox_name[%s]", mailbox_name);
12972 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
12973 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
12974 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
12975 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
12976 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
12977 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
12978 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
12979 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
12980 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
12981 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
12982 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
12983 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
12985 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
12986 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
12987 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
12988 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
12990 temp_unix_time = timegm(&(meeting_req->start_time));
12991 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12992 temp_unix_time = timegm(&(meeting_req->end_time));
12993 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12995 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
12996 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
12998 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
12999 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13000 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13001 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13002 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13004 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13005 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13006 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13007 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13010 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13011 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13012 ("sqlite3_step fail:%d", rc));
13013 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13014 ("sqlite3_step fail:%d", rc));
13019 if (hStmt != NULL) {
13020 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13022 rc = sqlite3_finalize(hStmt);
13023 if (rc != SQLITE_OK) {
13024 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13025 error = EMAIL_ERROR_DB_FAILURE;
13029 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13032 if (err_code != NULL)
13035 EM_DEBUG_FUNC_END("ret [%d]", ret);
13039 INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
13041 EM_DEBUG_FUNC_BEGIN();
13046 int error = EMAIL_ERROR_NONE;
13047 email_meeting_request_t *p_temp_meeting_req = NULL;
13049 time_t temp_unix_time;
13051 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
13053 DB_STMT hStmt = NULL;
13054 char sql_query_string[QUERY_SIZE] = {0, };
13056 sqlite3 *local_db_handle = emstorage_get_db_connection();
13057 EMSTORAGE_START_READ_TRANSACTION(transaction);
13059 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13060 "SELECT mail_id, meeting_response, start_time, end_time, location, global_object_id, offset, standard_name, standard_time_start_date, standard_bias, daylight_name, daylight_time_start_date, daylight_bias "
13061 " FROM mail_meeting_tbl "
13062 " WHERE mail_id = %d", mail_id);
13063 EM_DEBUG_LOG("sql : %s ", sql_query_string);
13066 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13068 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13069 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13072 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13073 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13074 ("sqlite3_step fail:%d", rc));
13076 if (rc == SQLITE_DONE) {
13077 EM_DEBUG_EXCEPTION(" no Meeting request found...");
13080 error= EMAIL_ERROR_DATA_NOT_FOUND;
13084 if (!(p_temp_meeting_req = (email_meeting_request_t*)malloc(sizeof(email_meeting_request_t)))) {
13085 EM_DEBUG_EXCEPTION(" malloc failed...");
13086 error = EMAIL_ERROR_OUT_OF_MEMORY;
13090 memset(p_temp_meeting_req, 0x00, sizeof(email_meeting_request_t));
13094 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->mail_id), col_index++);
13095 _get_stmt_field_data_int(hStmt, (int *)&(p_temp_meeting_req->meeting_response), col_index++);
13096 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13097 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->start_time));
13098 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13099 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->end_time));
13100 _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->location, 1, col_index++);
13101 _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->global_object_id, 1, col_index++);
13102 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.offset_from_GMT), col_index++);
13104 _get_stmt_field_data_string_without_allocation(hStmt, p_temp_meeting_req->time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
13105 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13106 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.standard_time_start_date));
13107 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.standard_bias), col_index++);
13109 _get_stmt_field_data_string_without_allocation(hStmt, p_temp_meeting_req->time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
13110 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13111 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.daylight_time_start_date));
13112 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.daylight_bias), col_index++);
13115 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", p_temp_meeting_req->mail_id);
13116 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", p_temp_meeting_req->meeting_response);
13117 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(p_temp_meeting_req->start_time)));
13118 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(p_temp_meeting_req->end_time)));
13119 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", p_temp_meeting_req->location);
13120 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", p_temp_meeting_req->global_object_id);
13121 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", p_temp_meeting_req->time_zone.offset_from_GMT);
13122 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", p_temp_meeting_req->time_zone.standard_name);
13123 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.standard_time_start_date)));
13124 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", p_temp_meeting_req->time_zone.standard_bias);
13125 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", p_temp_meeting_req->time_zone.daylight_name);
13126 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.daylight_time_start_date)));
13127 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", p_temp_meeting_req->time_zone.daylight_bias);
13133 *meeting_req = p_temp_meeting_req;
13135 EM_SAFE_FREE(p_temp_meeting_req);
13138 if (hStmt != NULL) {
13139 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13141 rc = sqlite3_finalize(hStmt);
13143 if (rc != SQLITE_OK) {
13144 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
13145 error = EMAIL_ERROR_DB_FAILURE;
13149 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13152 if (err_code != NULL)
13155 EM_DEBUG_FUNC_END("ret [%d]", ret);
13160 INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
13162 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
13165 int error = EMAIL_ERROR_NONE;
13167 DB_STMT hStmt = NULL;
13168 char sql_query_string[QUERY_SIZE] = {0, };
13169 time_t temp_unix_time = 0;
13171 if (!meeting_req) {
13172 EM_DEBUG_EXCEPTION("Invalid Parameter!");
13173 error = EMAIL_ERROR_INVALID_PARAM;
13177 sqlite3 *local_db_handle = emstorage_get_db_connection();
13178 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13180 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13181 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13182 "UPDATE mail_meeting_tbl "
13184 /* " account_id = ?, " // not update here, this can be changed when move or copy */
13185 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
13186 " meeting_response = ?, "
13187 " start_time = ?, "
13190 " global_object_id = ?, "
13192 " standard_name = ?, "
13193 " standard_time_start_date = ?, "
13194 " standard_bias = ?, "
13195 " daylight_name = ?, "
13196 " daylight_time_start_date = ?, "
13197 " daylight_bias = ? "
13198 "WHERE mail_id = %d",
13199 meeting_req->mail_id);
13201 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
13203 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13204 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13205 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13207 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13208 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13209 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13210 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13211 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13212 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13213 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13214 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13215 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13216 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13217 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13218 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13219 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13223 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13224 temp_unix_time = timegm(&(meeting_req->start_time));
13225 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13226 temp_unix_time = timegm(&(meeting_req->end_time));
13227 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13228 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13229 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13230 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13231 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13232 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13233 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13234 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13235 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13236 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13237 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13238 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13241 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13242 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13243 ("sqlite3_step fail:%d", rc));
13248 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13250 if (hStmt != NULL) {
13251 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13253 rc = sqlite3_finalize(hStmt);
13254 if (rc != SQLITE_OK) {
13255 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
13257 error = EMAIL_ERROR_DB_FAILURE;
13263 if (err_code != NULL)
13266 EM_DEBUG_FUNC_END("ret [%d]", ret);
13270 INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
13272 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, input_mailbox_id, transaction, err_code);
13274 if (account_id < ALL_ACCOUNT || mail_id < 0) {
13275 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
13277 if (err_code != NULL)
13278 *err_code = EMAIL_ERROR_INVALID_PARAM;
13284 int error = EMAIL_ERROR_NONE;
13286 char sql_query_string[QUERY_SIZE] = {0, };
13288 sqlite3 *local_db_handle = emstorage_get_db_connection();
13289 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13291 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
13293 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
13294 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
13298 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mail_id = %d", (and ? "AND" : "WHERE"), mail_id);
13301 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
13302 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = '%d'", (and ? "AND" : "WHERE"), input_mailbox_id);
13305 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13306 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13307 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13312 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13318 EM_DEBUG_FUNC_END("ret [%d]", ret);
13323 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
13325 EM_DEBUG_FUNC_BEGIN();
13327 if (!meeting_req ) {
13328 EM_DEBUG_EXCEPTION("NULL PARAM");
13332 EM_SAFE_FREE(meeting_req->location);
13333 EM_SAFE_FREE(meeting_req->global_object_id);
13335 EM_DEBUG_FUNC_END();
13339 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
13341 EM_DEBUG_FUNC_BEGIN("account_id [%d], input_mailbox_id [%d], mail_slot_size [%d], mail_id_list [%p], mail_id_count [%p], transaction [%d], err_code [%p]", account_id, input_mailbox_id, mail_slot_size, mail_id_list, mail_id_count, transaction, err_code);
13342 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
13343 char sql_query_string[QUERY_SIZE] = {0, };
13344 char **result = NULL;
13345 int rc = -1, ret = false;
13346 int error = EMAIL_ERROR_NONE;
13347 int counter = 0, col_index = 0;
13348 int result_mail_id_count = 0;
13349 int *result_mail_id_list = NULL;
13351 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
13352 EM_DEBUG_EXCEPTION("Invalid Parameter");
13353 error = EMAIL_ERROR_INVALID_PARAM;
13357 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d ORDER BY date_time DESC LIMIT %d, 10000", account_id, input_mailbox_id, mail_slot_size);
13359 EM_DEBUG_LOG("query[%s].", sql_query_string);
13361 sqlite3 *local_db_handle = emstorage_get_db_connection();
13362 EMSTORAGE_START_READ_TRANSACTION(transaction);
13364 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
13365 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13366 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13368 if (!result_mail_id_count) {
13369 EM_DEBUG_LOG("No mail found...");
13371 error= EMAIL_ERROR_MAIL_NOT_FOUND;
13375 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
13377 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
13378 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
13379 error = EMAIL_ERROR_OUT_OF_MEMORY;
13380 sqlite3_free_table(result);
13384 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
13388 for (counter = 0; counter < result_mail_id_count; counter++)
13389 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
13394 EM_DEBUG_LOG("finish off [%d]", ret);
13397 sqlite3_free_table(result);
13400 *mail_id_list = result_mail_id_list;
13401 *mail_id_count = result_mail_id_count;
13404 EM_SAFE_FREE(result_mail_id_list);
13406 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13409 if (err_code != NULL)
13412 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
13413 EM_DEBUG_FUNC_END("ret [%d]", ret);
13418 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
13420 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
13422 int rc = -1, ret = false;
13423 int err = EMAIL_ERROR_NONE;
13424 char sql_query_string[QUERY_SIZE] = {0, };
13426 int result_count = 0;
13428 if (mail_id == 0 || thread_id == NULL) {
13429 EM_DEBUG_EXCEPTION("Invalid Parameter");
13430 if (err_code != NULL)
13431 *err_code = EMAIL_ERROR_INVALID_PARAM;
13435 sqlite3 *local_db_handle = emstorage_get_db_connection();
13437 memset(sql_query_string, 0, QUERY_SIZE);
13438 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
13440 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13441 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13442 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13443 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13445 if (!result_count) {
13446 EM_DEBUG_EXCEPTION("No mail found...");
13448 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13449 /* sqlite3_free_table(result); */
13453 _get_table_field_data_int(result, thread_id, 1);
13455 sqlite3_free_table(result);
13463 if (err_code != NULL)
13466 EM_DEBUG_FUNC_END("ret [%d]", ret);
13471 INTERNAL_FUNC int emstorage_update_latest_thread_mail(int account_id, int thread_id, int latest_mail_id, int thread_item_count, int transaction, int *err_code)
13473 EM_DEBUG_FUNC_BEGIN("account_id [%d], thread_id[%d], latest_mail_id [%d], thread_item_count[%d], err_code[%p]", account_id, thread_id, latest_mail_id, thread_item_count, err_code);
13475 int rc = -1, ret = false;
13476 int err = EMAIL_ERROR_NONE;
13477 char sql_query_string[QUERY_SIZE] = {0, };
13478 char **result = NULL;
13479 int result_count = 0;
13481 if (thread_id == 0) {
13482 EM_DEBUG_EXCEPTION("Invalid Parameter");
13483 if (err_code != NULL)
13484 *err_code = EMAIL_ERROR_INVALID_PARAM;
13488 sqlite3 *local_db_handle = emstorage_get_db_connection();
13490 if (thread_item_count == 0 || latest_mail_id == 0) {
13491 memset(sql_query_string, 0, QUERY_SIZE);
13492 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_type FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_type NOT in (3,5,7)", account_id, thread_id);
13494 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13496 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13497 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13498 EM_DEBUG_LOG("result_count[%d]", result_count);
13499 if (result_count == 0) {
13500 EM_DEBUG_EXCEPTION("No mail found...");
13502 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13503 sqlite3_free_table(result);
13507 _get_table_field_data_int(result, &latest_mail_id, 2);
13508 _get_table_field_data_int(result, &thread_item_count, 3);
13510 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
13511 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
13513 sqlite3_free_table(result);
13516 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13518 /* if (thread_item_count > 1) */
13520 memset(sql_query_string, 0, QUERY_SIZE);
13521 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0 WHERE account_id = %d AND thread_id = %d", account_id, thread_id);
13522 EM_DEBUG_LOG("query[%s]", sql_query_string);
13524 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13525 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13526 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13529 memset(sql_query_string, 0, QUERY_SIZE);
13530 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
13531 EM_DEBUG_LOG("query[%s]", sql_query_string);
13533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13534 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13535 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13541 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13545 if (err_code != NULL)
13548 EM_DEBUG_FUNC_END("ret [%d]", ret);
13553 INTERNAL_FUNC void emstorage_flush_db_cache()
13555 sqlite3_release_memory(-1);
13558 #ifdef __FEATURE_LOCAL_ACTIVITY__
13560 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
13563 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13565 EM_DEBUG_FUNC_BEGIN();
13567 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13569 int rc = -1, ret = false;
13570 int error = EMAIL_ERROR_NONE;
13571 DB_STMT hStmt = NULL;
13572 char sql_query_string[8192] = { 0x00, };
13575 if (!local_activity) {
13576 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13577 if (err_code != NULL)
13578 *err_code = EMAIL_ERROR_INVALID_PARAM;
13582 sqlite3 *local_db_handle = emstorage_get_db_connection();
13583 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
13584 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13586 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
13588 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
13589 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
13590 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
13591 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
13592 EM_DEBUG_LOG(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
13593 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
13594 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
13596 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13599 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13600 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13601 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13603 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13608 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13609 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13610 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13611 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13612 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
13613 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13614 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13617 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13619 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13620 ("sqlite3_step fail:%d", rc));
13621 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13622 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13628 if (hStmt != NULL) {
13629 rc = sqlite3_finalize(hStmt);
13630 if (rc != SQLITE_OK) {
13631 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13633 error = EMAIL_ERROR_DB_FAILURE;
13637 EM_DEBUG_LOG(" >>>>>>>>>> hStmt is NULL!!!");
13640 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13644 if (err_code != NULL)
13647 EM_DEBUG_FUNC_END("ret [%d]", ret);
13652 * emstorage_get_activity - Get the Local activity Information
13656 INTERNAL_FUNC int emstorage_get_activity(int account_id, int activityid, emstorage_activity_tbl_t** activity_list, int *select_num, int transaction, int *err_code)
13658 EM_DEBUG_FUNC_BEGIN();
13660 int i = 0, count = 0, rc = -1, ret = false;
13661 int error = EMAIL_ERROR_NONE;
13662 emstorage_activity_tbl_t *p_activity_tbl = NULL;
13663 char sql_query_string[1024] = {0x00, };
13664 char **result = NULL;
13667 EM_IF_NULL_RETURN_VALUE(activity_list, false);
13668 EM_IF_NULL_RETURN_VALUE(select_num, false);
13671 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
13672 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
13673 if (err_code != NULL)
13674 *err_code = EMAIL_ERROR_INVALID_PARAM;
13678 EMSTORAGE_START_READ_TRANSACTION(transaction);
13680 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13682 if (activityid == ALL_ACTIVITIES) {
13683 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
13686 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
13689 EM_DEBUG_LOG("Query [%s]", sql_query_string);
13693 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13694 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13695 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13701 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
13702 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13703 error = EMAIL_ERROR_OUT_OF_MEMORY;
13708 for (i = 0; i < count; i++) {
13709 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13710 if (result[col_index])
13711 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
13713 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13714 if (result[col_index])
13715 p_activity_tbl[i].account_id = atoi(result[col_index++]);
13717 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13718 if (result[col_index])
13719 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
13721 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13722 if (result[col_index])
13723 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
13726 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13727 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
13728 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
13732 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13733 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
13734 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
13738 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13739 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
13740 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
13747 sqlite3_free_table(result);
13755 *activity_list = p_activity_tbl;
13756 *select_num = count;
13757 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
13759 else if (p_activity_tbl != NULL) {
13760 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
13763 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13764 if (err_code != NULL)
13767 EM_DEBUG_FUNC_END("ret [%d]", ret);
13772 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
13775 EM_DEBUG_FUNC_BEGIN();
13778 int err = EMAIL_ERROR_NONE;
13781 char **result = NULL;
13783 if (NULL == activity_id)
13785 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
13787 err = EMAIL_ERROR_INVALID_PARAM;
13791 /* increase unique id */
13793 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
13795 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); n EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc); */
13796 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13797 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13799 if (NULL==result[1])
13802 rc = atoi(result[1])+1;
13807 sqlite3_free_table(result);
13813 if (NULL != err_code) {
13817 EM_DEBUG_FUNC_END("ret [%d]", ret);
13822 INTERNAL_FUNC int emstorage_get_activity_id_list(int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
13825 EM_DEBUG_FUNC_BEGIN();
13827 EM_DEBUG_LOG(" account_id[%d], activity_id_list[%p], activity_id_count[%p] err_code[%p]", account_id, activity_id_list, activity_id_count, err_code);
13829 if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0) {
13830 if (err_code != NULL)
13831 *err_code = EMAIL_ERROR_INVALID_PARAM;
13836 int error = EMAIL_ERROR_NONE;
13837 int i = 0, rc = -1, count = 0;
13838 char sql_query_string[1024] = {0x00, };
13839 int *activity_ids = NULL;
13841 char **result = NULL;
13843 EMSTORAGE_START_READ_TRANSACTION(transaction);
13845 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13847 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct activity_id FROM mail_local_activity_tbl WHERE account_id = %d AND activity_type >= %d AND activity_type <= %d order by activity_id", account_id, lowest_activity_type, highest_activity_type);
13849 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
13852 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13853 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13854 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13858 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
13860 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
13861 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13862 error = EMAIL_ERROR_OUT_OF_MEMORY;
13866 for (i = 0; i < count; i++) {
13867 activity_ids[i] = atoi(result[col_index]);
13869 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
13878 *activity_id_count = count;
13879 *activity_id_list = activity_ids;
13882 else if (activity_ids != NULL) /* Prevent defect - 216566 */
13883 EM_SAFE_FREE(activity_ids);
13886 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13887 if (err_code != NULL) {
13891 EM_DEBUG_FUNC_END("ret [%d]", ret);
13895 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
13897 EM_DEBUG_FUNC_BEGIN();
13899 int error = EMAIL_ERROR_NONE;
13902 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
13904 if (NULL == activity_id_list) {
13905 error = EMAIL_ERROR_INVALID_PARAM;
13909 free(activity_id_list);
13910 activity_id_list = NULL;
13918 if (NULL != error_code) {
13919 *error_code = error;
13922 EM_DEBUG_FUNC_END("ret [%d]", ret);
13927 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
13928 * or based on server mail id
13931 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13933 EM_DEBUG_FUNC_BEGIN();
13936 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
13938 if (!local_activity) {
13939 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
13940 if (err_code != NULL)
13941 *err_code = EMAIL_ERROR_INVALID_PARAM;
13945 int rc = -1, ret = false; /* Prevent_FIX */
13946 int err = EMAIL_ERROR_NONE;
13948 int query_where = 0;
13949 char sql_query_string[8192] = { 0x00, };
13950 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13952 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13954 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
13956 EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
13958 if (local_activity->account_id) {
13959 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13960 " WHERE account_id = %d ", local_activity->account_id);
13965 if (local_activity->server_mailid) {
13966 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13967 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
13973 if (local_activity->mail_id) {
13974 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
13976 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13977 " %s %s mail_id = %d ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
13984 if (local_activity->activity_type > 0) {
13985 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13986 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
13989 if (local_activity->activity_id > 0) {
13990 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13991 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
13995 EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
13997 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13998 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13999 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14001 rc = sqlite3_changes(local_db_handle);
14003 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
14004 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
14010 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14011 if (err_code != NULL)
14013 EM_DEBUG_FUNC_END("ret [%d]", ret);
14018 * emstorage_free_local_activity - Free the Local Activity data
14020 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
14022 EM_DEBUG_FUNC_BEGIN();
14024 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
14027 int error = EMAIL_ERROR_INVALID_PARAM;
14030 if (!local_activity_list || !*local_activity_list) {
14031 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
14033 error = EMAIL_ERROR_INVALID_PARAM;
14037 emstorage_activity_tbl_t* p = *local_activity_list;
14040 for (; i < count; i++) {
14041 EM_SAFE_FREE(p[i].dest_mbox);
14042 EM_SAFE_FREE(p[i].src_mbox);
14043 EM_SAFE_FREE(p[i].server_mailid);
14046 free(p); *local_activity_list = NULL;
14053 if (err_code != NULL)
14056 EM_DEBUG_FUNC_END("ret [%d]", ret);
14060 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
14062 static char *_mail_field_name_list[] = {
14070 "server_mail_status",
14071 "server_mailbox_name",
14074 "reference_mail_id",
14075 "full_address_from",
14078 "full_address_bcc",
14079 "body_download_status",
14083 "flags_seen_field",
14084 "flags_deleted_field",
14085 "flags_flagged_field",
14086 "flags_answered_field",
14087 "flags_recent_field",
14088 "flags_draft_field",
14089 "flags_forwarded_field",
14095 "attachment_count",
14096 "inline_content_count",
14098 "thread_item_count",
14100 "meeting_request_status",
14106 static char*_get_mail_field_name_by_attribute_type(email_mail_attribute_type input_attribute_type)
14108 EM_DEBUG_FUNC_BEGIN("input_attribute_type [%d]", input_attribute_type);
14110 if(input_attribute_type > EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS || input_attribute_type < 0) {
14111 EM_DEBUG_EXCEPTION("Invalid input_attribute_type [%d]", input_attribute_type);
14115 EM_DEBUG_FUNC_END("return [%s]", _mail_field_name_list[input_attribute_type]);
14116 return _mail_field_name_list[input_attribute_type];
14119 static int _get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type)
14121 EM_DEBUG_FUNC_BEGIN("input_mail_field_name [%p], output_mail_attribute_type [%p]", input_mail_field_name, output_mail_attribute_type);
14123 int err = EMAIL_ERROR_DATA_NOT_FOUND;
14125 if(!input_mail_field_name || !output_mail_attribute_type) {
14126 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14127 return EMAIL_ERROR_INVALID_PARAM;
14130 for (i = 0; i < EMAIL_MAIL_ATTRIBUTE_END; i++) {
14131 if (EM_SAFE_STRCMP(input_mail_field_name, _mail_field_name_list[i]) == 0) {
14132 *output_mail_attribute_type = i;
14133 err = EMAIL_ERROR_NONE;
14138 EM_DEBUG_FUNC_END("found mail attribute type [%d]", (int)*output_mail_attribute_type);
14142 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
14144 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
14146 int ret = EMAIL_ERROR_NONE;
14147 char key_value_string[QUERY_SIZE] = { 0, };
14148 char *temp_key_value_1 = NULL;
14149 char *temp_key_value_2 = NULL;
14151 if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
14152 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14153 return EMAIL_ERROR_INVALID_PARAM;
14156 switch(input_list_filter_rule->target_attribute) {
14157 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID :
14158 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID :
14159 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID :
14160 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE :
14161 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS :
14162 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID :
14163 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS :
14164 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14165 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14166 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE :
14167 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD :
14168 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD :
14169 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD :
14170 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD :
14171 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD :
14172 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD :
14173 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD :
14174 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS :
14175 case EMAIL_MAIL_ATTRIBUTE_PRIORITY :
14176 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS :
14177 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS :
14178 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS :
14179 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT :
14180 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT :
14181 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID :
14182 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT :
14183 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS :
14184 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS :
14185 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE :
14186 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE :
14187 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
14190 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14191 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14192 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14193 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14194 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14195 case EMAIL_MAIL_ATTRIBUTE_FROM :
14196 case EMAIL_MAIL_ATTRIBUTE_TO :
14197 case EMAIL_MAIL_ATTRIBUTE_CC :
14198 case EMAIL_MAIL_ATTRIBUTE_BCC :
14199 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14200 if(input_list_filter_rule->key_value.string_type_value == NULL) {
14201 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
14202 ret = EMAIL_ERROR_INVALID_PARAM;
14206 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
14208 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
14209 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
14211 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
14212 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
14214 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
14217 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME :
14218 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
14222 ret = EMAIL_ERROR_INVALID_PARAM;
14223 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14227 if(ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
14228 *output_key_value_string = strdup(key_value_string);
14233 EM_SAFE_FREE(temp_key_value_1);
14234 EM_SAFE_FREE(temp_key_value_2);
14236 EM_DEBUG_FUNC_END("ret [%d]", ret);
14240 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14241 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
14243 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
14245 if (index <= 0 || !cert_password_file_name) {
14246 EM_DEBUG_EXCEPTION("Invalid parameter");
14247 return EMAIL_ERROR_INVALID_PARAM;
14250 sprintf(cert_password_file_name, ".email_cert_%d", index);
14252 EM_DEBUG_FUNC_END();
14253 return EMAIL_ERROR_NONE;
14257 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
14259 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14261 int ret = EMAIL_ERROR_NONE;
14263 int length_field_name = 0;
14264 int length_value = 0;
14265 char result_rule_string[QUERY_SIZE] = { 0 , };
14266 char *mod_field_name_string = NULL;
14267 char *mod_value_string = NULL;
14268 char *temp_field_name_string = NULL;
14269 char *temp_key_value_string = NULL;
14271 if(input_list_filter_rule == NULL || output_string == NULL) {
14272 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14273 return EMAIL_ERROR_INVALID_PARAM;
14276 temp_field_name_string = _get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
14278 if(temp_field_name_string == NULL) {
14279 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14280 return EMAIL_ERROR_INVALID_PARAM;
14283 if( _get_key_value_string_for_list_filter_rule(input_list_filter_rule, &temp_key_value_string) != EMAIL_ERROR_NONE || temp_key_value_string == NULL) {
14284 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
14285 return EMAIL_ERROR_INVALID_PARAM;
14288 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
14289 length_value = EM_SAFE_STRLEN(temp_key_value_string);
14291 switch(input_list_filter_rule->target_attribute) {
14292 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14293 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14294 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14295 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14296 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14297 case EMAIL_MAIL_ATTRIBUTE_FROM :
14298 case EMAIL_MAIL_ATTRIBUTE_TO :
14299 case EMAIL_MAIL_ATTRIBUTE_CC :
14300 case EMAIL_MAIL_ATTRIBUTE_BCC :
14301 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14309 if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
14310 length_field_name += strlen("UPPER() ");
14311 length_value += strlen("UPPER() ");
14312 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
14313 mod_value_string = em_malloc(sizeof(char) * length_value);
14314 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
14315 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
14316 EM_SAFE_FREE(temp_key_value_string);
14319 mod_field_name_string = strdup(temp_field_name_string);
14320 mod_value_string = temp_key_value_string;
14323 switch (input_list_filter_rule->rule_type) {
14324 case EMAIL_LIST_FILTER_RULE_EQUAL :
14325 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14327 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
14328 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
14330 case EMAIL_LIST_FILTER_RULE_LESS_THAN :
14331 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
14333 case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
14334 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
14336 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
14337 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
14339 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
14340 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
14342 case EMAIL_LIST_FILTER_RULE_INCLUDE :
14343 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
14345 case EMAIL_LIST_FILTER_RULE_IN :
14346 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
14348 case EMAIL_LIST_FILTER_RULE_NOT_IN :
14349 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
14352 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14353 ret = EMAIL_ERROR_INVALID_PARAM;
14357 *output_string = strdup(result_rule_string);
14360 EM_SAFE_FREE(mod_field_name_string);
14361 EM_SAFE_FREE(mod_value_string);
14363 EM_DEBUG_FUNC_END("ret [%d]", ret);
14367 static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
14368 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
14370 char result_rule_string[QUERY_SIZE] = { 0 , };
14371 int ret = EMAIL_ERROR_NONE;
14373 if(input_sorting_rule->force_boolean_check) {
14374 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", _get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14377 EM_SAFE_STRCPY(result_rule_string, _get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14379 switch (input_sorting_rule->sort_order) {
14380 case EMAIL_SORT_ORDER_ASCEND :
14381 EM_SAFE_STRCAT(result_rule_string, " ASC ");
14384 case EMAIL_SORT_ORDER_DESCEND :
14385 EM_SAFE_STRCAT(result_rule_string, " DESC ");
14389 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
14390 ret = EMAIL_ERROR_INVALID_PARAM;
14394 *output_string = strdup(result_rule_string);
14397 EM_DEBUG_FUNC_END("ret [%d]", ret);
14401 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause)
14403 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d], input_sorting_rule_list[%p], input_sorting_rule_count [%d], input_start_index [%d], input_limit_count [%d], output_conditional_clause [%p]", input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, output_conditional_clause);
14404 int ret = EMAIL_ERROR_NONE;
14406 int string_offset = 0;
14407 char conditional_clause_string[QUERY_SIZE] = {0, };
14408 char *result_string_for_a_item = NULL;
14410 if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
14411 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14412 return EMAIL_ERROR_INVALID_PARAM;
14415 if(input_filter_count > 0) {
14416 strcpy(conditional_clause_string, " WHERE ");
14418 for ( i = 0; i < input_filter_count; i++) {
14419 switch (input_filter_list[i].list_filter_item_type) {
14420 case EMAIL_LIST_FILTER_ITEM_RULE :
14421 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
14422 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
14425 case EMAIL_LIST_FILTER_ITEM_OPERATOR :
14426 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
14427 switch(input_filter_list[i].list_filter_item.operator_type) {
14428 case EMAIL_LIST_FILTER_OPERATOR_AND :
14429 result_string_for_a_item = strdup("AND ");
14431 case EMAIL_LIST_FILTER_OPERATOR_OR :
14432 result_string_for_a_item = strdup("OR ");
14434 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
14435 result_string_for_a_item = strdup(" (");
14437 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
14438 result_string_for_a_item = strdup(") ");
14444 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
14445 ret = EMAIL_ERROR_INVALID_PARAM;
14449 if(result_string_for_a_item == NULL) {
14450 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
14451 ret = EMAIL_ERROR_INVALID_PARAM;
14455 if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= QUERY_SIZE) { /* prevent 34364 */
14456 EM_DEBUG_EXCEPTION("Query is too long");
14457 ret = EMAIL_ERROR_DATA_TOO_LONG;
14460 strcat(conditional_clause_string, result_string_for_a_item);
14461 EM_SAFE_FREE(result_string_for_a_item);
14465 if(input_sorting_rule_count > 0) {
14466 strcat(conditional_clause_string, "ORDER BY ");
14468 for ( i = 0; i < input_sorting_rule_count; i++) {
14469 if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
14470 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
14474 strcat(conditional_clause_string, ", ");
14475 strcat(conditional_clause_string, result_string_for_a_item);
14476 EM_SAFE_FREE(result_string_for_a_item);
14480 if (input_start_index != -1 && input_limit_count != -1) {
14481 string_offset = strlen(conditional_clause_string);
14482 SNPRINTF_OFFSET(conditional_clause_string, string_offset, QUERY_SIZE, " LIMIT %d, %d", input_start_index, input_limit_count);
14485 *output_conditional_clause = strdup(conditional_clause_string);
14488 EM_SAFE_FREE(result_string_for_a_item);
14490 EM_DEBUG_FUNC_END("ret [%d]", ret);
14494 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
14496 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
14497 int err = EMAIL_ERROR_NONE;
14499 email_list_filter_t *temp_filter_list = NULL;
14501 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
14503 for ( i = 0; i < input_filter_count; i++) {
14504 temp_filter_list = (*input_filter_list) + i;
14505 if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
14506 switch(temp_filter_list->list_filter_item.rule.target_attribute) {
14507 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14508 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14509 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14510 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14511 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14512 case EMAIL_MAIL_ATTRIBUTE_FROM :
14513 case EMAIL_MAIL_ATTRIBUTE_TO :
14514 case EMAIL_MAIL_ATTRIBUTE_CC :
14515 case EMAIL_MAIL_ATTRIBUTE_BCC :
14516 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14517 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14518 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14519 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
14527 free(*input_filter_list);
14528 *input_filter_list = NULL;
14530 EM_DEBUG_FUNC_END("err [%d]", err);
14534 INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14536 EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14538 if (!certificate) {
14539 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14540 if (err_code != NULL)
14541 *err_code = EMAIL_ERROR_INVALID_PARAM;
14546 int rc = -1, ret = false;
14547 int error = EMAIL_ERROR_NONE;
14548 DB_STMT hStmt = NULL;
14549 char sql_query_string[QUERY_SIZE] = {0, };
14550 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14551 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14553 sqlite3 *local_db_handle = emstorage_get_db_connection();
14555 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14557 char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
14558 char **result = NULL;
14560 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14561 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14562 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14563 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14565 if (NULL==result[1]) rc = 1;
14566 else rc = atoi(result[1])+1;
14567 sqlite3_free_table(result);
14569 certificate->certificate_id = rc;
14570 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14571 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14572 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14576 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14577 "INSERT INTO mail_certificate_tbl VALUES "
14579 " ? " /* Index of certificate */
14580 " , ? " /* Select the account */
14581 " , ? " /* Year of issue */
14582 " , ? " /* Month of issue */
14583 " , ? " /* Day of issue */
14584 " , ? " /* Year of expiration */
14585 " , ? " /* Month of expiration */
14586 " , ? " /* Day of expiration */
14587 " , ? " /* Organization of issue */
14588 " , ? " /* Email address */
14589 " , ? " /* Subject of certificate */
14590 " , ? " /* Name of saved certificate */
14594 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
14595 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14596 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14597 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14599 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
14600 _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
14601 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
14602 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
14603 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
14604 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
14605 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
14606 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
14607 _bind_stmt_field_data_string(hStmt, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_organization_name, 0, ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL);
14608 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
14609 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
14610 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
14611 /* rc = sqlite3_step(hStmt); */
14612 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14614 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14615 ("sqlite3_step fail:%d", rc));
14616 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14617 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
14618 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14619 if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14620 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
14621 error = EMAIL_ERROR_SYSTEM_FAILURE;
14627 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14629 if (!emcore_notify_storage_event(NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
14630 EM_DEBUG_EXCEPTION("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
14634 if (hStmt != NULL) {
14635 rc = sqlite3_finalize(hStmt);
14636 if (rc != SQLITE_OK) {
14637 EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
14638 error = EMAIL_ERROR_DB_FAILURE;
14642 EM_DEBUG_LOG("hStmt is NULL!!!");
14644 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14647 if (err_code != NULL)
14650 EM_DEBUG_FUNC_END("ret [%d]", ret);
14654 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
14656 EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
14659 int error = EMAIL_ERROR_NONE;
14662 if (!certificate_list || !*certificate_list) {
14663 EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
14664 error = EMAIL_ERROR_INVALID_PARAM;
14668 emstorage_certificate_tbl_t *p = *certificate_list;
14671 for (; i < count; i++) {
14672 EM_SAFE_FREE(p[i].issue_organization_name);
14673 EM_SAFE_FREE(p[i].email_address);
14674 EM_SAFE_FREE(p[i].subject_str);
14675 EM_SAFE_FREE(p[i].filepath);
14676 EM_SAFE_FREE(p[i].password);
14680 *certificate_list = NULL;
14686 if (err_code != NULL)
14689 EM_DEBUG_FUNC_END("ret [%d]", ret);
14693 INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
14695 EM_DEBUG_FUNC_BEGIN();
14697 int i = 0, count = 0, rc = -1, ret = false;
14698 int error = EMAIL_ERROR_NONE;
14699 emstorage_certificate_tbl_t *p_data_tbl = NULL;
14701 DB_STMT hStmt = NULL;
14703 if (!select_num || !certificate_list) {
14704 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
14705 if (err_code != NULL)
14706 *err_code = EMAIL_ERROR_INVALID_PARAM;
14710 sqlite3 *local_db_handle = emstorage_get_db_connection();
14711 EMSTORAGE_START_READ_TRANSACTION(transaction);
14713 char sql_query_string[QUERY_SIZE] = {0, };
14714 char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
14717 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14718 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14719 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14720 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14722 count = atoi(result[1]);
14723 sqlite3_free_table(result);
14726 EM_DEBUG_EXCEPTION("no account found...");
14727 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14731 EM_DEBUG_LOG("count = %d", rc);
14732 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
14734 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
14735 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14737 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
14738 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14739 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14741 /* rc = sqlite3_step(hStmt); */
14742 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14743 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14744 ("sqlite3_step fail:%d", rc));
14745 if (rc == SQLITE_DONE) {
14746 EM_DEBUG_EXCEPTION("no account found...");
14748 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14754 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count))) {
14755 EM_DEBUG_EXCEPTION("malloc failed...");
14756 error = EMAIL_ERROR_OUT_OF_MEMORY;
14759 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
14760 for (i = 0; i < count; i++) {
14761 /* get recordset */
14762 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14763 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14764 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14765 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14766 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14767 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14768 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14769 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14770 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14771 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14772 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14773 if (with_password == true) {
14774 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14775 /* get password from the secure storage */
14776 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14778 EM_SAFE_FREE(p_data_tbl[i].password);
14780 /* get password file name */
14781 if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14782 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14786 /* read password from secure storage */
14787 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
14788 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
14791 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl[i].password);
14795 /* rc = sqlite3_step(hStmt); */
14796 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14797 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
14798 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14799 ("sqlite3_step fail:%d", rc));
14806 *certificate_list = p_data_tbl;
14807 *select_num = count;
14808 EM_DEBUG_LOG("COUNT : %d", count);
14810 else if (p_data_tbl != NULL)
14811 emstorage_free_certificate(&p_data_tbl, count, NULL);
14812 if (hStmt != NULL) {
14813 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
14815 rc = sqlite3_finalize(hStmt);
14817 if (rc != SQLITE_OK) {
14818 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14819 error = EMAIL_ERROR_DB_FAILURE;
14823 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14826 if (err_code != NULL)
14829 EM_DEBUG_FUNC_END("ret [%d]", ret);
14833 INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
14835 EM_DEBUG_FUNC_BEGIN("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
14837 if (!certificate) {
14838 EM_DEBUG_EXCEPTION("email_address[%s], certificate[%p]", email_address, certificate);
14839 if (err_code != NULL)
14840 *err_code = EMAIL_ERROR_INVALID_PARAM;
14845 int error = EMAIL_ERROR_NONE;
14846 emstorage_certificate_tbl_t *p_data_tbl = NULL;
14847 DB_STMT hStmt = NULL;
14848 char sql_query_string[QUERY_SIZE] = {0, };
14851 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14852 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14854 sqlite3 *local_db_handle = emstorage_get_db_connection();
14855 EMSTORAGE_START_READ_TRANSACTION(transaction);
14857 /* Make query string */
14858 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
14859 sql_len = EM_SAFE_STRLEN(sql_query_string);
14861 /* dummy value, FROM WHERE clause */
14862 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
14865 EM_DEBUG_LOG("query = [%s]", sql_query_string);
14867 /* execute a sql and count rows */
14868 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14869 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14870 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14872 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14873 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14874 ("sqlite3_step fail:%d", rc));
14876 if (rc == SQLITE_DONE) {
14877 EM_DEBUG_EXCEPTION("no matched certificate found...");
14878 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14882 /* Assign query result to structure */
14883 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
14884 EM_DEBUG_EXCEPTION("malloc failed...");
14885 error = EMAIL_ERROR_OUT_OF_MEMORY;
14889 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
14890 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14891 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14892 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14893 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14894 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14895 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14896 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14897 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14898 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14899 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14900 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14902 if (with_password) {
14903 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14904 /* get password file name */
14905 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14906 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14910 /* read password from secure storage */
14911 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
14912 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
14915 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
14922 *certificate = p_data_tbl;
14924 if (hStmt != NULL) {
14925 rc = sqlite3_finalize(hStmt);
14926 if (rc != SQLITE_OK) {
14927 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14928 error = EMAIL_ERROR_DB_FAILURE;
14932 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14935 if (err_code != NULL)
14938 EM_DEBUG_FUNC_END("ret [%d]", ret);
14942 INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
14944 EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
14946 if (!certificate) {
14947 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
14948 if (err_code != NULL)
14949 *err_code = EMAIL_ERROR_INVALID_PARAM;
14954 int error = EMAIL_ERROR_NONE;
14955 emstorage_certificate_tbl_t *p_data_tbl = NULL;
14956 DB_STMT hStmt = NULL;
14957 char sql_query_string[QUERY_SIZE] = {0, };
14960 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14961 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14963 sqlite3 *local_db_handle = emstorage_get_db_connection();
14964 EMSTORAGE_START_READ_TRANSACTION(transaction);
14966 /* Make query string */
14967 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
14968 sql_len = EM_SAFE_STRLEN(sql_query_string);
14970 /* dummy value, FROM WHERE clause */
14971 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
14974 EM_DEBUG_LOG("query = [%s]", sql_query_string);
14976 /* execute a sql and count rows */
14977 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14978 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14979 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14981 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14982 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14983 ("sqlite3_step fail:%d", rc));
14985 if (rc == SQLITE_DONE) {
14986 EM_DEBUG_EXCEPTION("no matched certificate found...");
14987 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14991 /* Assign query result to structure */
14992 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
14993 EM_DEBUG_EXCEPTION("malloc failed...");
14994 error = EMAIL_ERROR_OUT_OF_MEMORY;
14998 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15000 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15001 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15002 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15003 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15004 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15005 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15006 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15007 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15008 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15009 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15010 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15012 if (with_password) {
15013 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15014 /* get password file name */
15015 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15016 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15020 /* read password from secure storage */
15021 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15022 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
15025 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
15032 *certificate = p_data_tbl;
15034 if (hStmt != NULL) {
15035 rc = sqlite3_finalize(hStmt);
15036 if (rc != SQLITE_OK) {
15037 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15038 error = EMAIL_ERROR_DB_FAILURE;
15042 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15045 if (err_code != NULL)
15048 EM_DEBUG_FUNC_END("ret [%d]", ret);
15052 INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
15054 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
15056 if (certificate_id < 1) {
15057 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15059 if (err_code != NULL)
15060 *err_code = EMAIL_ERROR_INVALID_PARAM;
15064 int rc = -1, ret = false;
15065 int error = EMAIL_ERROR_NONE;
15066 sqlite3 *local_db_handle = emstorage_get_db_connection();
15067 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15069 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
15070 DB_STMT hStmt = NULL;
15071 char sql_query_string[QUERY_SIZE] = {0, };
15072 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15073 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15074 /* get password file name */
15075 if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15076 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15080 /* delete from db */
15081 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15082 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
15084 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15085 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15086 ("sqlite3_exec fail:%d", rc));
15087 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15088 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15090 /* validate account existence */
15091 rc = sqlite3_changes(local_db_handle);
15093 EM_DEBUG_EXCEPTION(" no matched certificate found...");
15094 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15097 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15098 /* delete from secure storage */
15099 if (ssm_delete_file(cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15100 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
15101 error = EMAIL_ERROR_SYSTEM_FAILURE;
15109 if (hStmt != NULL) {
15110 rc = sqlite3_finalize(hStmt);
15111 if (rc != SQLITE_OK) {
15112 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15113 error = EMAIL_ERROR_DB_FAILURE;
15117 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15120 if (err_code != NULL)
15123 EM_DEBUG_FUNC_END("ret [%d]", ret);
15127 INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
15129 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
15131 if (certificate_id < 1) {
15132 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15134 if (err_code != NULL)
15135 *err_code = EMAIL_ERROR_INVALID_PARAM;
15139 int error = EMAIL_ERROR_NONE;
15140 int rc, ret = false;
15142 DB_STMT hStmt = NULL;
15143 char sql_query_string[QUERY_SIZE] = {0, };
15144 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15145 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15147 sqlite3 *local_db_handle = emstorage_get_db_connection();
15148 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15150 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15151 "UPDATE mail_certificate_tbl SET"
15153 ", issue_month = ?" /* Index of certificate */
15154 ", issue_day = ?" /* Select the account */
15155 ", expiration_year = ?" /* Year of issue */
15156 ", expiration_month = ?" /* Month of issue */
15157 ", expiration_day = ?" /* Day of issue */
15158 ", issue_organization_name = ?" /* Year of expiration */
15159 ", email_address = ?" /* Month of expiration */
15160 ", subject_str = ?" /* Day of expiration */
15161 ", filepath = ?" /* Organization of issue */
15163 " WHERE certificate_id = ?");
15165 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15166 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
15167 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15168 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15170 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
15171 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
15172 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
15173 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
15174 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
15175 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
15176 _bind_stmt_field_data_string(hStmt, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_organization_name, 0, ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL);
15177 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
15178 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
15179 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
15181 /* rc = sqlite3_step(hStmt); */
15182 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15183 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15184 ("sqlite3_step fail:%d", rc));
15185 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15186 ("sqlite3_step fail:%d", rc));
15188 /* validate account existence */
15189 rc = sqlite3_changes(local_db_handle);
15191 EM_DEBUG_EXCEPTION(" no matched account found...");
15193 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15197 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15198 /* get password file name */
15199 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15200 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15204 /* save passwords to the secure storage */
15205 if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15206 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
15207 error = EMAIL_ERROR_SYSTEM_FAILURE;
15211 if (!emcore_notify_storage_event(NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
15212 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
15218 if (hStmt != NULL) {
15219 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
15221 rc = sqlite3_finalize(hStmt);
15222 if (rc != SQLITE_OK) {
15223 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15224 error = EMAIL_ERROR_DB_FAILURE;
15228 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15232 if (err_code != NULL)
15235 EM_DEBUG_FUNC_END("ret [%d]", ret);
15239 /* Tasks --------------------------------------------------------------------------*/
15240 INTERNAL_FUNC int emstorage_add_task(email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id)
15242 EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_priority[%p], input_task_parameter[%p] input_task_parameter_length[%d] input_transaction[%d] output_task_id[%p]", input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, input_transaction, output_task_id);
15246 int err = EMAIL_ERROR_NONE;
15248 DB_STMT hStmt = NULL;
15249 char sql_query_string[QUERY_SIZE] = {0, };
15250 sqlite3 *local_db_handle = NULL;
15251 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
15252 char **result = NULL;
15254 if (input_task_parameter == NULL || output_task_id == NULL) {
15255 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15256 err = EMAIL_ERROR_INVALID_PARAM;
15260 local_db_handle = emstorage_get_db_connection();
15262 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, err);
15264 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
15265 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15266 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15268 if (NULL == result[1])
15271 task_id = atoi(result[1])+1;
15273 *output_task_id = task_id;
15275 sqlite3_free_table(result);
15278 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15279 "INSERT INTO mail_task_tbl VALUES "
15281 " ? " /* task_id */
15282 " , ? " /* task_type */
15283 " , ? " /* task_status */
15284 " , ? " /* task_priority */
15285 " , ? " /* task_parameter_length */
15286 " , ? " /* task_parameter */
15287 " , ? " /* date_time */
15290 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15291 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15292 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15294 EM_DEBUG_LOG(">>>> SQL STMT [%s] ", sql_query_string);
15297 _bind_stmt_field_data_int(hStmt, i++, task_id);
15298 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
15299 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
15300 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
15301 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
15302 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
15303 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
15305 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15307 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15308 ("sqlite3_step fail:%d", rc));
15309 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15310 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15312 ret = (err == EMAIL_ERROR_NONE);
15313 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, err);
15317 if (hStmt != NULL) {
15318 rc = sqlite3_finalize(hStmt);
15319 if (rc != SQLITE_OK) {
15320 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
15321 err = EMAIL_ERROR_DB_FAILURE;
15325 EM_DEBUG_LOG("hStmt is NULL!!!");
15329 EM_DEBUG_FUNC_END("err [%d]", err);
15333 INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction)
15335 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
15336 int rc, ret = false;
15337 int err = EMAIL_ERROR_NONE;
15338 char sql_query_string[QUERY_SIZE] = {0, };
15339 sqlite3 *local_db_handle = NULL;
15342 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15343 err = EMAIL_ERROR_INVALID_PARAM;
15347 local_db_handle = emstorage_get_db_connection();
15349 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15351 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
15353 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15354 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15355 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15360 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15363 EM_DEBUG_FUNC_END("err [%d]", err);
15367 INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_type_t task_status, int transaction)
15369 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
15370 int rc, ret = false;
15371 int err = EMAIL_ERROR_NONE;
15372 char sql_query_string[QUERY_SIZE] = {0, };
15374 sqlite3 *local_db_handle = emstorage_get_db_connection();
15376 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15378 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15379 "UPDATE mail_task_tbl SET"
15380 " task_status = %d"
15381 " WHERE task_id = %d"
15384 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15385 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15386 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15391 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15395 EM_DEBUG_FUNC_END("err [%d]", err);
15399 INTERNAL_FUNC int emstorage_query_task(const char *input_conditional_clause, const char *input_ordering_clause, email_task_t **output_task_list, int *output_task_count)
15401 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], output_task_list[%p], output_task_count[%d]", input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
15402 int i = 0, count = 0, rc = -1;
15404 int field_index = 0;
15405 int err = EMAIL_ERROR_NONE;
15406 email_task_t *task_item_from_tbl = NULL;
15407 char sql_query_string[QUERY_SIZE] = {0, };
15408 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
15410 sqlite3 *local_db_handle = NULL;
15411 DB_STMT hStmt = NULL;
15413 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
15414 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
15416 local_db_handle = emstorage_get_db_connection();
15418 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
15419 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15421 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
15422 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15423 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15425 count = atoi(result[1]);
15426 sqlite3_free_table(result);
15428 EM_DEBUG_LOG("count = %d", rc);
15431 EM_DEBUG_EXCEPTION("no task found...");
15432 err = EMAIL_ERROR_TASK_NOT_FOUND;
15436 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
15437 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15439 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15441 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
15442 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15443 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15445 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15446 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15447 ("sqlite3_step fail:%d", rc));
15449 if (rc == SQLITE_DONE) {
15450 EM_DEBUG_EXCEPTION("no task found...");
15451 err = EMAIL_ERROR_TASK_NOT_FOUND;
15456 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
15457 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
15458 err = EMAIL_ERROR_OUT_OF_MEMORY;
15462 for (i = 0; i < count; i++) {
15463 /* get recordset */
15466 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
15467 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
15468 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
15469 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
15470 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
15471 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
15473 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15474 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
15475 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15476 ("sqlite3_step fail:%d", rc));
15480 if (err == EMAIL_ERROR_NONE) {
15481 if (output_task_list)
15482 *output_task_list = task_item_from_tbl;
15483 *output_task_count = count;
15486 EM_SAFE_FREE(task_item_from_tbl);
15488 if (hStmt != NULL) {
15489 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15490 rc = sqlite3_finalize(hStmt);
15492 if (rc != SQLITE_OK) {
15493 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15494 err = EMAIL_ERROR_DB_FAILURE;
15500 EM_DEBUG_FUNC_END("err [%d]", err);
15503 /* Tasks --------------------------------------------------------------------------*/