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 #define _MULTIPLE_DB_HANDLE
196 #ifdef _MULTIPLE_DB_HANDLE
198 #define _DISCONNECT_DB /* db_util_close(_db_handle); */
206 #define MAX_DB_CLIENT 100
208 /* static int _db_handle_count = 0; */
209 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{0, 0}, };
211 sqlite3 *emstorage_get_db_handle()
213 EM_DEBUG_FUNC_BEGIN();
215 pthread_t current_thread_id = THREAD_SELF();
216 sqlite3 *result_db_handle = NULL;
218 ENTER_CRITICAL_SECTION(_db_handle_lock);
219 for (i = 0; i < MAX_DB_CLIENT; i++) {
220 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
221 EM_DEBUG_LOG("found db handle at [%d]", i);
222 result_db_handle = _db_handle_list[i].db_handle;
226 LEAVE_CRITICAL_SECTION(_db_handle_lock);
228 if (!result_db_handle)
229 EM_DEBUG_EXCEPTION("Can't find proper handle for [%d]", current_thread_id);
232 return result_db_handle;
235 int emstorage_set_db_handle(sqlite3 *db_handle)
237 EM_DEBUG_FUNC_BEGIN();
238 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
239 pthread_t current_thread_id = THREAD_SELF();
241 ENTER_CRITICAL_SECTION(_db_handle_lock);
242 for (i = 0; i < MAX_DB_CLIENT; i++) {
243 if (_db_handle_list[i].thread_id == 0) {
244 _db_handle_list[i].thread_id = current_thread_id;
245 _db_handle_list[i].db_handle = db_handle;
246 EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
247 error_code = EMAIL_ERROR_NONE;
251 LEAVE_CRITICAL_SECTION(_db_handle_lock);
253 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
254 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
256 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
260 int emstorage_remove_db_handle()
262 EM_DEBUG_FUNC_BEGIN();
263 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
264 ENTER_CRITICAL_SECTION(_db_handle_lock);
265 for (i = 0; i < MAX_DB_CLIENT; i++)
267 if (_db_handle_list[i].thread_id == THREAD_SELF())
269 _db_handle_list[i].thread_id = 0;
270 _db_handle_list[i].db_handle = NULL;
271 EM_DEBUG_LOG("index [%d]", i);
272 error_code = EMAIL_ERROR_NONE;
276 LEAVE_CRITICAL_SECTION(_db_handle_lock);
278 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
279 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
281 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
286 int emstorage_reset_db_handle_list()
288 EM_DEBUG_FUNC_BEGIN();
291 ENTER_CRITICAL_SECTION(_db_handle_lock);
292 for (i = 0; i < MAX_DB_CLIENT; i++)
294 _db_handle_list[i].thread_id = 0;
295 _db_handle_list[i].db_handle = NULL;
297 LEAVE_CRITICAL_SECTION(_db_handle_lock)
300 return EMAIL_ERROR_NONE;
303 sqlite3 *emstorage_get_db_connection()
305 sqlite3 *tmp_db_handle = emstorage_get_db_handle();
306 if (NULL == tmp_db_handle)
307 tmp_db_handle = emstorage_db_open(NULL);
308 return tmp_db_handle;
312 #else /* _MULTIPLE_DB_HANDLE */
313 #define _DISCONNECT_DB /* db_util_close(_db_handle); */
315 sqlite3 *_db_handle = NULL;
317 sqlite3 *emstorage_get_db_connection()
319 if (NULL == _db_handle)
320 emstorage_db_open(NULL);
323 #endif /* _MULTIPLE_DB_HANDLE */
325 /* ------------------------------------------------------------------------------ */
326 /* Mutex using shared memory */
329 pthread_mutex_t mutex;
333 mmapped_t *mapped_for_db_lock = NULL;
334 int shm_fd_for_db_lock = 0;
336 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
337 mmapped_t *mapped_for_generating_mail_id = NULL;
338 int shm_fd_for_generating_mail_id = 0;
339 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
341 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
342 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
343 { _timedlock_shm_mutex(&mapped_for_db_lock, 2); return_value = function_call; _unlockshm_mutex(&mapped_for_db_lock); }
344 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
345 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
346 { return_value = function_call; }
347 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
349 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
351 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
354 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
355 return EMAIL_ERROR_INVALID_PARAM;
358 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
362 EM_DEBUG_LOG("** Create SHM FILE **");
363 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
364 EM_DEBUG_EXCEPTION("ftruncate failed: %s", strerror(errno));
365 return EMAIL_ERROR_SYSTEM_FAILURE;
368 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
369 if (m == MAP_FAILED) {
370 EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
371 return EMAIL_ERROR_SYSTEM_FAILURE;
376 pthread_mutexattr_t mattr;
377 pthread_mutexattr_init(&mattr);
378 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
379 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
380 pthread_mutex_init(&(m->mutex), &mattr);
381 pthread_mutexattr_destroy(&mattr);
384 EM_DEBUG_EXCEPTION("shm_open failed: %s", strerror(errno));
385 return EMAIL_ERROR_SYSTEM_FAILURE;
389 return EMAIL_ERROR_NONE;
392 int emstorage_shm_file_destroy(const char *shm_file_name)
394 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
396 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
397 return EMAIL_ERROR_INVALID_PARAM;
400 if (shm_unlink(shm_file_name) != 0)
401 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", strerror(errno));
403 return EMAIL_ERROR_NONE;
406 static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
408 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
410 if(!shm_file_name || !param_shm_fd || !param_mapped) {
411 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
412 return EMAIL_ERROR_INVALID_PARAM;
415 if (!(*param_mapped)) {
416 EM_DEBUG_LOG("** mapping begin **");
418 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
419 *param_shm_fd = shm_open(shm_file_name, O_RDWR, 0);
420 if ((*param_shm_fd) == -1) {
421 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", strerror(errno));
422 return EMAIL_ERROR_SYSTEM_FAILURE;
425 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
427 if (tmp == MAP_FAILED) {
428 EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
429 return EMAIL_ERROR_SYSTEM_FAILURE;
435 return EMAIL_ERROR_NONE;
438 static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
440 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
443 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
444 return EMAIL_ERROR_INVALID_PARAM;
447 struct timespec abs_time;
448 clock_gettime(CLOCK_REALTIME, &abs_time);
449 abs_time.tv_sec += sec;
451 int err = pthread_mutex_timedlock(&((*param_mapped)->mutex), &abs_time);
453 if (err == EOWNERDEAD) {
454 err = pthread_mutex_consistent(&((*param_mapped)->mutex));
455 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(err));
458 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(err));
463 return EMAIL_ERROR_NONE;
466 void _unlockshm_mutex(mmapped_t **param_mapped)
468 EM_DEBUG_FUNC_BEGIN();
469 pthread_mutex_unlock(&((*param_mapped)->mutex));
472 /* ------------------------------------------------------------------------------ */
475 static int _open_counter = 0;
476 static int g_transaction = false;
478 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name);
479 static int _read_password_from_secure_storage(char *file_name, char **password);
481 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
482 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
486 const char *object_name;
487 unsigned int data_flag;
490 static const email_db_object_t _g_db_tables[] =
492 { "mail_read_mail_uid_tbl", 1},
494 { "mail_attachment_tbl", 1},
498 static const email_db_object_t _g_db_indexes[] =
500 { "mail_read_mail_uid_idx1", 1},
502 { "mail_attachment_idx1", 1},
508 CREATE_TABLE_MAIL_ACCOUNT_TBL,
509 CREATE_TABLE_MAIL_BOX_TBL,
510 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
511 CREATE_TABLE_MAIL_RULE_TBL,
512 CREATE_TABLE_MAIL_TBL,
513 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
514 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
515 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
517 CREATE_TABLE_MAIL_MEETING_TBL,
518 #ifdef __FEATURE_LOCAL_ACTIVITY__
519 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
521 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
522 CREATE_TABLE_MAIL_TASK_TBL,
523 #ifdef __FEATURE_BODY_SEARCH__
524 CREATE_TABLE_MAIL_TEXT_TBL,
528 CREATE_TABLE_MAIL_ACCOUNT_IDX,
529 CREATE_TABLE_MAIL_BOX_IDX,
530 CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
531 CREATE_TABLE_MAIL_IDX,
532 CREATE_TABLE_MAIL_ATTACHMENT_IDX,
533 CREATE_TABLE_MAIL_MEETING_IDX,
534 CREATE_TABLE_MAIL_TASK_IDX,
535 CREATE_TABLE_MAIL_DATETIME_IDX,
536 CREATE_TABLE_MAIL_THREAD_IDX,
542 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
543 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
544 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
549 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
550 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
551 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
552 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
553 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
554 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
555 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
556 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
557 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
558 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
559 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
570 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
571 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
572 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
573 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
574 ALIAS_IDX_IN_MAIL_BOX_TBL,
575 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
576 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
577 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
578 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
579 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
584 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
585 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
586 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
587 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
588 S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
589 DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL,
590 DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL,
591 FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL,
592 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
595 #ifdef __FEATURE_BODY_SEARCH__
598 MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
599 ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
600 MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
601 BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
602 FIELD_COUNT_OF_MAIL_TEXT_TBL,
608 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
609 RULE_ID_IDX_IN_MAIL_RULE_TBL,
610 TYPE_IDX_IN_MAIL_RULE_TBL,
611 VALUE_IDX_IN_MAIL_RULE_TBL,
612 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
613 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
614 FLAG1_IDX_IN_MAIL_RULE_TBL,
615 FLAG2_IDX_IN_MAIL_RULE_TBL,
620 MAIL_ID_IDX_IN_MAIL_TBL = 0,
621 ACCOUNT_ID_IDX_IN_MAIL_TBL,
622 MAILBOX_ID_IDX_IN_MAIL_TBL,
623 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
624 SUBJECT_IDX_IN_MAIL_TBL,
625 DATETIME_IDX_IN_MAIL_TBL,
626 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
627 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
628 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
629 MESSAGE_ID_IDX_IN_MAIL_TBL,
630 REFERENCE_ID_IDX_IN_MAIL_TBL,
631 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
632 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
633 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
634 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
635 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
636 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
637 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
638 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
639 ALIAS_SENDER_IDX_IN_MAIL_TBL,
640 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
641 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
642 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
643 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
644 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
645 MAIL_SIZE_IDX_IN_MAIL_TBL,
646 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
647 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
648 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
649 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
650 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
651 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
652 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
653 DRM_STATUS_IDX_IN_MAIL_TBL,
654 PRIORITY_IDX_IN_MAIL_TBL,
655 SAVE_STATUS_IDX_IN_MAIL_TBL,
656 LOCK_STATUS_IDX_IN_MAIL_TBL,
657 REPORT_STATUS_IDX_IN_MAIL_TBL,
658 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
659 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
660 THREAD_ID_IDX_IN_MAIL_TBL,
661 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
662 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
663 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
664 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
665 DIGEST_TYPE_IDX_IN_MAIL_TBL,
666 SMIME_TYPE_IDX_IN_MAIL_TBL,
667 SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
668 EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
669 EAS_DATA_IDX_IN_MAIL_TBL,
670 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
675 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
676 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
677 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
678 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
679 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
680 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
681 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
682 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
683 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
684 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
685 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
686 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
687 #ifdef __ATTACHMENT_OPTI__
688 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
689 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
694 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
695 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
696 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
697 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
698 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
699 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
701 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
702 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
703 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
704 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
705 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
706 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
707 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
711 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
712 typedef struct _em_mail_id_list {
714 struct _em_mail_id_list *next;
717 static char *g_test_query[] = {
718 /* 1. select mail_account_tbl */
721 " incoming_server_type, "
722 " incoming_server_address, "
723 " user_email_address, "
724 " incoming_server_user_name, "
725 " incoming_server_password, "
727 " incoming_server_port_number, "
728 " incoming_server_secure_connection, "
729 " outgoing_server_type, "
730 " outgoing_server_address, "
731 " outgoing_server_port_number, "
732 " outgoing_server_need_authentication, "
733 " outgoing_server_secure_connection, "
734 " outgoing_server_user_name, "
735 " outgoing_server_password, "
740 " keep_mails_on_pop_server_after_download, "
744 " incoming_server_requires_apop"
746 " is_preset_account, "
750 " req_delivery_receipt, "
751 " req_read_receipt, "
755 " display_name_from, "
757 " forward_with_files, "
761 ", add_my_address_to_bcc"
770 " FROM mail_account_tbl",
771 /* 2. select mail_box_tbl */
781 " total_mail_count_on_server, "
782 " has_archived_mails, "
786 " FROM mail_box_tbl ",
787 /* 3. select mail_read_mail_uid_tbl */
798 " FROM mail_read_mail_uid_tbl ",
799 /* 4. select mail_rule_tbl */
806 " target_mailbox_id, "
809 " FROM mail_rule_tbl ",
810 /* 5. select mail_tbl */
819 " server_mail_status, "
820 " server_mailbox_name, "
823 " reference_mail_id, "
824 " full_address_from, "
825 " full_address_reply, "
828 " full_address_bcc, "
829 " full_address_return, "
830 " email_address_sender, "
831 " email_address_recipient, "
834 " body_download_status, "
837 " file_path_mime_entity, "
839 " flags_seen_field ,"
840 " flags_deleted_field ,"
841 " flags_flagged_field ,"
842 " flags_answered_field ,"
843 " flags_recent_field ,"
844 " flags_draft_field ,"
845 " flags_forwarded_field,"
851 " attachment_count, "
852 " inline_content_count, "
854 " thread_item_count, "
856 " meeting_request_status, "
861 /* 6. select mail_attachment_tbl */
870 " attachment_save_status, "
871 " attachment_drm_type, "
872 " attachment_drm_method, "
873 " attachment_inline_content_status, "
874 " attachment_mime_type "
875 " FROM mail_attachment_tbl ",
877 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
886 " FROM mail_partial_body_activity_tbl ",
893 " meeting_response, "
897 " global_object_id, "
900 " standard_time_start_date, "
903 " daylight_time_start_date, "
905 " FROM mail_meeting_tbl ",
907 #ifdef __FEATURE_LOCAL_ACTIVITY__
916 " FROM mail_local_activity_tbl ",
924 " expiration_month, "
926 " issue_organization_name, "
931 " FROM mail_certificate_tbl ",
932 #ifdef __FEATURE_BODY_SEARCH__
938 " FROM mail_text_tbl ",
945 " task_parameter_length, "
948 " FROM mail_task_tbl ",
953 static int _get_table_field_data_char(char **table, char *buf, int index)
955 if ((table == NULL) || (buf == NULL) || (index < 0)) {
956 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
960 if (table[index] != NULL) {
961 *buf = (char)atoi(table[index]);
965 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
971 static int _get_table_field_data_int(char **table, int *buf, int index)
973 if ((table == NULL) || (buf == NULL) || (index < 0)) {
974 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
978 if (table[index] != NULL) {
979 *buf = atoi(table[index]);
983 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
989 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
991 if ((table == NULL) || (buf == NULL) || (index < 0)) {
992 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
996 if (table[index] != NULL) {
997 *buf = (time_t)atol(table[index]);
1001 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1007 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1011 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1012 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1016 char *pTemp = table[index];
1021 sLen = EM_SAFE_STRLEN(pTemp);
1023 *buf = (char *) em_malloc(sLen + 1);
1025 EM_DEBUG_EXCEPTION("malloc is failed");
1028 strncpy(*buf, pTemp, sLen);
1033 #ifdef _PRINT_STORAGE_LOG_
1035 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1037 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1045 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1047 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1048 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1052 char *pTemp = table[index];
1057 memset(buf, 0, buffer_size);
1058 strncpy(buf, pTemp, buffer_size - 1);
1060 #ifdef _PRINT_STORAGE_LOG_
1062 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1064 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1070 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1072 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1073 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1077 char *temp_buffer = table[index];
1079 if (temp_buffer == NULL)
1082 *buffer = malloc(buffer_size);
1083 if(*buffer == NULL) {
1084 EM_DEBUG_EXCEPTION("allocation failed.");
1087 memset(*buffer, 0, buffer_size);
1088 memcpy(*buffer, temp_buffer, buffer_size);
1090 #ifdef _PRINT_STORAGE_LOG_
1092 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1094 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1100 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1102 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1103 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1107 if (sqlite3_column_text(hStmt, index) != NULL) {
1108 *buf = (char)sqlite3_column_int(hStmt, index);
1109 #ifdef _PRINT_STORAGE_LOG_
1110 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1115 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1120 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1122 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1123 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1127 if (sqlite3_column_text(hStmt, index) != NULL) {
1128 *buf = sqlite3_column_int(hStmt, index);
1129 #ifdef _PRINT_STORAGE_LOG_
1130 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1135 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1140 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1142 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1143 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1147 if (sqlite3_column_text(hStmt, index) != NULL) {
1148 *buf = (time_t)sqlite3_column_int(hStmt, index);
1149 #ifdef _PRINT_STORAGE_LOG_
1150 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1155 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1159 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1161 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1162 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1167 sLen = sqlite3_column_bytes(hStmt, index);
1169 #ifdef _PRINT_STORAGE_LOG_
1170 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1174 *buf = (char *) em_malloc(sLen + 1);
1175 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1180 #ifdef _PRINT_STORAGE_LOG_
1182 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1184 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1190 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1192 if( !hStmt || !buf || (index < 0)) { /*prevent 39618*/
1193 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1198 sLen = sqlite3_column_bytes(hStmt, index);
1200 #ifdef _PRINT_STORAGE_LOG_
1201 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1205 *buf = (char *) em_malloc(sLen);
1206 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1213 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1215 if (!hStmt || !buf || (index < 0)) { /*prevent 39620*/
1216 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1221 sLen = sqlite3_column_bytes(hStmt, index);
1223 #ifdef _PRINT_STORAGE_LOG_
1224 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1228 memset(buf, 0, buffer_size);
1229 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1234 #ifdef _PRINT_STORAGE_LOG_
1235 EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1241 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1243 if ((hStmt == NULL) || (index < 0)) {
1244 EM_DEBUG_EXCEPTION("index[%d]", index);
1248 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1250 if (ret != SQLITE_OK) {
1251 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1258 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1260 if ((hStmt == NULL) || (index < 0)) {
1261 EM_DEBUG_EXCEPTION("index[%d]", index);
1265 int ret = sqlite3_bind_int(hStmt, index+1, value);
1267 if (ret != SQLITE_OK) {
1268 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1275 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1277 if ((hStmt == NULL) || (index < 0)) {
1278 EM_DEBUG_EXCEPTION("index[%d]", index);
1282 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1284 if (ret != SQLITE_OK) {
1285 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1292 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1294 if ((hStmt == NULL) || (index < 0)) {
1295 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1299 #ifdef _PRINT_STORAGE_LOG_
1300 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1305 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1307 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1309 if (ret != SQLITE_OK) {
1310 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1317 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1319 if ((hStmt == NULL) || (index < 0)) {
1320 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1324 #ifdef _PRINT_STORAGE_LOG_
1325 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1330 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1332 ret = sqlite3_bind_null(hStmt, index+1);
1334 if (ret != SQLITE_OK) {
1335 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1342 static int _delete_temp_file(const char *path)
1344 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1347 struct dirent *entry = NULL;
1349 char buf[1024] = {0x00, };
1351 if ((dp = opendir(path)) == NULL) {
1352 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1356 while ((entry = readdir(dp)) != NULL) {
1357 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1362 EM_DEBUG_FUNC_END();
1366 char *cpy_str(char *src)
1371 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1372 EM_DEBUG_EXCEPTION("mailoc failed...");
1375 strncpy(p, src, EM_SAFE_STRLEN(src));
1381 static void _emstorage_close_once(void)
1383 EM_DEBUG_FUNC_BEGIN();
1385 DELETE_CRITICAL_SECTION(_transactionBeginLock);
1386 DELETE_CRITICAL_SECTION(_transactionEndLock);
1388 EM_DEBUG_FUNC_END();
1391 INTERNAL_FUNC int emstorage_close(int *err_code)
1393 EM_DEBUG_FUNC_BEGIN();
1396 int error = EMAIL_ERROR_NONE;
1398 emstorage_db_close(&error);
1400 if (--_open_counter == 0)
1401 _emstorage_close_once();
1405 if (err_code != NULL)
1408 EM_DEBUG_FUNC_END("ret [%d]", ret);
1412 static void *_emstorage_open_once(int *err_code)
1414 EM_DEBUG_FUNC_BEGIN();
1416 int error = EMAIL_ERROR_NONE;
1418 mkdir(USERDATA_PATH, DIRECTORY_PERMISSION);
1419 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1420 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1421 mkdir(MAILHOME, DIRECTORY_PERMISSION);
1422 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1424 _delete_temp_file(MAILTEMP);
1426 g_transaction = false;
1428 if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) {
1429 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1434 if (err_code != NULL)
1441 /* pData : a parameter which is registered when busy handler is registerd */
1442 /* count : retry count */
1443 #define EMAIL_STORAGE_MAX_RETRY_COUNT 20
1444 static int _callback_sqlite_busy_handler(void *pData, int count)
1446 EM_DEBUG_LOG("Busy Handler Called!!: [%d]", count);
1447 usleep(200000); /* sleep time when SQLITE_LOCK */
1449 /* retry will be stopped if busy handler return 0 */
1450 return EMAIL_STORAGE_MAX_RETRY_COUNT - count;
1453 static int _delete_all_files_and_directories(int *err_code)
1455 EM_DEBUG_FUNC_BEGIN();
1457 int error = EMAIL_ERROR_NONE;
1460 if (!emstorage_delete_file(EMAIL_SERVICE_DB_FILE_PATH, &error)) {
1461 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1462 EM_DEBUG_EXCEPTION("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1467 if (!emstorage_delete_dir(MAILHOME, &error)) {
1468 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1477 EM_DEBUG_FUNC_END();
1481 static int _recovery_from_malformed_db_file(int *err_code)
1483 EM_DEBUG_FUNC_BEGIN();
1485 int error = EMAIL_ERROR_NONE;
1488 /* Delete all files and directories */
1489 if (!_delete_all_files_and_directories(&error)) {
1490 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1494 /* Delete all accounts on MyAccount */
1496 /* Delete all managed connection to DB */
1497 emstorage_reset_db_handle_list();
1504 EM_DEBUG_FUNC_END();
1509 INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
1511 EM_DEBUG_FUNC_BEGIN();
1513 int error = EMAIL_ERROR_NONE;
1516 EM_DEBUG_LOG("*sqlite_handle[%p]", *sqlite_handle);
1518 if (*sqlite_handle) { /*prevent 33351*/
1519 EM_DEBUG_LOG(">>>>> DB Already Opened......");
1520 if (err_code != NULL)
1526 EM_DEBUG_LOG("Open DB");
1527 EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1528 if (SQLITE_OK != rc) {
1529 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1530 error = EMAIL_ERROR_DB_FAILURE;
1531 db_util_close(*sqlite_handle);
1532 *sqlite_handle = NULL;
1534 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1535 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1536 if (!_recovery_from_malformed_db_file(&error)) {
1537 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1541 EM_DEBUG_LOG("Open DB again");
1542 EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1543 if (SQLITE_OK != rc) {
1544 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1545 error = EMAIL_ERROR_DB_FAILURE;
1546 db_util_close(*sqlite_handle);
1547 *sqlite_handle = NULL;
1548 goto FINISH_OFF; /*prevent 33351*/
1554 EM_DEBUG_LOG(">>>>> DB Handle : *sqlite_handle[%p]", *sqlite_handle);
1556 /* register busy handler */
1557 EM_DEBUG_LOG(">>>>> Register busy handler.....");
1558 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1559 if (SQLITE_OK != rc) {
1560 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1561 error = EMAIL_ERROR_DB_FAILURE;
1562 db_util_close(*sqlite_handle);
1563 *sqlite_handle = NULL;
1571 if (err_code != NULL)
1574 EM_DEBUG_FUNC_END("ret [%d]", ret);
1578 INTERNAL_FUNC sqlite3* emstorage_db_open(int *err_code)
1580 EM_DEBUG_FUNC_BEGIN();
1581 #ifdef _MULTIPLE_DB_HANDLE
1582 sqlite3 *_db_handle = NULL;
1585 int error = EMAIL_ERROR_NONE;
1587 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1589 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1590 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1591 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1593 if (!em_db_open(&_db_handle, &error)) {
1594 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1598 #ifdef _MULTIPLE_DB_HANDLE
1599 emstorage_set_db_handle(_db_handle);
1603 if (err_code != NULL)
1606 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1610 INTERNAL_FUNC int emstorage_db_close(int *err_code)
1612 EM_DEBUG_FUNC_BEGIN();
1613 #ifdef _MULTIPLE_DB_HANDLE
1614 sqlite3 *_db_handle = emstorage_get_db_handle();
1617 int error = EMAIL_ERROR_NONE;
1620 DELETE_CRITICAL_SECTION(_transactionBeginLock);
1621 DELETE_CRITICAL_SECTION(_transactionEndLock);
1624 ret = db_util_close(_db_handle);
1626 if (ret != SQLITE_OK) {
1627 EM_DEBUG_EXCEPTION(" db_util_close fail - %d", ret);
1628 error = EMAIL_ERROR_DB_FAILURE;
1632 #ifdef _MULTIPLE_DB_HANDLE
1633 emstorage_remove_db_handle();
1641 if (err_code != NULL)
1644 EM_DEBUG_FUNC_END("ret [%d]", ret);
1649 INTERNAL_FUNC int emstorage_open(int *err_code)
1651 EM_DEBUG_FUNC_BEGIN();
1654 int error = EMAIL_ERROR_NONE;
1658 retValue = mkdir(DB_PATH, DIRECTORY_PERMISSION);
1660 EM_DEBUG_LOG("mkdir return- %d", retValue);
1661 EM_DEBUG_LOG("emstorage_open - before db_util_open - pid = %d", getpid());
1663 if (emstorage_db_open(&error) == NULL) {
1664 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
1669 if (_open_counter++ == 0)
1670 _emstorage_open_once(&error);
1675 if (err_code != NULL)
1678 EM_DEBUG_FUNC_END("ret [%d]", ret);
1682 INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *err_code)
1684 EM_DEBUG_FUNC_BEGIN();
1686 int error = EMAIL_ERROR_NONE;
1687 int rc = -1, ret = false;
1689 char sql_query_string[QUERY_SIZE] = {0, };
1690 char **create_table_query = NULL;
1692 error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
1693 if (error != EMAIL_ERROR_NONE) {
1694 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
1698 if (query_len < CREATE_TABLE_MAX) {
1699 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
1700 error = EMAIL_ERROR_SYSTEM_FAILURE;
1704 sqlite3 *local_db_handle = emstorage_get_db_connection();
1705 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
1710 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
1711 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1712 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1713 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1715 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
1717 if (atoi(result[1]) < 1) {
1718 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1719 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1720 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1722 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
1724 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1726 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1727 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)));
1729 /* create mail_account_tbl unique index */
1730 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
1732 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1733 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)));
1735 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1737 } /* mail_account_tbl */
1738 else if (type == EMAIL_CREATE_DB_CHECK) {
1739 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
1740 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)));
1743 sqlite3_free_table(result);
1746 /* 2. create mail_box_tbl */
1747 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
1749 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1750 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1751 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1753 if (atoi(result[1]) < 1) {
1754 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1755 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1756 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1758 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
1760 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1762 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1763 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1764 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1766 /* create mail_local_mailbox_tbl unique index */
1767 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
1769 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1770 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1771 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1773 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1775 } /* mail_box_tbl */
1776 else if (type == EMAIL_CREATE_DB_CHECK) {
1777 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
1778 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1779 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
1781 sqlite3_free_table(result);
1783 /* 3. create mail_read_mail_uid_tbl */
1784 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
1785 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1786 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1787 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1790 if (atoi(result[1]) < 1) {
1791 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1792 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1793 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1795 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
1797 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1799 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1800 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1801 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1803 /* create mail_read_mail_uid_tbl unique index */
1804 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
1806 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1807 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1808 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1809 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1811 } /* mail_read_mail_uid_tbl */
1812 else if (type == EMAIL_CREATE_DB_CHECK) {
1813 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
1814 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1815 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
1817 sqlite3_free_table(result);
1820 /* 4. create mail_rule_tbl */
1821 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
1823 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1824 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1825 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1827 if (atoi(result[1]) < 1) {
1828 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1829 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1830 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1832 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
1834 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1836 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1837 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1838 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1839 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1841 } /* mail_rule_tbl */
1842 else if (type == EMAIL_CREATE_DB_CHECK) {
1843 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
1844 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1845 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
1847 sqlite3_free_table(result);
1850 /* 5. create mail_tbl */
1851 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
1852 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1853 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1854 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1856 if (atoi(result[1]) < 1) {
1857 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1858 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1859 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1860 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
1862 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1864 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1865 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1866 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1868 /* create mail_tbl unique index */
1869 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
1870 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1871 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1872 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1874 /* create mail_tbl index for date_time */
1875 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
1876 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1877 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1878 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1880 /* create mail_tbl index for thread_item_count */
1881 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
1882 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1883 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1884 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1886 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1888 /* just one time call */
1889 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
1891 else if (type == EMAIL_CREATE_DB_CHECK) {
1892 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
1893 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1894 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
1896 sqlite3_free_table(result);
1899 /* 6. create mail_attachment_tbl */
1900 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
1901 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1902 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1903 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1905 if (atoi(result[1]) < 1) {
1906 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1907 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1908 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1909 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
1911 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1913 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1914 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1915 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1917 /* create mail_attachment_tbl unique index */
1918 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
1920 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1921 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1922 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1923 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1925 } /* mail_attachment_tbl */
1926 else if (type == EMAIL_CREATE_DB_CHECK) {
1927 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
1928 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1929 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
1931 sqlite3_free_table(result);
1933 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1935 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
1936 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1937 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1938 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1940 if (atoi(result[1]) < 1) {
1942 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1943 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1944 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1946 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
1948 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1950 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1951 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1952 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1953 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1955 } /* mail_rule_tbl */
1956 else if (type == EMAIL_CREATE_DB_CHECK) {
1957 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
1958 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1959 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
1961 sqlite3_free_table(result);
1963 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
1965 /* create mail_meeting_tbl */
1966 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
1967 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1968 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1969 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1971 if (atoi(result[1]) < 1) {
1972 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1973 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1974 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1976 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
1978 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1980 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1981 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1982 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1983 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
1985 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1986 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1987 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1988 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1990 } /* mail_contact_sync_tbl */
1991 else if (type == EMAIL_CREATE_DB_CHECK) {
1992 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
1993 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1994 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
1996 sqlite3_free_table(result);
1998 #ifdef __FEATURE_LOCAL_ACTIVITY__
2000 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2001 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2002 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2003 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2005 if (atoi(result[1]) < 1) {
2007 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2008 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2009 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2011 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2013 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2015 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2016 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2017 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2018 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2020 } /* mail_rule_tbl */
2021 else if (type == EMAIL_CREATE_DB_CHECK) {
2022 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2023 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2024 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2025 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2027 sqlite3_free_table(result);
2029 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2030 /* create mail_certificate_tbl */
2031 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2032 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2033 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2034 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)));
2036 if (atoi(result[1]) < 1) {
2037 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2038 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2040 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2042 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2044 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2045 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)));
2046 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2047 } /* mail_contact_sync_tbl */
2048 else if (type == EMAIL_CREATE_DB_CHECK) {
2049 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2050 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)));
2053 /* create mail_task_tbl */
2054 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2055 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2056 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)));
2058 if (atoi(result[1]) < 1) {
2059 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2060 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2062 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2064 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2066 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2067 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)));
2068 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2070 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2071 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)));
2072 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2073 } /* mail_task_tbl */
2074 else if (type == EMAIL_CREATE_DB_CHECK) {
2075 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2076 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)));
2079 #ifdef __FEATURE_BODY_SEARCH__
2080 /* create mail_text_tbl */
2081 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2082 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2083 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)));
2085 if (atoi(result[1]) < 1) {
2086 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2087 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2089 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2091 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2093 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2094 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)));
2096 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2097 } /* mail_text_tbl */
2098 else if (type == EMAIL_CREATE_DB_CHECK) {
2099 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2100 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_TEXT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2104 sqlite3_free_table(result);
2110 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2113 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2119 if (create_table_query) {
2121 for (i = 0; i < query_len; i++) {
2122 if (create_table_query[i]) {
2123 EM_SAFE_FREE(create_table_query[i]);
2126 EM_SAFE_FREE(create_table_query);
2129 if (err_code != NULL)
2132 EM_DEBUG_FUNC_END("ret [%d]", ret);
2136 /* Query series --------------------------------------------------------------*/
2138 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)
2140 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);
2142 int error = EMAIL_ERROR_NONE;
2143 DB_STMT hStmt = NULL;
2144 char sql_query_string[QUERY_SIZE] = {0, };
2147 sqlite3 *local_db_handle = NULL;
2149 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2150 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2151 return EMAIL_ERROR_INVALID_PARAM;
2154 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
2155 local_db_handle = emstorage_get_db_connection();
2157 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2159 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT COUNT(*) FROM mail_tbl");
2160 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2162 if (output_total_mail_count) {
2163 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2164 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2165 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2167 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2168 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2169 ("sqlite3_step fail:%d", rc));
2170 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2173 if (output_unseen_mail_count) {
2174 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2176 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2177 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2178 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2180 *output_unseen_mail_count = atoi(result[1]);
2181 sqlite3_free_table(result);
2186 if (hStmt != NULL) {
2187 rc = sqlite3_finalize(hStmt);
2188 if (rc != SQLITE_OK) {
2189 EM_DEBUG_LOG("sqlite3_finalize failed [%d] : %s", rc, err_msg);
2190 error = EMAIL_ERROR_DB_FAILURE;
2194 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2197 EM_DEBUG_FUNC_END("error [%d]", error);
2201 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)
2203 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);
2210 int error = EMAIL_ERROR_NONE;
2211 int *result_mail_id_list = NULL;
2212 char **result = NULL;
2213 char sql_query_string[QUERY_SIZE] = {0, };
2214 sqlite3 *local_db_handle = emstorage_get_db_connection();
2216 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2217 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2218 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2220 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2222 /* Composing query */
2223 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl ");
2224 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2226 EM_DEBUG_LOG("query[%s].", sql_query_string);
2228 /* Performing query */
2229 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2230 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2231 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2235 /* to get mail list */
2237 EM_DEBUG_EXCEPTION("No mail found...");
2238 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2242 EM_DEBUG_LOG("There are [%d] mails.", count);
2244 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2245 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2246 error = EMAIL_ERROR_OUT_OF_MEMORY;
2250 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2252 for (i = 0; i < count; i++)
2253 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2255 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2257 *output_mail_id_list = result_mail_id_list;
2258 *output_mail_id_count = count;
2263 sqlite3_free_table(result);
2265 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2268 if(error != EMAIL_ERROR_NONE)
2269 EM_SAFE_FREE(result_mail_id_list);
2271 EM_DEBUG_FUNC_END("error [%d]", error);
2275 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)
2277 EM_DEBUG_FUNC_BEGIN();
2278 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2280 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list)?0:1;
2281 int local_inline_content_count = 0, local_attachment_count = 0;
2282 int cur_query = 0, base_count = 0, col_index;
2283 int ret = false, error = EMAIL_ERROR_NONE;
2284 char *date_time_string = NULL;
2285 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2286 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, scheduled_sending_time, eas_data_length, eas_data ";
2287 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2288 sqlite3 *local_db_handle = emstorage_get_db_connection();
2290 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2291 EM_IF_NULL_RETURN_VALUE(result_count, false);
2293 EMSTORAGE_START_READ_TRANSACTION(transaction);
2296 if (to_get_count) /* count only */
2297 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl");
2298 else /* mail list in plain form */
2299 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_tbl ", field_list);
2301 /* cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, conditional_clause); This code caused some crashes.*/
2302 strncat(sql_query_string, conditional_clause, QUERY_SIZE - cur_query);
2304 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
2306 /* performing query */
2307 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2308 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2309 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2310 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2311 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2314 base_count = ({ int i=0; char *tmp = field_list; for (i=0; tmp && *(tmp + 1); tmp = index(tmp + 1, ','), i++); i; });
2316 col_index = base_count;
2318 EM_DEBUG_LOG("base_count [%d]", base_count);
2323 EM_DEBUG_EXCEPTION("No mail found...");
2325 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2328 EM_DEBUG_LOG("There are [%d] mails.", count);
2331 /* to get mail list */
2333 EM_DEBUG_EXCEPTION("No mail found...");
2335 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2339 EM_DEBUG_LOG("There are [%d] mails.", count);
2340 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2341 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2342 error = EMAIL_ERROR_OUT_OF_MEMORY;
2346 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2347 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2348 for (i = 0; i < count; i++) {
2349 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2350 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2351 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2352 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2353 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2354 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2355 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2356 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2357 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2358 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2359 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2360 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2361 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2362 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2363 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2364 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2365 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2366 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2367 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2368 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2369 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2370 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2371 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2372 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2373 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2374 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2375 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2376 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2377 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2378 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2379 _get_table_field_data_blob(result, (void**)&(mail_list_item_from_tbl[i].eas_data), mail_list_item_from_tbl[i].eas_data_length, col_index++);
2381 mail_list_item_from_tbl[i].attachment_count = ((local_attachment_count - local_inline_content_count)>0)?1:0;
2383 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2384 EM_PROFILE_END(emstorage_query_mail_list_loop);
2387 sqlite3_free_table(result);
2392 EM_DEBUG_LOG("COUNT [%d]", count);
2395 *result_count = count;
2398 if (result_mail_list)
2399 *result_mail_list = mail_list_item_from_tbl;
2400 *result_count = count;
2403 EM_SAFE_FREE(mail_list_item_from_tbl);
2406 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2408 sqlite3_release_memory(-1);
2412 EM_SAFE_FREE(date_time_string);
2414 if (err_code != NULL)
2417 EM_PROFILE_END(emstorage_query_mail_list_func);
2418 EM_DEBUG_FUNC_END("ret [%d]", ret);
2423 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)
2425 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);
2427 if (!conditional_clause) {
2428 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2430 if (err_code != NULL)
2431 *err_code = EMAIL_ERROR_INVALID_PARAM;
2435 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2436 int error = EMAIL_ERROR_NONE;
2437 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2438 emstorage_mail_tbl_t* p_data_tbl = NULL;
2439 sqlite3 *local_db_handle = emstorage_get_db_connection();
2441 EMSTORAGE_START_READ_TRANSACTION(transaction);
2443 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2445 EM_DEBUG_LOG("Query [%s]", sql_query_string);
2447 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2448 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2449 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2452 EM_DEBUG_EXCEPTION("No mail found...");
2454 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2458 EM_DEBUG_LOG("There are [%d] mails.", count);
2459 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2460 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2461 error = EMAIL_ERROR_OUT_OF_MEMORY;
2465 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2466 for (i = 0; i < count; i++) {
2467 _get_table_field_data_int (result, &(p_data_tbl[i].mail_id), col_index++);
2468 _get_table_field_data_int (result, &(p_data_tbl[i].account_id), col_index++);
2469 _get_table_field_data_int (result, &(p_data_tbl[i].mailbox_id), col_index++);
2470 _get_table_field_data_int (result, &(p_data_tbl[i].mailbox_type), col_index++);
2471 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
2472 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
2473 _get_table_field_data_int (result, &(p_data_tbl[i].server_mail_status), col_index++);
2474 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
2475 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
2476 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
2477 _get_table_field_data_int (result, &(p_data_tbl[i].reference_mail_id), col_index++);
2478 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
2479 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
2480 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
2481 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
2482 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
2483 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
2484 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
2485 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
2486 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
2487 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
2488 _get_table_field_data_int (result, &(p_data_tbl[i].body_download_status), col_index++);
2489 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
2490 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
2491 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
2492 _get_table_field_data_int (result, &(p_data_tbl[i].mail_size), col_index++);
2493 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
2494 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
2495 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
2496 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
2497 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
2498 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
2499 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
2500 _get_table_field_data_int (result, &(p_data_tbl[i].DRM_status), col_index++);
2501 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].priority), col_index++);
2502 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].save_status), col_index++);
2503 _get_table_field_data_int (result, &(p_data_tbl[i].lock_status), col_index++);
2504 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].report_status), col_index++);
2505 _get_table_field_data_int (result, &(p_data_tbl[i].attachment_count), col_index++);
2506 _get_table_field_data_int (result, &(p_data_tbl[i].inline_content_count), col_index++);
2507 _get_table_field_data_int (result, &(p_data_tbl[i].thread_id), col_index++);
2508 _get_table_field_data_int (result, &(p_data_tbl[i].thread_item_count), col_index++);
2509 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
2510 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
2511 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].message_class), col_index++);
2512 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].digest_type), col_index++);
2513 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].smime_type), col_index++);
2514 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
2515 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
2516 _get_table_field_data_blob (result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
2517 /* check real body file... */
2518 if (p_data_tbl[i].body_download_status) {
2521 if (p_data_tbl[i].file_path_html && EM_SAFE_STRLEN(p_data_tbl[i].file_path_html) > 0) {
2522 if (stat(p_data_tbl[i].file_path_html, &buf) == -1) {
2524 p_data_tbl[i].body_download_status = 0;
2527 else if (p_data_tbl[i].file_path_plain && EM_SAFE_STRLEN(p_data_tbl[i].file_path_plain) > 0) {
2528 if (stat(p_data_tbl[i].file_path_plain, &buf) == -1){
2530 p_data_tbl[i].body_download_status = 0;
2534 p_data_tbl[i].body_download_status = 0;
2542 sqlite3_free_table(result);
2545 if (result_mail_tbl)
2546 *result_mail_tbl = p_data_tbl;
2547 *result_count = count;
2550 EM_SAFE_FREE(p_data_tbl);
2552 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2554 sqlite3_release_memory(-1);
2558 if (err_code != NULL)
2561 EM_DEBUG_FUNC_END("ret [%d]", ret);
2565 #ifdef __FEATURE_BODY_SEARCH__
2566 INTERNAL_FUNC int emstorage_query_mail_text_tbl(const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code)
2568 EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_text_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_text_tbl, result_count, transaction, err_code);
2570 if (!conditional_clause) {
2571 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2572 if (err_code != NULL)
2573 *err_code = EMAIL_ERROR_INVALID_PARAM;
2581 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
2582 int error = EMAIL_ERROR_NONE;
2583 char **result = NULL;
2584 char sql_query_string[QUERY_SIZE] = {0, };
2585 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
2586 sqlite3 *local_db_handle = emstorage_get_db_connection();
2588 EMSTORAGE_START_READ_TRANSACTION(transaction);
2590 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
2592 EM_DEBUG_LOG("Query [%s]", sql_query_string);
2594 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2595 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2596 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2599 EM_DEBUG_EXCEPTION("No mail found...");
2601 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2605 EM_DEBUG_LOG("There are [%d] mails.", count);
2606 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
2607 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
2608 error = EMAIL_ERROR_OUT_OF_MEMORY;
2612 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
2613 for (i = 0; i < count; i++) {
2614 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
2615 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
2616 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
2617 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
2624 sqlite3_free_table(result);
2627 if (result_mail_text_tbl)
2628 *result_mail_text_tbl = p_data_tbl;
2629 *result_count = count;
2632 EM_SAFE_FREE(p_data_tbl);
2634 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2636 sqlite3_release_memory(-1);
2640 if (err_code != NULL)
2643 EM_DEBUG_FUNC_END("ret [%d]", ret);
2648 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)
2650 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);
2656 int error = EMAIL_ERROR_NONE;
2658 int total_count = 0;
2660 char sql_query_string[QUERY_SIZE] = {0, };
2661 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 ";
2662 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
2663 sqlite3 *local_db_handle = emstorage_get_db_connection();
2665 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2667 if (input_get_mail_count == 0) { /* without mail count */
2669 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s", fields, input_conditional_clause);
2671 else { /* with read count and total count */
2673 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_id, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl %s GROUP BY mailbox_id) AS MT ON MBT.mailbox_id = MT.mailbox_id %s %s", fields, input_conditional_clause, input_conditional_clause, input_ordering_clause);
2676 EM_DEBUG_LOG("query[%s]", sql_query_string);
2678 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
2679 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2680 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)))
2682 EM_DEBUG_LOG("result count [%d]", count);
2685 EM_DEBUG_EXCEPTION("Can't find mailbox");
2686 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2690 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
2691 EM_DEBUG_EXCEPTION("em_malloc failed...");
2692 error = EMAIL_ERROR_OUT_OF_MEMORY;
2696 for (i = 0; i < count; i++) {
2697 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
2698 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
2699 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
2700 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2701 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
2702 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
2703 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
2704 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
2705 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
2706 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
2707 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
2708 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
2709 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
2711 if (input_get_mail_count == 1) {
2712 _get_table_field_data_int(result, &(total_count), col_index++);
2713 p_data_tbl[i].total_mail_count_on_local = total_count;
2714 _get_table_field_data_int(result, &(read_count), col_index++);
2715 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
2722 sqlite3_free_table(result);
2724 if (error == EMAIL_ERROR_NONE) {
2725 *output_mailbox_list = p_data_tbl;
2726 *output_mailbox_count = count;
2727 EM_DEBUG_LOG("Mailbox Count [ %d]", count);
2730 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2732 sqlite3_release_memory(-1);
2736 EM_DEBUG_FUNC_END("error [%d]", error);
2740 /* Query series --------------------------------------------------------------*/
2742 INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, int transaction, int *err_code)
2744 EM_DEBUG_FUNC_BEGIN();
2747 if (err_code != NULL)
2748 *err_code = EMAIL_ERROR_INVALID_PARAM;
2752 int rc = -1, ret = false;
2753 int error = EMAIL_ERROR_NONE;
2756 char sql_query_string[QUERY_SIZE] = {0, };
2758 sqlite3 *local_db_handle = emstorage_get_db_connection();
2759 EMSTORAGE_START_READ_TRANSACTION(transaction);
2761 SNPRINTF(sql_query_string, sizeof(sql_query_string),
2762 "SELECT COUNT(*) FROM mail_account_tbl "
2764 " user_email_address = '%s' AND "
2765 " incoming_server_user_name = '%s' AND "
2766 " incoming_server_type = %d AND "
2767 " incoming_server_address = '%s' AND "
2768 " outgoing_server_user_name = '%s' AND "
2769 " outgoing_server_type = %d AND "
2770 " outgoing_server_address = '%s'; ",
2771 account->user_email_address,
2772 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
2773 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
2775 EM_DEBUG_LOG("Query[%s]", sql_query_string);
2776 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2777 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2778 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2780 count = atoi(result[1]);
2781 sqlite3_free_table(result);
2783 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
2785 if (count == 0) { /* not duplicated account */
2787 EM_DEBUG_LOG("NOT duplicated account: user_email_address[%s]", account->user_email_address);
2789 else { /* duplicated account */
2791 EM_DEBUG_LOG("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
2792 error = EMAIL_ERROR_ALREADY_EXISTS;
2797 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2800 if (err_code != NULL)
2803 EM_DEBUG_FUNC_END("ret [%d]", ret);
2808 INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *err_code)
2810 EM_DEBUG_FUNC_BEGIN();
2813 if (err_code != NULL)
2814 *err_code = EMAIL_ERROR_INVALID_PARAM;
2818 int rc = -1, ret = false;
2819 int error = EMAIL_ERROR_NONE;
2821 DB_STMT hStmt = NULL;
2822 char sql_query_string[QUERY_SIZE] = {0, };
2825 sqlite3 *local_db_handle = emstorage_get_db_connection();
2826 EMSTORAGE_START_READ_TRANSACTION(transaction);
2827 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
2828 EM_DEBUG_LOG("SQL STMT [ %s ]", sql_query_string);
2829 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2830 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
2831 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2832 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2834 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2835 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2836 ("sqlite3_step fail:%d", rc));
2838 *count = sqlite3_column_int(hStmt, 0);
2844 if (hStmt != NULL) {
2845 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
2847 rc = sqlite3_finalize(hStmt);
2849 if (rc != SQLITE_OK) {
2850 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d: %s", rc, err_msg);
2851 error = EMAIL_ERROR_DB_FAILURE;
2853 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
2856 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2859 if (err_code != NULL)
2862 EM_DEBUG_FUNC_END("ret [%d]", ret);
2867 INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_tbl_t** account_list, int transaction, int with_password, int *err_code)
2869 EM_DEBUG_FUNC_BEGIN();
2871 int i = 0, count = 0, rc = -1, ret = false;
2872 int field_index = 0;
2873 int error = EMAIL_ERROR_NONE;
2874 emstorage_account_tbl_t *p_data_tbl = NULL;
2875 char sql_query_string[QUERY_SIZE] = {0, };
2876 char *sql = "SELECT count(*) FROM mail_account_tbl;";
2878 sqlite3 *local_db_handle = NULL;
2879 DB_STMT hStmt = NULL;
2881 if (!select_num || !account_list) {
2882 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
2883 if (err_code != NULL)
2884 *err_code = EMAIL_ERROR_INVALID_PARAM;
2888 local_db_handle = emstorage_get_db_connection();
2889 EMSTORAGE_START_READ_TRANSACTION(transaction);
2891 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2892 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2893 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2895 count = atoi(result[1]);
2896 sqlite3_free_table(result);
2898 EM_DEBUG_LOG("count = %d", rc);
2901 EM_DEBUG_EXCEPTION("no account found...");
2902 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
2906 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_account_tbl ORDER BY account_id");
2908 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2910 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
2911 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2912 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2914 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2915 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2916 ("sqlite3_step fail:%d", rc));
2917 if (rc == SQLITE_DONE) {
2918 EM_DEBUG_EXCEPTION("no account found...");
2920 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
2926 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
2927 EM_DEBUG_EXCEPTION("malloc failed...");
2928 error = EMAIL_ERROR_OUT_OF_MEMORY;
2931 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
2932 for (i = 0; i < count; i++) {
2935 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
2936 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
2937 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
2938 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
2939 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
2940 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
2941 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
2942 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
2943 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
2944 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
2945 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
2946 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
2947 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
2948 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
2949 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
2950 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
2951 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
2952 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
2953 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
2954 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
2955 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
2956 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
2957 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
2958 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
2959 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
2960 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
2961 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
2962 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
2963 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
2964 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
2965 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
2966 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
2967 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
2968 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
2969 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
2970 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
2971 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
2972 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
2973 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
2974 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
2975 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
2976 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
2977 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
2978 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
2979 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
2980 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
2981 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
2982 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
2983 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
2984 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
2985 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
2987 if (with_password == true) {
2988 /* get password from the secure storage */
2989 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
2990 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
2992 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
2993 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
2995 /* get password file name */
2996 if ((error = _get_password_file_name(p_data_tbl[i].account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
2997 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
2998 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3002 /* read password from secure storage */
3003 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl[i].incoming_server_password))) < 0 ) {
3004 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed. [%d]", error);
3005 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3009 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl[i].outgoing_server_password))) < 0) {
3010 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed. [%d]", error);
3011 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3016 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3017 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3018 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3019 ("sqlite3_step fail:%d", rc));
3026 *account_list = p_data_tbl;
3027 *select_num = count;
3028 EM_DEBUG_LOG("COUNT : %d", count);
3030 else if (p_data_tbl != NULL)
3031 emstorage_free_account(&p_data_tbl, count, NULL);
3033 if (hStmt != NULL) {
3034 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
3036 rc = sqlite3_finalize(hStmt);
3038 if (rc != SQLITE_OK) {
3039 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3040 error = EMAIL_ERROR_DB_FAILURE;
3044 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3047 if (err_code != NULL)
3050 EM_DEBUG_FUNC_END("ret [%d]", ret);
3055 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)
3057 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);
3059 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3060 char conditional_clause[QUERY_SIZE] = {0, };
3061 emstorage_mail_tbl_t* p_data_tbl = NULL;
3063 if (!server_mail_id || !mail) {
3064 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3065 error = EMAIL_ERROR_INVALID_PARAM;
3069 if (account_id == ALL_ACCOUNT)
3070 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
3072 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND account_id = %d", server_mail_id, account_id);
3074 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3076 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
3077 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3087 if (err_code != NULL)
3090 EM_DEBUG_FUNC_END("ret [%d]", ret);
3095 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)
3097 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3099 if (account_id < ALL_ACCOUNT) {
3100 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3101 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3105 if (account_id == ALL_ACCOUNT) {
3106 cur_clause += (conditional_clause_count++ == 0)?
3107 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3108 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3111 cur_clause += (conditional_clause_count++ == 0)?
3112 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
3113 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3116 if (mailbox_id > 0) {
3117 cur_clause += (conditional_clause_count++ == 0)?
3118 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
3119 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3121 else if(account_id != ALL_ACCOUNT) {
3122 cur_clause += (conditional_clause_count++ == 0)?
3123 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3124 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3127 if (thread_id > 0) {
3128 cur_clause += (conditional_clause_count++ == 0)?
3129 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id):
3130 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3132 else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3133 cur_clause += (conditional_clause_count++ == 0)?
3134 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0"):
3135 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3137 else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3138 cur_clause += (conditional_clause_count++ == 0)?
3139 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0"):
3140 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3142 else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3143 cur_clause += (conditional_clause_count++ == 0)?
3144 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0"):
3145 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3148 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3149 if (addr_list && addr_list->address_count > 0) {
3150 if (!addr_list->address_type) {
3151 cur_clause += (conditional_clause_count++ == 0)?
3152 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]):
3153 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3155 for (i = 1; i < addr_list->address_count; i++)
3156 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3158 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3160 cur_clause += (conditional_clause_count++ == 0)?
3161 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]):
3162 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]);
3164 for (i = 1; i < addr_list->address_count; i++)
3165 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3167 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3171 if (input_except_delete_flagged_mails) {
3172 cur_clause += (conditional_clause_count++ == 0)?
3173 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0"):
3174 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3178 switch (search_type) {
3179 case EMAIL_SEARCH_FILTER_SUBJECT:
3180 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3181 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3183 case EMAIL_SEARCH_FILTER_SENDER:
3184 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3185 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3186 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3188 case EMAIL_SEARCH_FILTER_RECIPIENT:
3189 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3190 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3191 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3192 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3193 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3195 case EMAIL_SEARCH_FILTER_ALL:
3196 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3197 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3198 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3199 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3200 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3201 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3204 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3209 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3210 static char sorting_str[][50] = {
3211 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3212 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3213 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3214 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3215 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3216 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3217 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3218 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3219 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3220 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3221 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3222 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3223 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3224 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3225 " ORDER BY mailbox_name DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_NAME_HIGH: */
3226 " ORDER BY mailbox_name ASC, date_time DESC" /* case EMAIL_SORT_MAILBOX_NAME_LOW: */
3229 if (sorting < EMAIL_SORT_END && sorting >= 0)
3230 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3232 EM_DEBUG_LOG(" Invalid Sorting order ");
3235 if (start_index != -1 && limit_count != -1)
3236 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3243 * emstorage_get_mail_list - Get the mail list information.
3247 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)
3249 EM_DEBUG_FUNC_BEGIN();
3250 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3252 int ret = false, error = EMAIL_ERROR_NONE;
3253 char conditional_clause_string[QUERY_SIZE] = { 0, };
3255 if (account_id < ALL_ACCOUNT) {
3256 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3257 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3259 EM_IF_NULL_RETURN_VALUE(result_count, false);
3261 _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);
3263 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3265 if(!emstorage_query_mail_list(conditional_clause_string, transaction, mail_list, result_count, &error)) {
3266 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3273 if (err_code != NULL)
3276 EM_PROFILE_END(emstorage_get_mail_list_func);
3277 EM_DEBUG_FUNC_END("ret [%d]", ret);
3283 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3287 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)
3289 EM_DEBUG_FUNC_BEGIN();
3290 EM_PROFILE_BEGIN(emStorageGetMails);
3292 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3293 emstorage_mail_tbl_t *p_data_tbl = NULL;
3294 char conditional_clause_string[QUERY_SIZE] = {0, };
3296 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3297 EM_IF_NULL_RETURN_VALUE(result_count, false);
3299 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3300 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3301 if (err_code != NULL)
3302 *err_code = EMAIL_ERROR_INVALID_PARAM;
3306 _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);
3308 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3310 if(!emstorage_query_mail_tbl(conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
3311 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3319 *mail_list = p_data_tbl;
3320 *result_count = count;
3321 EM_DEBUG_LOG("COUNT : %d", count);
3323 else if (p_data_tbl != NULL)
3324 emstorage_free_mail(&p_data_tbl, count, NULL);
3326 if (err_code != NULL)
3329 EM_PROFILE_END(emStorageGetMails);
3330 EM_DEBUG_FUNC_END("ret [%d]", ret);
3337 * emstorage_get_searched_mail_list - Get the mail list information after filtering
3341 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)
3343 EM_DEBUG_FUNC_BEGIN();
3345 int ret = false, error = EMAIL_ERROR_NONE;
3346 char conditional_clause[QUERY_SIZE] = {0, };
3347 char *temp_search_value = NULL;
3348 char *temp_search_value2 = NULL;
3350 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3351 EM_IF_NULL_RETURN_VALUE(result_count, false);
3353 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3354 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3355 error = EMAIL_ERROR_INVALID_PARAM;
3359 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
3360 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
3362 _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);
3364 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3366 if(!emstorage_query_mail_list(conditional_clause, transaction, mail_list, result_count, &error)) {
3367 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3374 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3376 if (err_code != NULL)
3379 EM_SAFE_FREE(temp_search_value);
3380 EM_SAFE_FREE(temp_search_value2);
3382 EM_DEBUG_FUNC_END("ret [%d]", ret);
3387 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name)
3389 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
3391 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
3392 EM_DEBUG_EXCEPTION("Invalid parameter");
3393 return EMAIL_ERROR_INVALID_PARAM;
3396 sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
3397 sprintf(send_password_file_name, ".email_account_%d_send", account_id);
3398 EM_DEBUG_FUNC_END();
3399 return EMAIL_ERROR_NONE;
3402 static int _read_password_from_secure_storage(char *file_name, char **password)
3404 EM_DEBUG_FUNC_BEGIN("file_name[%s], password[%p]", file_name, password);
3406 if (!file_name || !password) {
3407 EM_DEBUG_EXCEPTION("Invalid Parameter");
3408 return EMAIL_ERROR_INVALID_PARAM;
3411 size_t buf_len = 0, read_len = 0;
3412 ssm_file_info_t sfi;
3413 char *temp_password = NULL;
3414 int error_code_from_ssm = 0;
3415 int ret = EMAIL_ERROR_NONE;
3417 if ( (error_code_from_ssm = ssm_getinfo(file_name, &sfi, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3418 EM_DEBUG_EXCEPTION("ssm_getinfo() failed. [%d]", error_code_from_ssm);
3419 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3423 buf_len = sfi.originSize;
3424 EM_DEBUG_LOG("password buf_len[%d]", buf_len);
3425 if ((temp_password = (char *)malloc(buf_len + 1)) == NULL) {
3426 EM_DEBUG_EXCEPTION("malloc failed...");
3427 ret = EMAIL_ERROR_OUT_OF_MEMORY;
3430 memset(temp_password, 0x00, buf_len + 1);
3432 if ( (error_code_from_ssm = ssm_read(file_name, temp_password, buf_len, &read_len, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3433 EM_DEBUG_EXCEPTION("ssm_read() failed. [%d]", error_code_from_ssm);
3434 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3438 EM_DEBUG_LOG("password_file_name[%s], password[%s], originSize[%d], read len[%d]", file_name, temp_password, sfi.originSize, read_len);
3440 *password = temp_password;
3441 temp_password = NULL;
3444 EM_SAFE_FREE(temp_password);
3446 EM_DEBUG_FUNC_END("ret [%d]", ret);
3451 INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
3453 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
3456 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
3457 if (err_code != NULL)
3458 *err_code = EMAIL_ERROR_INVALID_PARAM;
3463 int error = EMAIL_ERROR_NONE;
3464 emstorage_account_tbl_t* p_data_tbl = NULL;
3465 DB_STMT hStmt = NULL;
3466 char sql_query_string[QUERY_SIZE] = {0, };
3469 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3470 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3472 sqlite3 *local_db_handle = emstorage_get_db_connection();
3473 EMSTORAGE_START_READ_TRANSACTION(transaction);
3475 /* Make query string */
3476 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3477 sql_len = EM_SAFE_STRLEN(sql_query_string);
3479 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3480 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3481 "incoming_server_type,"
3482 "incoming_server_address,"
3483 "user_email_address,"
3484 "incoming_server_user_name,"
3486 "incoming_server_port_number,"
3487 "incoming_server_secure_connection,"
3488 "outgoing_server_type,"
3489 "outgoing_server_address,"
3490 "outgoing_server_port_number,"
3491 "outgoing_server_need_authentication,"
3492 "outgoing_server_secure_connection,"
3493 "outgoing_server_user_name,"
3494 "user_display_name,"
3498 "keep_mails_on_pop_server_after_download,"
3499 "auto_download_size,"
3500 "outgoing_server_use_same_authenticator,"
3502 "incoming_server_requires_apop,"
3508 sql_len = EM_SAFE_STRLEN(sql_query_string);
3511 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
3512 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
3513 sql_len = EM_SAFE_STRLEN(sql_query_string);
3516 /* get from secure storage, not from db */
3517 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3518 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3521 "req_delivery_receipt,"
3526 "display_name_from,"
3528 "forward_with_files,"
3532 "add_my_address_to_bcc,"
3535 "default_mail_slot_size,"
3541 sql_len = EM_SAFE_STRLEN(sql_query_string);
3544 /* dummy value, FROM WHERE clause */
3545 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
3548 EM_DEBUG_LOG("query = [%s]", sql_query_string);
3550 /* execute a sql and count rows */
3551 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3552 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3553 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3555 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3556 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3557 ("sqlite3_step fail:%d", rc));
3559 if (rc == SQLITE_DONE) {
3560 EM_DEBUG_EXCEPTION("no matched account found...");
3561 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3565 /* Assign query result to structure */
3566 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
3567 EM_DEBUG_EXCEPTION("malloc failed...");
3568 error = EMAIL_ERROR_OUT_OF_MEMORY;
3572 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
3575 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3576 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
3577 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
3578 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
3579 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
3580 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
3581 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
3582 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
3583 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
3584 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
3585 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
3586 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
3587 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
3588 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
3589 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
3590 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
3591 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
3592 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
3593 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
3594 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
3595 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
3596 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
3597 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
3598 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
3599 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
3600 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
3601 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
3602 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
3605 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
3606 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
3608 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
3609 /* get password file name */
3610 if ((error = _get_password_file_name(p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3611 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3615 /* read password from secure storage */
3616 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
3617 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed. [%d]", error);
3621 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
3623 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
3624 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed. [%d]", error);
3627 EM_DEBUG_LOG("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
3630 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3631 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
3632 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
3633 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
3634 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
3635 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
3636 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
3637 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
3638 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
3639 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
3640 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
3641 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
3642 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
3643 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
3644 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
3645 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
3646 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
3647 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
3648 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
3649 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
3650 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
3651 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
3658 *account = p_data_tbl;
3661 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
3663 if (hStmt != NULL) {
3664 rc = sqlite3_finalize(hStmt);
3665 if (rc != SQLITE_OK) {
3666 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3667 error = EMAIL_ERROR_DB_FAILURE;
3671 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3674 if (err_code != NULL)
3677 EM_DEBUG_FUNC_END("ret [%d]", ret);
3681 INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int *password_length, int *err_code)
3683 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
3685 if (account_id <= 0 || password_length == NULL) {
3686 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3687 if (err_code != NULL)
3688 *err_code = EMAIL_ERROR_INVALID_PARAM;
3693 int error = EMAIL_ERROR_NONE;
3694 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3695 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3696 char *temp_password = NULL;
3699 /* get password file name */
3700 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3701 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3705 /* read password from secure storage */
3706 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
3707 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
3711 *password_length = EM_SAFE_STRLEN(temp_password);
3713 EM_DEBUG_LOG("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
3718 EM_SAFE_FREE(temp_password);
3720 if (err_code != NULL)
3723 EM_DEBUG_FUNC_END("ret [%d]", ret);
3727 INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
3729 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
3731 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
3732 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3733 if (err_code != NULL)
3734 *err_code = EMAIL_ERROR_INVALID_PARAM;
3738 int error = EMAIL_ERROR_NONE;
3739 int rc, ret = false;
3741 DB_STMT hStmt = NULL;
3742 char sql_query_string[QUERY_SIZE] = {0, };
3743 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3744 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3746 sqlite3 *local_db_handle = emstorage_get_db_connection();
3747 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
3749 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3750 "UPDATE mail_account_tbl SET"
3752 ", logo_icon_path = ?"
3754 ", user_data_length = ?"
3755 ", account_svc_id = ?"
3757 ", sync_disabled = ?"
3758 ", default_mail_slot_size = ?"
3759 ", user_display_name = ?"
3760 ", user_email_address = ?"
3761 ", reply_to_address = ?"
3762 ", return_address = ?"
3763 ", incoming_server_type = ?"
3764 ", incoming_server_address = ?"
3765 ", incoming_server_port_number = ?"
3766 ", incoming_server_user_name = ?"
3767 ", incoming_server_secure_connection = ?"
3768 ", retrieval_mode = ?"
3769 ", keep_mails_on_pop_server_after_download = ?"
3770 ", check_interval = ?"
3771 ", auto_download_size = ?"
3772 ", outgoing_server_type = ?"
3773 ", outgoing_server_address = ?"
3774 ", outgoing_server_port_number = ?"
3775 ", outgoing_server_user_name = ?"
3776 ", outgoing_server_secure_connection = ?"
3777 ", outgoing_server_need_authentication = ?"
3778 ", outgoing_server_use_same_authenticator = ?"
3780 ", keep_local_copy = ?"
3781 ", req_delivery_receipt = ?"
3782 ", req_read_receipt = ?"
3783 ", download_limit = ?"
3784 ", block_address = ?"
3785 ", block_subject = ?"
3786 ", display_name_from = ?"
3787 ", reply_with_body = ?"
3788 ", forward_with_files = ?"
3789 ", add_myname_card = ?"
3790 ", add_signature = ?"
3792 ", add_my_address_to_bcc = ?"
3793 ", pop_before_smtp = ?"
3794 ", incoming_server_requires_apop = ?"
3796 ", certificate_path = ?"
3799 " WHERE account_id = ?");
3801 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3802 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
3803 EM_DEBUG_LOG("SQL[%s]", sql_query_string);
3804 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3805 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
3809 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3810 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3811 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
3812 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
3813 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
3814 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
3815 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
3816 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
3817 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3818 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3819 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3820 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3821 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
3822 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3823 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
3824 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3825 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
3826 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
3827 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
3828 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
3829 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
3830 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
3831 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3832 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
3833 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
3834 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
3835 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
3836 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
3837 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
3838 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
3839 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
3840 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
3841 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
3842 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
3843 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
3844 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
3845 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
3846 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
3847 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
3848 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
3849 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
3850 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
3851 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
3852 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
3853 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
3854 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3855 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
3856 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
3857 _bind_stmt_field_data_int(hStmt, i++, account_id);
3859 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3860 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
3861 ("sqlite3_step fail:%d", rc));
3862 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3863 ("sqlite3_step fail:%d", rc));
3865 /* validate account existence */
3866 rc = sqlite3_changes(local_db_handle);
3868 EM_DEBUG_EXCEPTION(" no matched account found...");
3870 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3874 /* get password file name */
3875 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3876 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3880 /* save passwords to the secure storage */
3881 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);
3882 if (account_tbl->incoming_server_password && (EM_SAFE_STRLEN(account_tbl->incoming_server_password) > 0)) {
3883 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) {
3884 EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -recv incoming_server_password : file[%s]", recv_password_file_name);
3885 error = EMAIL_ERROR_SYSTEM_FAILURE;
3890 if (account_tbl->outgoing_server_password && (EM_SAFE_STRLEN(account_tbl->outgoing_server_password) > 0)) {
3891 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) {
3892 EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -send password : file[%s]", send_password_file_name);
3893 error = EMAIL_ERROR_SYSTEM_FAILURE;
3898 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
3899 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
3904 if (hStmt != NULL) {
3905 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
3907 rc = sqlite3_finalize(hStmt);
3908 if (rc != SQLITE_OK) {
3909 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
3910 error = EMAIL_ERROR_DB_FAILURE;
3914 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
3918 if (err_code != NULL)
3921 EM_DEBUG_FUNC_END("ret [%d]", ret);
3925 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account_id, char *field_name, int value, int transaction)
3927 EM_DEBUG_FUNC_BEGIN("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
3928 int error = EMAIL_ERROR_NONE;
3931 char sql_query_string[QUERY_SIZE] = {0, };
3932 sqlite3 *local_db_handle = NULL;
3934 if (!account_id || !field_name) {
3935 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3936 return EMAIL_ERROR_INVALID_PARAM;
3939 local_db_handle = emstorage_get_db_connection();
3941 /* Write query string */
3942 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
3944 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
3947 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
3948 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
3949 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3950 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3951 if (sqlite3_changes(local_db_handle) == 0)
3952 EM_DEBUG_LOG("no mail matched...");
3956 result = (error == EMAIL_ERROR_NONE) ? true : false;
3957 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, error);
3960 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
3961 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
3963 EM_DEBUG_FUNC_END("error [%d]", error);
3967 INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *result_sync_status,int *err_code)
3969 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
3971 if(!result_sync_status) {
3972 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3973 if (err_code != NULL)
3974 *err_code = EMAIL_ERROR_INVALID_PARAM;
3978 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
3979 char sql_query_string[QUERY_SIZE] = {0, };
3980 char **result = NULL;
3981 sqlite3 *local_db_handle = emstorage_get_db_connection();
3984 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
3986 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
3988 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3989 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3990 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3993 EM_DEBUG_EXCEPTION("no matched account found...");
3994 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3999 *result_sync_status = 0;
4001 for(i = 0; i < count; i++) {
4002 _get_table_field_data_int(result, &sync_status, col_index++);
4003 *result_sync_status |= sync_status;
4006 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4008 sqlite3_free_table(result);
4016 if (err_code != NULL)
4019 EM_DEBUG_FUNC_END("ret [%d]", ret);
4023 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)
4025 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);
4027 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4028 char sql_query_string[QUERY_SIZE] = {0, };
4029 sqlite3 *local_db_handle = emstorage_get_db_connection();
4031 if(set_operator != SET_TYPE_SET && account_id) {
4032 if(!emstorage_get_sync_status_of_account(account_id, &result_sync_status, &error)) {
4033 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4036 switch(set_operator) {
4037 case SET_TYPE_UNION :
4038 set_value = result_sync_status | set_value;
4040 case SET_TYPE_MINUS :
4041 set_value = result_sync_status & (~set_value);
4044 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4045 error = EMAIL_ERROR_NOT_SUPPORTED;
4048 EM_DEBUG_LOG("set_value [%d]", set_value);
4051 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4054 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4056 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4058 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4060 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4061 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4062 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4064 rc = sqlite3_changes(local_db_handle);
4067 EM_DEBUG_EXCEPTION("no matched account found...");
4068 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4072 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, 0, NULL, 0))
4073 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4078 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4082 if (err_code != NULL)
4085 EM_DEBUG_FUNC_END("ret [%d]", ret);
4089 INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
4091 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4094 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4095 if (err_code != NULL)
4096 *err_code = EMAIL_ERROR_INVALID_PARAM;
4100 int rc = -1, ret = false;
4101 int error = EMAIL_ERROR_NONE;
4102 int error_from_ssm = 0;
4103 DB_STMT hStmt = NULL;
4104 char sql_query_string[QUERY_SIZE] = {0, };
4105 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4106 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4108 sqlite3 *local_db_handle = emstorage_get_db_connection();
4110 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4112 char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
4113 char **result = NULL;
4115 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
4116 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4117 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4119 if (NULL==result[1]) rc = 1;
4120 else rc = atoi(result[1])+1;
4121 sqlite3_free_table(result);
4124 account_tbl->account_id = rc;
4126 if ((error = _get_password_file_name(account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4127 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4131 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4132 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4133 "INSERT INTO mail_account_tbl VALUES "
4135 " ? " /* account_id */
4136 " , ? " /* account_name */
4137 " , ? " /* logo_icon_path */
4138 " , ? " /* user_data */
4139 " , ? " /* user_data_length */
4140 " , ? " /* account_svc_id */
4141 " , ? " /* sync_status */
4142 " , ? " /* sync_disabled */
4143 " , ? " /* default_mail_slot_size */
4144 " , ? " /* user_display_name */
4145 " , ? " /* user_email_address */
4146 " , ? " /* reply_to_address */
4147 " , ? " /* return_address */
4148 " , ? " /* incoming_server_type */
4149 " , ? " /* incoming_server_address */
4150 " , ? " /* incoming_server_port_number */
4151 " , ? " /* incoming_server_user_name */
4152 " , ? " /* incoming_server_password */
4153 " , ? " /* incoming_server_secure_connection */
4154 " , ? " /* retrieval_mode */
4155 " , ? " /* keep_mails_on_pop_server_after_download */
4156 " , ? " /* check_interval */
4157 " , ? " /* auto_download_size */
4158 " , ? " /* outgoing_server_type */
4159 " , ? " /* outgoing_server_address */
4160 " , ? " /* outgoing_server_port_number */
4161 " , ? " /* outgoing_server_user_name */
4162 " , ? " /* outgoing_server_password */
4163 " , ? " /* outgoing_server_secure_connection */
4164 " , ? " /* outgoing_server_need_authentication */
4165 " , ? " /* outgoing_server_use_same_authenticator */
4166 " , ? " /* priority */
4167 " , ? " /* keep_local_copy */
4168 " , ? " /* req_delivery_receipt */
4169 " , ? " /* req_read_receipt */
4170 " , ? " /* download_limit */
4171 " , ? " /* block_address */
4172 " , ? " /* block_subject */
4173 " , ? " /* display_name_from */
4174 " , ? " /* reply_with_body */
4175 " , ? " /* forward_with_files */
4176 " , ? " /* add_myname_card */
4177 " , ? " /* add_signature */
4178 " , ? " /* signature */
4179 " , ? " /* add_my_address_to_bcc */
4180 " , ? " /* pop_before_smtp */
4181 " , ? " /* incoming_server_requires_apop */
4182 " , ? " /* smime_type */
4183 " , ? " /* certificate_path */
4184 " , ? " /* cipher_type */
4185 " , ? " /* digest_type */
4188 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4189 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4190 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4192 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
4195 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4196 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4197 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4198 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4199 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4200 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4201 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4202 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4203 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4204 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4205 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4206 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4207 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4208 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4209 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4210 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4211 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4212 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4213 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4214 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4215 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4216 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4217 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4218 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4219 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4220 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4221 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4222 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4223 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4224 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4225 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4226 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4227 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4228 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4229 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4230 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4231 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4232 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4233 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4234 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4235 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4236 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4237 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4238 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4239 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4240 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4241 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4242 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4243 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
4244 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4245 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4247 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4249 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4250 ("sqlite3_step fail:%d", rc));
4251 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4252 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
4255 /* save passwords to the secure storage */
4256 EM_DEBUG_LOG("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
4257 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) {
4258 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - recv password : file[%s]", error_from_ssm, recv_password_file_name);
4259 error = EMAIL_ERROR_SYSTEM_FAILURE;
4262 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) {
4263 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - send password : file[%s]", error_from_ssm, send_password_file_name);
4264 error = EMAIL_ERROR_SYSTEM_FAILURE;
4270 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4272 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
4273 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
4277 if (hStmt != NULL) {
4278 rc = sqlite3_finalize(hStmt);
4279 if (rc != SQLITE_OK) {
4280 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
4281 error = EMAIL_ERROR_DB_FAILURE;
4285 EM_DEBUG_LOG("hStmt is NULL!!!");
4289 if (err_code != NULL)
4292 EM_DEBUG_FUNC_END("ret [%d]", ret);
4297 INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int *err_code)
4299 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
4301 if (account_id < FIRST_ACCOUNT_ID) {
4302 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
4304 if (err_code != NULL)
4305 *err_code = EMAIL_ERROR_INVALID_PARAM;
4309 int rc = -1, ret = false;
4310 int error = EMAIL_ERROR_NONE;
4311 sqlite3 *local_db_handle = emstorage_get_db_connection();
4312 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4314 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
4315 DB_STMT hStmt = NULL;
4316 char sql_query_string[QUERY_SIZE] = {0, };
4317 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4318 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4320 /* get password file name */
4321 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4322 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4326 /* delete from db */
4327 memset(sql_query_string, 0x00, sizeof(sql_query_string));
4328 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
4330 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4331 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4332 ("sqlite3_exec fail:%d", rc));
4333 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4334 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4336 /* validate account existence */
4337 rc = sqlite3_changes(local_db_handle);
4339 EM_DEBUG_EXCEPTION(" no matched account found...");
4340 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4344 /* delete from secure storage */
4345 if (ssm_delete_file(recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4346 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -recv password : file[%s]", recv_password_file_name);
4347 error = EMAIL_ERROR_SYSTEM_FAILURE;
4350 if (ssm_delete_file(send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4351 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -send password : file[%s]", send_password_file_name);
4352 error = EMAIL_ERROR_SYSTEM_FAILURE;
4359 if (hStmt != NULL) {
4360 rc = sqlite3_finalize(hStmt);
4361 if (rc != SQLITE_OK) {
4362 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
4363 error = EMAIL_ERROR_DB_FAILURE;
4367 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4370 if (err_code != NULL)
4373 EM_DEBUG_FUNC_END("ret [%d]", ret);
4378 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
4380 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
4383 int error = EMAIL_ERROR_NONE;
4386 if (!account_list || !*account_list) {
4387 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
4388 error = EMAIL_ERROR_INVALID_PARAM;
4392 emstorage_account_tbl_t* p = *account_list;
4395 for (; i < count; i++) {
4396 EM_SAFE_FREE(p[i].account_name);
4397 EM_SAFE_FREE(p[i].incoming_server_address);
4398 EM_SAFE_FREE(p[i].user_email_address);
4399 EM_SAFE_FREE(p[i].user_data);
4400 EM_SAFE_FREE(p[i].incoming_server_user_name);
4401 EM_SAFE_FREE(p[i].incoming_server_password);
4402 EM_SAFE_FREE(p[i].outgoing_server_address);
4403 EM_SAFE_FREE(p[i].outgoing_server_user_name);
4404 EM_SAFE_FREE(p[i].outgoing_server_password);
4405 EM_SAFE_FREE(p[i].user_display_name);
4406 EM_SAFE_FREE(p[i].reply_to_address);
4407 EM_SAFE_FREE(p[i].return_address);
4408 EM_SAFE_FREE(p[i].logo_icon_path);
4409 EM_SAFE_FREE(p[i].options.display_name_from);
4410 EM_SAFE_FREE(p[i].options.signature);
4411 EM_SAFE_FREE(p[i].certificate_path);
4415 *account_list = NULL;
4421 if (err_code != NULL)
4424 EM_DEBUG_FUNC_END("ret [%d]", ret);
4428 INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int *count, int transaction, int *err_code)
4430 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);
4432 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
4433 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
4435 if (err_code != NULL)
4436 *err_code = EMAIL_ERROR_INVALID_PARAM;
4440 int rc = -1, ret = false;
4441 int error = EMAIL_ERROR_NONE;
4442 char sql_query_string[QUERY_SIZE] = {0, };
4444 sqlite3 *local_db_handle = emstorage_get_db_connection();
4445 EMSTORAGE_START_READ_TRANSACTION(transaction);
4447 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);
4451 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
4452 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4453 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4455 *count = atoi(result[1]);
4456 sqlite3_free_table(result);
4463 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4466 if (err_code != NULL)
4469 EM_DEBUG_FUNC_END("ret [%d]", ret);
4473 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)
4475 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);
4477 if (!select_num || !mailbox_list) {
4478 EM_DEBUG_EXCEPTION("Invalid parameters");
4480 if (err_code != NULL)
4481 *err_code = EMAIL_ERROR_INVALID_PARAM;
4487 int error = EMAIL_ERROR_NONE;
4488 char conditional_clause_string[QUERY_SIZE] = {0, };
4489 char ordering_clause_string[QUERY_SIZE] = {0, };
4491 if (account_id == ALL_ACCOUNT) {
4492 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4493 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);
4496 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
4497 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4498 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);
4501 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4503 switch (sort_type) {
4504 case EMAIL_MAILBOX_SORT_BY_NAME_ASC :
4505 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
4508 case EMAIL_MAILBOX_SORT_BY_NAME_DSC :
4509 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
4512 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC :
4513 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
4516 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC :
4517 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
4521 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
4523 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4524 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4532 if (err_code != NULL)
4535 EM_DEBUG_FUNC_END("ret [%d]", ret);
4539 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)
4541 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);
4544 int error = EMAIL_ERROR_NONE;
4545 int where_clause_count = 0;
4546 char conditional_clause_string[QUERY_SIZE] = {0, };
4547 char ordering_clause_string[QUERY_SIZE] = {0, };
4549 if (!select_num || !mailbox_list) {
4550 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4551 error = EMAIL_ERROR_INVALID_PARAM;
4555 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
4556 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);
4557 where_clause_count++;
4560 if (account_id > 0) {
4561 if (where_clause_count == 0) {
4562 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
4563 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);
4566 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);
4569 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY MBT.mailbox_name ");
4570 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4571 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
4573 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4574 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4582 if (err_code != NULL)
4585 EM_DEBUG_FUNC_END("ret [%d]", ret);
4589 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)
4591 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);
4592 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
4593 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);
4595 if (err_code != NULL)
4596 *err_code = EMAIL_ERROR_INVALID_PARAM;
4601 int error = EMAIL_ERROR_NONE;
4602 char conditional_clause_string[QUERY_SIZE] = {0, };
4604 if (parent_mailbox_name)
4605 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND UPPER(mailbox_name) LIKE UPPER('%s/%%')", account_id, parent_mailbox_name);
4607 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND (mailbox_name NOT LIKE '%%/%%')", account_id);
4609 EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
4611 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4612 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4620 if (err_code != NULL)
4623 EM_DEBUG_FUNC_END("ret [%d]", ret);
4627 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)
4629 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);
4630 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
4631 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4633 if (err_code != NULL)
4634 *err_code = EMAIL_ERROR_INVALID_PARAM;
4640 int error = EMAIL_ERROR_NONE;
4641 char conditional_clause_string[QUERY_SIZE] = {0, };
4643 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
4644 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4646 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4647 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4655 if (err_code != NULL)
4658 EM_DEBUG_FUNC_END("ret [%d]", ret);
4662 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
4664 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
4666 int result_code = false;
4667 int error = EMAIL_ERROR_NONE;
4669 time_t current_time = 0;
4670 char sql_query_string[QUERY_SIZE] = {0, };
4671 sqlite3 *local_db_handle = NULL;
4673 if (!input_mailbox_id ) {
4674 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4675 return EMAIL_ERROR_INVALID_PARAM;
4678 time(¤t_time);
4680 local_db_handle = emstorage_get_db_connection();
4682 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
4684 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4685 "UPDATE mail_box_tbl SET"
4686 " last_sync_time = %d"
4687 " WHERE mailbox_id = %d"
4689 , input_mailbox_id);
4691 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4693 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4694 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4695 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4699 if(error == EMAIL_ERROR_NONE)
4702 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
4705 EM_DEBUG_FUNC_END("error [%d]", error);
4709 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)
4711 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);
4712 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
4714 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
4715 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
4717 if (err_code != NULL)
4718 *err_code = EMAIL_ERROR_INVALID_PARAM;
4723 int error = EMAIL_ERROR_NONE;
4724 int result_count = 0;
4725 char conditional_clause_string[QUERY_SIZE] = {0, };
4726 char *replaced_mailbox_name = NULL;
4728 if (strstr(mailbox_name, "'")) {
4729 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
4730 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
4732 replaced_mailbox_name = strdup(mailbox_name);
4735 if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
4736 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
4737 EM_DEBUG_EXCEPTION("malloc failed...");
4738 error = EMAIL_ERROR_OUT_OF_MEMORY;
4742 (*result_mailbox)->mailbox_id = 0;
4743 (*result_mailbox)->account_id = account_id;
4744 (*result_mailbox)->local_yn = 1;
4745 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
4746 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
4747 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
4748 (*result_mailbox)->deleted_flag = 0;
4749 (*result_mailbox)->modifiable_yn = 1;
4750 (*result_mailbox)->total_mail_count_on_server = 1;
4751 (*result_mailbox)->has_archived_mails = 0;
4752 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
4753 (*result_mailbox)->no_select = 0;
4758 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND mailbox_name = '%s'", account_id, replaced_mailbox_name);
4760 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%s'", account_id, local_yn, replaced_mailbox_name);
4762 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4764 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4765 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4774 EM_SAFE_FREE(replaced_mailbox_name);
4776 if (err_code != NULL)
4779 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
4780 EM_DEBUG_FUNC_END("ret [%d]", ret);
4784 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)
4786 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);
4788 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !mailbox_name) {
4790 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4792 if (err_code != NULL)
4793 *err_code = EMAIL_ERROR_INVALID_PARAM;
4797 int rc, ret = false;
4798 int error = EMAIL_ERROR_NONE;
4799 emstorage_mailbox_tbl_t *p_data_tbl = NULL;
4800 emstorage_account_tbl_t *account = NULL;
4801 DB_STMT hStmt = NULL;
4802 char sql_query_string[QUERY_SIZE] = {0,};
4803 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 ";
4805 sqlite3 *local_db_handle = emstorage_get_db_connection();
4806 EMSTORAGE_START_READ_TRANSACTION(transaction);
4808 /* validate account */
4809 /* Check whether the account exists. */
4810 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
4811 EM_DEBUG_EXCEPTION(" emstorage_get_account_by_id failed - %d", error);
4816 emstorage_free_account(&account, 1, NULL);
4818 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);
4820 EM_DEBUG_LOG("query = [%s]", sql_query_string);
4823 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4824 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)));
4827 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4828 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
4830 if (rc == SQLITE_DONE) {
4831 EM_DEBUG_EXCEPTION(" no matched mailbox_name found...");
4832 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4837 if (!(p_data_tbl = (emstorage_mailbox_tbl_t*)malloc(sizeof(emstorage_mailbox_tbl_t)))) {
4838 EM_DEBUG_EXCEPTION(" malloc failed...");
4839 error = EMAIL_ERROR_OUT_OF_MEMORY;
4843 memset(p_data_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
4847 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
4848 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4849 _get_stmt_field_data_int(hStmt, &(p_data_tbl->local_yn), col_index++);
4850 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, col_index++);
4851 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->mailbox_type), col_index++);
4852 _get_stmt_field_data_string(hStmt, &(p_data_tbl->alias), 0, col_index++);
4853 _get_stmt_field_data_int(hStmt, &(p_data_tbl->deleted_flag), col_index++);
4854 _get_stmt_field_data_int(hStmt, &(p_data_tbl->modifiable_yn), col_index++);
4855 _get_stmt_field_data_int(hStmt, &(p_data_tbl->total_mail_count_on_server), col_index++);
4856 _get_stmt_field_data_int(hStmt, &(p_data_tbl->has_archived_mails), col_index++);
4857 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_slot_size), col_index++);
4858 _get_stmt_field_data_int(hStmt, &(p_data_tbl->no_select), col_index++);
4859 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->last_sync_time), col_index++);
4865 *mailbox_name = p_data_tbl;
4867 if (hStmt != NULL) {
4868 rc = sqlite3_finalize(hStmt);
4869 if (rc != SQLITE_OK) {
4870 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
4871 error = EMAIL_ERROR_DB_FAILURE;
4875 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4878 if (err_code != NULL)
4881 EM_DEBUG_FUNC_END("ret [%d]", ret);
4885 INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
4887 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
4889 if (input_mailbox_id <= 0 || !output_mailbox) {
4890 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4891 return EMAIL_ERROR_INVALID_PARAM;
4894 int ret = EMAIL_ERROR_NONE;
4895 int result_count = 0;
4896 char conditional_clause_string[QUERY_SIZE] = {0, };
4898 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE mailbox_id = %d", input_mailbox_id);
4900 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4902 if( (ret = emstorage_query_mailbox_tbl(conditional_clause_string, "", false, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4903 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
4909 EM_DEBUG_FUNC_END("ret [%d]", ret);
4913 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)
4915 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);
4916 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
4917 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
4918 if (err_code != NULL)
4919 *err_code = EMAIL_ERROR_INVALID_PARAM;
4923 int rc, ret = false;
4924 int error = EMAIL_ERROR_NONE;
4925 emstorage_account_tbl_t* account = NULL;
4926 DB_STMT hStmt = NULL;
4927 char sql_query_string[QUERY_SIZE] = {0, };
4929 sqlite3 *local_db_handle = emstorage_get_db_connection();
4930 EMSTORAGE_START_READ_TRANSACTION(transaction);
4932 /* Check whether the account exists. */
4933 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
4934 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
4939 emstorage_free_account(&account, 1, NULL);
4941 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);
4943 EM_DEBUG_LOG("query = [%s]", sql_query_string);
4946 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4948 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4949 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4952 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4953 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4954 ("sqlite3_step fail:%d", rc));
4956 if (rc == SQLITE_DONE) {
4957 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
4958 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4962 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
4967 if (hStmt != NULL) {
4968 rc = sqlite3_finalize(hStmt);
4969 if (rc != SQLITE_OK) {
4970 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
4971 error = EMAIL_ERROR_DB_FAILURE;
4975 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4978 if (err_code != NULL)
4981 EM_DEBUG_FUNC_END("ret [%d]", ret);
4985 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)
4987 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);
4988 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
4989 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4990 if (err_code != NULL)
4991 *err_code = EMAIL_ERROR_INVALID_PARAM;
4995 int rc, ret = false;
4996 int error = EMAIL_ERROR_NONE;
4997 emstorage_account_tbl_t* account = NULL;
4998 DB_STMT hStmt = NULL;
4999 char sql_query_string[QUERY_SIZE] = {0, };
5001 sqlite3 *local_db_handle = emstorage_get_db_connection();
5002 EMSTORAGE_START_READ_TRANSACTION(transaction);
5004 /* Check whether the account exists. */
5005 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5006 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5011 emstorage_free_account(&account, 1, NULL);
5013 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);
5015 EM_DEBUG_LOG("query = [%s]", sql_query_string);
5018 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5020 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5021 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5024 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5025 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5026 ("sqlite3_step fail:%d", rc));
5028 if (rc == SQLITE_DONE) {
5029 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5030 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5034 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5039 if (hStmt != NULL) {
5040 rc = sqlite3_finalize(hStmt);
5041 if (rc != SQLITE_OK) {
5042 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5043 error = EMAIL_ERROR_DB_FAILURE;
5047 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5050 if (err_code != NULL)
5053 EM_DEBUG_FUNC_END("ret [%d]", ret);
5057 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)
5059 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);
5060 int rc, ret = false;
5061 int error = EMAIL_ERROR_NONE;
5062 char sql_query_string[QUERY_SIZE] = {0, };
5063 char *replaced_mailbox_name = NULL;
5066 if (strstr(mailbox_name, "'")) {
5067 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5069 replaced_mailbox_name = strdup(mailbox_name);
5073 sqlite3 *local_db_handle = emstorage_get_db_connection();
5075 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5077 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5078 "UPDATE mail_box_tbl SET"
5079 " modifiable_yn = %d"
5080 " WHERE account_id = %d"
5081 " AND local_yn = %d"
5082 " AND mailbox_name = '%s'"
5086 , replaced_mailbox_name);
5087 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5088 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5089 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5094 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5097 EM_SAFE_FREE(replaced_mailbox_name);
5099 if (err_code != NULL)
5102 EM_DEBUG_FUNC_END("ret [%d]", ret);
5108 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)
5110 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);
5111 int rc, ret = false;
5112 int error = EMAIL_ERROR_NONE;
5113 char sql_query_string[QUERY_SIZE] = {0, };
5115 if (account_id <= 0 || input_mailbox_id <= 0) {
5116 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5118 if (err_code != NULL)
5119 *err_code = EMAIL_ERROR_INVALID_PARAM;
5122 sqlite3 *local_db_handle = emstorage_get_db_connection();
5124 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5126 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5127 "UPDATE mail_box_tbl SET"
5128 " total_mail_count_on_server = %d"
5129 " WHERE account_id = %d"
5130 " AND mailbox_id = %d"
5131 , total_count_on_server
5133 , input_mailbox_id);
5134 EM_DEBUG_LOG("query[%s]", sql_query_string);
5135 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5136 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5137 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5142 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5145 if (err_code != NULL)
5148 EM_DEBUG_FUNC_END("ret [%d]", ret);
5154 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)
5156 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);
5158 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5159 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);
5161 if (err_code != NULL)
5162 *err_code = EMAIL_ERROR_INVALID_PARAM;
5166 int rc, ret = false;
5167 int error = EMAIL_ERROR_NONE;
5168 char sql_query_string[QUERY_SIZE] = {0, };
5169 DB_STMT hStmt = NULL;
5172 sqlite3 *local_db_handle = emstorage_get_db_connection();
5173 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5175 if (local_yn != -1) {
5176 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5177 "UPDATE mail_box_tbl SET"
5179 ", mailbox_name = ?"
5180 ", mailbox_type = ?"
5182 ", deleted_flag = ?"
5183 ", modifiable_yn= ?"
5184 ", mail_slot_size= ?"
5185 ", total_mail_count_on_server = ?"
5186 " WHERE account_id = %d"
5187 " AND local_yn = %d"
5188 " AND mailbox_id = '%d'"
5191 , input_mailbox_id);
5194 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5195 "UPDATE mail_box_tbl SET"
5197 ", mailbox_name = ?"
5198 ", mailbox_type = ?"
5200 ", deleted_flag = ?"
5201 ", modifiable_yn= ?"
5202 ", mail_slot_size= ?"
5203 ", total_mail_count_on_server = ?"
5204 " WHERE account_id = %d"
5205 " AND mailbox_id = '%d'"
5207 , input_mailbox_id);
5212 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5213 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5214 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5216 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5217 _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);
5218 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5219 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5220 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5221 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5222 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5223 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5226 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5227 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5228 ("sqlite3_step fail:%d", rc));
5229 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5230 ("sqlite3_step fail:%d", rc));
5235 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5238 if (hStmt != NULL) {
5239 rc = sqlite3_finalize(hStmt);
5240 if (rc != SQLITE_OK) {
5241 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5242 error = EMAIL_ERROR_DB_FAILURE;
5246 if (err_code != NULL)
5249 EM_DEBUG_FUNC_END("ret [%d]", ret);
5254 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)
5256 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);
5258 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name) {
5259 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s]", account_id, local_yn, mailbox_name);
5261 if (err_code != NULL)
5262 *err_code = EMAIL_ERROR_INVALID_PARAM;
5266 int rc, ret = false;
5267 int error = EMAIL_ERROR_NONE;
5268 char sql_query_string[QUERY_SIZE] = {0, };
5269 char *replaced_mailbox_name = NULL;
5271 if (strstr(mailbox_name, "'")) {
5272 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5274 replaced_mailbox_name = strdup(mailbox_name);
5277 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5279 sqlite3 *local_db_handle = emstorage_get_db_connection();
5280 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5282 EM_DEBUG_LOG("emstorage_update_mailbox_type");
5284 DB_STMT hStmt_box_tbl = NULL;
5285 DB_STMT hStmt_mail_tbl = NULL;
5288 /* Update mail_box_tbl */
5289 if (local_yn != -1) {
5290 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5291 "UPDATE mail_box_tbl SET"
5293 " WHERE account_id = %d"
5294 " AND local_yn = %d"
5295 " AND mailbox_name = '%s'"
5298 , replaced_mailbox_name);
5301 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5302 "UPDATE mail_box_tbl SET"
5304 " WHERE account_id = %d"
5305 " AND mailbox_name = '%s'"
5307 , replaced_mailbox_name);
5310 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5312 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
5314 if(SQLITE_OK != rc) {
5315 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5316 error = EMAIL_ERROR_DB_FAILURE;
5320 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
5323 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
5325 if(rc == SQLITE_FULL) {
5326 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5327 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5331 if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
5332 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5333 error = EMAIL_ERROR_DB_FAILURE;
5338 /* Update mail_tbl */
5340 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5341 "UPDATE mail_tbl SET"
5343 " WHERE account_id = %d"
5344 " AND mailbox_name = '%s'"
5346 , replaced_mailbox_name);
5348 EM_DEBUG_LOG("SQL[%s]", sql_query_string);
5350 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
5351 if(SQLITE_OK != rc) {
5352 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5353 error = EMAIL_ERROR_DB_FAILURE;
5357 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
5359 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
5360 if(rc == SQLITE_FULL) {
5361 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5362 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5366 if(rc != SQLITE_ROW && rc != SQLITE_DONE){
5367 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5368 error = EMAIL_ERROR_DB_FAILURE;
5375 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5378 EM_SAFE_FREE(replaced_mailbox_name);
5380 if (hStmt_box_tbl != NULL) {
5381 rc = sqlite3_finalize(hStmt_box_tbl);
5382 if (rc != SQLITE_OK) {
5383 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5384 error = EMAIL_ERROR_DB_FAILURE;
5388 if (hStmt_mail_tbl != NULL) {
5389 rc = sqlite3_finalize(hStmt_mail_tbl);
5390 if (rc != SQLITE_OK) {
5391 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5392 error = EMAIL_ERROR_DB_FAILURE;
5396 if (err_code != NULL)
5399 EM_DEBUG_FUNC_END("ret [%d]", ret);
5403 INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox, int transaction)
5405 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);
5407 int rc, ret = false;
5408 int error = EMAIL_ERROR_NONE;
5409 char sql_query_string[QUERY_SIZE] = {0, };
5411 if (input_mailbox_id < 0) {
5412 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5413 return EMAIL_ERROR_INVALID_PARAM;
5416 sqlite3 *local_db_handle = emstorage_get_db_connection();
5418 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5420 EM_DEBUG_LOG("emstorage_update_mailbox_type");
5422 DB_STMT hStmt = NULL;
5425 /* Update mail_box_tbl */
5426 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5427 "UPDATE mail_box_tbl SET"
5429 " WHERE mailbox_id = %d"
5430 , input_mailbox_id);
5432 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5434 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5435 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5436 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5438 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
5440 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5441 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5442 ("sqlite3_step fail:%d", rc));
5443 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5444 ("sqlite3_step fail:%d", rc));
5446 if (hStmt != NULL) {
5447 rc = sqlite3_finalize(hStmt);
5448 if (rc != SQLITE_OK) {
5449 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5450 error = EMAIL_ERROR_DB_FAILURE;
5458 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5461 if (hStmt != NULL) {
5462 rc = sqlite3_finalize(hStmt);
5463 if (rc != SQLITE_OK) {
5464 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5465 error = EMAIL_ERROR_DB_FAILURE;
5469 EM_DEBUG_FUNC_END("error [%d]", error);
5473 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)
5475 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);
5477 int err = EMAIL_ERROR_NONE;
5480 int cur_mailbox_id_string = 0;
5481 int mailbox_id_string_buffer_length = 0;
5482 char sql_query_string[QUERY_SIZE] = {0, };
5483 char *mailbox_id_string_buffer = NULL;
5484 char *parameter_string = NULL;
5485 sqlite3 *local_db_handle = NULL;
5487 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
5488 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5489 return EMAIL_ERROR_INVALID_PARAM;
5492 local_db_handle = emstorage_get_db_connection();
5494 /* Generating mail id list string */
5495 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
5497 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
5499 if(!mailbox_id_string_buffer) {
5500 EM_DEBUG_EXCEPTION("em_malloc failed");
5501 err = EMAIL_ERROR_OUT_OF_MEMORY;
5505 for(i = 0; i < input_mailbox_id_count; i++)
5506 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]);
5508 if(EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
5509 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
5511 /* Generating notification parameter string */
5512 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
5514 if(!parameter_string) {
5515 EM_DEBUG_EXCEPTION("em_malloc failed");
5516 err = EMAIL_ERROR_OUT_OF_MEMORY;
5520 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
5522 /* Write query string */
5523 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);
5525 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
5528 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
5529 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5530 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5531 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5532 if (sqlite3_changes(local_db_handle) == 0)
5533 EM_DEBUG_LOG("no mail matched...");
5538 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, err);
5541 if (result && parameter_string && !emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
5542 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", input_field_name, input_value);
5544 EM_SAFE_FREE(mailbox_id_string_buffer);
5545 EM_SAFE_FREE(parameter_string);
5547 EM_DEBUG_FUNC_END("err [%d]", err);
5551 INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
5553 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
5556 if (err_code != NULL)
5557 *err_code = EMAIL_ERROR_INVALID_PARAM;
5561 int rc, ret = false;
5562 int error = EMAIL_ERROR_NONE;
5563 DB_STMT hStmt = NULL;
5564 char sql_query_string[QUERY_SIZE] = {0,};
5565 char **result = NULL;
5566 time_t current_time;
5567 sqlite3 *local_db_handle = emstorage_get_db_connection();
5568 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5570 EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
5572 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5573 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5574 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5576 time(¤t_time);
5578 if (NULL == result[1])
5581 rc = atoi(result[1]) + 1;
5582 sqlite3_free_table(result);
5584 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
5586 mailbox_tbl->mailbox_id = rc;
5588 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5589 "INSERT INTO mail_box_tbl VALUES "
5590 "( ?" /* mailbox_id */
5591 ", ?" /* account_id */
5592 ", ?" /* local_yn */
5593 ", ?" /* mailbox_name */
5594 ", ?" /* mailbox_type */
5596 ", ?" /* deleted_flag */
5597 ", ?" /* modifiable_yn */
5598 ", ?" /* total_mail_count_on_server */
5599 ", ?" /* has_archived_mails */
5600 ", ?" /* mail_slot_size */
5601 ", ?" /* no_select */
5602 ", ? )");/* last_sync_time */
5605 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5606 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
5607 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5608 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5612 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
5613 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
5614 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
5615 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
5616 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
5617 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5618 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
5619 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
5620 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
5621 _bind_stmt_field_data_int(hStmt, col_index++, 0);
5622 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
5623 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
5624 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
5627 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5628 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5629 ("sqlite3_step fail:%dn", rc));
5634 if (hStmt != NULL) {
5635 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5637 rc = sqlite3_finalize(hStmt);
5638 if (rc != SQLITE_OK) {
5639 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
5640 error = EMAIL_ERROR_DB_FAILURE;
5644 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5647 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))
5648 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5650 if (err_code != NULL)
5653 EM_DEBUG_FUNC_END("ret [%d]", ret);
5657 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
5659 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
5661 if (account_id < FIRST_ACCOUNT_ID) {
5663 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
5665 if (err_code != NULL)
5666 *err_code = EMAIL_ERROR_INVALID_PARAM;
5669 int rc, ret = false;
5670 int error = EMAIL_ERROR_NONE;
5671 char sql_query_string[QUERY_SIZE] = {0,};
5673 sqlite3 *local_db_handle = emstorage_get_db_connection();
5674 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5677 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
5679 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5680 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5681 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5683 rc = sqlite3_changes(local_db_handle);
5685 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
5691 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5694 if (err_code != NULL)
5696 EM_DEBUG_FUNC_END("ret [%d]", ret);
5702 INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
5704 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);
5706 if (account_id < FIRST_ACCOUNT_ID) {
5707 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5709 if (err_code != NULL)
5710 *err_code = EMAIL_ERROR_INVALID_PARAM;
5714 int rc, ret = false;
5715 int error = EMAIL_ERROR_NONE;
5716 char sql_query_string[QUERY_SIZE] = {0, };
5718 sqlite3 *local_db_handle = emstorage_get_db_connection();
5719 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5722 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
5724 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);
5726 if (input_mailbox_id > 0) { /* 0 means all mailbox */
5727 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);
5730 EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
5731 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5732 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5733 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5735 rc = sqlite3_changes(local_db_handle);
5737 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
5738 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5745 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5748 if(error == EMAIL_ERROR_NONE) {
5749 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
5750 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5753 if (err_code != NULL)
5756 EM_DEBUG_FUNC_END("ret [%d]", ret);
5760 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
5762 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
5765 int error = EMAIL_ERROR_NONE;
5768 if (!mailbox_list || !*mailbox_list) {
5769 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
5771 error = EMAIL_ERROR_INVALID_PARAM;
5775 emstorage_mailbox_tbl_t* p = *mailbox_list;
5778 for (; i < count; i++) {
5779 EM_SAFE_FREE(p[i].mailbox_name);
5780 EM_SAFE_FREE(p[i].alias);
5783 EM_SAFE_FREE(p); *mailbox_list = NULL;
5789 if (err_code != NULL)
5792 EM_DEBUG_FUNC_END("ret [%d]", ret);
5796 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
5798 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);
5800 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
5801 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
5803 if (err_code != NULL)
5804 *err_code = EMAIL_ERROR_INVALID_PARAM;
5808 int rc = -1, ret = false;
5809 int error = EMAIL_ERROR_NONE;
5810 char sql_query_string[QUERY_SIZE] = {0, };
5811 char *replaced_mailbox_name = NULL;
5813 if (strstr(mailbox_name, "'")) {
5814 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5816 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5819 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5822 sqlite3 *local_db_handle = emstorage_get_db_connection();
5823 EMSTORAGE_START_READ_TRANSACTION(transaction);
5824 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, replaced_mailbox_name);
5825 EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
5829 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5830 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5831 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5833 *count = atoi(result[1]);
5834 sqlite3_free_table(result);
5839 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5842 EM_SAFE_FREE(replaced_mailbox_name);
5844 if (err_code != NULL)
5847 EM_DEBUG_FUNC_END("ret [%d]", ret);
5853 INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
5855 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);
5857 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
5858 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
5860 if (err_code != NULL)
5861 *err_code = EMAIL_ERROR_INVALID_PARAM;
5865 int rc = -1, ret = false;
5866 int error = EMAIL_ERROR_NONE;
5867 char sql_query_string[QUERY_SIZE] = {0, };
5868 char *replaced_mailbox_name = NULL;
5870 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5872 sqlite3 *local_db_handle = emstorage_get_db_connection();
5873 EMSTORAGE_START_READ_TRANSACTION(transaction);
5876 if (strstr(mailbox_name, "'")) {
5877 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5879 replaced_mailbox_name = strdup(mailbox_name);
5882 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, replaced_mailbox_name, uid);
5885 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);
5890 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5891 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5892 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5894 *exist = atoi(result[1]);
5895 sqlite3_free_table(result);
5905 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5908 EM_SAFE_FREE(replaced_mailbox_name);
5910 if (err_code != NULL)
5913 EM_DEBUG_FUNC_END("ret [%d]", ret);
5917 INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
5919 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
5921 if (!mail || mail_id <= 0) {
5922 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
5923 if (err_code != NULL)
5924 *err_code = EMAIL_ERROR_INVALID_PARAM;
5928 int rc, ret = false, temp_rule;
5929 int error = EMAIL_ERROR_NONE;
5930 DB_STMT hStmt = NULL;
5931 char sql_query_string[QUERY_SIZE] = {0, };
5933 sqlite3 *local_db_handle = emstorage_get_db_connection();
5934 EMSTORAGE_START_READ_TRANSACTION(transaction);
5936 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
5938 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5939 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5941 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5942 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5945 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5946 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5947 ("sqlite3_step fail:%d", rc));
5949 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
5950 if (*mail == NULL) {
5951 error = EMAIL_ERROR_OUT_OF_MEMORY;
5952 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
5956 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
5958 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5959 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5960 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5961 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5962 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5963 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5964 _get_stmt_field_data_int(hStmt, &temp_rule, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5966 (*mail)->server_mail_status = 1;
5971 if (hStmt != NULL) {
5972 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5973 rc = sqlite3_finalize(hStmt);
5974 if (rc != SQLITE_OK) {
5975 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5976 error = EMAIL_ERROR_DB_FAILURE;
5980 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5983 if (err_code != NULL)
5986 EM_DEBUG_FUNC_END("ret [%d]", ret);
5990 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)
5992 EM_PROFILE_BEGIN(emStorageGetDownloadList);
5993 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);
5994 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
5995 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
5997 if (err_code != NULL)
5998 *err_code = EMAIL_ERROR_INVALID_PARAM;
6002 int rc, ret = false;
6003 int error = EMAIL_ERROR_NONE;
6005 DB_STMT hStmt = NULL;
6006 char sql_query_string[QUERY_SIZE] = {0, };
6008 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6011 sqlite3 *local_db_handle = emstorage_get_db_connection();
6012 EMSTORAGE_START_READ_TRANSACTION(transaction);
6015 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);
6017 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6019 EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
6023 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6024 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6025 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6026 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6029 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6030 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6031 ("sqlite3_step fail:%d", rc));
6034 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6035 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6036 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6037 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6039 sqlite3_free_table(result);
6041 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6047 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6048 EM_DEBUG_EXCEPTION(" malloc failed...");
6049 error = EMAIL_ERROR_OUT_OF_MEMORY;
6053 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
6055 for (i = 0; i < *count; ++i) {
6056 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6057 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6058 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6059 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6060 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].s_uid), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6061 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].data1), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6062 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].data2), 0, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6063 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag), FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6065 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6066 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6067 ("sqlite3_step fail:%d", rc));
6074 *read_mail_uid = p_data_tbl;
6075 else if (p_data_tbl)
6076 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6078 if (hStmt != NULL) {
6079 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6080 rc = sqlite3_finalize(hStmt);
6081 if (rc != SQLITE_OK) {
6082 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6084 error = EMAIL_ERROR_DB_FAILURE;
6088 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6091 if (err_code != NULL)
6094 EM_PROFILE_END(emStorageGetDownloadList);
6095 EM_DEBUG_FUNC_END("ret [%d]", ret);
6099 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)
6101 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);
6103 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6104 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);
6106 if (err_code != NULL)
6107 *err_code = EMAIL_ERROR_INVALID_PARAM;
6111 int rc, ret = false;
6112 int error = EMAIL_ERROR_NONE;
6113 DB_STMT hStmt = NULL;
6114 char sql_query_string[QUERY_SIZE] = {0, };
6115 char *replaced_mailbox_name = NULL;
6117 sqlite3 *local_db_handle = emstorage_get_db_connection();
6118 EMSTORAGE_START_READ_TRANSACTION(transaction);
6121 if (strstr(mailbox_name, "'")) {
6122 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6124 replaced_mailbox_name = strdup(mailbox_name);
6127 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6129 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6130 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6131 "WHERE account_id = %d "
6132 "AND mailbox_id = '%s' "
6133 "AND local_uid = %d "
6134 "AND mailbox_name = '%s' "
6136 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6139 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6140 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6141 "WHERE account_id = %d "
6142 "AND mailbox_id = '%s' "
6143 "AND local_uid = %d "
6145 account_id, mailbox_id, local_uid, uid);
6149 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6150 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6151 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6154 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6155 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6156 ("sqlite3_step fail:%d", rc));
6158 if (rc == SQLITE_DONE) {
6159 EM_DEBUG_EXCEPTION(" no matched mail found....");
6160 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6164 _get_stmt_field_data_int(hStmt, mail_size, 0);
6169 EM_SAFE_FREE(replaced_mailbox_name);
6171 if (hStmt != NULL) {
6172 rc = sqlite3_finalize(hStmt);
6173 if (rc != SQLITE_OK) {
6174 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6176 error = EMAIL_ERROR_DB_FAILURE;
6180 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6183 if (err_code != NULL)
6186 EM_DEBUG_FUNC_END("ret [%d]", ret);
6190 INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6192 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6194 if (!read_mail_uid) {
6195 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6196 if (err_code != NULL)
6197 *err_code = EMAIL_ERROR_INVALID_PARAM;
6201 int rc, rc2, ret = false;
6202 int error = EMAIL_ERROR_NONE;
6203 DB_STMT hStmt = NULL;
6204 char sql_query_string[QUERY_SIZE] = {0, };
6206 sqlite3 *local_db_handle = emstorage_get_db_connection();
6207 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6209 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6210 char **result = NULL;
6213 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6214 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6215 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6217 if (NULL==result[1]) rc = 1;
6218 else rc = atoi(result[1])+1;
6219 sqlite3_free_table(result);
6221 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6222 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6234 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6235 if (rc2 != SQLITE_OK) {
6236 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6237 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6239 error = EMAIL_ERROR_DB_FAILURE;
6243 EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
6244 EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
6245 EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
6246 EM_DEBUG_LOG("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
6247 EM_DEBUG_LOG("s_uid VALUE [%s] ", read_mail_uid->s_uid);
6248 EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->data1);
6249 EM_DEBUG_LOG("data2 VALUE [%s] ", read_mail_uid->data2);
6250 EM_DEBUG_LOG("flag VALUE [%d] ", read_mail_uid->flag);
6251 EM_DEBUG_LOG("rc VALUE [%d] ", rc);
6253 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6254 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6255 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6256 _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);
6257 _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);
6258 _bind_stmt_field_data_int(hStmt, DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->data1);
6259 _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);
6260 _bind_stmt_field_data_int(hStmt, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flag);
6261 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6264 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6265 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6266 ("sqlite3_step fail:%d", rc));
6267 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6268 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6274 if (hStmt != NULL) {
6275 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6277 rc = sqlite3_finalize(hStmt);
6278 if (rc != SQLITE_OK) {
6279 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6280 error = EMAIL_ERROR_DB_FAILURE;
6284 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6287 if (err_code != NULL)
6290 EM_DEBUG_FUNC_END("ret [%d]", ret);
6294 #ifdef __FEATURE_BODY_SEARCH__
6295 INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
6297 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
6300 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
6301 if (err_code != NULL)
6302 *err_code = EMAIL_ERROR_INVALID_PARAM;
6306 int rc, rc2, ret = false;
6307 int error = EMAIL_ERROR_NONE;
6308 DB_STMT hStmt = NULL;
6309 char sql_query_string[QUERY_SIZE] = {0, };
6311 sqlite3 *local_db_handle = emstorage_get_db_connection();
6312 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6314 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
6315 char **result = NULL;
6317 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6318 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6319 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6320 sqlite3_free_table(result);
6322 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6323 "INSERT INTO mail_text_tbl VALUES "
6329 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6330 if (rc2 != SQLITE_OK) {
6331 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6332 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
6334 error = EMAIL_ERROR_DB_FAILURE;
6338 EM_DEBUG_LOG("mail_id VALUE [%d] ", mail_text->mail_id);
6339 EM_DEBUG_LOG("account_id VALUE [%d] ", mail_text->account_id);
6340 EM_DEBUG_LOG("mailbox_id VALUE [%d] ", mail_text->mailbox_id);
6341 EM_DEBUG_LOG("body_text VALUE [%s] ", mail_text->body_text);
6343 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
6344 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
6345 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
6346 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
6348 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6349 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6350 ("sqlite3_step fail:%d", rc));
6351 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6352 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6357 if (hStmt != NULL) {
6358 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6360 rc = sqlite3_finalize(hStmt);
6361 if (rc != SQLITE_OK) {
6362 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6363 error = EMAIL_ERROR_DB_FAILURE;
6367 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6370 if (err_code != NULL)
6373 EM_DEBUG_FUNC_END("ret [%d]", ret);
6378 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)
6380 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);
6382 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
6383 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);
6385 if (err_code != NULL)
6386 *err_code = EMAIL_ERROR_INVALID_PARAM;
6390 int rc, ret = false;
6391 int error = EMAIL_ERROR_NONE;
6392 DB_STMT hStmt = NULL;
6393 char sql_query_string[QUERY_SIZE] = {0, };
6395 sqlite3 *local_db_handle = emstorage_get_db_connection();
6397 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6399 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6400 "UPDATE mail_read_mail_uid_tbl SET"
6404 ", mailbox_name = ?"
6409 " WHERE account_id = ?"
6410 " AND mailbox_id = ?"
6411 " AND local_uid = ?"
6412 " AND mailbox_name= ?"
6416 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6417 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6418 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6419 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6424 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
6425 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
6426 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
6427 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6428 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6429 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->data1);
6430 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6431 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flag);
6432 _bind_stmt_field_data_int(hStmt, i++, account_id);
6433 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
6434 _bind_stmt_field_data_int(hStmt, i++, local_uid);
6435 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6436 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6439 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6440 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6441 ("sqlite3_step fail:%d", rc));
6446 if (hStmt != NULL) {
6447 EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
6449 rc = sqlite3_finalize(hStmt);
6450 if (rc != SQLITE_OK) {
6451 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6453 error = EMAIL_ERROR_DB_FAILURE;
6457 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6460 if (err_code != NULL)
6463 EM_DEBUG_FUNC_END("ret [%d]", ret);
6467 INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
6469 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);
6471 if (account_id < FIRST_ACCOUNT_ID) {
6472 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
6474 if (err_code != NULL)
6475 *err_code = EMAIL_ERROR_INVALID_PARAM;
6479 int rc, ret = false;
6480 int error = EMAIL_ERROR_NONE;
6481 char sql_query_string[QUERY_SIZE] = {0, };
6482 char *replaced_mailbox_name = NULL;
6484 sqlite3 *local_db_handle = emstorage_get_db_connection();
6486 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6488 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
6490 if (mailbox_name) { /* NULL means all mailbox_name */
6491 if (strstr(mailbox_name, "'")) {
6492 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6494 replaced_mailbox_name = strdup(mailbox_name);
6497 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' ", replaced_mailbox_name);
6500 if (uid) { /* NULL means all mail */
6501 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);
6504 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6505 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6506 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6513 EM_SAFE_FREE(replaced_mailbox_name);
6515 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6518 if (err_code != NULL)
6521 EM_DEBUG_FUNC_END("ret [%d]", ret);
6525 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
6527 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
6530 int error = EMAIL_ERROR_NONE;
6533 if (!read_mail_uid || !*read_mail_uid) {
6534 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
6536 error = EMAIL_ERROR_INVALID_PARAM;
6540 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
6543 for (i = 0; i < count; i++) {
6544 EM_SAFE_FREE(p[i].mailbox_name);
6545 EM_SAFE_FREE(p[i].s_uid);
6546 EM_SAFE_FREE(p[i].data2);
6549 EM_SAFE_FREE(p); *read_mail_uid = NULL;
6555 if (err_code != NULL)
6558 EM_DEBUG_FUNC_END("ret [%d]", ret);
6562 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(int account_id, int *count, int transaction, int *err_code)
6564 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
6567 EM_DEBUG_EXCEPTION("count[%p]", count);
6569 if (err_code != NULL)
6570 *err_code = EMAIL_ERROR_INVALID_PARAM;
6574 int rc = -1, ret = false;
6575 int error = EMAIL_ERROR_NONE;
6576 char sql_query_string[QUERY_SIZE] = {0, };
6578 sqlite3 *local_db_handle = emstorage_get_db_connection();
6579 EMSTORAGE_START_READ_TRANSACTION(transaction);
6581 if (account_id != ALL_ACCOUNT)
6582 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
6584 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
6588 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6589 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6590 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6592 *count = atoi(result[1]);
6593 sqlite3_free_table(result);
6598 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6601 if (err_code != NULL)
6604 EM_DEBUG_FUNC_END("ret [%d]", ret);
6608 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)
6610 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);
6612 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
6613 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);
6615 if (err_code != NULL)
6616 *err_code = EMAIL_ERROR_INVALID_PARAM;
6621 int error = EMAIL_ERROR_NONE;
6623 emstorage_rule_tbl_t* p_data_tbl = NULL;
6624 int i = 0, count = 0;
6625 DB_STMT hStmt = NULL;
6626 char sql_query_string[QUERY_SIZE] = {0, };
6630 sqlite3 *local_db_handle = emstorage_get_db_connection();
6631 EMSTORAGE_START_READ_TRANSACTION(transaction);
6633 if (account_id != ALL_ACCOUNT) {
6635 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
6637 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
6640 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
6642 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
6645 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6646 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6647 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6648 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6651 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6652 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6653 ("sqlite3_step fail:%d", rc));
6656 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
6657 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
6658 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6659 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6661 sqlite3_free_table(result);
6664 EM_DEBUG_LOG(" no matched rule found...");
6670 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
6671 EM_DEBUG_EXCEPTION(" malloc failed...");
6673 error = EMAIL_ERROR_OUT_OF_MEMORY;
6677 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
6679 for (i = 0; i < count; i++) {
6680 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6681 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6682 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
6683 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6684 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6685 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6686 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6687 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6689 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6690 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6691 ("sqlite3_step fail:%d", rc));
6698 EM_DEBUG_LOG("[%d] rules found.", count);
6701 *rule_list = p_data_tbl;
6702 *select_num = count;
6704 else if (p_data_tbl != NULL)
6705 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
6707 if (hStmt != NULL) {
6708 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6710 rc = sqlite3_finalize(hStmt);
6711 if (rc != SQLITE_OK) {
6712 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6713 error = EMAIL_ERROR_DB_FAILURE;
6718 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6721 if (err_code != NULL)
6724 EM_DEBUG_FUNC_END("ret [%d]", ret);
6728 INTERNAL_FUNC int emstorage_get_rule_by_id(int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
6730 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
6733 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
6735 if (err_code != NULL)
6736 *err_code = EMAIL_ERROR_INVALID_PARAM;
6740 emstorage_rule_tbl_t* p_data_tbl = NULL;
6741 int rc, ret = false;
6742 int error = EMAIL_ERROR_NONE;
6744 DB_STMT hStmt = NULL;
6745 char sql_query_string[QUERY_SIZE] = {0, };
6746 sqlite3 *local_db_handle = emstorage_get_db_connection();
6747 EMSTORAGE_START_READ_TRANSACTION(transaction);
6749 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
6751 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6752 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6753 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6756 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6757 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6758 ("sqlite3_step fail:%d", rc));
6760 if (rc == SQLITE_DONE) {
6761 EM_DEBUG_EXCEPTION(" no matched rule found...");
6762 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6766 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
6767 EM_DEBUG_EXCEPTION(" malloc failed...");
6768 error = EMAIL_ERROR_OUT_OF_MEMORY;
6772 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
6773 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6774 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6775 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
6776 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6777 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6778 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6779 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6780 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6788 if (hStmt != NULL) {
6789 EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
6791 rc = sqlite3_finalize(hStmt);
6792 if (rc != SQLITE_OK) {
6793 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6795 error = EMAIL_ERROR_DB_FAILURE;
6799 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6802 if (err_code != NULL)
6805 EM_DEBUG_FUNC_END("ret [%d]", ret);
6809 INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
6811 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
6813 if (!new_rule) { /* only global rule supported. */
6814 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
6816 if (err_code != NULL)
6817 *err_code = EMAIL_ERROR_INVALID_PARAM;
6821 int rc, ret = false;
6822 int error = EMAIL_ERROR_NONE;
6824 DB_STMT hStmt = NULL;
6825 char sql_query_string[QUERY_SIZE] = {0, };
6826 sqlite3 *local_db_handle = emstorage_get_db_connection();
6827 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6829 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6830 "UPDATE mail_rule_tbl SET"
6834 ", target_mailbox_id = ?"
6837 " WHERE account_id = ?"
6838 " AND rule_id = ?");
6841 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6842 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
6843 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6844 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6848 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
6849 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6850 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
6851 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
6852 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
6853 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
6854 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
6855 _bind_stmt_field_data_int(hStmt, i++, rule_id);
6858 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6859 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6860 ("sqlite3_step fail:%d", rc));
6865 if (hStmt != NULL) {
6866 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6868 rc = sqlite3_finalize(hStmt);
6869 if (rc != SQLITE_OK) {
6870 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6872 error = EMAIL_ERROR_DB_FAILURE;
6876 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6879 if (err_code != NULL)
6882 EM_DEBUG_FUNC_END("ret [%d]", ret);
6886 INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6888 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6892 EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6894 if (err_code != NULL)
6895 *err_code = EMAIL_ERROR_INVALID_PARAM;
6899 DB_STMT hStmt = NULL;
6900 char sql_query_string[QUERY_SIZE] = {0, };
6902 int error = EMAIL_ERROR_NONE;
6903 int rc, ret = false;
6905 sqlite3 *local_db_handle = emstorage_get_db_connection();
6906 EMSTORAGE_START_READ_TRANSACTION(transaction);
6908 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6909 "SELECT rule_id FROM mail_rule_tbl WHERE type = %d AND UPPER(value) = UPPER('%s')",
6910 rule->type, rule->value);
6913 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6914 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6915 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6918 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6919 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6920 ("sqlite3_step fail:%d", rc));
6922 if (rc == SQLITE_DONE) {
6923 EM_DEBUG_EXCEPTION(" no matched rule found...");
6924 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6932 if (hStmt != NULL) {
6933 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
6935 rc = sqlite3_finalize(hStmt);
6936 if (rc != SQLITE_OK) {
6937 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
6938 error = EMAIL_ERROR_DB_FAILURE;
6943 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6949 EM_DEBUG_FUNC_END("ret [%d]", ret);
6953 INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6955 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6957 if (!rule) { /* only global rule supported. */
6959 EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6961 if (err_code != NULL)
6962 *err_code = EMAIL_ERROR_INVALID_PARAM;
6966 int rc, rc_2, ret = false;
6967 int error = EMAIL_ERROR_NONE;
6968 DB_STMT hStmt = NULL;
6969 char sql_query_string[QUERY_SIZE] = {0, };
6972 sqlite3 *local_db_handle = emstorage_get_db_connection();
6974 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6978 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
6980 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6981 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6982 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6984 if (NULL==result[1])
6987 rc = atoi(result[1])+1;
6989 sqlite3_free_table(result);
6993 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6994 "INSERT INTO mail_rule_tbl VALUES "
6995 "( ?" /* account id */
6999 ", ?" /* action_type */
7000 ", ?" /* target_mailbox_id */
7002 ", ?)"); /* flag2 */
7004 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7005 if (rc_2 != SQLITE_OK) {
7006 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7007 error = EMAIL_ERROR_DB_FAILURE;
7011 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7012 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7013 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7014 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7015 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7016 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7017 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7018 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7020 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7021 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7022 ("sqlite3_step fail:%d", rc));
7027 if (hStmt != NULL) {
7028 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7030 rc = sqlite3_finalize(hStmt);
7031 if (rc != SQLITE_OK) {
7032 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
7034 error = EMAIL_ERROR_DB_FAILURE;
7038 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7041 if (err_code != NULL)
7044 EM_DEBUG_FUNC_END("ret [%d]", ret);
7048 INTERNAL_FUNC int emstorage_delete_rule(int rule_id, int transaction, int *err_code)
7050 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7052 if (rule_id <= 0) { /* only global rule supported. */
7053 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7055 if (err_code != NULL)
7056 *err_code = EMAIL_ERROR_INVALID_PARAM;
7060 int rc, ret = false;
7061 int error = EMAIL_ERROR_NONE;
7062 char sql_query_string[QUERY_SIZE] = {0, };
7064 sqlite3 *local_db_handle = emstorage_get_db_connection();
7065 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7067 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7069 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7070 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7071 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7072 rc = sqlite3_changes(local_db_handle);
7074 EM_DEBUG_EXCEPTION(" no matched rule found...");
7076 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7083 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7086 if (err_code != NULL)
7089 EM_DEBUG_FUNC_END("ret [%d]", ret);
7093 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7095 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7098 int error = EMAIL_ERROR_NONE;
7101 if (!rule_list || !*rule_list) {
7102 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7104 error = EMAIL_ERROR_INVALID_PARAM;
7108 emstorage_rule_tbl_t* p = *rule_list;
7111 for (; i < count; i++) {
7112 EM_SAFE_FREE(p[i].value);
7115 EM_SAFE_FREE(p); *rule_list = NULL;
7119 if (err_code != NULL)
7122 EM_DEBUG_FUNC_END("ret [%d]", ret);
7126 INTERNAL_FUNC int emstorage_get_mail_count(int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
7128 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], total[%p], unseen[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, total, unseen, transaction, err_code);
7130 if (!total && !unseen) {
7131 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7132 if (err_code != NULL)
7133 *err_code = EMAIL_ERROR_INVALID_PARAM;
7137 int rc = -1, ret = false;
7138 int error = EMAIL_ERROR_NONE;
7139 DB_STMT hStmt = NULL;
7140 char sql_query_string[QUERY_SIZE] = {0, };
7142 char *replaced_mailbox_name = NULL;
7144 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7145 sqlite3 *local_db_handle = emstorage_get_db_connection();
7146 EMSTORAGE_START_READ_TRANSACTION(transaction);
7149 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7151 if (account_id != ALL_ACCOUNT) {
7152 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);
7154 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7156 else if (mailbox_id)
7157 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d", mailbox_id);
7159 #ifdef USE_GET_RECORD_COUNT_API
7162 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7163 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
7164 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7166 *total = atoi(result[1]);
7167 sqlite3_free_table(result);
7170 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7171 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7172 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7174 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7175 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7176 ("sqlite3_step fail:%d", rc));
7177 _get_stmt_field_data_int(hStmt, total, 0);
7178 #endif /* USE_GET_RECORD_COUNT_API */
7182 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7184 if (account_id != ALL_ACCOUNT)
7185 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);
7188 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = '%d", mailbox_id);
7191 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)");
7194 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7195 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7196 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7198 *unseen = atoi(result[1]);
7199 sqlite3_free_table(result);
7207 #ifndef USE_PREPARED_QUERY_
7208 if (hStmt != NULL) {
7209 rc = sqlite3_finalize(hStmt);
7210 if (rc != SQLITE_OK) {
7211 EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
7212 error = EMAIL_ERROR_DB_FAILURE;
7217 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7220 EM_SAFE_FREE(replaced_mailbox_name);
7222 if (err_code != NULL)
7225 EM_DEBUG_FUNC_END("ret [%d]", ret);
7229 INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7231 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7233 if (mail_id <= 0 || !mail) {
7234 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7235 if (err_code != NULL)
7236 *err_code = EMAIL_ERROR_INVALID_PARAM;
7241 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7243 if (p_data_tbl == NULL) {
7244 EM_DEBUG_EXCEPTION("malloc failed...");
7245 if (err_code != NULL)
7246 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7250 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7251 DB_STMT hStmt = NULL;
7252 char sql_query_string[QUERY_SIZE] = {0, };
7254 int rc, ret = false;
7255 int error = EMAIL_ERROR_NONE;
7257 sqlite3 *local_db_handle = emstorage_get_db_connection();
7258 EMSTORAGE_START_READ_TRANSACTION(transaction);
7261 case RETRIEVE_SUMMARY:
7262 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7263 "SELECT account_id, mail_id, mailbox_id, 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);
7266 case RETRIEVE_FIELDS_FOR_DELETE:
7267 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7268 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7271 case RETRIEVE_ACCOUNT:
7272 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7273 "SELECT account_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7277 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7278 "SELECT account_id, flags_seen_field, thread_id, mailbox_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7282 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7283 error = EMAIL_ERROR_INVALID_PARAM;
7287 EM_DEBUG_LOG("Query [%s]", sql_query_string);
7289 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7290 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7291 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7294 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7295 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7296 ("sqlite3_step fail:%d", rc));
7298 if (rc == SQLITE_DONE) {
7299 EM_DEBUG_EXCEPTION("no matched mail found...");
7300 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7304 case RETRIEVE_SUMMARY:
7305 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7306 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7307 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7308 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7309 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7310 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
7311 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
7312 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
7313 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
7314 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
7315 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
7316 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
7317 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
7320 case RETRIEVE_FIELDS_FOR_DELETE:
7321 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7322 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7323 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7324 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7325 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
7328 case RETRIEVE_ACCOUNT:
7329 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7333 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7334 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
7335 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
7336 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7345 else if (p_data_tbl != NULL)
7346 emstorage_free_mail(&p_data_tbl, 1, NULL);
7348 if (hStmt != NULL) {
7349 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7351 rc = sqlite3_finalize(hStmt);
7352 if (rc != SQLITE_OK) {
7353 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7354 error = EMAIL_ERROR_DB_FAILURE;
7359 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7362 if (err_code != NULL)
7365 EM_DEBUG_FUNC_END("ret [%d]", ret);
7369 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)
7371 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);
7374 int error = EMAIL_ERROR_NONE;
7375 int query_string_length = 0;
7376 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
7377 char **result = NULL;
7378 char *sql_query_string = NULL;
7379 emstorage_mail_tbl_t* p_data_tbl = NULL;
7380 sqlite3 *local_db_handle = NULL;
7382 if (number_of_mails <= 0 || !mail_ids) {
7383 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7384 if (err_code != NULL)
7385 *err_code = EMAIL_ERROR_INVALID_PARAM;
7389 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
7391 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
7392 sql_query_string = (char*)em_malloc(query_string_length);
7394 if (p_data_tbl == NULL || sql_query_string == NULL) {
7395 EM_DEBUG_EXCEPTION("malloc failed...");
7397 EM_SAFE_FREE(p_data_tbl);
7398 EM_SAFE_FREE(sql_query_string);
7400 if (err_code != NULL)
7401 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7405 local_db_handle = emstorage_get_db_connection();
7407 EMSTORAGE_START_READ_TRANSACTION(transaction);
7410 case RETRIEVE_SUMMARY:
7411 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7412 "SELECT account_id, mail_id, 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 (");
7416 case RETRIEVE_FIELDS_FOR_DELETE:
7417 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7418 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id in (");
7422 case RETRIEVE_ACCOUNT:
7423 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7424 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
7429 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7430 "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
7435 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7436 error = EMAIL_ERROR_INVALID_PARAM;
7440 for(i = 0; i < number_of_mails; i++)
7441 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
7442 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
7444 EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
7446 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
7447 if (SQLITE_OK != rc && -1 != rc) {
7448 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7449 error = EMAIL_ERROR_DB_FAILURE;
7453 if (rc == SQLITE_DONE) {
7454 EM_DEBUG_EXCEPTION("no matched mail found...");
7455 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7459 EM_DEBUG_LOG("item_count [%d]", item_count);
7461 if(number_of_mails != item_count) {
7462 EM_DEBUG_EXCEPTION("Can't find all emails");
7463 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7467 col_index = field_count;
7469 for(i = 0; i < item_count; i++) {
7471 case RETRIEVE_SUMMARY:
7472 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7473 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7474 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7475 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7476 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7477 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
7478 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
7479 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7480 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
7481 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
7482 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7483 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
7486 case RETRIEVE_FIELDS_FOR_DELETE:
7487 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7488 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7489 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7490 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7491 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7494 case RETRIEVE_ACCOUNT:
7495 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7499 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7500 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
7501 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7502 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7513 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
7516 sqlite3_free_table(result);
7518 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7521 EM_SAFE_FREE(sql_query_string);
7523 if (err_code != NULL)
7526 EM_DEBUG_FUNC_END("ret [%d]", ret);
7530 INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7532 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7534 if (mail_id <= 0 || !mail) {
7535 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7536 if (err_code != NULL)
7537 *err_code = EMAIL_ERROR_INVALID_PARAM;
7541 int ret = false, error = EMAIL_ERROR_NONE, count;
7542 char conditional_clause[QUERY_SIZE] = {0, };
7543 emstorage_mail_tbl_t* p_data_tbl = NULL;
7545 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7546 EM_DEBUG_LOG("query = [%s]", conditional_clause);
7548 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7549 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7558 else if (p_data_tbl != NULL)
7559 emstorage_free_mail(&p_data_tbl, 1, &error);
7561 if (err_code != NULL)
7564 EM_DEBUG_FUNC_END("ret [%d]", ret);
7568 #ifdef __FEATURE_BODY_SEARCH__
7569 INTERNAL_FUNC int emstorage_get_mail_text_by_id(int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
7571 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
7573 if (mail_id <= 0 || !mail_text) {
7574 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
7575 if (err_code != NULL)
7576 *err_code = EMAIL_ERROR_INVALID_PARAM;
7581 int error = EMAIL_ERROR_NONE;
7583 char conditional_clause[QUERY_SIZE] = {0, };
7584 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
7586 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7587 EM_DEBUG_LOG("query = [%s]", conditional_clause);
7589 if(!emstorage_query_mail_text_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7590 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7598 *mail_text = p_data_tbl;
7599 else if (p_data_tbl != NULL)
7600 emstorage_free_mail_text(&p_data_tbl, 1, &error);
7602 if (err_code != NULL)
7605 EM_DEBUG_FUNC_END("ret [%d]", ret);
7610 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)
7612 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);
7614 if (!search_handle || !searched) {
7615 if (err_code != NULL)
7616 *err_code = EMAIL_ERROR_INVALID_PARAM;
7617 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7618 EM_DEBUG_FUNC_END("false");
7622 emstorage_search_filter_t* p = search;
7623 int error = EMAIL_ERROR_NONE;
7624 DB_STMT hStmt = NULL;
7625 char sql_query_string[QUERY_SIZE] = {0, };
7626 int rc, ret = false;
7627 int and = false, mail_count = 0;
7628 char *replaced_mailbox_name = NULL;
7630 sqlite3 *local_db_handle = emstorage_get_db_connection();
7631 EMSTORAGE_START_READ_TRANSACTION(transaction);
7633 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
7635 if (account_id != ALL_ACCOUNT) {
7636 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);
7641 if (strstr(mailbox_name, "'")) {
7642 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7644 replaced_mailbox_name = strdup(mailbox_name);
7647 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", replaced_mailbox_name);
7654 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
7655 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);
7658 else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
7659 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);
7662 else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
7663 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);
7666 else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
7667 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);
7675 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");
7677 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7680 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7681 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7682 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7685 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7686 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7687 ("sqlite3_step fail:%d", rc));
7691 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
7692 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7693 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7695 sqlite3_free_table(result);
7701 *search_handle = (int)hStmt;
7702 *searched = mail_count;
7703 EM_DEBUG_LOG("mail_count [%d]", mail_count);
7706 if (hStmt != NULL) {
7707 rc = sqlite3_finalize(hStmt);
7708 if (rc != SQLITE_OK) {
7709 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7710 error = EMAIL_ERROR_DB_FAILURE;
7714 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7718 EM_SAFE_FREE(replaced_mailbox_name);
7720 if (err_code != NULL)
7723 EM_DEBUG_FUNC_END("ret [%d]", ret);
7727 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
7729 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
7731 if (search_handle == 0 || !data) {
7732 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
7734 if (err_code != NULL)
7735 *err_code = EMAIL_ERROR_INVALID_PARAM;
7739 emstorage_mail_tbl_t* p_data_tbl = NULL;
7740 DB_STMT hStmt = (DB_STMT)search_handle;
7741 int rc, ret = false;
7742 int error = EMAIL_ERROR_NONE;
7746 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
7749 case RETRIEVE_ENVELOPE:
7751 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
7752 EM_DEBUG_EXCEPTION(" em_malloc failed...");
7754 error = EMAIL_ERROR_OUT_OF_MEMORY;
7758 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7759 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7760 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
7761 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7762 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
7763 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
7764 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
7765 _get_stmt_field_data_int (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
7766 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
7767 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7768 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7769 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
7770 _get_stmt_field_data_char (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
7771 _get_stmt_field_data_int (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
7772 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
7773 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
7774 _get_stmt_field_data_int (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
7775 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
7776 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7777 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
7778 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
7779 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
7780 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
7781 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
7782 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
7783 _get_stmt_field_data_blob (hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
7785 if (type == RETRIEVE_ALL) {
7786 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7787 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7788 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
7789 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
7790 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
7791 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
7792 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
7793 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7794 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7795 _get_stmt_field_data_int (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
7796 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7800 if (p_data_tbl->body_download_status) {
7803 if (p_data_tbl->file_path_html) {
7804 if (stat(p_data_tbl->file_path_html, &buf) == -1)
7805 p_data_tbl->body_download_status = 0;
7807 else if (p_data_tbl->file_path_plain) {
7808 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
7809 p_data_tbl->body_download_status = 0;
7812 p_data_tbl->body_download_status = 0;
7815 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7818 case RETRIEVE_SUMMARY:
7819 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
7820 EM_DEBUG_EXCEPTION(" malloc failed...");
7822 error = EMAIL_ERROR_OUT_OF_MEMORY;
7826 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7828 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7829 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7830 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7831 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7832 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7834 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7837 case RETRIEVE_ADDRESS:
7838 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
7839 EM_DEBUG_EXCEPTION(" malloc failed...");
7840 error = EMAIL_ERROR_OUT_OF_MEMORY;
7844 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7845 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7846 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7847 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7848 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7856 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7857 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7858 ("sqlite3_step fail:%d", rc));
7864 if (err_code != NULL)
7867 EM_DEBUG_FUNC_END("ret [%d]", ret);
7871 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code)
7873 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
7875 int error = EMAIL_ERROR_NONE;
7876 int rc, ret = false;
7878 if (search_handle == 0) {
7879 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
7880 error = EMAIL_ERROR_INVALID_PARAM;
7884 DB_STMT hStmt = (DB_STMT)search_handle;
7886 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7888 rc = sqlite3_finalize(hStmt);
7889 if (rc != SQLITE_OK) {
7890 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
7891 error = EMAIL_ERROR_DB_FAILURE;
7897 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7900 if (err_code != NULL)
7903 EM_DEBUG_FUNC_END("ret [%d]", ret);
7907 INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7909 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7911 if (mail_id <= 0 || !mail) {
7912 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
7914 if (err_code != NULL)
7915 *err_code = EMAIL_ERROR_INVALID_PARAM;
7919 DB_STMT hStmt = NULL;
7920 char sql_query_string[QUERY_SIZE] = {0, };
7923 int error = EMAIL_ERROR_NONE;
7925 sqlite3 *local_db_handle = emstorage_get_db_connection();
7926 char mailbox_id_param_string[10] = {0,};
7928 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7930 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7931 "UPDATE mail_tbl SET"
7936 ", server_mail_status = ?"
7937 ", server_mailbox_name = ?"
7938 ", server_mail_id = ?"
7939 ", reference_mail_id = ?"
7940 ", full_address_from = ?"
7941 ", full_address_reply = ?" /* 10 */
7942 ", full_address_to = ?"
7943 ", full_address_cc = ?"
7944 ", full_address_bcc = ?"
7945 ", full_address_return = ?"
7947 ", body_download_status = ?"
7948 ", file_path_plain = ?"
7949 ", file_path_html = ?"
7951 ", flags_seen_field = ?"
7952 ", flags_deleted_field = ?"
7953 ", flags_flagged_field = ?"
7954 ", flags_answered_field = ?"
7955 ", flags_recent_field = ?"
7956 ", flags_draft_field = ?"
7957 ", flags_forwarded_field = ?"
7963 ", report_status = ?"
7964 ", preview_text = ?"
7966 ", scheduled_sending_time = ?"
7967 ", eas_data_length = ?"
7969 " WHERE mail_id = %d AND account_id != 0 "
7973 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7974 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7975 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7977 _bind_stmt_field_data_int (hStmt, i++, mail->mail_id);
7978 _bind_stmt_field_data_int (hStmt, i++, mail->account_id);
7979 _bind_stmt_field_data_int (hStmt, i++, mail->mailbox_id);
7980 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
7981 _bind_stmt_field_data_int (hStmt, i++, mail->server_mail_status);
7982 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
7983 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
7984 _bind_stmt_field_data_int (hStmt, i++, mail->reference_mail_id);
7985 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
7986 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
7987 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
7988 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
7989 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
7990 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
7991 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
7992 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
7993 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
7994 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
7995 _bind_stmt_field_data_int (hStmt, i++, mail->date_time);
7996 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
7997 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
7998 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
7999 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
8000 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
8001 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
8002 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
8003 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
8004 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
8005 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
8006 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
8007 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8008 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
8009 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8010 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
8011 _bind_stmt_field_data_int (hStmt, i++, mail->scheduled_sending_time);
8012 _bind_stmt_field_data_int (hStmt, i++, mail->eas_data_length);
8013 _bind_stmt_field_data_blob (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8015 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8016 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8017 ("sqlite3_step fail:%d", rc));
8019 rc = sqlite3_changes(local_db_handle);
8021 EM_DEBUG_EXCEPTION(" no matched mail found...");
8022 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8025 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8026 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8027 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8032 if (hStmt != NULL) {
8033 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
8034 rc = sqlite3_finalize(hStmt);
8035 if (rc != SQLITE_OK) {
8036 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
8037 error = EMAIL_ERROR_DB_FAILURE;
8041 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8044 if (err_code != NULL)
8047 EM_DEBUG_FUNC_END("ret [%d]", ret);
8052 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8056 INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int *err_code)
8058 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8060 EM_IF_NULL_RETURN_VALUE(err_code, false);
8063 int error = EMAIL_ERROR_NONE;
8065 char sql_query_string[QUERY_SIZE] = {0, };
8066 sqlite3 *local_db_handle = emstorage_get_db_connection();
8068 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8069 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);
8070 EM_DEBUG_LOG("Query [%s]", sql_query_string);
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)));
8076 rc = sqlite3_changes(local_db_handle);
8078 EM_DEBUG_LOG(" No Matched Mail Exists ");
8079 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8086 if (err_code != NULL)
8089 EM_DEBUG_FUNC_END("ret [%d]", ret);
8093 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)
8095 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);
8097 int error = EMAIL_ERROR_NONE;
8100 int cur_mail_id_string = 0;
8101 int mail_id_string_buffer_length = 0;
8102 char sql_query_string[QUERY_SIZE] = {0, };
8103 char *mail_id_string_buffer = NULL;
8104 char *parameter_string = NULL;
8105 sqlite3 *local_db_handle = emstorage_get_db_connection();
8106 email_mail_attribute_type target_mail_attribute_type = 0;
8108 if (!mail_ids || !field_name || account_id == 0) {
8109 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8110 if (err_code != NULL)
8111 *err_code = EMAIL_ERROR_INVALID_PARAM;
8115 if( (error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8116 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8117 if (err_code != NULL)
8122 /* Generating mail id list string */
8123 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8125 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8127 if(!mail_id_string_buffer) {
8128 EM_DEBUG_EXCEPTION("em_malloc failed");
8129 error = EMAIL_ERROR_OUT_OF_MEMORY;
8133 for(i = 0; i < mail_ids_count; i++)
8134 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8136 if(EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8137 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8139 /* Generating notification parameter string */
8140 parameter_string = em_malloc(mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2);
8142 if(!parameter_string) {
8143 EM_DEBUG_EXCEPTION("em_malloc failed");
8144 error = EMAIL_ERROR_OUT_OF_MEMORY;
8148 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8150 /* Write query string */
8151 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);
8153 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
8156 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8157 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8158 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8159 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8160 if (sqlite3_changes(local_db_handle) == 0)
8161 EM_DEBUG_LOG("no mail matched...");
8166 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8169 if (ret && parameter_string && !emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8170 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8172 EM_SAFE_FREE(mail_id_string_buffer);
8173 EM_SAFE_FREE(parameter_string);
8175 if (err_code != NULL)
8178 EM_DEBUG_FUNC_END("error [%d]", error);
8182 #ifdef __FEATURE_BODY_SEARCH__
8183 INTERNAL_FUNC int emstorage_change_mail_text_field(int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
8185 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8187 if (mail_id <= 0 || !mail_text) {
8188 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
8189 if (err_code != NULL)
8190 *err_code = EMAIL_ERROR_INVALID_PARAM;
8195 int error = EMAIL_ERROR_NONE;
8196 DB_STMT hStmt = NULL;
8197 char sql_query_string[QUERY_SIZE] = {0, };
8202 sqlite3 *local_db_handle = emstorage_get_db_connection();
8203 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8205 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8206 "UPDATE mail_text_tbl SET"
8208 " WHERE mail_id = %d AND account_id != 0"
8210 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8212 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8213 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8214 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8217 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
8219 if (hStmt != NULL) {
8220 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8221 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8222 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8224 rc = sqlite3_changes(local_db_handle);
8226 EM_DEBUG_EXCEPTION(" no matched mail found...");
8227 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8236 if (hStmt != NULL) {
8237 rc = sqlite3_finalize(hStmt);
8238 if (rc != SQLITE_OK) {
8239 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8240 error = EMAIL_ERROR_DB_FAILURE;
8245 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8248 if (err_code != NULL)
8251 EM_DEBUG_FUNC_END("ret [%d]", ret);
8256 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)
8258 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8261 int error = EMAIL_ERROR_NONE;
8266 DB_STMT hStmt = NULL;
8267 char sql_query_string[QUERY_SIZE] = {0, };
8268 char mailbox_id_param_string[10] = {0,};
8269 sqlite3 *local_db_handle = NULL;
8271 if (mail_id <= 0 || !mail) {
8272 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
8273 if (err_code != NULL)
8274 *err_code = EMAIL_ERROR_INVALID_PARAM;
8278 local_db_handle = emstorage_get_db_connection();
8280 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8284 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8285 "UPDATE mail_tbl SET"
8286 " body_download_status = ?"
8287 ", file_path_plain = ?"
8288 ", file_path_html = ?"
8289 ", flags_seen_field = ?"
8290 ", flags_deleted_field = ?"
8291 ", flags_flagged_field = ?"
8292 ", flags_answered_field = ?"
8293 ", flags_recent_field = ?"
8294 ", flags_draft_field = ?"
8295 ", flags_forwarded_field = ?"
8297 ", attachment_count = ?"
8299 ", meeting_request_status = ? "
8300 ", message_class = ? "
8301 ", digest_type = ? "
8303 " WHERE mail_id = %d AND account_id != 0"
8307 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8308 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8309 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8312 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8313 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8314 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8315 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
8316 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
8317 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
8318 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
8319 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
8320 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
8321 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
8322 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8323 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8324 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8325 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
8326 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
8327 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
8328 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8331 case UPDATE_MAILBOX: {
8333 emstorage_mailbox_tbl_t *mailbox_tbl;
8335 if ((err = emstorage_get_mailbox_by_id(mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
8336 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
8340 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8341 "UPDATE mail_tbl SET"
8342 " mailbox_id = '%d'"
8343 ",mailbox_type = '%d'"
8344 " WHERE mail_id = %d AND account_id != 0"
8345 , mailbox_tbl->mailbox_id
8346 , mailbox_tbl->mailbox_type
8351 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8352 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8353 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8356 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
8358 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
8364 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8365 "UPDATE mail_tbl SET"
8366 " flags_seen_field = %d"
8367 ",flags_deleted_field = %d"
8368 ",flags_flagged_field = %d"
8369 ",flags_answered_field = %d"
8370 ",flags_recent_field = %d"
8371 ",flags_draft_field = %d"
8372 ",flags_forwarded_field = %d"
8373 " WHERE mail_id = %d AND account_id != 0"
8374 , mail->flags_seen_field
8375 , mail->flags_deleted_field
8376 , mail->flags_flagged_field
8377 , mail->flags_answered_field
8378 , mail->flags_recent_field
8379 , mail->flags_draft_field
8380 , mail->flags_forwarded_field
8382 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8385 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8386 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8387 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8391 case UPDATE_EXTRA_FLAG:
8392 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8393 "UPDATE mail_tbl SET"
8395 ", save_status = %d"
8396 ", lock_status = %d"
8397 ", report_status = %d"
8399 " WHERE mail_id = %d AND account_id != 0"
8403 , mail->report_status
8406 EM_DEBUG_LOG("Query [%s]", sql_query_string);
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)));
8414 case UPDATE_STICKY_EXTRA_FLAG:
8415 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8416 "UPDATE mail_tbl SET"
8418 " WHERE mail_id = %d AND account_id != 0"
8421 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8424 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8425 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8426 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8430 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8431 "UPDATE mail_tbl SET"
8432 " full_address_from = ?"
8433 ", full_address_reply = ?"
8434 ", full_address_to = ?"
8435 ", full_address_cc = ?"
8436 ", full_address_bcc = ?"
8437 ", full_address_return = ?"
8439 ", file_path_plain = ?"
8441 ", flags_seen_field = ?"
8442 ", flags_deleted_field = ?"
8443 ", flags_flagged_field = ?"
8444 ", flags_answered_field = ?"
8445 ", flags_recent_field = ?"
8446 ", flags_draft_field = ?"
8447 ", flags_forwarded_field = ?"
8451 ", report_status = ?"
8453 ", file_path_html = ?"
8454 ", file_path_mime_entity = ?"
8456 ", preview_text = ?"
8457 ", body_download_status = ?"
8458 ", attachment_count = ?"
8459 ", inline_content_count = ?"
8460 ", meeting_request_status = ?"
8461 ", message_class = ?"
8464 ", scheduled_sending_time = ?"
8465 ", eas_data_length = ?"
8467 " WHERE mail_id = %d AND account_id != 0"
8471 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8472 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8473 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8474 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8476 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8477 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8478 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8479 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8480 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8481 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8482 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8483 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8484 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
8485 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
8486 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
8487 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
8488 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
8489 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
8490 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
8491 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
8492 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
8493 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
8494 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
8495 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
8496 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
8497 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8498 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8499 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
8500 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8501 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
8502 _bind_stmt_field_data_int (hStmt, i++, mail->attachment_count);
8503 _bind_stmt_field_data_int (hStmt, i++, mail->inline_content_count);
8504 _bind_stmt_field_data_int (hStmt, i++, mail->meeting_request_status);
8505 _bind_stmt_field_data_int (hStmt, i++, mail->message_class);
8506 _bind_stmt_field_data_int (hStmt, i++, mail->digest_type);
8507 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
8508 _bind_stmt_field_data_int (hStmt, i++, mail->scheduled_sending_time);
8509 _bind_stmt_field_data_int (hStmt, i++, mail->eas_data_length);
8510 _bind_stmt_field_data_blob (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8513 case UPDATE_DATETIME: {
8514 time_t now = time(NULL);
8516 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8517 "UPDATE mail_tbl SET"
8519 " WHERE mail_id = %d AND account_id != 0"
8523 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8524 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8525 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8529 case UPDATE_FROM_CONTACT_INFO:
8530 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
8531 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8532 "UPDATE mail_tbl SET"
8533 " email_address_sender = ?,"
8534 " WHERE mail_id = %d",
8539 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8540 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8541 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8543 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8546 case UPDATE_TO_CONTACT_INFO:
8547 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
8548 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8549 "UPDATE mail_tbl SET"
8550 " email_address_recipient = ?,"
8551 " WHERE mail_id = %d",
8556 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8557 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8558 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8560 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8563 case UPDATE_ALL_CONTACT_INFO:
8564 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
8565 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8566 "UPDATE mail_tbl SET"
8567 " email_address_sender = ?,"
8568 " email_address_recipient = ?,"
8569 " WHERE mail_id = %d",
8574 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8575 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8576 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8578 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8579 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8583 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
8584 case UPDATE_PARTIAL_BODY_DOWNLOAD:
8586 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8587 "UPDATE mail_tbl SET"
8588 " body_download_status = ?"
8589 ", file_path_plain = ?"
8590 ", file_path_html = ?"
8591 ", attachment_count = ?"
8592 ", inline_content_count = ?"
8594 " WHERE mail_id = %d"
8598 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8599 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8600 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8603 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8604 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8605 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8606 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8607 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
8608 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8613 case UPDATE_FILE_PATH:
8614 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8615 "UPDATE mail_tbl SET"
8616 ", file_path_plain = ?"
8617 ", file_path_html = ?"
8618 ", file_path_mime_entity = ?"
8619 " WHERE mail_id = %d"
8623 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8624 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8625 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8626 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8628 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8629 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8630 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8634 EM_DEBUG_LOG(" type[%d]", type);
8636 error = EMAIL_ERROR_INVALID_PARAM;
8640 if (hStmt != NULL) {
8642 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8643 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
8644 ("sqlite3_step fail:%d", rc));
8645 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8646 ("sqlite3_step fail:%d", rc));
8647 rc = sqlite3_changes(local_db_handle);
8649 EM_DEBUG_EXCEPTION(" no matched mail found...");
8651 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8656 if (mail->account_id == 0) {
8657 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
8658 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
8659 EM_DEBUG_EXCEPTION(" emstorage_get_mail_field_by_id failed - %d", error);
8663 mail->account_id = mail_for_account_tbl->account_id;
8664 if (mail_for_account_tbl)
8665 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
8672 if (hStmt != NULL) {
8673 rc = sqlite3_finalize(hStmt);
8674 if (rc != SQLITE_OK) {
8675 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8676 error = EMAIL_ERROR_DB_FAILURE;
8681 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8684 /*h.gahlaut@samsung.com: Moving publication of notification after committing transaction to DB */
8686 if (ret == true && move_flag != 1) {
8687 if (!emstorage_get_mailbox_id_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
8688 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_id_by_mailbox_type failed - %d", error);
8690 if (mail->mailbox_id == mailbox_id) {
8691 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8692 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
8693 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8696 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
8697 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
8698 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] ");
8702 if (err_code != NULL)
8705 EM_DEBUG_FUNC_END("ret [%d]", ret);
8708 INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
8710 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8712 int rc, ret = false;
8713 int error = EMAIL_ERROR_NONE;
8714 int latest_mail_id = 0;
8715 sqlite3 *local_db_handle = NULL;
8716 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
8717 char **result = NULL;
8719 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8720 _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
8721 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8723 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
8724 if (ret < 0 || latest_mail_id == 0) {
8725 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
8727 local_db_handle = emstorage_get_db_connection();
8729 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8730 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8731 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
8732 if (NULL == result[1])
8735 rc = atoi(result[1]) + 1;
8737 sqlite3_free_table(result);
8738 latest_mail_id = rc;
8743 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
8746 *mail_id = latest_mail_id;
8748 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8749 _unlockshm_mutex(&mapped_for_generating_mail_id);
8750 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8757 if (err_code != NULL)
8760 EM_DEBUG_FUNC_END("ret [%d]", ret);
8765 INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
8767 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
8768 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
8770 if (!mail_tbl_data) {
8771 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
8772 if (err_code != NULL)
8773 *err_code = EMAIL_ERROR_INVALID_PARAM;
8777 int rc, ret = false;
8778 int error = EMAIL_ERROR_NONE;
8779 char sql_query_string[QUERY_SIZE] = {0, };
8780 DB_STMT hStmt = NULL;
8781 sqlite3 *local_db_handle = emstorage_get_db_connection();
8783 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8786 /* increase unique id */
8787 char *sql = "SELECT max(rowid) FROM mail_tbl;";
8790 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8791 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8792 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
8794 if (NULL == result[1])
8797 rc = atoi(result[1])+1;
8799 sqlite3_free_table(result);
8801 mail_tbl_data->mail_id = rc;
8802 mail_tbl_data->thread_id = rc;
8805 if (mail_tbl_data->date_time == 0)
8806 mail_tbl_data->date_time = time(NULL);
8808 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8809 "INSERT INTO mail_tbl VALUES "
8811 ", ?" /* account_id */
8812 ", ?" /* mailbox_id */
8813 ", ?" /* mailbox_type */
8815 ", ?" /* date_time */
8816 ", ?" /* server_mail_status */
8817 ", ?" /* server_mailbox_name */
8818 ", ?" /* server_mail_id */
8819 ", ?" /* message_id */
8820 ", ?" /* reference_mail_id */
8821 ", ?" /* full_address_from */
8822 ", ?" /* full_address_reply */
8823 ", ?" /* full_address_to */
8824 ", ?" /* full_address_cc */
8825 ", ?" /* full_address_bcc */
8826 ", ?" /* full_address_return */
8827 ", ?" /* email_address_sender */
8828 ", ?" /* email_address_recipient */
8829 ", ?" /* alias_sender */
8830 ", ?" /* alias_recipient */
8831 ", ?" /* body_download_status */
8832 ", ?" /* file_path_plain */
8833 ", ?" /* file_path_html */
8834 ", ?" /* file_path_mime_entity */
8835 ", ?" /* mail_size */
8836 ", ?" /* flags_seen_field */
8837 ", ?" /* flags_deleted_field */
8838 ", ?" /* flags_flagged_field */
8839 ", ?" /* flags_answered_field */
8840 ", ?" /* flags_recent_field */
8841 ", ?" /* flags_draft_field */
8842 ", ?" /* flags_forwarded_field */
8843 ", ?" /* DRM_status */
8844 ", ?" /* priority */
8845 ", ?" /* save_status */
8846 ", ?" /* lock_status */
8847 ", ?" /* report_status */
8848 ", ?" /* attachment_count */
8849 ", ?" /* inline_content_count */
8850 ", ?" /* thread_id */
8851 ", ?" /* thread_item_count */
8852 ", ?" /* preview_text */
8853 ", ?" /* meeting_request_status */
8854 ", ?" /* message_class */
8855 ", ?" /* digest_type */
8856 ", ?" /* smime_type */
8857 ", ?" /* scheduled_sending_time */
8858 ", ?" /* eas_data_length */
8859 ", ?" /* eas_data */
8862 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8863 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8864 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8866 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
8867 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
8868 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
8869 _bind_stmt_field_data_int (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
8870 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8871 _bind_stmt_field_data_int (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
8872 _bind_stmt_field_data_int (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
8873 _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);
8874 _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);
8875 _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);
8876 _bind_stmt_field_data_int (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
8877 _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);
8878 _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);
8879 _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);
8880 _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);
8881 _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);
8882 _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);
8883 _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);
8884 _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);
8885 _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);
8886 _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);
8887 _bind_stmt_field_data_int (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
8888 _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);
8889 _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);
8890 _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);
8891 _bind_stmt_field_data_int (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
8892 _bind_stmt_field_data_int (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
8893 _bind_stmt_field_data_int (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
8894 _bind_stmt_field_data_int (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
8895 _bind_stmt_field_data_int (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
8896 _bind_stmt_field_data_int (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
8897 _bind_stmt_field_data_int (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
8898 _bind_stmt_field_data_int (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
8899 _bind_stmt_field_data_int (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
8900 _bind_stmt_field_data_int (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
8901 _bind_stmt_field_data_int (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
8902 _bind_stmt_field_data_int (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
8903 _bind_stmt_field_data_int (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
8904 _bind_stmt_field_data_int (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
8905 _bind_stmt_field_data_int (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
8906 _bind_stmt_field_data_int (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
8907 _bind_stmt_field_data_int (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
8908 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8909 _bind_stmt_field_data_int (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
8910 _bind_stmt_field_data_int (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
8911 _bind_stmt_field_data_int (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
8912 _bind_stmt_field_data_int (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
8913 _bind_stmt_field_data_int (hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
8914 _bind_stmt_field_data_int (hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
8915 _bind_stmt_field_data_blob (hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
8917 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8918 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8919 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8924 if (hStmt != NULL) {
8925 rc = sqlite3_finalize(hStmt);
8926 if (rc != SQLITE_OK) {
8927 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
8928 error = EMAIL_ERROR_DB_FAILURE;
8932 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8936 if (err_code != NULL)
8939 EM_PROFILE_END(profile_emstorage_add_mail);
8940 EM_DEBUG_FUNC_END("ret [%d]", ret);
8944 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)
8946 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);
8948 int rc, ret = false, i, cur_conditional_clause = 0;
8949 int error = EMAIL_ERROR_NONE;
8950 int target_account_id;
8951 char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
8952 emstorage_mailbox_tbl_t *result_mailbox = NULL;
8953 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
8954 char* target_mailbox_name = NULL;
8956 if (!mail_ids || input_mailbox_id <= 0) {
8957 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8958 if (err_code != NULL)
8959 *err_code = EMAIL_ERROR_INVALID_PARAM;
8963 sqlite3 *local_db_handle = emstorage_get_db_connection();
8965 if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
8966 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
8970 if (result_mailbox->mailbox_name) {
8971 if (strstr(result_mailbox->mailbox_name, "'")) {
8972 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
8974 target_mailbox_name = strdup(result_mailbox->mailbox_name);
8978 target_mailbox_type = result_mailbox->mailbox_type;
8979 target_account_id = result_mailbox->account_id;
8980 emstorage_free_mailbox(&result_mailbox, 1, NULL);
8982 cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
8984 for(i = 0; i < number_of_mails; i++)
8985 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8988 char *last_comma = rindex(conditional_clause, ',');
8989 if(last_comma) *last_comma = ')'; /* replace , with ) */
8991 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8993 /* Updating a mail_tbl */
8995 memset(sql_query_string, 0x00, QUERY_SIZE);
8996 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_tbl SET mailbox_type = %d, mailbox_id = %d, account_id = %d %s", target_mailbox_type, input_mailbox_id, target_account_id, conditional_clause);
8997 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8999 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9000 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9001 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9003 /* Updating a mail_attachment_tbl */
9004 memset(sql_query_string, 0x00, QUERY_SIZE);
9005 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);
9006 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9008 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9009 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9010 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9012 /* Updating a mail_meeting_tbl */
9013 memset(sql_query_string, 0x00, QUERY_SIZE);
9014 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);
9015 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9017 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9018 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9019 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9021 #ifdef __FEATURE_BODY_SEARCH__
9022 /* Updating mail_text_tbl */
9023 memset(sql_query_string, 0x00, QUERY_SIZE);
9024 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9025 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9027 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9028 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9029 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9032 /* Updating a mail_read_mail_uid_tbl */
9033 memset(conditional_clause, 0x00, QUERY_SIZE);
9034 cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
9036 for(i = 0; i < number_of_mails; i++)
9037 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
9040 last_comma = rindex(conditional_clause, ',');
9041 if(last_comma) *last_comma = ')'; /* replace , with ) */
9043 memset(sql_query_string, 0x00, QUERY_SIZE);
9044 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);
9045 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9047 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9048 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9049 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9054 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9057 EM_SAFE_FREE(target_mailbox_name);
9059 if (err_code != NULL)
9062 EM_DEBUG_FUNC_END("ret [%d]", ret);
9066 INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
9068 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9071 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9072 if (err_code != NULL)
9073 *err_code = EMAIL_ERROR_INVALID_PARAM;
9077 int rc, ret = false;
9078 int error = EMAIL_ERROR_NONE;
9079 char sql_query_string[QUERY_SIZE] = {0, };
9081 sqlite3 *local_db_handle = emstorage_get_db_connection();
9082 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9084 memset(sql_query_string, 0x00, sizeof(sql_query_string));
9086 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9087 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9090 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9091 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9092 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9097 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9100 if (err_code != NULL)
9103 EM_DEBUG_FUNC_END("ret [%d]", ret);
9107 INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
9109 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
9111 int rc, ret = false, i, cur_sql_query_string = 0;
9112 int error = EMAIL_ERROR_NONE;
9113 char sql_query_string[QUERY_SIZE] = {0, };
9116 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9117 if (err_code != NULL)
9118 *err_code = EMAIL_ERROR_INVALID_PARAM;
9122 sqlite3 *local_db_handle = emstorage_get_db_connection();
9123 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9125 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
9127 for(i = 0; i < number_of_mails; i++)
9128 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
9131 char *last_comma = rindex(sql_query_string, ',');
9132 *last_comma = ')'; /* replace , with ) */
9134 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9136 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9137 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9138 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9140 #ifdef __FEATURE_BODY_SEARCH__
9141 /* delete mail_text from mail_text_tbl */
9142 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE mail_id in (");
9144 for(i = 0; i < number_of_mails; i++)
9145 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
9147 last_comma = rindex(sql_query_string, ',');
9148 *last_comma = ')'; /* replace , with ) */
9150 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9152 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9153 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9154 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9160 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9163 if (err_code != NULL)
9166 EM_DEBUG_FUNC_END("ret [%d]", ret);
9170 INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
9172 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
9174 if (account_id < FIRST_ACCOUNT_ID) {
9175 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
9177 if (err_code != NULL)
9178 *err_code = EMAIL_ERROR_INVALID_PARAM;
9182 int rc, ret = false;
9183 int error = EMAIL_ERROR_NONE;
9184 char sql_query_string[QUERY_SIZE] = {0, };
9186 sqlite3 *local_db_handle = emstorage_get_db_connection();
9187 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9189 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
9190 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9192 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9193 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9194 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9196 rc = sqlite3_changes(local_db_handle);
9198 EM_DEBUG_EXCEPTION(" no mail found...");
9199 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9202 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
9203 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
9204 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9206 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9207 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9208 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9210 rc = sqlite3_changes(local_db_handle);
9212 EM_DEBUG_EXCEPTION("no mail found...");
9213 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9216 #ifdef __FEATURE_BODY_SEARCH__
9217 /* Delete all mail_text in mail_text_tbl table based on account id */
9218 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
9219 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9221 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9222 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9223 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9225 rc = sqlite3_changes(local_db_handle);
9227 EM_DEBUG_EXCEPTION("no mail found...");
9228 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9232 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
9233 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
9239 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9242 if (err_code != NULL)
9245 EM_DEBUG_FUNC_END("ret [%d]", ret);
9249 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, int mailbox_id, int transaction, int *err_code)
9251 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
9253 if (account_id < FIRST_ACCOUNT_ID || mailbox_id == 0) {
9254 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%d]", account_id, mailbox_id);
9255 if (err_code != NULL)
9256 *err_code = EMAIL_ERROR_INVALID_PARAM;
9260 int rc, ret = false;
9261 int error = EMAIL_ERROR_NONE;
9262 char sql_query_string[QUERY_SIZE] = {0, };
9264 sqlite3 *local_db_handle = emstorage_get_db_connection();
9266 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9268 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = '%d'", account_id, mailbox_id);
9269 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9271 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9272 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9273 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9276 /* Delete Mails from mail_read_mail_uid_tbl */
9277 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = '%d'", account_id, mailbox_id);
9278 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9280 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9281 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9282 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9284 #ifdef __FEATURE_BODY_SEARCH__
9285 /* Delete Mails from mail_text_tbl */
9286 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
9287 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9289 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9290 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9291 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9294 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, mailbox_id , 0, 0))
9295 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
9300 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9303 if (err_code != NULL)
9306 EM_DEBUG_FUNC_END("ret [%d]", ret);
9310 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
9312 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
9315 if ((mail_list == NULL) || (*mail_list == NULL)) {
9316 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
9319 *err_code = EMAIL_ERROR_INVALID_PARAM;
9323 emstorage_mail_tbl_t* p = *mail_list;
9326 for (; i < count; i++, p++) {
9327 EM_SAFE_FREE(p->server_mailbox_name);
9328 EM_SAFE_FREE(p->server_mail_id);
9329 EM_SAFE_FREE(p->full_address_from);
9330 EM_SAFE_FREE(p->full_address_reply);
9331 EM_SAFE_FREE(p->full_address_to);
9332 EM_SAFE_FREE(p->full_address_cc);
9333 EM_SAFE_FREE(p->full_address_bcc);
9334 EM_SAFE_FREE(p->full_address_return);
9335 EM_SAFE_FREE(p->subject);
9336 EM_SAFE_FREE(p->file_path_plain);
9337 EM_SAFE_FREE(p->file_path_html);
9338 EM_SAFE_FREE(p->file_path_mime_entity);
9339 EM_SAFE_FREE(p->message_id);
9340 EM_SAFE_FREE(p->email_address_sender);
9341 EM_SAFE_FREE(p->email_address_recipient);
9342 EM_SAFE_FREE(p->preview_text);
9343 EM_SAFE_FREE(p->alias_sender);
9344 EM_SAFE_FREE(p->alias_recipient);
9345 EM_SAFE_FREE(p->eas_data);
9347 EM_SAFE_FREE(*mail_list);
9350 if (err_code != NULL)
9351 *err_code = EMAIL_ERROR_NONE;
9353 EM_DEBUG_FUNC_END();
9357 #ifdef __FEATURE_BODY_SEARCH__
9358 INTERNAL_FUNC int emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
9360 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
9363 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
9364 EM_DEBUG_EXCEPTION("mail_text_list[%p], count[%d]", mail_text_list, count);
9367 *err_code = EMAIL_ERROR_INVALID_PARAM;
9371 emstorage_mail_text_tbl_t *p = *mail_text_list;
9374 for (; i < count; i++, p++) {
9375 EM_SAFE_FREE(p->body_text);
9377 EM_SAFE_FREE(*mail_text_list);
9380 if (err_code != NULL)
9381 *err_code = EMAIL_ERROR_NONE;
9383 EM_DEBUG_FUNC_END();
9389 INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
9391 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
9393 if (mail_id <= 0 || !count) {
9394 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
9395 if (err_code != NULL)
9396 *err_code = EMAIL_ERROR_INVALID_PARAM;
9400 int rc = -1, ret = false;
9401 int error = EMAIL_ERROR_NONE;
9402 char sql_query_string[QUERY_SIZE] = {0, };
9404 sqlite3 *local_db_handle = emstorage_get_db_connection();
9405 EMSTORAGE_START_READ_TRANSACTION(transaction);
9407 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9411 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9412 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9413 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9415 *count = atoi(result[1]);
9416 sqlite3_free_table(result);
9422 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9425 if (err_code != NULL)
9428 EM_DEBUG_FUNC_END("ret [%d]", ret);
9432 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)
9434 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);
9436 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
9437 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9438 return EMAIL_ERROR_INVALID_PARAM;
9441 int error = EMAIL_ERROR_NONE;
9444 char **result = NULL;
9445 char sql_query_string[QUERY_SIZE] = {0, };
9446 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9447 DB_STMT hStmt = NULL;
9448 sqlite3 *local_db_handle = emstorage_get_db_connection();
9450 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
9451 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
9452 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9453 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9454 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9456 *output_attachment_count = atoi(result[1]);
9457 sqlite3_free_table(result);
9459 if(*output_attachment_count == 0) {
9460 error = EMAIL_ERROR_NONE;
9464 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
9467 EM_DEBUG_EXCEPTION("em_malloc failed...");
9468 error = EMAIL_ERROR_OUT_OF_MEMORY;
9472 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
9473 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
9475 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9476 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)));
9478 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9479 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9481 if (rc == SQLITE_DONE) {
9482 EM_DEBUG_EXCEPTION("no matched attachment found...");
9483 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9486 for (i = 0; i < *output_attachment_count; i++) {
9487 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9488 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9489 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9490 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9491 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9492 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9493 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9494 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9495 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9496 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9497 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9498 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9500 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
9501 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9502 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9507 if (error == EMAIL_ERROR_NONE)
9508 *output_attachment_list = p_data_tbl;
9509 else if (p_data_tbl != NULL)
9510 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
9513 rc = sqlite3_finalize(hStmt);
9514 if (rc != SQLITE_OK) {
9515 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
9516 error = EMAIL_ERROR_DB_FAILURE;
9520 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
9524 EM_DEBUG_FUNC_END("error [%d]", error);
9528 INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
9530 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
9532 if (attachment_id <= 0 || !attachment) {
9533 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
9534 if (err_code != NULL)
9535 *err_code = EMAIL_ERROR_INVALID_PARAM;
9539 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9540 int rc, ret = false;
9541 int error = EMAIL_ERROR_NONE;
9542 char sql_query_string[QUERY_SIZE] = {0, };
9544 sqlite3 *local_db_handle = emstorage_get_db_connection();
9545 EMSTORAGE_START_READ_TRANSACTION(transaction);
9547 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
9549 sqlite3_stmt* hStmt = NULL;
9551 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9552 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9554 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9555 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9558 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9559 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9560 ("sqlite3_step fail:%d", rc));
9562 if (rc == SQLITE_DONE) {
9563 EM_DEBUG_EXCEPTION("no matched attachment found...");
9564 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9568 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
9569 EM_DEBUG_EXCEPTION("malloc failed...");
9570 error = EMAIL_ERROR_OUT_OF_MEMORY;
9574 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9575 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9576 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9577 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9578 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9579 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9580 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9581 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9582 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9583 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9584 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9585 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9587 #ifdef __ATTACHMENT_OPTI__
9588 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9589 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
9596 *attachment = p_data_tbl;
9598 if (hStmt != NULL) {
9599 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
9601 rc = sqlite3_finalize(hStmt);
9602 if (rc != SQLITE_OK) {
9603 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9604 error = EMAIL_ERROR_DB_FAILURE;
9608 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9611 if (err_code != NULL)
9614 EM_DEBUG_FUNC_END("ret [%d]", ret);
9618 INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
9620 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);
9622 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
9623 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
9625 if (err_code != NULL)
9626 *err_code = EMAIL_ERROR_INVALID_PARAM;
9630 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9632 int rc, ret = false;
9633 int error = EMAIL_ERROR_NONE;
9634 char sql_query_string[QUERY_SIZE] = {0, };
9636 sqlite3 *local_db_handle = emstorage_get_db_connection();
9637 EMSTORAGE_START_READ_TRANSACTION(transaction);
9639 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));
9640 EM_DEBUG_LOG("query = [%s]", sql_query_string);
9642 DB_STMT hStmt = NULL;
9644 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9645 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9646 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9649 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9650 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9651 ("sqlite3_step fail:%d", rc));
9653 if (rc == SQLITE_DONE) {
9654 EM_DEBUG_EXCEPTION(" no matched attachment found...");
9655 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9659 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
9660 EM_DEBUG_EXCEPTION(" malloc failed...");
9661 error = EMAIL_ERROR_OUT_OF_MEMORY;
9665 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9666 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9667 p_data_tbl->attachment_name = cpy_str(p);
9668 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9669 p_data_tbl->attachment_path = cpy_str(p);
9670 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9671 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9672 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9673 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9674 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9675 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9676 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9677 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9678 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9679 p_data_tbl->attachment_mime_type = cpy_str(p);
9680 #ifdef __ATTACHMENT_OPTI__
9681 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9682 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9683 p_data_tbl->section= cpy_str(p);
9689 *attachment_tbl = p_data_tbl;
9691 if (hStmt != NULL) {
9692 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
9694 rc = sqlite3_finalize(hStmt);
9695 if (rc != SQLITE_OK) {
9696 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9697 error = EMAIL_ERROR_DB_FAILURE;
9701 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9704 if (err_code != NULL)
9707 EM_DEBUG_FUNC_END("ret [%d]", ret);
9711 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)
9713 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
9715 if (mail_id <= 0 || !attachment) {
9716 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
9717 if (err_code != NULL)
9718 *err_code = EMAIL_ERROR_INVALID_PARAM;
9722 int rc, ret = false;
9723 int error = EMAIL_ERROR_NONE;
9724 DB_STMT hStmt = NULL;
9725 char sql_query_string[QUERY_SIZE] = {0, };
9729 sqlite3 *local_db_handle = emstorage_get_db_connection();
9731 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9734 case UPDATE_MAILBOX:
9735 EM_DEBUG_LOG("UPDATE_MAILBOX");
9736 if (!attachment->mailbox_id) {
9737 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
9738 error = EMAIL_ERROR_INVALID_PARAM;
9741 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9742 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
9744 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9745 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9746 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9747 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9749 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
9752 case UPDATE_SAVENAME:
9753 EM_DEBUG_LOG("UPDATE_SAVENAME");
9754 if (!attachment->attachment_path) {
9755 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
9756 error = EMAIL_ERROR_INVALID_PARAM;
9760 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9761 "UPDATE mail_attachment_tbl SET"
9762 " attachment_size = ?"
9763 ", attachment_save_status = 1"
9764 ", attachment_path = ?"
9765 " WHERE mail_id = %d"
9766 " AND attachment_id = %d"
9767 , attachment->mail_id
9768 , attachment->attachment_id);
9771 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9772 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9773 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9774 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9776 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
9777 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9781 EM_DEBUG_LOG("type[%d]", type);
9782 error = EMAIL_ERROR_INVALID_PARAM;
9785 EM_DEBUG_LOG("query = [%s]", sql_query_string);
9787 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9788 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9789 ("sqlite3_step fail:%d", rc));
9793 if (hStmt != NULL) {
9794 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
9795 rc = sqlite3_finalize(hStmt);
9796 if (rc != SQLITE_OK) {
9797 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
9798 error = EMAIL_ERROR_DB_FAILURE;
9802 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9805 if (err_code != NULL)
9807 EM_DEBUG_FUNC_END("ret [%d]", ret);
9812 INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, int input_transaction)
9814 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);
9818 int error = EMAIL_ERROR_NONE;
9819 char sql_query_string[QUERY_SIZE] = {0, };
9820 sqlite3 *local_db_handle = NULL;
9822 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
9823 char *replaced_mailbox_name = NULL;
9824 char *replaced_alias = NULL;
9826 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
9827 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9828 return EMAIL_ERROR_INVALID_PARAM;
9831 if (strstr(input_new_mailbox_name, "'")) {
9832 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
9834 replaced_mailbox_name = strdup(input_new_mailbox_name);
9837 if (strstr(input_new_mailbox_alias, "'")) {
9838 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
9840 replaced_alias = strdup(input_new_mailbox_alias);
9843 local_db_handle = emstorage_get_db_connection();
9845 if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
9846 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
9849 account_id = old_mailbox_data->account_id;
9851 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
9853 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9854 "UPDATE mail_box_tbl SET"
9855 " mailbox_name = '%s'"
9857 " WHERE mailbox_id = %d"
9858 , replaced_mailbox_name
9860 , input_mailbox_id);
9862 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9864 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9865 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9867 if (sqlite3_changes(local_db_handle) == 0)
9868 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
9874 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
9877 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9878 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
9881 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
9882 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
9885 EM_SAFE_FREE(replaced_mailbox_name);
9886 EM_SAFE_FREE(replaced_alias);
9888 if (old_mailbox_data)
9889 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
9893 EM_DEBUG_FUNC_END("error [%d]", error);
9897 INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
9899 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
9900 int rc, ret = false;
9901 int error = EMAIL_ERROR_NONE;
9902 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9905 if (!attachment_no) {
9906 EM_DEBUG_EXCEPTION("Invalid attachment");
9907 error = EMAIL_ERROR_INVALID_PARAM;
9911 *attachment_no = -1;
9913 sqlite3 *local_db_handle = emstorage_get_db_connection();
9916 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9917 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9918 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9920 if (NULL == result[1])
9923 rc = atoi(result[1])+1;
9925 sqlite3_free_table(result);
9927 *attachment_no = rc;
9928 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
9934 if (err_code != NULL)
9937 EM_DEBUG_FUNC_END("ret [%d]", ret);
9941 INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
9943 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
9947 int rc, ret = false;
9948 int error = EMAIL_ERROR_NONE;
9949 DB_STMT hStmt = NULL;
9950 char sql_query_string[QUERY_SIZE] = {0, };
9951 sqlite3 *local_db_handle = emstorage_get_db_connection();
9953 if (!attachment_tbl) {
9954 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
9955 if (err_code != NULL)
9956 *err_code = EMAIL_ERROR_INVALID_PARAM;
9960 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9962 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9964 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9965 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9966 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9968 if (NULL==result[1]) rc = 1;
9969 else rc = atoi(result[1]) + 1;
9970 sqlite3_free_table(result);
9972 attachment_tbl->attachment_id = rc;
9974 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9975 "INSERT INTO mail_attachment_tbl VALUES "
9976 "( ?" /* attachment_id */
9977 ", ?" /* attachment_name */
9978 ", ?" /* attachment_path */
9979 ", ?" /* attachment_size */
9981 ", ?" /* account_id */
9982 ", ?" /* mailbox_id */
9983 ", ?" /* attachment_save_status */
9984 ", ?" /* attachment_drm_type */
9985 ", ?" /* attachment_drm_method */
9986 ", ?" /* attachment_inline_content_status */
9987 ", ?" /* attachment_mime_type */
9988 #ifdef __ATTACHMENT_OPTI__
9995 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9996 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9997 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9999 _bind_stmt_field_data_int (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
10000 _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);
10001 _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);
10002 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
10003 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
10004 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
10005 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
10006 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
10007 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
10008 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
10009 _bind_stmt_field_data_int (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
10010 _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);
10011 #ifdef __ATTACHMENT_OPTI__
10012 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
10013 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
10017 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10018 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10019 ("sqlite3_step fail:%d", rc));
10020 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10021 ("sqlite3_step fail:%d", rc));
10023 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10024 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
10026 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10028 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10029 ("sqlite3_exec fail:%d", rc));
10030 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10031 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10033 rc = sqlite3_changes(local_db_handle);
10035 EM_DEBUG_EXCEPTION(" no matched mail found...");
10036 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10043 if (hStmt != NULL) {
10044 rc = sqlite3_finalize(hStmt);
10045 if (rc != SQLITE_OK) {
10046 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
10047 error = EMAIL_ERROR_DB_FAILURE;
10051 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10054 if (err_code != NULL)
10057 EM_DEBUG_FUNC_END("ret [%d]", ret);
10061 INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
10063 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
10065 int rc, ret = false, field_idx = 0;
10066 int error = EMAIL_ERROR_NONE;
10067 DB_STMT hStmt = NULL;
10068 char sql_query_string[QUERY_SIZE] = {0, };
10070 if (!attachment_tbl) {
10071 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
10072 error = EMAIL_ERROR_INVALID_PARAM;
10076 sqlite3 *local_db_handle = emstorage_get_db_connection();
10078 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10080 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10081 "UPDATE mail_attachment_tbl SET "
10082 " attachment_name = ?"
10083 ", attachment_path = ?"
10084 ", attachment_size = ?"
10088 ", attachment_save_status = ?"
10089 ", attachment_drm_type = ?"
10090 ", attachment_drm_method = ?"
10091 ", attachment_inline_content_status = ? "
10092 ", attachment_mime_type = ? "
10093 " WHERE attachment_id = ?;");
10096 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10098 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10099 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10101 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10102 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10103 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_size);
10104 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mail_id);
10105 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->account_id);
10106 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mailbox_id);
10107 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
10108 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
10109 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
10110 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
10111 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
10112 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_id);
10115 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10116 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10117 ("sqlite3_step fail:%d", rc));
10118 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10119 ("sqlite3_step fail:%d", rc));
10121 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10122 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
10124 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10126 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10127 ("sqlite3_exec fail:%d", rc));
10128 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10129 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10131 rc = sqlite3_changes(local_db_handle);
10133 EM_DEBUG_EXCEPTION(" no matched mail found...");
10134 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10141 if (hStmt != NULL) {
10142 rc = sqlite3_finalize(hStmt);
10143 if (rc != SQLITE_OK) {
10144 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
10145 error = EMAIL_ERROR_DB_FAILURE;
10149 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10152 if (err_code != NULL)
10155 EM_DEBUG_FUNC_END("ret [%d]", ret);
10159 INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
10161 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
10163 if (attachment_id < 0) {
10164 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
10165 if (err_code != NULL)
10166 *err_code = EMAIL_ERROR_INVALID_PARAM;
10170 int rc, ret = false;
10171 int error = EMAIL_ERROR_NONE;
10172 char sql_query_string[QUERY_SIZE] = {0, };
10174 sqlite3 *local_db_handle = emstorage_get_db_connection();
10176 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10178 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10180 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10181 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10182 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10187 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10193 EM_DEBUG_FUNC_END("ret [%d]", ret);
10197 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
10199 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10200 int rc, ret = false;
10201 int error = EMAIL_ERROR_NONE;
10202 char sql_query_string[QUERY_SIZE] = {0, };
10203 sqlite3 *local_db_handle = NULL;
10205 if (mail_id <= 0) {
10206 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10207 if (err_code != NULL)
10208 *err_code = EMAIL_ERROR_INVALID_PARAM;
10212 local_db_handle = emstorage_get_db_connection();
10214 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10216 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10218 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10219 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10220 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10225 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10231 EM_DEBUG_FUNC_END("ret [%d]", ret);
10235 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
10237 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
10239 int error = EMAIL_ERROR_NONE;
10240 int rc, ret = false;
10241 char sql_query_string[QUERY_SIZE] = {0, };
10242 char *replaced_mailbox = NULL;
10244 sqlite3 *local_db_handle = emstorage_get_db_connection();
10246 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10248 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
10250 if (account_id != ALL_ACCOUNT) /* '0' means all account */
10251 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);
10253 if (mailbox) /* NULL means all mailbox_name */ {
10254 if (strstr(mailbox, "'")) {
10255 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
10257 replaced_mailbox = strdup(mailbox);
10260 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", replaced_mailbox);
10261 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
10263 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10264 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10265 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10270 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10273 if (err_code != NULL)
10276 EM_DEBUG_FUNC_END("ret [%d]", ret);
10280 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
10282 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
10285 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
10286 EM_DEBUG_EXCEPTION(" attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
10287 if (err_code != NULL)
10288 *err_code = EMAIL_ERROR_INVALID_PARAM;
10292 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
10295 for (i = 0; i < count; i++) {
10296 EM_SAFE_FREE(p[i].attachment_name);
10297 EM_SAFE_FREE(p[i].attachment_path);
10298 EM_SAFE_FREE(p[i].attachment_mime_type);
10299 #ifdef __ATTACHMENT_OPTI__
10300 EM_SAFE_FREE(p[i].section);
10305 *attachment_tbl_list = NULL;
10308 if (err_code != NULL)
10309 *err_code = EMAIL_ERROR_NONE;
10310 EM_DEBUG_FUNC_END();
10316 INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
10318 EM_PROFILE_BEGIN(emStorageBeginTransaction);
10321 ENTER_CRITICAL_SECTION(_transactionBeginLock);
10323 /* wait for the trnasaction authority to be changed. */
10324 while (g_transaction) {
10325 EM_DEBUG_LOG(">>>>>>>> Wait for the transaction authority to be changed");
10329 /* take the transaction authority. */
10330 g_transaction = true;
10332 LEAVE_CRITICAL_SECTION(_transactionBeginLock);
10334 sqlite3 *local_db_handle = emstorage_get_db_connection();
10336 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
10337 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10338 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10340 if (ret == false) {
10341 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
10342 ENTER_CRITICAL_SECTION(_transactionEndLock);
10343 g_transaction = false;
10344 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10347 EM_PROFILE_END(emStorageBeginTransaction);
10348 EM_DEBUG_FUNC_END("ret [%d]", ret);
10352 INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
10354 EM_DEBUG_FUNC_BEGIN();
10356 sqlite3 *local_db_handle = emstorage_get_db_connection();
10358 ENTER_CRITICAL_SECTION(_transactionEndLock);
10361 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
10362 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10363 /* release the transaction authority. */
10364 g_transaction = false;
10366 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10367 if (ret == false && err_code != NULL)
10368 *err_code = EMAIL_ERROR_DB_FAILURE;
10370 EM_DEBUG_FUNC_END("ret [%d]", ret);
10374 INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
10376 EM_DEBUG_FUNC_BEGIN();
10378 sqlite3 *local_db_handle = emstorage_get_db_connection();
10381 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
10382 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10383 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10385 ENTER_CRITICAL_SECTION(_transactionEndLock);
10387 /* release the transaction authority. */
10388 g_transaction = false;
10390 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10392 if (ret == false && err_code != NULL)
10393 *err_code = EMAIL_ERROR_DB_FAILURE;
10395 EM_DEBUG_FUNC_END("ret [%d]", ret);
10399 INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
10401 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
10403 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
10405 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
10407 if (err_code != NULL)
10408 *err_code = EMAIL_ERROR_INVALID_PARAM;
10414 int error = EMAIL_ERROR_NONE;
10415 int mail_count = 0;
10417 if (!emstorage_get_mail_count(account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
10418 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
10423 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
10424 if (mail_count >= mailbox->mail_slot_size)
10435 if (err_code != NULL)
10438 EM_DEBUG_FUNC_END("ret [%d]", ret);
10442 INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
10444 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
10446 int rc, ret = false;
10447 int error = EMAIL_ERROR_NONE;
10448 char sql_query_string[QUERY_SIZE] = {0, };
10450 const email_db_object_t* tables = _g_db_tables;
10451 const email_db_object_t* indexes = _g_db_indexes;
10453 sqlite3 *local_db_handle = emstorage_get_db_connection();
10454 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10456 if (!emstorage_delete_dir(MAILHOME, &error)) {
10457 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
10462 mkdir(MAILHOME, DIRECTORY_PERMISSION);
10463 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10465 /* first clear index. */
10466 while (indexes->object_name) {
10467 if (indexes->data_flag) {
10468 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
10469 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10470 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10471 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10476 while (tables->object_name) {
10477 if (tables->data_flag) {
10478 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
10479 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10480 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10481 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10489 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10492 if (err_code != NULL)
10495 EM_DEBUG_FUNC_END("ret [%d]", ret);
10498 /*======================= DB File Utils =============================================*/
10499 #include <dirent.h>
10500 #include <sys/types.h>
10501 #define DIR_SEPERATOR "/"
10503 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
10505 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
10506 char delims[] = "/";
10507 char *result = NULL;
10508 gchar **token = NULL;
10510 token = g_strsplit_set(file_name, delims, 1);
10512 if (token && token[0]) {
10513 EM_DEBUG_LOG(">>>> Directory_name [ %s ]", token[0]);
10514 result = EM_SAFE_STRDUP(token[0]);
10516 EM_DEBUG_LOG(">>>> No Need to create Directory");
10523 INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10525 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);
10526 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
10529 int error = EMAIL_ERROR_NONE;
10530 char *dir_name = NULL;
10531 char create_dir[1024]={0};
10532 char *temp_file = NULL;
10534 if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
10535 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);
10536 error = EMAIL_ERROR_INVALID_PARAM;
10540 sprintf(name_buf, "%s", MAILHOME);
10541 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, account_id);
10544 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10547 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10550 temp_file = EM_SAFE_STRDUP(fname);
10551 if (strstr(temp_file, "/")) {
10552 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
10557 sprintf(create_dir, "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
10558 EM_DEBUG_LOG(">>>>> DIR PATH [ %s ]", create_dir);
10559 mkdir(create_dir, DIRECTORY_PERMISSION);
10560 EM_SAFE_FREE(dir_name);
10564 EM_DEBUG_LOG(">>>>> fname [ %s ]", fname);
10565 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s", DIR_SEPERATOR, fname);
10568 EM_DEBUG_LOG(">>>>> name_buf [ %s ]", name_buf);
10573 EM_SAFE_FREE(temp_file);
10575 if (err_code != NULL)
10578 EM_PROFILE_END(profile_emstorage_get_save_name);
10579 EM_DEBUG_FUNC_END("ret [%d]", ret);
10583 INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10585 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);
10587 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
10588 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);
10589 if (err_code != NULL)
10590 *err_code = EMAIL_ERROR_INVALID_PARAM;
10594 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10597 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10602 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10607 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
10609 EM_DEBUG_FUNC_END();
10613 INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
10615 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
10616 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
10618 int error = EMAIL_ERROR_NONE;
10622 if (account_id >= FIRST_ACCOUNT_ID) {
10623 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10625 if (stat(buf, &sbuf) == 0) {
10626 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10627 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10628 error = EMAIL_ERROR_SYSTEM_FAILURE;
10633 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10634 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10635 EM_DEBUG_EXCEPTION("mkdir failed l(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10636 error = EMAIL_ERROR_SYSTEM_FAILURE;
10643 if (account_id < FIRST_ACCOUNT_ID) {
10644 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10645 error = EMAIL_ERROR_INVALID_PARAM;
10649 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf), "%s%d", DIR_SEPERATOR, mail_id);
10651 if (stat(buf, &sbuf) == 0) {
10652 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10653 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10655 error = EMAIL_ERROR_SYSTEM_FAILURE;
10660 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10661 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10662 EM_DEBUG_EXCEPTION("mkdir failed l (Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10663 error = EMAIL_ERROR_SYSTEM_FAILURE;
10670 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
10671 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10673 error = EMAIL_ERROR_INVALID_PARAM;
10677 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
10679 if (stat(buf, &sbuf) == 0) {
10680 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10681 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10683 error = EMAIL_ERROR_SYSTEM_FAILURE;
10688 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10689 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10690 error = EMAIL_ERROR_SYSTEM_FAILURE;
10699 if (err_code != NULL)
10702 EM_PROFILE_END(profile_emcore_save_create_dir);
10703 EM_DEBUG_FUNC_END("ret [%d]", ret);
10707 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10709 EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
10710 EM_DEBUG_LOG("Using the fsync function");
10712 int error = EMAIL_ERROR_NONE;
10713 struct stat st_buf;
10722 if (!src_file || !dst_file) {
10723 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10725 error = EMAIL_ERROR_INVALID_PARAM;
10729 if (stat(src_file, &st_buf) < 0) {
10730 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
10732 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
10736 buf_size = st_buf.st_size;
10737 EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
10738 buf = (char *)calloc(1, buf_size+1);
10741 EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
10742 error = EMAIL_ERROR_OUT_OF_MEMORY;
10747 if ((fp_src = open(src_file, O_RDONLY))<0) { /*prevent 24474*/
10748 EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed [ %d ] - Error [ %s ]", src_file, errno, strerror(errno));
10749 error = EMAIL_ERROR_SYSTEM_FAILURE;
10753 if ((fp_dst = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) { /*prevent 24474*/
10754 EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s Failed [ %d ] - Error [ %s ]", dst_file, errno, strerror(errno));
10755 error = EMAIL_ERROR_SYSTEM_FAILURE;
10759 while ((nread = read(fp_src, buf, buf_size)) > 0) {
10760 if (nread > 0 && nread <= buf_size) {
10761 EM_DEBUG_LOG("Nread Value [%d]", nread);
10762 if ((nwritten = write(fp_dst, buf, nread)) != nread) {
10763 EM_DEBUG_EXCEPTION("fwrite failed...[%d] : [%s]", errno, strerror(errno));
10764 error = EMAIL_ERROR_UNKNOWN;
10767 EM_DEBUG_LOG("NWRITTEN [%d]", nwritten);
10775 if (fp_src>0) /*prevent 24474*/
10778 if (fp_dst>0) { /*prevent 24474*/
10780 EM_DEBUG_LOG("Before fsync");
10786 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
10789 if (err_code != NULL)
10791 EM_DEBUG_FUNC_END("ret [%d]", ret);
10794 /* create Directory if user has deleted [deepam.p@samsung.com] */
10795 INTERNAL_FUNC void emstorage_create_dir_if_delete()
10797 EM_DEBUG_FUNC_BEGIN();
10799 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
10800 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
10801 mkdir(MAILHOME, DIRECTORY_PERMISSION);
10802 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10804 EM_DEBUG_FUNC_END();
10806 static int _get_temp_file_name(char **filename, int *err_code)
10808 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
10811 int error = EMAIL_ERROR_NONE;
10813 if (filename == NULL) {
10814 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
10815 error = EMAIL_ERROR_INVALID_PARAM;
10819 char tempname[512] = {0x00, };
10822 gettimeofday(&tv, NULL);
10825 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
10827 char *p = EM_SAFE_STRDUP(tempname);
10829 EM_DEBUG_EXCEPTION(" strdup failed...");
10830 error = EMAIL_ERROR_OUT_OF_MEMORY;
10839 if (err_code != NULL)
10842 EM_DEBUG_FUNC_END("ret [%d]", ret);
10846 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
10848 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
10850 EM_IF_NULL_RETURN_VALUE(file_path, false);
10851 EM_IF_NULL_RETURN_VALUE(char_set, false);
10852 EM_IF_NULL_RETURN_VALUE(err_code, false);
10856 struct stat st_buf;
10858 char *low_char_set = NULL;
10859 char *match_str = NULL;
10862 int error = EMAIL_ERROR_NONE;
10863 int data_count_to_written = 0;
10864 char *temp_file_name = NULL;
10867 FILE* fp_src = NULL;
10868 FILE* fp_dest = NULL;
10872 if (stat(file_path, &st_buf) < 0) {
10873 EM_DEBUG_EXCEPTION(" stat(\"%s\") failed...", file_path);
10875 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
10879 buf_size = st_buf.st_size;
10881 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
10883 buf = (char *)calloc(1, buf_size+1);
10886 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
10890 if (!(fp_src = fopen(file_path, "rb"))) {
10891 EM_DEBUG_EXCEPTION(" file_path fopen failed - %s", file_path);
10893 error = EMAIL_ERROR_SYSTEM_FAILURE;
10897 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
10898 if (nread > 0 && nread <= buf_size) {
10899 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
10902 * 1.Add check for whether content type is there.
10903 * 2. If not based on the character set, Append it in File
10906 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
10908 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
10910 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
10912 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
10914 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
10916 EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
10918 match_str = strstr(buf, CONTENT_TYPE_DATA);
10919 EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
10921 if (match_str == NULL) {
10922 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
10923 if (fp_src !=NULL) {
10924 fclose(fp_src);fp_src = NULL;
10926 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
10927 EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
10928 buf1 = (char *)calloc(1, data_count_to_written);
10929 EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
10932 EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
10933 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
10935 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
10937 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
10939 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
10942 /* 1. Create a temporary file name */
10943 if (!_get_temp_file_name(&temp_file_name, &err)) {
10944 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
10945 if (err_code != NULL) *err_code = err;
10946 EM_SAFE_FREE(temp_file_name);
10949 EM_DEBUG_LOG(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
10951 /* Open the Temp file in Append mode */
10952 if (!(fp_dest = fopen(temp_file_name, "ab"))) {
10953 EM_DEBUG_EXCEPTION(" fopen failed - %s", temp_file_name);
10954 error = EMAIL_ERROR_SYSTEM_FAILURE;
10958 /* 2. write the Latest data */
10959 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
10961 if (nwritten > 0) {
10962 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
10964 /* 3. Append old data */
10965 nwritten = fwrite(buf, nread-1, 1, fp_dest);
10967 if (nwritten <= 0) {
10968 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
10969 error = EMAIL_ERROR_SYSTEM_FAILURE;
10973 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
10975 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
10976 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
10983 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
10984 error = EMAIL_ERROR_SYSTEM_FAILURE;
10991 EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
11001 EM_SAFE_FREE(buf1);
11002 EM_SAFE_FREE(low_char_set);
11004 if (fp_src != NULL) {
11009 if (fp_dest != NULL) {
11013 EM_DEBUG_FUNC_END("ret [%d]", ret);
11018 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11020 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
11023 int error = EMAIL_ERROR_NONE;
11025 if (src_file == NULL || dst_file == NULL) {
11026 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11027 error = EMAIL_ERROR_INVALID_PARAM;
11031 EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
11033 if (strcmp(src_file, dst_file) != 0) {
11034 if (rename(src_file, dst_file) != 0) {
11035 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() */
11036 /* does not work across different mount points, even if the same file system is mounted on both.) */
11037 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
11038 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
11039 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
11043 EM_DEBUG_LOG("src[%s] removed", src_file);
11047 if (errno == ENOENT) {
11048 struct stat temp_file_stat;
11049 if (stat(src_file, &temp_file_stat) < 0)
11050 EM_DEBUG_EXCEPTION("no src file found [%s]", src_file);
11051 if (stat(dst_file, &temp_file_stat) < 0)
11052 EM_DEBUG_EXCEPTION("no dst file found [%s]", src_file);
11054 EM_DEBUG_EXCEPTION("no file found [%d]", errno);
11055 error = EMAIL_ERROR_FILE_NOT_FOUND;
11060 EM_DEBUG_EXCEPTION("rename failed [%d]", errno);
11061 error = EMAIL_ERROR_SYSTEM_FAILURE;
11068 EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
11074 if (err_code != NULL)
11077 EM_DEBUG_FUNC_END("ret [%d]", ret);
11081 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
11083 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
11086 int error = EMAIL_ERROR_NONE;
11088 if (src_file == NULL) {
11089 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
11091 error = EMAIL_ERROR_INVALID_PARAM;
11095 if (remove(src_file) != 0) {
11096 if (errno != ENOENT) {
11097 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
11099 error = EMAIL_ERROR_SYSTEM_FAILURE;
11103 EM_DEBUG_EXCEPTION(" no file found...");
11105 error = EMAIL_ERROR_FILE_NOT_FOUND;
11112 if (err_code != NULL)
11115 EM_DEBUG_FUNC_END("ret [%d]", ret);
11120 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
11122 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
11124 if (src_dir == NULL) {
11125 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
11127 if (err_code != NULL)
11128 *err_code = EMAIL_ERROR_INVALID_PARAM;
11132 int error = EMAIL_ERROR_NONE;
11139 dirp = opendir(src_dir);
11141 if (dirp == NULL) {
11142 if (errno == ENOENT) {
11143 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
11144 if (err_code != NULL)
11145 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11149 EM_DEBUG_EXCEPTION("opendir failed - %d", errno);
11150 if (err_code != NULL)
11151 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11156 while ((dp=readdir(dirp))) {
11157 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
11160 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
11162 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
11163 /* check directory */
11164 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
11165 /* recursive call */
11166 if (!emstorage_delete_dir(buf, &error)) {
11168 if (err_code != NULL)
11174 if (remove(buf) < 0) {
11175 EM_DEBUG_EXCEPTION("remove failed - %s", buf);
11177 if (err_code != NULL)
11178 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11184 EM_DEBUG_EXCEPTION("content does not exist...");
11189 EM_DEBUG_LOG("remove direcotory [%s]", src_dir);
11191 /* EM_DEBUG_FUNC_BEGIN(); */
11193 if (remove(src_dir) < 0) {
11194 EM_DEBUG_EXCEPTION("remove failed [%s]", src_dir);
11195 if (err_code != NULL)
11196 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11200 if (err_code != NULL)
11206 /* faizan.h@samsung.com */
11207 INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
11209 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
11210 int rc, ret = false;
11211 int error = EMAIL_ERROR_NONE;
11212 char sql_query_string[QUERY_SIZE] = {0, };
11214 int transaction = true;
11216 if (!old_server_uid || !new_server_uid) {
11217 EM_DEBUG_EXCEPTION("Invalid parameters");
11218 error = EMAIL_ERROR_INVALID_PARAM;
11221 sqlite3 *local_db_handle = emstorage_get_db_connection();
11223 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11225 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11226 "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
11227 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
11229 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11230 ("sqlite3_exec fail:%d", rc));
11231 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11232 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11237 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11240 if (err_code != NULL)
11243 EM_DEBUG_FUNC_END("ret [%d]", ret);
11248 INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
11250 EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
11252 int rc, ret = false;
11253 int error = EMAIL_ERROR_NONE;
11254 char sql_query_string[QUERY_SIZE] = {0, };
11256 int transaction = true;
11258 if (!mail_id || !new_server_uid || !mbox_name) {
11259 EM_DEBUG_EXCEPTION("Invalid parameters");
11260 error = EMAIL_ERROR_INVALID_PARAM;
11263 sqlite3 *local_db_handle = emstorage_get_db_connection();
11264 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11267 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11268 "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);
11269 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
11270 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11271 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11276 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11279 if (err_code != NULL)
11282 EM_DEBUG_FUNC_END("ret [%d]", ret);
11288 int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
11290 EM_DEBUG_FUNC_BEGIN();
11292 if ((!mail_id) ||(account_id <= 0 && account_id != -1)) {
11293 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
11294 if (err_code != NULL)
11295 *err_code = EMAIL_ERROR_INVALID_PARAM;
11301 int error = EMAIL_ERROR_NONE;
11302 DB_STMT hStmt = NULL;
11305 int transaction = false;
11306 char sql_query_string[QUERY_SIZE] = {0, };
11308 sqlite3 *local_db_handle = emstorage_get_db_connection();
11309 EMSTORAGE_START_READ_TRANSACTION(transaction);
11311 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11313 if (account_id == -1)
11314 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
11316 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);
11318 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11319 EM_DEBUG_LOG(" sqlite3_prepare hStmt = %p", hStmt);
11320 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11321 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11323 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11324 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11325 ("sqlite3_step fail:%d", rc));
11329 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11330 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11331 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11333 sqlite3_free_table(result);
11335 EM_DEBUG_EXCEPTION("no Mails found...");
11337 error= EMAIL_ERROR_MAIL_NOT_FOUND;
11341 _get_stmt_field_data_int(hStmt, &mailid, 0);
11342 EM_DEBUG_LOG("mailid [%d]", mailid);
11347 if (hStmt != NULL) {
11348 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11350 rc = sqlite3_finalize(hStmt);
11351 if (rc != SQLITE_OK) {
11352 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11354 error = EMAIL_ERROR_DB_FAILURE;
11357 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11360 if (mail_id != NULL)
11363 if (err_code != NULL)
11366 EM_DEBUG_FUNC_END("ret [%d]", ret);
11370 int setting_system_command(const char *command)
11373 int pid = 0, status = 0;
11374 char *const environ[] = { NULL };
11389 argv[2] = (char *)command;
11392 execve("/bin/sh", argv, environ);
11396 if (waitpid(pid, &status, 0) == -1) {
11397 if (errno != EINTR)
11409 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
11411 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
11413 if (!total_usage) {
11414 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
11416 if (err_code != NULL)
11417 *err_code = EMAIL_ERROR_INVALID_PARAM;
11422 int error = EMAIL_ERROR_NONE;
11423 char syscmd[256] = {0, };
11424 char line[256] = {0, };
11425 char *line_from_file = NULL;
11427 unsigned long total_diskusage = 0;
11429 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
11430 if (setting_system_command(syscmd) == -1) {
11431 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11433 error = EMAIL_ERROR_SYSTEM_FAILURE;
11437 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
11438 EM_DEBUG_LOG(" cmd : %s", syscmd);
11439 if (setting_system_command(syscmd) == -1) {
11440 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
11442 error = EMAIL_ERROR_SYSTEM_FAILURE;
11446 fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
11448 perror(SETTING_MEMORY_TEMP_FILE_PATH);
11450 error = EMAIL_ERROR_SYSTEM_FAILURE;
11454 line_from_file = fgets(line, sizeof(line), fp);
11456 if(line_from_file == NULL) {
11457 EM_DEBUG_EXCEPTION("fgets failed");
11458 error = EMAIL_ERROR_SYSTEM_FAILURE;
11461 total_diskusage = strtoul(line, NULL, 10);
11463 memset(syscmd, 0, sizeof(syscmd));
11464 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
11465 if (setting_system_command(syscmd) == -1) {
11466 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11467 error = EMAIL_ERROR_SYSTEM_FAILURE;
11471 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
11476 if (err_code != NULL)
11480 *total_usage = total_diskusage;
11484 if(fp) fclose(fp); /* prevent 32730 */
11486 EM_DEBUG_FUNC_END("ret [%d]", ret);
11491 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)
11493 DB_STMT hStmt = NULL;
11495 int error = EMAIL_ERROR_NONE;
11497 char sql_query_string[QUERY_SIZE] = {0, };
11499 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11500 "INSERT INTO mail_tbl VALUES "
11501 "( ?" /* mail_id */
11502 ", ?" /* account_id */
11503 ", ?" /* mail_size */
11504 ", ?" /* server_mail_status */
11505 ", ?" /* server_mailbox_name */
11506 ", ?" /* server_mail_id */
11507 ", ?" /* reference_mail_id */
11508 ", ?" /* full_address_from */
11509 ", ?" /* full_address_reply */
11510 ", ?" /* full_address_to */
11511 ", ?" /* full_address_cc */
11512 ", ?" /* full_address_bcc */
11513 ", ?" /* full_address_return */
11514 ", ?" /* subject */
11515 ", ?" /* body_download_status */
11516 ", ?" /* file_path_plain */
11517 ", ?" /* file_path_html */
11518 ", ?" /* date_time */
11519 ", ?" /* flags_seen_field */
11520 ", ?" /* flags_deleted_field */
11521 ", ?" /* flags_flagged_field */
11522 ", ?" /* flags_answered_field */
11523 ", ?" /* flags_recent_field */
11524 ", ?" /* flags_draft_field */
11525 ", ?" /* flags_forwarded_field */
11526 ", ?" /* DRM_status */
11527 ", ?" /* priority */
11528 ", ?" /* save_status */
11529 ", ?" /* lock_status */
11530 ", ?" /* message_id */
11531 ", ?" /* report_status */
11532 ", ?" /* email_address_sender */
11533 ", ?" /* email_address_recipient */
11534 ", ?" /* attachment_count */
11535 ", ?" /* inline_content_count */
11536 ", ?" /* preview_text */
11537 ", ?" /* thread_id */
11538 ", ?" /* mailbox_type */
11539 ", ?" /* alias_sender */
11540 ", ?" /* alias_recipient */
11541 ", ?" /* thread_item_count */
11542 ", ?" /* meeting_request_status */
11543 ", ?" /* message_class */
11544 ", ?" /* digest_type */
11545 ", ?" /* smime_type */
11546 ", ?" /* scheduled_sending_time */
11547 ", ?" /* eas_data_length */
11548 ", ?" /* eas_data */
11551 int transaction = true;
11552 sqlite3 *local_db_handle = emstorage_get_db_connection();
11554 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11557 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11558 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11559 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11561 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
11562 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
11563 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
11564 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
11565 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
11566 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
11567 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
11568 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
11569 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
11570 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
11571 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
11572 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
11573 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
11574 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
11575 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
11576 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
11577 _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);
11578 _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);
11579 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11580 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11581 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
11582 _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);
11583 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
11584 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
11585 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
11586 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
11587 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
11588 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
11589 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
11590 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
11591 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
11592 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
11593 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
11594 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
11595 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
11596 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
11597 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11598 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
11599 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11600 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
11601 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
11602 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
11603 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
11604 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
11605 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
11606 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
11607 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
11608 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
11610 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11611 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11612 ("sqlite3_step fail:%d", rc));
11613 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11614 ("sqlite3_step fail:%d", rc));
11618 if (hStmt != NULL) {
11619 rc = sqlite3_finalize(hStmt);
11620 if (rc != SQLITE_OK) {
11621 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11622 error = EMAIL_ERROR_DB_FAILURE;
11626 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11629 if (err_code != NULL)
11632 EM_DEBUG_FUNC_END("ret [%d]", ret);
11637 INTERNAL_FUNC int emstorage_get_max_mail_count()
11639 return EMAIL_MAIL_MAX_COUNT;
11642 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
11644 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)
11646 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);
11647 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
11648 int rc = 0, query_size = 0, query_size_account = 0;
11649 int account_id = 0;
11650 int err_code = EMAIL_ERROR_NONE;
11651 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
11652 time_t latest_date_time = 0;
11653 time_t date_time = 0;
11654 char *subject = NULL;
11655 char *sql_query_string = NULL, *sql_account = NULL;
11656 char *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
11657 char *sql_format_account = " AND account_id = %d ";
11658 char *sql_format_order_by = " ORDER BY date_time DESC ";
11659 char **result = NULL;
11660 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
11661 sqlite3 *local_db_handle = emstorage_get_db_connection();
11663 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
11664 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
11665 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
11666 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
11668 account_id = mail_tbl->account_id;
11669 subject = mail_tbl->subject;
11670 date_time = mail_tbl->date_time;
11672 EM_DEBUG_LOG("subject : %s", subject);
11674 if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
11675 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
11676 err_code = EMAIL_ERROR_UNKNOWN;
11677 result_thread_id = -1;
11681 if (EM_SAFE_STRLEN(stripped_subject) < 2) {
11682 result_thread_id = -1;
11686 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
11688 if (account_id > 0) {
11689 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
11690 sql_account = malloc(query_size_account);
11691 if (sql_account == NULL) {
11692 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
11693 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
11696 snprintf(sql_account, query_size_account, sql_format_account, account_id);
11699 /* prevent 34362 */
11700 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
11701 sql_query_string = malloc(query_size);
11703 if (sql_query_string == NULL) {
11704 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
11705 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
11709 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
11711 if (account_id > 0)
11712 strcat(sql_query_string, sql_account);
11714 strcat(sql_query_string, sql_format_order_by);
11715 strcat(sql_query_string, ";");
11717 EM_DEBUG_LOG("Query : %s", sql_query_string);
11719 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11721 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11722 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11724 EM_DEBUG_LOG("Result rows count : %d", count);
11727 result_thread_id = -1;
11729 _get_table_field_data_int (result, &result_thread_id, 3);
11730 _get_table_field_data_time_t(result, &latest_date_time, 4);
11731 _get_table_field_data_int (result, &latest_mail_id_in_thread, 5);
11733 if (latest_date_time < mail_tbl->date_time)
11734 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
11736 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
11737 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
11741 *thread_id = result_thread_id;
11742 *thread_item_count = count;
11744 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
11745 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
11746 EM_DEBUG_LOG("err_code : %d", err_code);
11748 EM_SAFE_FREE(sql_account);
11749 EM_SAFE_FREE(sql_query_string);
11751 sqlite3_free_table(result);
11753 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
11759 INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
11761 EM_DEBUG_FUNC_BEGIN();
11763 int count = 0, ret = false;
11764 int error = EMAIL_ERROR_NONE;
11765 emstorage_mail_tbl_t *p_data_tbl = NULL;
11766 char conditional_clause[QUERY_SIZE] = {0, };
11768 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
11770 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
11771 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
11773 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
11774 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
11779 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
11785 *mail_tbl = p_data_tbl;
11786 else if (p_data_tbl != NULL)
11787 emstorage_free_mail(&p_data_tbl, 1, NULL);
11789 if (err_code != NULL)
11792 EM_DEBUG_FUNC_END("ret [%d]", ret);
11797 INTERNAL_FUNC int emstorage_get_sender_list(int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count, int *err_code)
11799 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
11800 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
11802 if ((!sender_list) ||(!sender_count)) {
11803 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11804 if (err_code != NULL)
11805 *err_code = EMAIL_ERROR_INVALID_PARAM;
11809 int rc = -1, ret = false;
11810 int error = EMAIL_ERROR_NONE;
11812 int i, col_index = 0;
11813 int read_count = 0;
11814 email_sender_list_t *p_sender_list = NULL;
11815 char sql_query_string[QUERY_SIZE] = {0, };
11816 char **result = NULL;
11817 sqlite3 *local_db_handle = emstorage_get_db_connection();
11819 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11820 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
11825 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d ", mailbox_id);
11826 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
11827 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) ");
11830 /* '0' (ALL_ACCOUNT) means all account */
11831 if (account_id > ALL_ACCOUNT)
11832 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);
11834 if (search_value) {
11835 switch (search_type) {
11836 case EMAIL_SEARCH_FILTER_SUBJECT:
11837 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11838 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
11840 case EMAIL_SEARCH_FILTER_SENDER:
11841 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11842 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11843 ") ", search_value);
11845 case EMAIL_SEARCH_FILTER_RECIPIENT:
11846 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11847 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11848 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11849 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11850 ") ", search_value, search_value, search_value);
11852 case EMAIL_SEARCH_FILTER_ALL:
11853 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11854 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
11855 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11856 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11857 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11858 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11861 ")", search_value, search_value, search_value, search_value, search_value);
11867 /* sorting option is not available now. The order of sender list is ascending order by display name */
11868 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
11869 "GROUP BY email_address_sender "
11870 "ORDER BY UPPER(alias_sender) ");
11872 EM_DEBUG_LOG("query[%s]", sql_query_string);
11874 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
11875 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11876 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11878 EM_DEBUG_LOG("Count of Sender [%d]", count);
11880 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
11881 EM_DEBUG_EXCEPTION("em_malloc failed...");
11882 error = EMAIL_ERROR_OUT_OF_MEMORY;
11888 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
11889 for (i = 0; i < count; i++) {
11890 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
11891 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
11892 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
11893 _get_table_field_data_int(result, &(read_count), col_index++);
11894 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
11896 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
11898 sqlite3_free_table(result);
11905 *sender_list = p_sender_list;
11906 *sender_count = count;
11907 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
11912 if (err_code != NULL)
11915 EM_DEBUG_FUNC_END("ret [%d]", ret);
11919 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
11921 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
11923 int err = EMAIL_ERROR_NONE;
11926 if (!sender_list || !*sender_list) {
11927 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
11928 err = EMAIL_ERROR_INVALID_PARAM;
11932 email_sender_list_t* p = *sender_list;
11935 for (; i < count; i++) {
11936 EM_SAFE_FREE(p[i].address);
11937 EM_SAFE_FREE(p[i].display_name);
11941 *sender_list = NULL;
11948 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
11950 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
11952 int err = EMAIL_ERROR_NONE;
11953 email_address_info_t *p_address_info = NULL;
11954 GList *list = NULL;
11955 GList *node = NULL;
11958 if (!address_info_list || !*address_info_list) {
11959 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
11960 err = EMAIL_ERROR_INVALID_PARAM;
11964 /* delete GLists */
11965 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
11967 case EMAIL_ADDRESS_TYPE_FROM:
11968 list = (*address_info_list)->from;
11970 case EMAIL_ADDRESS_TYPE_TO:
11971 list = (*address_info_list)->to;
11973 case EMAIL_ADDRESS_TYPE_CC:
11974 list = (*address_info_list)->cc;
11976 case EMAIL_ADDRESS_TYPE_BCC:
11977 list = (*address_info_list)->bcc;
11981 /* delete dynamic-allocated memory for each item */
11982 node = g_list_first(list);
11983 while (node != NULL) {
11984 p_address_info = (email_address_info_t*)node->data;
11985 EM_SAFE_FREE(p_address_info->address);
11986 EM_SAFE_FREE(p_address_info->display_name);
11987 EM_SAFE_FREE(node->data);
11989 node = g_list_next(node);
11993 EM_SAFE_FREE(*address_info_list);
11994 *address_info_list = NULL;
11996 EM_DEBUG_FUNC_END("err [%d]", err);
11999 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
12001 INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
12003 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
12005 if (!local_activity || !activity_id) {
12006 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
12007 if (err_code != NULL)
12008 *err_code = EMAIL_ERROR_INVALID_PARAM;
12014 int error = EMAIL_ERROR_NONE;
12017 char sql_query_string[QUERY_SIZE] = {0, };
12018 DB_STMT hStmt = NULL;
12019 sqlite3 *local_db_handle = emstorage_get_db_connection();
12020 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12021 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12022 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12023 "INSERT INTO mail_partial_body_activity_tbl VALUES "
12025 "? " /* Account ID */
12026 ",?" /* Local Mail ID */
12027 ",?" /* Server mail ID */
12028 ",?" /* Activity ID */
12029 ",?" /* Activity type*/
12030 ",?" /* Mailbox ID*/
12031 ",?" /* Mailbox name*/
12034 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
12035 char **result = NULL;
12038 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
12040 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12041 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
12043 if (NULL==result[1]) rc = 1;
12044 else rc = atoi(result[1])+1;
12045 sqlite3_free_table(result);
12048 *activity_id = local_activity->activity_id = rc;
12050 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
12051 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
12053 if (local_activity->mailbox_id)
12054 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
12057 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12058 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12059 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12061 EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
12063 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
12064 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
12065 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
12066 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
12067 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
12068 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
12069 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
12072 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12074 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12075 ("sqlite3_step fail:%d", rc));
12076 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12077 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
12083 if (hStmt != NULL) {
12084 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12086 rc = sqlite3_finalize(hStmt);
12088 if (rc != SQLITE_OK) {
12089 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12090 error = EMAIL_ERROR_DB_FAILURE;
12094 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12096 if (err_code != NULL)
12099 EM_DEBUG_FUNC_END("ret [%d]", ret);
12104 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
12106 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
12108 if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
12109 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
12110 if (err_code != NULL)
12111 *err_code = EMAIL_ERROR_INVALID_PARAM;
12116 int error = EMAIL_ERROR_NONE;
12118 int i = 0, rc = -1;
12119 int *mbox_list = NULL;
12120 DB_STMT hStmt = NULL;
12121 char sql_query_string[QUERY_SIZE] = {0, };
12123 sqlite3 *local_db_handle = emstorage_get_db_connection();
12125 EMSTORAGE_START_READ_TRANSACTION(transaction);
12127 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12128 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);
12131 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12132 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12133 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12135 *count = atoi(result[1]);
12136 sqlite3_free_table(result);
12139 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
12140 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
12144 EM_DEBUG_LOG("Mailbox count = %d", *count);
12146 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12148 /* 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); */
12149 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);
12151 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12154 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12156 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12157 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12158 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12161 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12162 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12163 ("sqlite3_step fail:%d", rc));
12165 if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
12166 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12167 error = EMAIL_ERROR_OUT_OF_MEMORY;
12171 memset(mbox_list, 0x00, sizeof(int) * (*count));
12173 for (i = 0; i < (*count); i++) {
12174 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
12175 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12176 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
12177 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12178 ("sqlite3_step fail:%d", rc));
12179 EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
12186 *mailbox_list = mbox_list;
12188 EM_SAFE_FREE(mbox_list);
12190 if (hStmt != NULL) {
12191 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12193 rc = sqlite3_finalize(hStmt);
12195 if (rc != SQLITE_OK) {
12196 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
12197 error = EMAIL_ERROR_DB_FAILURE;
12201 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12203 if (err_code != NULL)
12205 EM_DEBUG_FUNC_END("ret [%d]", ret);
12210 INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
12212 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
12214 if (NULL == &account_list || NULL == count) {
12215 EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
12216 if (err_code != NULL)
12217 *err_code = EMAIL_ERROR_INVALID_PARAM;
12222 int error = EMAIL_ERROR_NONE;
12225 int i = 0, rc = -1;
12226 int *result_account_list = NULL;
12227 DB_STMT hStmt = NULL;
12228 char sql_query_string[QUERY_SIZE] = {0, };
12230 sqlite3 *local_db_handle = emstorage_get_db_connection();
12232 EMSTORAGE_START_READ_TRANSACTION(transaction);
12235 sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
12238 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
12239 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12240 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
12242 *count = atoi(result[1]);
12243 sqlite3_free_table(result);
12246 EM_DEBUG_EXCEPTION("no account found...");
12247 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
12252 EM_DEBUG_LOG("Account count [%d]", *count);
12254 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12256 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
12258 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12261 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12263 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
12264 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12265 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12268 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12269 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12270 ("sqlite3_step fail:%d", rc));
12272 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
12273 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12274 error = EMAIL_ERROR_OUT_OF_MEMORY;
12278 memset(result_account_list, 0x00, sizeof(int) * (*count));
12280 for (i = 0; i < (*count); i++) {
12281 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
12283 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12284 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12285 ("sqlite3_step fail:%d", rc));
12286 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
12293 *account_list = result_account_list;
12295 EM_SAFE_FREE(result_account_list);
12297 if (hStmt != NULL) {
12298 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12300 rc = sqlite3_finalize(hStmt);
12302 if (rc != SQLITE_OK) {
12303 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12304 error = EMAIL_ERROR_DB_FAILURE;
12308 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12310 if (err_code != NULL)
12312 EM_DEBUG_FUNC_END("ret [%d]", ret);
12318 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)
12320 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
12322 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
12323 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);
12325 if (err_code != NULL)
12326 *err_code = EMAIL_ERROR_INVALID_PARAM;
12333 int error = EMAIL_ERROR_NONE;
12335 DB_STMT hStmt = NULL;
12336 email_event_partial_body_thd* event_list = NULL;
12337 char sql_query_string[QUERY_SIZE] = {0, };
12339 sqlite3 *local_db_handle = emstorage_get_db_connection();
12341 EMSTORAGE_START_READ_TRANSACTION(transaction);
12343 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12344 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);
12347 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12348 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12349 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12351 *count = atoi(result[1]);
12352 sqlite3_free_table(result);
12354 EM_DEBUG_LOG("Query = [%s]", sql_query_string);
12357 EM_DEBUG_EXCEPTION("No matched activity found in mail_partial_body_activity_tbl");
12359 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12363 EM_DEBUG_LOG("Activity Count = %d", *count);
12365 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12366 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);
12368 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12371 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12373 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12374 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12375 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12378 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12379 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12380 ("sqlite3_step fail:%d", rc));
12382 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
12383 EM_DEBUG_EXCEPTION("Malloc failed");
12385 error = EMAIL_ERROR_OUT_OF_MEMORY;
12388 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
12390 for (i=0; i < (*count); i++) {
12391 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12392 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12393 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12394 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12395 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12396 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12397 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12399 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12400 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
12401 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12402 ("sqlite3_step fail:%d", rc));
12404 event_list[i].event_type = 0;
12411 *event_start = event_list;
12413 EM_SAFE_FREE(event_list);
12416 if (hStmt != NULL) {
12417 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12419 rc = sqlite3_finalize(hStmt);
12421 if (rc != SQLITE_OK) {
12422 EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
12423 error = EMAIL_ERROR_DB_FAILURE;
12427 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12429 if (err_code != NULL)
12432 EM_DEBUG_FUNC_END("ret [%d]", ret);
12438 INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
12440 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);
12443 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
12444 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);
12446 if (err_code != NULL)
12447 *err_code = EMAIL_ERROR_INVALID_PARAM;
12453 int error = EMAIL_ERROR_NONE;
12454 char sql_query_string[QUERY_SIZE] = {0, };
12456 sqlite3 *local_db_handle = emstorage_get_db_connection();
12458 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12459 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12461 if (activity_id == 0)
12462 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);
12464 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);
12466 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12468 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12469 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12470 ("sqlite3_exec fail:%d", rc));
12471 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12472 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12474 /* validate activity existence */
12475 rc = sqlite3_changes(local_db_handle);
12477 EM_DEBUG_EXCEPTION("No matching activity found");
12478 error = EMAIL_ERROR_DATA_NOT_FOUND;
12487 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12490 if (err_code != NULL)
12493 EM_DEBUG_FUNC_END("ret [%d]", ret);
12497 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
12499 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12501 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
12502 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12503 if (err_code != NULL)
12504 *err_code = EMAIL_ERROR_INVALID_PARAM;
12509 int error = EMAIL_ERROR_NONE;
12510 char sql_query_string[QUERY_SIZE] = {0, };
12512 DB_STMT hStmt = NULL;
12514 sqlite3 *local_db_handle = emstorage_get_db_connection();
12516 EMSTORAGE_START_READ_TRANSACTION(transaction);
12517 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12519 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);
12521 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12524 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12525 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
12526 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12527 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12530 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12531 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12532 ("sqlite3_step fail:%d", rc));
12534 _get_stmt_field_data_int(hStmt, activity_count, 0);
12536 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12542 if (hStmt != NULL) {
12543 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12544 rc = sqlite3_finalize(hStmt);
12546 if (rc != SQLITE_OK) {
12547 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12548 error = EMAIL_ERROR_DB_FAILURE;
12550 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12553 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12556 if (err_code != NULL)
12559 EM_DEBUG_FUNC_END("ret [%d]", ret);
12564 INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
12566 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
12568 if (NULL == activity_count || NULL == err_code) {
12569 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
12570 if (err_code != NULL)
12571 *err_code = EMAIL_ERROR_INVALID_PARAM;
12576 int error = EMAIL_ERROR_NONE;
12577 DB_STMT hStmt = NULL;
12578 char sql_query_string[QUERY_SIZE] = {0, };
12580 sqlite3 *local_db_handle = emstorage_get_db_connection();
12582 EMSTORAGE_START_READ_TRANSACTION(transaction);
12583 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12585 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
12587 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12590 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12591 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
12592 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12593 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12596 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12597 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12598 ("sqlite3_step fail:%d", rc));
12600 _get_stmt_field_data_int(hStmt, activity_count, 0);
12602 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12609 if (hStmt != NULL) {
12610 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12612 rc = sqlite3_finalize(hStmt);
12614 if (rc != SQLITE_OK) {
12615 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12616 error = EMAIL_ERROR_DB_FAILURE;
12618 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12621 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12623 if (err_code != NULL)
12626 EM_DEBUG_FUNC_END("ret [%d]", ret);
12630 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
12632 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12633 if (account_id < FIRST_ACCOUNT_ID) {
12634 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12635 if (err_code != NULL)
12636 *err_code = EMAIL_ERROR_INVALID_PARAM;
12642 int error = EMAIL_ERROR_NONE;
12643 char sql_query_string[QUERY_SIZE] = {0, };
12645 sqlite3 *local_db_handle = emstorage_get_db_connection();
12646 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12647 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12648 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
12650 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12652 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12653 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12654 ("sqlite3_exec fail:%d", rc));
12655 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12656 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12659 rc = sqlite3_changes(local_db_handle);
12661 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
12662 error = EMAIL_ERROR_DATA_NOT_FOUND;
12671 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12673 if (err_code != NULL)
12676 EM_DEBUG_FUNC_END("ret [%d]", ret);
12680 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
12681 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*/
12683 INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12685 EM_DEBUG_FUNC_BEGIN("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12687 int rc = -1, ret = false;
12688 int error = EMAIL_ERROR_NONE;
12689 char sql_query_string[QUERY_SIZE] = {0, };
12691 int transaction = true;
12693 if (!old_server_uid || !new_server_uid || !mbox_name) {
12694 EM_DEBUG_EXCEPTION("Invalid parameters");
12695 error = EMAIL_ERROR_INVALID_PARAM;
12699 sqlite3 *local_db_handle = emstorage_get_db_connection();
12700 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12701 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12702 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12703 "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);
12705 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12708 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12709 ("sqlite3_exec fail:%d", rc));
12710 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12711 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12713 rc = sqlite3_changes(local_db_handle);
12715 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12716 error = EMAIL_ERROR_DATA_NOT_FOUND;
12724 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12726 if (err_code != NULL)
12729 EM_DEBUG_FUNC_END("ret [%d]", ret);
12734 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
12736 EM_DEBUG_FUNC_BEGIN("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
12739 int error = EMAIL_ERROR_NONE;
12741 FILE* fp_dst = NULL;
12743 if (!data_string || !dst_file_name) {
12744 EM_DEBUG_EXCEPTION("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
12745 error = EMAIL_ERROR_INVALID_PARAM;
12749 fp_dst = fopen(dst_file_name, "w");
12752 EM_DEBUG_EXCEPTION("fopen failed - %s", dst_file_name);
12754 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
12756 error = EMAIL_ERROR_SYSTEM_FAILURE;
12760 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
12761 EM_DEBUG_EXCEPTION("fwrite failed...");
12762 error = EMAIL_ERROR_UNKNOWN;
12770 if (fp_dst != NULL)
12773 if (err_code != NULL)
12776 EM_DEBUG_FUNC_END("ret [%d]", ret);
12780 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
12784 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
12786 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)
12788 EM_DEBUG_FUNC_BEGIN();
12791 int error = EMAIL_ERROR_NONE;
12792 char sql_query_string[QUERY_SIZE] = {0, };
12794 int transaction = true;
12796 if (!old_server_uid || !new_server_uid || !mbox_name) {
12797 EM_DEBUG_EXCEPTION("Invalid parameters");
12798 error = EMAIL_ERROR_INVALID_PARAM;
12802 EM_DEBUG_LOG("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12804 sqlite3 *local_db_handle = emstorage_get_db_connection();
12805 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12808 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12809 "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);
12811 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12813 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12814 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12815 ("sqlite3_exec fail:%d", rc));
12816 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12817 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12820 rc = sqlite3_changes(local_db_handle);
12823 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12824 error = EMAIL_ERROR_DATA_NOT_FOUND;
12833 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12836 if (err_code != NULL)
12839 EM_DEBUG_FUNC_END("ret [%d]", ret);
12846 * @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);
12847 * Prepare an array of mail_id and corresponding server mail id.
12849 *@author h.gahlaut@samsung.com
12850 * @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)
12851 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
12852 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
12853 * @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
12854 * @param[out] err_code Returns the error code.
12855 * @remarks An Example of Query to be exexuted in this API:
12856 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
12857 * @return This function returns true on success or false on failure.
12860 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)
12862 EM_DEBUG_FUNC_BEGIN();
12863 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
12865 int error = EMAIL_ERROR_NONE;
12867 email_id_set_t* p_id_set = NULL;
12869 const int buf_size = QUERY_SIZE;
12870 char sql_query_string[QUERY_SIZE] = {0, };
12871 int space_left_in_query_buffer = buf_size;
12874 char *server_mail_id = NULL;
12875 char **result = NULL;
12879 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
12880 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]", mail_ids, idset, id_set_count);
12881 error = EMAIL_ERROR_INVALID_PARAM;
12885 sqlite3 *local_db_handle = emstorage_get_db_connection();
12887 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);
12889 EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
12891 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12892 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12893 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12894 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12896 EM_DEBUG_LOG(" Count of mails [%d ]", count);
12899 EM_DEBUG_EXCEPTION("Can't find proper mail");
12900 error = EMAIL_ERROR_DATA_NOT_FOUND;
12905 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
12906 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12907 error = EMAIL_ERROR_OUT_OF_MEMORY;
12913 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
12914 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
12915 for (i = 0; i < count; i++) {
12916 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
12917 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
12918 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
12919 EM_SAFE_FREE(server_mail_id);
12921 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
12922 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
12924 sqlite3_free_table(result);
12933 *id_set_count = count;
12934 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
12937 EM_SAFE_FREE(p_id_set);
12941 if (err_code != NULL)
12944 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
12946 EM_DEBUG_FUNC_END("ret [%d]", ret);
12954 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
12956 EM_DEBUG_FUNC_BEGIN();
12957 int rc, ret = true, transaction = true;
12958 int error = EMAIL_ERROR_NONE;
12959 char sql_query_string[QUERY_SIZE] = {0, };
12961 sqlite3 *local_db_handle = emstorage_get_db_connection();
12962 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12964 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
12966 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12967 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12968 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12970 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
12972 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12973 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12974 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12977 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
12979 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12980 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12981 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12987 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12991 EM_DEBUG_FUNC_END("ret [%d]", ret);
12995 INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbox_id, int dest_mailbox_type, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
12997 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);
12999 if ((account_id <= 0) || (dest_mailbox_id <= 0) || (!rule) || (!rule->value)|| (!filtered_mail_id_list) || (!count_of_mails)) {
13000 EM_DEBUG_EXCEPTION("Invalid Parameter");
13002 if (err_code != NULL)
13003 *err_code = EMAIL_ERROR_INVALID_PARAM;
13007 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
13008 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
13009 char **result = NULL, *where_pararaph = NULL;
13010 char sql_query_string[QUERY_SIZE] = {0, };
13012 sqlite3 *local_db_handle = emstorage_get_db_connection();
13014 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
13016 EM_DEBUG_LOG("rule->value [%s]", rule->value);
13018 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 100;
13019 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
13021 if (where_pararaph == NULL) {
13022 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
13023 error = EMAIL_ERROR_OUT_OF_MEMORY;
13027 memset(where_pararaph, 0, sizeof(char) * where_pararaph_length);
13029 EM_DEBUG_LOG("rule->type [%d], rule->flag2[%d]", rule->type, rule->flag2);
13031 if (rule->type == EMAIL_FILTER_FROM) {
13032 if (rule->flag2 == RULE_TYPE_INCLUDES)
13033 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);
13034 else /* RULE_TYPE_EXACTLY */
13035 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);
13037 else if (rule->type == EMAIL_FILTER_SUBJECT) {
13038 if (rule->flag2 == RULE_TYPE_INCLUDES)
13039 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);
13040 else /* RULE_TYPE_EXACTLY */
13041 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject = \'%s\'", account_id, rule->value);
13044 error = EMAIL_ERROR_INVALID_PARAM;
13045 EM_DEBUG_EXCEPTION("rule->type is invald");
13049 /* prevent 34361 */
13050 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13051 strcat(sql_query_string, where_pararaph);
13053 EM_DEBUG_LOG("query[%s]", sql_query_string);
13055 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
13056 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13057 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13058 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13060 EM_DEBUG_LOG("Count of mails [%d]", count);
13063 mail_list = malloc(sizeof(int) * count);
13064 if (mail_list == NULL) {
13065 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
13066 error = EMAIL_ERROR_OUT_OF_MEMORY;
13072 for (i = 0; i < count; i++)
13073 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
13075 memset(sql_query_string, 0x00, QUERY_SIZE);
13076 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
13078 /* prevent 34361 */
13079 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13080 strcat(sql_query_string, where_pararaph);
13082 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13083 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13084 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13086 #ifdef __FEATURE_BODY_SEARCH__
13087 /* Updating mail_text_tbl */
13088 memset(sql_query_string, 0x00, QUERY_SIZE);
13089 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
13090 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13091 strcat(sql_query_string, where_pararaph);
13092 EM_DEBUG_LOG("Query [%s]", sql_query_string);
13094 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13095 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13096 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13105 *filtered_mail_id_list = mail_list;
13107 EM_SAFE_FREE(mail_list);
13109 sqlite3_free_table(result);
13114 EM_SAFE_FREE(where_pararaph);
13116 if (ret && count_of_mails)
13117 *count_of_mails = count;
13119 if (err_code != NULL)
13122 EM_DEBUG_FUNC_END("ret [%d]", ret);
13126 #define EMAIL_SLOT_UNIT 25
13128 INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
13130 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);
13131 int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
13132 int where_pararaph_length = 0;
13133 char *where_pararaph = NULL;
13134 char sql_query_string[QUERY_SIZE] = {0, };
13136 sqlite3 *local_db_handle = emstorage_get_db_connection();
13138 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13140 if (new_slot_size > 0)
13141 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
13142 else if (new_slot_size == 0)
13143 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
13145 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
13149 where_pararaph_length = 80;
13151 where_pararaph_length = 50;
13153 if (new_slot_size == 0)
13154 where_pararaph_length += 70;
13156 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
13157 if (where_pararaph == NULL) {
13158 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
13159 err = EMAIL_ERROR_OUT_OF_MEMORY;
13162 memset(where_pararaph, 0x00, where_pararaph_length);
13164 if (account_id > ALL_ACCOUNT) {
13167 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
13169 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
13172 if (new_slot_size == 0)
13173 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"));
13175 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
13176 strcat(sql_query_string, where_pararaph);
13178 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
13179 err = EMAIL_ERROR_OUT_OF_MEMORY;
13183 EM_DEBUG_LOG("query[%s]", sql_query_string);
13185 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13186 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13187 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13193 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13197 EM_SAFE_FREE(where_pararaph);
13199 if (err_code != NULL)
13202 EM_DEBUG_FUNC_END("ret [%d]", ret);
13206 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)
13208 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);
13210 if (!meeting_req || meeting_req->mail_id <= 0) {
13212 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
13214 if (err_code != NULL)
13215 *err_code = EMAIL_ERROR_INVALID_PARAM;
13222 int error = EMAIL_ERROR_NONE;
13223 DB_STMT hStmt = NULL;
13224 char sql_query_string[QUERY_SIZE] = {0, };
13226 time_t temp_unix_time = 0;
13228 sqlite3 *local_db_handle = emstorage_get_db_connection();
13229 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13231 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13232 "INSERT INTO mail_meeting_tbl VALUES "
13233 "( ?" /* mail_id */
13234 ", ?" /* account_id */
13235 ", ?" /* mailbox_id */
13236 ", ?" /* meeting_response */
13237 ", ?" /* start_time */
13238 ", ?" /* end_time */
13239 ", ?" /* location */
13240 ", ?" /* global_object_id */
13242 ", ?" /* standard_name */
13243 ", ?" /* standard_time_start_date */
13244 ", ?" /* standard_biad */
13245 ", ?" /* daylight_name */
13246 ", ?" /* daylight_time_start_date */
13247 ", ?" /* daylight_bias */
13250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13251 if (rc != SQLITE_OK) {
13252 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
13253 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
13255 error = EMAIL_ERROR_DB_FAILURE;
13261 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13262 EM_DEBUG_LOG(">>>>> account_id[%d]", account_id);
13263 EM_DEBUG_LOG(">>>>> mailbox_name[%s]", mailbox_name);
13264 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13265 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13266 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13267 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13268 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13269 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13270 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13271 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13272 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13273 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13274 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13275 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13277 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
13278 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
13279 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
13280 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13282 temp_unix_time = timegm(&(meeting_req->start_time));
13283 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13284 temp_unix_time = timegm(&(meeting_req->end_time));
13285 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13287 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13288 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13290 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13291 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13292 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13293 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13294 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13296 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13297 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13298 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13299 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13302 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13303 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13304 ("sqlite3_step fail:%d", rc));
13305 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13306 ("sqlite3_step fail:%d", rc));
13311 if (hStmt != NULL) {
13312 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13314 rc = sqlite3_finalize(hStmt);
13315 if (rc != SQLITE_OK) {
13316 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13317 error = EMAIL_ERROR_DB_FAILURE;
13321 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13324 if (err_code != NULL)
13327 EM_DEBUG_FUNC_END("ret [%d]", ret);
13331 INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
13333 EM_DEBUG_FUNC_BEGIN();
13338 int error = EMAIL_ERROR_NONE;
13339 email_meeting_request_t *p_temp_meeting_req = NULL;
13341 time_t temp_unix_time;
13343 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
13345 DB_STMT hStmt = NULL;
13346 char sql_query_string[QUERY_SIZE] = {0, };
13348 sqlite3 *local_db_handle = emstorage_get_db_connection();
13349 EMSTORAGE_START_READ_TRANSACTION(transaction);
13351 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13352 "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 "
13353 " FROM mail_meeting_tbl "
13354 " WHERE mail_id = %d", mail_id);
13355 EM_DEBUG_LOG("sql : %s ", sql_query_string);
13358 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13360 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13361 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13364 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13365 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13366 ("sqlite3_step fail:%d", rc));
13368 if (rc == SQLITE_DONE) {
13369 EM_DEBUG_EXCEPTION(" no Meeting request found...");
13372 error= EMAIL_ERROR_DATA_NOT_FOUND;
13376 if (!(p_temp_meeting_req = (email_meeting_request_t*)malloc(sizeof(email_meeting_request_t)))) {
13377 EM_DEBUG_EXCEPTION(" malloc failed...");
13378 error = EMAIL_ERROR_OUT_OF_MEMORY;
13382 memset(p_temp_meeting_req, 0x00, sizeof(email_meeting_request_t));
13386 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->mail_id), col_index++);
13387 _get_stmt_field_data_int(hStmt, (int *)&(p_temp_meeting_req->meeting_response), col_index++);
13388 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13389 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->start_time));
13390 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13391 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->end_time));
13392 _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->location, 1, col_index++);
13393 _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->global_object_id, 1, col_index++);
13394 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.offset_from_GMT), col_index++);
13396 _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++);
13397 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13398 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.standard_time_start_date));
13399 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.standard_bias), col_index++);
13401 _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++);
13402 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13403 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.daylight_time_start_date));
13404 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.daylight_bias), col_index++);
13407 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", p_temp_meeting_req->mail_id);
13408 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", p_temp_meeting_req->meeting_response);
13409 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(p_temp_meeting_req->start_time)));
13410 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(p_temp_meeting_req->end_time)));
13411 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", p_temp_meeting_req->location);
13412 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", p_temp_meeting_req->global_object_id);
13413 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", p_temp_meeting_req->time_zone.offset_from_GMT);
13414 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", p_temp_meeting_req->time_zone.standard_name);
13415 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.standard_time_start_date)));
13416 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", p_temp_meeting_req->time_zone.standard_bias);
13417 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", p_temp_meeting_req->time_zone.daylight_name);
13418 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.daylight_time_start_date)));
13419 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", p_temp_meeting_req->time_zone.daylight_bias);
13425 *meeting_req = p_temp_meeting_req;
13427 EM_SAFE_FREE(p_temp_meeting_req);
13430 if (hStmt != NULL) {
13431 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13433 rc = sqlite3_finalize(hStmt);
13435 if (rc != SQLITE_OK) {
13436 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
13437 error = EMAIL_ERROR_DB_FAILURE;
13441 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13444 if (err_code != NULL)
13447 EM_DEBUG_FUNC_END("ret [%d]", ret);
13452 INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
13454 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
13457 int error = EMAIL_ERROR_NONE;
13459 DB_STMT hStmt = NULL;
13460 char sql_query_string[QUERY_SIZE] = {0, };
13461 time_t temp_unix_time = 0;
13463 if (!meeting_req) {
13464 EM_DEBUG_EXCEPTION("Invalid Parameter!");
13465 error = EMAIL_ERROR_INVALID_PARAM;
13469 sqlite3 *local_db_handle = emstorage_get_db_connection();
13470 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13472 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13473 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13474 "UPDATE mail_meeting_tbl "
13476 /* " account_id = ?, " // not update here, this can be changed when move or copy */
13477 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
13478 " meeting_response = ?, "
13479 " start_time = ?, "
13482 " global_object_id = ?, "
13484 " standard_name = ?, "
13485 " standard_time_start_date = ?, "
13486 " standard_bias = ?, "
13487 " daylight_name = ?, "
13488 " daylight_time_start_date = ?, "
13489 " daylight_bias = ? "
13490 "WHERE mail_id = %d",
13491 meeting_req->mail_id);
13493 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
13495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13496 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13497 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13499 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13500 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13501 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13502 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13503 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13504 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13505 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13506 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13507 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13508 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13509 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13510 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13511 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13515 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13516 temp_unix_time = timegm(&(meeting_req->start_time));
13517 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13518 temp_unix_time = timegm(&(meeting_req->end_time));
13519 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13520 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13521 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13522 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13523 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13524 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13525 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13526 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13527 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13528 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13529 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13530 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13534 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13535 ("sqlite3_step fail:%d", rc));
13540 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13542 if (hStmt != NULL) {
13543 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13545 rc = sqlite3_finalize(hStmt);
13546 if (rc != SQLITE_OK) {
13547 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
13549 error = EMAIL_ERROR_DB_FAILURE;
13555 if (err_code != NULL)
13558 EM_DEBUG_FUNC_END("ret [%d]", ret);
13562 INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
13564 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);
13566 if (account_id < ALL_ACCOUNT || mail_id < 0) {
13567 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
13569 if (err_code != NULL)
13570 *err_code = EMAIL_ERROR_INVALID_PARAM;
13576 int error = EMAIL_ERROR_NONE;
13578 char sql_query_string[QUERY_SIZE] = {0, };
13580 sqlite3 *local_db_handle = emstorage_get_db_connection();
13581 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13583 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
13585 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
13586 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);
13590 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);
13593 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
13594 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);
13597 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13598 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13599 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13604 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13610 EM_DEBUG_FUNC_END("ret [%d]", ret);
13615 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
13617 EM_DEBUG_FUNC_BEGIN();
13619 if (!meeting_req ) {
13620 EM_DEBUG_EXCEPTION("NULL PARAM");
13624 EM_SAFE_FREE(meeting_req->location);
13625 EM_SAFE_FREE(meeting_req->global_object_id);
13627 EM_DEBUG_FUNC_END();
13631 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)
13633 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);
13634 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
13635 char sql_query_string[QUERY_SIZE] = {0, };
13636 char **result = NULL;
13637 int rc = -1, ret = false;
13638 int error = EMAIL_ERROR_NONE;
13639 int counter = 0, col_index = 0;
13640 int result_mail_id_count = 0;
13641 int *result_mail_id_list = NULL;
13643 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
13644 EM_DEBUG_EXCEPTION("Invalid Parameter");
13645 error = EMAIL_ERROR_INVALID_PARAM;
13649 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);
13651 EM_DEBUG_LOG("query[%s].", sql_query_string);
13653 sqlite3 *local_db_handle = emstorage_get_db_connection();
13654 EMSTORAGE_START_READ_TRANSACTION(transaction);
13656 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
13657 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13658 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13660 if (!result_mail_id_count) {
13661 EM_DEBUG_LOG("No mail found...");
13663 error= EMAIL_ERROR_MAIL_NOT_FOUND;
13667 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
13669 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
13670 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
13671 error = EMAIL_ERROR_OUT_OF_MEMORY;
13672 sqlite3_free_table(result);
13676 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
13680 for (counter = 0; counter < result_mail_id_count; counter++)
13681 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
13686 EM_DEBUG_LOG("finish off [%d]", ret);
13689 sqlite3_free_table(result);
13692 *mail_id_list = result_mail_id_list;
13693 *mail_id_count = result_mail_id_count;
13696 EM_SAFE_FREE(result_mail_id_list);
13698 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13701 if (err_code != NULL)
13704 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
13705 EM_DEBUG_FUNC_END("ret [%d]", ret);
13710 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
13712 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
13714 int rc = -1, ret = false;
13715 int err = EMAIL_ERROR_NONE;
13716 char sql_query_string[QUERY_SIZE] = {0, };
13718 int result_count = 0;
13720 if (mail_id == 0 || thread_id == NULL) {
13721 EM_DEBUG_EXCEPTION("Invalid Parameter");
13722 if (err_code != NULL)
13723 *err_code = EMAIL_ERROR_INVALID_PARAM;
13727 sqlite3 *local_db_handle = emstorage_get_db_connection();
13729 memset(sql_query_string, 0, QUERY_SIZE);
13730 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
13732 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13733 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13734 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13735 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13737 if (!result_count) {
13738 EM_DEBUG_EXCEPTION("No mail found...");
13740 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13741 /* sqlite3_free_table(result); */
13745 _get_table_field_data_int(result, thread_id, 1);
13747 sqlite3_free_table(result);
13755 if (err_code != NULL)
13758 EM_DEBUG_FUNC_END("ret [%d]", ret);
13763 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)
13765 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);
13767 int rc = -1, ret = false;
13768 int err = EMAIL_ERROR_NONE;
13769 char sql_query_string[QUERY_SIZE] = {0, };
13770 char **result = NULL;
13771 int result_count = 0;
13773 if (thread_id == 0) {
13774 EM_DEBUG_EXCEPTION("Invalid Parameter");
13775 if (err_code != NULL)
13776 *err_code = EMAIL_ERROR_INVALID_PARAM;
13780 sqlite3 *local_db_handle = emstorage_get_db_connection();
13782 if (thread_item_count == 0 || latest_mail_id == 0) {
13783 memset(sql_query_string, 0, QUERY_SIZE);
13784 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);
13786 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13787 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13788 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13789 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13790 EM_DEBUG_LOG("result_count[%d]", result_count);
13791 if (result_count == 0) {
13792 EM_DEBUG_EXCEPTION("No mail found...");
13794 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13795 sqlite3_free_table(result);
13799 _get_table_field_data_int(result, &latest_mail_id, 2);
13800 _get_table_field_data_int(result, &thread_item_count, 3);
13802 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
13803 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
13805 sqlite3_free_table(result);
13808 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13810 /* if (thread_item_count > 1) */
13812 memset(sql_query_string, 0, QUERY_SIZE);
13813 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);
13814 EM_DEBUG_LOG("query[%s]", sql_query_string);
13816 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13817 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13818 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13821 memset(sql_query_string, 0, QUERY_SIZE);
13822 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);
13823 EM_DEBUG_LOG("query[%s]", sql_query_string);
13825 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13826 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13827 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13833 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13837 if (err_code != NULL)
13840 EM_DEBUG_FUNC_END("ret [%d]", ret);
13845 INTERNAL_FUNC void emstorage_flush_db_cache()
13847 sqlite3_release_memory(-1);
13850 #ifdef __FEATURE_LOCAL_ACTIVITY__
13852 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
13855 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13857 EM_DEBUG_FUNC_BEGIN();
13859 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13861 int rc = -1, ret = false;
13862 int error = EMAIL_ERROR_NONE;
13863 DB_STMT hStmt = NULL;
13864 char sql_query_string[8192] = { 0x00, };
13867 if (!local_activity) {
13868 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13869 if (err_code != NULL)
13870 *err_code = EMAIL_ERROR_INVALID_PARAM;
13874 sqlite3 *local_db_handle = emstorage_get_db_connection();
13875 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
13876 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13878 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
13880 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
13881 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
13882 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
13883 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
13884 EM_DEBUG_LOG(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
13885 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
13886 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
13888 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13891 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13892 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13893 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13895 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13900 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13901 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13902 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13903 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13904 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
13905 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13906 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13909 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13911 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13912 ("sqlite3_step fail:%d", rc));
13913 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13914 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13920 if (hStmt != NULL) {
13921 rc = sqlite3_finalize(hStmt);
13922 if (rc != SQLITE_OK) {
13923 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13925 error = EMAIL_ERROR_DB_FAILURE;
13929 EM_DEBUG_LOG(" >>>>>>>>>> hStmt is NULL!!!");
13932 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13936 if (err_code != NULL)
13939 EM_DEBUG_FUNC_END("ret [%d]", ret);
13944 * emstorage_get_activity - Get the Local activity Information
13948 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)
13950 EM_DEBUG_FUNC_BEGIN();
13952 int i = 0, count = 0, rc = -1, ret = false;
13953 int error = EMAIL_ERROR_NONE;
13954 emstorage_activity_tbl_t *p_activity_tbl = NULL;
13955 char sql_query_string[1024] = {0x00, };
13956 char **result = NULL;
13959 EM_IF_NULL_RETURN_VALUE(activity_list, false);
13960 EM_IF_NULL_RETURN_VALUE(select_num, false);
13963 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
13964 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
13965 if (err_code != NULL)
13966 *err_code = EMAIL_ERROR_INVALID_PARAM;
13970 EMSTORAGE_START_READ_TRANSACTION(transaction);
13972 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13974 if (activityid == ALL_ACTIVITIES) {
13975 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
13978 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);
13981 EM_DEBUG_LOG("Query [%s]", sql_query_string);
13985 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13986 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13987 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13993 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
13994 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13995 error = EMAIL_ERROR_OUT_OF_MEMORY;
14000 for (i = 0; i < count; i++) {
14001 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14002 if (result[col_index])
14003 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
14005 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14006 if (result[col_index])
14007 p_activity_tbl[i].account_id = atoi(result[col_index++]);
14009 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14010 if (result[col_index])
14011 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
14013 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14014 if (result[col_index])
14015 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
14018 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14019 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14020 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
14024 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14025 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14026 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
14030 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14031 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14032 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
14039 sqlite3_free_table(result);
14047 *activity_list = p_activity_tbl;
14048 *select_num = count;
14049 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
14051 else if (p_activity_tbl != NULL) {
14052 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
14055 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14056 if (err_code != NULL)
14059 EM_DEBUG_FUNC_END("ret [%d]", ret);
14064 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
14067 EM_DEBUG_FUNC_BEGIN();
14070 int err = EMAIL_ERROR_NONE;
14073 char **result = NULL;
14075 if (NULL == activity_id)
14077 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
14079 err = EMAIL_ERROR_INVALID_PARAM;
14083 /* increase unique id */
14085 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
14087 /* 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); */
14088 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14089 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14091 if (NULL==result[1])
14094 rc = atoi(result[1])+1;
14099 sqlite3_free_table(result);
14105 if (NULL != err_code) {
14109 EM_DEBUG_FUNC_END("ret [%d]", ret);
14114 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)
14117 EM_DEBUG_FUNC_BEGIN();
14119 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);
14121 if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0) {
14122 if (err_code != NULL)
14123 *err_code = EMAIL_ERROR_INVALID_PARAM;
14128 int error = EMAIL_ERROR_NONE;
14129 int i = 0, rc = -1, count = 0;
14130 char sql_query_string[1024] = {0x00, };
14131 int *activity_ids = NULL;
14133 char **result = NULL;
14135 EMSTORAGE_START_READ_TRANSACTION(transaction);
14137 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14139 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);
14141 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
14144 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
14145 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14146 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14150 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
14152 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
14153 EM_DEBUG_EXCEPTION(" em_malloc failed...");
14154 error = EMAIL_ERROR_OUT_OF_MEMORY;
14158 for (i = 0; i < count; i++) {
14159 activity_ids[i] = atoi(result[col_index]);
14161 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
14170 *activity_id_count = count;
14171 *activity_id_list = activity_ids;
14174 else if (activity_ids != NULL) /* Prevent defect - 216566 */
14175 EM_SAFE_FREE(activity_ids);
14178 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14179 if (err_code != NULL) {
14183 EM_DEBUG_FUNC_END("ret [%d]", ret);
14187 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
14189 EM_DEBUG_FUNC_BEGIN();
14191 int error = EMAIL_ERROR_NONE;
14194 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
14196 if (NULL == activity_id_list) {
14197 error = EMAIL_ERROR_INVALID_PARAM;
14201 free(activity_id_list);
14202 activity_id_list = NULL;
14210 if (NULL != error_code) {
14211 *error_code = error;
14214 EM_DEBUG_FUNC_END("ret [%d]", ret);
14219 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
14220 * or based on server mail id
14223 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
14225 EM_DEBUG_FUNC_BEGIN();
14228 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
14230 if (!local_activity) {
14231 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
14232 if (err_code != NULL)
14233 *err_code = EMAIL_ERROR_INVALID_PARAM;
14237 int rc = -1, ret = false; /* Prevent_FIX */
14238 int err = EMAIL_ERROR_NONE;
14240 int query_where = 0;
14241 char sql_query_string[8192] = { 0x00, };
14242 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14244 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14246 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
14248 EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
14250 if (local_activity->account_id) {
14251 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14252 " WHERE account_id = %d ", local_activity->account_id);
14257 if (local_activity->server_mailid) {
14258 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14259 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
14265 if (local_activity->mail_id) {
14266 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
14268 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14269 " %s %s mail_id = %d ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
14276 if (local_activity->activity_type > 0) {
14277 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14278 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
14281 if (local_activity->activity_id > 0) {
14282 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14283 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
14287 EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
14289 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
14290 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14291 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14293 rc = sqlite3_changes(local_db_handle);
14295 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
14296 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
14302 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14303 if (err_code != NULL)
14305 EM_DEBUG_FUNC_END("ret [%d]", ret);
14310 * emstorage_free_local_activity - Free the Local Activity data
14312 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
14314 EM_DEBUG_FUNC_BEGIN();
14316 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
14319 int error = EMAIL_ERROR_INVALID_PARAM;
14322 if (!local_activity_list || !*local_activity_list) {
14323 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
14325 error = EMAIL_ERROR_INVALID_PARAM;
14329 emstorage_activity_tbl_t* p = *local_activity_list;
14332 for (; i < count; i++) {
14333 EM_SAFE_FREE(p[i].dest_mbox);
14334 EM_SAFE_FREE(p[i].src_mbox);
14335 EM_SAFE_FREE(p[i].server_mailid);
14338 free(p); *local_activity_list = NULL;
14345 if (err_code != NULL)
14348 EM_DEBUG_FUNC_END("ret [%d]", ret);
14352 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
14355 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
14357 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
14359 int ret = EMAIL_ERROR_NONE;
14360 char key_value_string[QUERY_SIZE] = { 0, };
14361 char *temp_key_value_1 = NULL;
14362 char *temp_key_value_2 = NULL;
14364 if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
14365 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14366 return EMAIL_ERROR_INVALID_PARAM;
14369 switch(input_list_filter_rule->target_attribute) {
14370 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID :
14371 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID :
14372 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID :
14373 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE :
14374 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS :
14375 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID :
14376 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS :
14377 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14378 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14379 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE :
14380 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD :
14381 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD :
14382 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD :
14383 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD :
14384 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD :
14385 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD :
14386 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD :
14387 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS :
14388 case EMAIL_MAIL_ATTRIBUTE_PRIORITY :
14389 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS :
14390 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS :
14391 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS :
14392 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT :
14393 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT :
14394 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID :
14395 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT :
14396 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS :
14397 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS :
14398 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE :
14399 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE :
14400 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
14403 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14404 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14405 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14406 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14407 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14408 case EMAIL_MAIL_ATTRIBUTE_FROM :
14409 case EMAIL_MAIL_ATTRIBUTE_TO :
14410 case EMAIL_MAIL_ATTRIBUTE_CC :
14411 case EMAIL_MAIL_ATTRIBUTE_BCC :
14412 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14413 if(input_list_filter_rule->key_value.string_type_value == NULL) {
14414 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
14415 ret = EMAIL_ERROR_INVALID_PARAM;
14419 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
14421 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
14422 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
14424 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
14425 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
14427 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
14430 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME :
14431 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
14435 ret = EMAIL_ERROR_INVALID_PARAM;
14436 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14440 if(ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
14441 *output_key_value_string = strdup(key_value_string);
14446 EM_SAFE_FREE(temp_key_value_1);
14447 EM_SAFE_FREE(temp_key_value_2);
14449 EM_DEBUG_FUNC_END("ret [%d]", ret);
14453 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14454 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
14456 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
14458 if (index <= 0 || !cert_password_file_name) {
14459 EM_DEBUG_EXCEPTION("Invalid parameter");
14460 return EMAIL_ERROR_INVALID_PARAM;
14463 sprintf(cert_password_file_name, ".email_cert_%d", index);
14465 EM_DEBUG_FUNC_END();
14466 return EMAIL_ERROR_NONE;
14470 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
14472 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14474 int ret = EMAIL_ERROR_NONE;
14476 int length_field_name = 0;
14477 int length_value = 0;
14478 char result_rule_string[QUERY_SIZE] = { 0 , };
14479 char *mod_field_name_string = NULL;
14480 char *mod_value_string = NULL;
14481 char *temp_field_name_string = NULL;
14482 char *temp_key_value_string = NULL;
14484 if(input_list_filter_rule == NULL || output_string == NULL) {
14485 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14486 return EMAIL_ERROR_INVALID_PARAM;
14489 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
14491 if(temp_field_name_string == NULL) {
14492 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14493 return EMAIL_ERROR_INVALID_PARAM;
14496 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) {
14497 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
14498 return EMAIL_ERROR_INVALID_PARAM;
14501 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
14502 length_value = EM_SAFE_STRLEN(temp_key_value_string);
14504 switch(input_list_filter_rule->target_attribute) {
14505 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14506 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14507 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14508 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14509 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14510 case EMAIL_MAIL_ATTRIBUTE_FROM :
14511 case EMAIL_MAIL_ATTRIBUTE_TO :
14512 case EMAIL_MAIL_ATTRIBUTE_CC :
14513 case EMAIL_MAIL_ATTRIBUTE_BCC :
14514 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14522 if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
14523 length_field_name += strlen("UPPER() ");
14524 length_value += strlen("UPPER() ");
14525 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
14526 mod_value_string = em_malloc(sizeof(char) * length_value);
14527 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
14528 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
14529 EM_SAFE_FREE(temp_key_value_string);
14532 mod_field_name_string = strdup(temp_field_name_string);
14533 mod_value_string = temp_key_value_string;
14536 switch (input_list_filter_rule->rule_type) {
14537 case EMAIL_LIST_FILTER_RULE_EQUAL :
14538 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14540 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
14541 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
14543 case EMAIL_LIST_FILTER_RULE_LESS_THAN :
14544 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
14546 case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
14547 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
14549 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
14550 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
14552 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
14553 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
14555 case EMAIL_LIST_FILTER_RULE_INCLUDE :
14556 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
14558 case EMAIL_LIST_FILTER_RULE_IN :
14559 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
14561 case EMAIL_LIST_FILTER_RULE_NOT_IN :
14562 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
14565 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14566 ret = EMAIL_ERROR_INVALID_PARAM;
14570 *output_string = strdup(result_rule_string);
14573 EM_SAFE_FREE(mod_field_name_string);
14574 EM_SAFE_FREE(mod_value_string);
14576 EM_DEBUG_FUNC_END("ret [%d]", ret);
14580 static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
14581 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
14583 char result_rule_string[QUERY_SIZE] = { 0 , };
14584 int ret = EMAIL_ERROR_NONE;
14586 if(input_sorting_rule->force_boolean_check) {
14587 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14590 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14592 switch (input_sorting_rule->sort_order) {
14593 case EMAIL_SORT_ORDER_ASCEND :
14594 EM_SAFE_STRCAT(result_rule_string, " ASC ");
14597 case EMAIL_SORT_ORDER_DESCEND :
14598 EM_SAFE_STRCAT(result_rule_string, " DESC ");
14602 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
14603 ret = EMAIL_ERROR_INVALID_PARAM;
14607 *output_string = strdup(result_rule_string);
14610 EM_DEBUG_FUNC_END("ret [%d]", ret);
14614 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)
14616 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);
14617 int ret = EMAIL_ERROR_NONE;
14619 int string_offset = 0;
14620 char conditional_clause_string[QUERY_SIZE] = {0, };
14621 char *result_string_for_a_item = NULL;
14623 if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
14624 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14625 return EMAIL_ERROR_INVALID_PARAM;
14628 if(input_filter_count > 0) {
14629 strcpy(conditional_clause_string, " WHERE ");
14631 for ( i = 0; i < input_filter_count; i++) {
14632 switch (input_filter_list[i].list_filter_item_type) {
14633 case EMAIL_LIST_FILTER_ITEM_RULE :
14634 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
14635 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
14638 case EMAIL_LIST_FILTER_ITEM_OPERATOR :
14639 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
14640 switch(input_filter_list[i].list_filter_item.operator_type) {
14641 case EMAIL_LIST_FILTER_OPERATOR_AND :
14642 result_string_for_a_item = strdup("AND ");
14644 case EMAIL_LIST_FILTER_OPERATOR_OR :
14645 result_string_for_a_item = strdup("OR ");
14647 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
14648 result_string_for_a_item = strdup(" (");
14650 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
14651 result_string_for_a_item = strdup(") ");
14657 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
14658 ret = EMAIL_ERROR_INVALID_PARAM;
14662 if(result_string_for_a_item == NULL) {
14663 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
14664 ret = EMAIL_ERROR_INVALID_PARAM;
14668 if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= QUERY_SIZE) { /* prevent 34364 */
14669 EM_DEBUG_EXCEPTION("Query is too long");
14670 ret = EMAIL_ERROR_DATA_TOO_LONG;
14673 strcat(conditional_clause_string, result_string_for_a_item);
14674 EM_SAFE_FREE(result_string_for_a_item);
14678 if(input_sorting_rule_count > 0) {
14679 strcat(conditional_clause_string, "ORDER BY ");
14681 for ( i = 0; i < input_sorting_rule_count; i++) {
14682 if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
14683 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
14687 strcat(conditional_clause_string, ", ");
14688 strcat(conditional_clause_string, result_string_for_a_item);
14689 EM_SAFE_FREE(result_string_for_a_item);
14693 if (input_start_index != -1 && input_limit_count != -1) {
14694 string_offset = strlen(conditional_clause_string);
14695 SNPRINTF_OFFSET(conditional_clause_string, string_offset, QUERY_SIZE, " LIMIT %d, %d", input_start_index, input_limit_count);
14698 *output_conditional_clause = strdup(conditional_clause_string);
14701 EM_SAFE_FREE(result_string_for_a_item);
14703 EM_DEBUG_FUNC_END("ret [%d]", ret);
14707 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
14709 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
14710 int err = EMAIL_ERROR_NONE;
14712 email_list_filter_t *temp_filter_list = NULL;
14714 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
14716 for ( i = 0; i < input_filter_count; i++) {
14717 temp_filter_list = (*input_filter_list) + i;
14718 if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
14719 switch(temp_filter_list->list_filter_item.rule.target_attribute) {
14720 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14721 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14722 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14723 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14724 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14725 case EMAIL_MAIL_ATTRIBUTE_FROM :
14726 case EMAIL_MAIL_ATTRIBUTE_TO :
14727 case EMAIL_MAIL_ATTRIBUTE_CC :
14728 case EMAIL_MAIL_ATTRIBUTE_BCC :
14729 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14730 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14731 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14732 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
14740 free(*input_filter_list);
14741 *input_filter_list = NULL;
14743 EM_DEBUG_FUNC_END("err [%d]", err);
14747 INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14749 EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14751 if (!certificate) {
14752 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14753 if (err_code != NULL)
14754 *err_code = EMAIL_ERROR_INVALID_PARAM;
14759 int rc = -1, ret = false;
14760 int error = EMAIL_ERROR_NONE;
14761 DB_STMT hStmt = NULL;
14762 char sql_query_string[QUERY_SIZE] = {0, };
14763 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14764 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14766 sqlite3 *local_db_handle = emstorage_get_db_connection();
14768 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14770 char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
14771 char **result = NULL;
14773 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14774 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14775 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14776 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14778 if (NULL==result[1]) rc = 1;
14779 else rc = atoi(result[1])+1;
14780 sqlite3_free_table(result);
14782 certificate->certificate_id = rc;
14783 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14784 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14785 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14789 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14790 "INSERT INTO mail_certificate_tbl VALUES "
14792 " ? " /* Index of certificate */
14793 " , ? " /* Select the account */
14794 " , ? " /* Year of issue */
14795 " , ? " /* Month of issue */
14796 " , ? " /* Day of issue */
14797 " , ? " /* Year of expiration */
14798 " , ? " /* Month of expiration */
14799 " , ? " /* Day of expiration */
14800 " , ? " /* Organization of issue */
14801 " , ? " /* Email address */
14802 " , ? " /* Subject of certificate */
14803 " , ? " /* Name of saved certificate */
14807 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
14808 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14809 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14810 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14812 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
14813 _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
14814 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
14815 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
14816 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
14817 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
14818 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
14819 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
14820 _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);
14821 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
14822 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
14823 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
14824 /* rc = sqlite3_step(hStmt); */
14825 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14827 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14828 ("sqlite3_step fail:%d", rc));
14829 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14830 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
14831 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14832 if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14833 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
14834 error = EMAIL_ERROR_SYSTEM_FAILURE;
14840 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14842 if (!emcore_notify_storage_event(NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
14843 EM_DEBUG_EXCEPTION("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
14847 if (hStmt != NULL) {
14848 rc = sqlite3_finalize(hStmt);
14849 if (rc != SQLITE_OK) {
14850 EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
14851 error = EMAIL_ERROR_DB_FAILURE;
14855 EM_DEBUG_LOG("hStmt is NULL!!!");
14857 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14860 if (err_code != NULL)
14863 EM_DEBUG_FUNC_END("ret [%d]", ret);
14867 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
14869 EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
14872 int error = EMAIL_ERROR_NONE;
14875 if (!certificate_list || !*certificate_list) {
14876 EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
14877 error = EMAIL_ERROR_INVALID_PARAM;
14881 emstorage_certificate_tbl_t *p = *certificate_list;
14884 for (; i < count; i++) {
14885 EM_SAFE_FREE(p[i].issue_organization_name);
14886 EM_SAFE_FREE(p[i].email_address);
14887 EM_SAFE_FREE(p[i].subject_str);
14888 EM_SAFE_FREE(p[i].filepath);
14889 EM_SAFE_FREE(p[i].password);
14893 *certificate_list = NULL;
14899 if (err_code != NULL)
14902 EM_DEBUG_FUNC_END("ret [%d]", ret);
14906 INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
14908 EM_DEBUG_FUNC_BEGIN();
14910 int i = 0, count = 0, rc = -1, ret = false;
14911 int error = EMAIL_ERROR_NONE;
14912 emstorage_certificate_tbl_t *p_data_tbl = NULL;
14914 DB_STMT hStmt = NULL;
14916 if (!select_num || !certificate_list) {
14917 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
14918 if (err_code != NULL)
14919 *err_code = EMAIL_ERROR_INVALID_PARAM;
14923 sqlite3 *local_db_handle = emstorage_get_db_connection();
14924 EMSTORAGE_START_READ_TRANSACTION(transaction);
14926 char sql_query_string[QUERY_SIZE] = {0, };
14927 char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
14930 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14931 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14932 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14933 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14935 count = atoi(result[1]);
14936 sqlite3_free_table(result);
14939 EM_DEBUG_EXCEPTION("no account found...");
14940 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14944 EM_DEBUG_LOG("count = %d", rc);
14945 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
14947 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
14948 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14950 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
14951 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14952 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14954 /* rc = sqlite3_step(hStmt); */
14955 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14956 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14957 ("sqlite3_step fail:%d", rc));
14958 if (rc == SQLITE_DONE) {
14959 EM_DEBUG_EXCEPTION("no account found...");
14961 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14967 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count))) {
14968 EM_DEBUG_EXCEPTION("malloc failed...");
14969 error = EMAIL_ERROR_OUT_OF_MEMORY;
14972 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
14973 for (i = 0; i < count; i++) {
14974 /* get recordset */
14975 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14976 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14977 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14978 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14979 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14980 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14981 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14982 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14983 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14984 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14985 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14986 if (with_password == true) {
14987 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14988 /* get password from the secure storage */
14989 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14991 EM_SAFE_FREE(p_data_tbl[i].password);
14993 /* get password file name */
14994 if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14995 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14999 /* read password from secure storage */
15000 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
15001 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
15004 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl[i].password);
15008 /* rc = sqlite3_step(hStmt); */
15009 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15010 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
15011 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15012 ("sqlite3_step fail:%d", rc));
15019 *certificate_list = p_data_tbl;
15020 *select_num = count;
15021 EM_DEBUG_LOG("COUNT : %d", count);
15023 else if (p_data_tbl != NULL)
15024 emstorage_free_certificate(&p_data_tbl, count, NULL);
15025 if (hStmt != NULL) {
15026 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15028 rc = sqlite3_finalize(hStmt);
15030 if (rc != SQLITE_OK) {
15031 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15032 error = EMAIL_ERROR_DB_FAILURE;
15036 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15039 if (err_code != NULL)
15042 EM_DEBUG_FUNC_END("ret [%d]", ret);
15046 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)
15048 EM_DEBUG_FUNC_BEGIN("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
15050 if (!certificate) {
15051 EM_DEBUG_EXCEPTION("email_address[%s], certificate[%p]", email_address, certificate);
15052 if (err_code != NULL)
15053 *err_code = EMAIL_ERROR_INVALID_PARAM;
15058 int error = EMAIL_ERROR_NONE;
15059 emstorage_certificate_tbl_t *p_data_tbl = NULL;
15060 DB_STMT hStmt = NULL;
15061 char sql_query_string[QUERY_SIZE] = {0, };
15064 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15065 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15067 sqlite3 *local_db_handle = emstorage_get_db_connection();
15068 EMSTORAGE_START_READ_TRANSACTION(transaction);
15070 /* Make query string */
15071 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
15072 sql_len = EM_SAFE_STRLEN(sql_query_string);
15074 /* dummy value, FROM WHERE clause */
15075 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
15078 EM_DEBUG_LOG("query = [%s]", sql_query_string);
15080 /* execute a sql and count rows */
15081 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15082 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15083 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15085 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15086 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15087 ("sqlite3_step fail:%d", rc));
15089 if (rc == SQLITE_DONE) {
15090 EM_DEBUG_EXCEPTION("no matched certificate found...");
15091 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15095 /* Assign query result to structure */
15096 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
15097 EM_DEBUG_EXCEPTION("malloc failed...");
15098 error = EMAIL_ERROR_OUT_OF_MEMORY;
15102 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15103 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15104 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15105 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15106 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15107 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15108 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15109 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15110 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15111 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15112 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15113 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15115 if (with_password) {
15116 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15117 /* get password file name */
15118 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15119 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15123 /* read password from secure storage */
15124 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15125 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
15128 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
15135 *certificate = p_data_tbl;
15137 if (hStmt != NULL) {
15138 rc = sqlite3_finalize(hStmt);
15139 if (rc != SQLITE_OK) {
15140 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15141 error = EMAIL_ERROR_DB_FAILURE;
15145 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15148 if (err_code != NULL)
15151 EM_DEBUG_FUNC_END("ret [%d]", ret);
15155 INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
15157 EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
15159 if (!certificate) {
15160 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
15161 if (err_code != NULL)
15162 *err_code = EMAIL_ERROR_INVALID_PARAM;
15167 int error = EMAIL_ERROR_NONE;
15168 emstorage_certificate_tbl_t *p_data_tbl = NULL;
15169 DB_STMT hStmt = NULL;
15170 char sql_query_string[QUERY_SIZE] = {0, };
15173 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15174 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15176 sqlite3 *local_db_handle = emstorage_get_db_connection();
15177 EMSTORAGE_START_READ_TRANSACTION(transaction);
15179 /* Make query string */
15180 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
15181 sql_len = EM_SAFE_STRLEN(sql_query_string);
15183 /* dummy value, FROM WHERE clause */
15184 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
15187 EM_DEBUG_LOG("query = [%s]", sql_query_string);
15189 /* execute a sql and count rows */
15190 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15191 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15192 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15194 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15195 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15196 ("sqlite3_step fail:%d", rc));
15198 if (rc == SQLITE_DONE) {
15199 EM_DEBUG_EXCEPTION("no matched certificate found...");
15200 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15204 /* Assign query result to structure */
15205 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
15206 EM_DEBUG_EXCEPTION("malloc failed...");
15207 error = EMAIL_ERROR_OUT_OF_MEMORY;
15211 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15213 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15214 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15215 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15216 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15217 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15218 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15219 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15220 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15221 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15222 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15223 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15225 if (with_password) {
15226 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15227 /* get password file name */
15228 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15229 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15233 /* read password from secure storage */
15234 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15235 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
15238 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
15245 *certificate = p_data_tbl;
15247 if (hStmt != NULL) {
15248 rc = sqlite3_finalize(hStmt);
15249 if (rc != SQLITE_OK) {
15250 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15251 error = EMAIL_ERROR_DB_FAILURE;
15255 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15258 if (err_code != NULL)
15261 EM_DEBUG_FUNC_END("ret [%d]", ret);
15265 INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
15267 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
15269 if (certificate_id < 1) {
15270 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15272 if (err_code != NULL)
15273 *err_code = EMAIL_ERROR_INVALID_PARAM;
15277 int rc = -1, ret = false;
15278 int error = EMAIL_ERROR_NONE;
15279 sqlite3 *local_db_handle = emstorage_get_db_connection();
15280 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15282 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
15283 DB_STMT hStmt = NULL;
15284 char sql_query_string[QUERY_SIZE] = {0, };
15285 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15286 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15287 /* get password file name */
15288 if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15289 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15293 /* delete from db */
15294 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15295 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
15297 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15298 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15299 ("sqlite3_exec fail:%d", rc));
15300 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15301 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15303 /* validate account existence */
15304 rc = sqlite3_changes(local_db_handle);
15306 EM_DEBUG_EXCEPTION(" no matched certificate found...");
15307 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15310 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15311 /* delete from secure storage */
15312 if (ssm_delete_file(cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15313 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
15314 error = EMAIL_ERROR_SYSTEM_FAILURE;
15322 if (hStmt != NULL) {
15323 rc = sqlite3_finalize(hStmt);
15324 if (rc != SQLITE_OK) {
15325 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15326 error = EMAIL_ERROR_DB_FAILURE;
15330 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15333 if (err_code != NULL)
15336 EM_DEBUG_FUNC_END("ret [%d]", ret);
15340 INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
15342 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
15344 if (certificate_id < 1) {
15345 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15347 if (err_code != NULL)
15348 *err_code = EMAIL_ERROR_INVALID_PARAM;
15352 int error = EMAIL_ERROR_NONE;
15353 int rc, ret = false;
15355 DB_STMT hStmt = NULL;
15356 char sql_query_string[QUERY_SIZE] = {0, };
15357 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15358 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15360 sqlite3 *local_db_handle = emstorage_get_db_connection();
15361 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15363 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15364 "UPDATE mail_certificate_tbl SET"
15366 ", issue_month = ?" /* Index of certificate */
15367 ", issue_day = ?" /* Select the account */
15368 ", expiration_year = ?" /* Year of issue */
15369 ", expiration_month = ?" /* Month of issue */
15370 ", expiration_day = ?" /* Day of issue */
15371 ", issue_organization_name = ?" /* Year of expiration */
15372 ", email_address = ?" /* Month of expiration */
15373 ", subject_str = ?" /* Day of expiration */
15374 ", filepath = ?" /* Organization of issue */
15376 " WHERE certificate_id = ?");
15378 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15379 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
15380 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15381 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15383 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
15384 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
15385 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
15386 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
15387 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
15388 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
15389 _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);
15390 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
15391 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
15392 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
15394 /* rc = sqlite3_step(hStmt); */
15395 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15396 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15397 ("sqlite3_step fail:%d", rc));
15398 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15399 ("sqlite3_step fail:%d", rc));
15401 /* validate account existence */
15402 rc = sqlite3_changes(local_db_handle);
15404 EM_DEBUG_EXCEPTION(" no matched account found...");
15406 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15410 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15411 /* get password file name */
15412 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15413 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15417 /* save passwords to the secure storage */
15418 if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15419 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
15420 error = EMAIL_ERROR_SYSTEM_FAILURE;
15424 if (!emcore_notify_storage_event(NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
15425 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
15431 if (hStmt != NULL) {
15432 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
15434 rc = sqlite3_finalize(hStmt);
15435 if (rc != SQLITE_OK) {
15436 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15437 error = EMAIL_ERROR_DB_FAILURE;
15441 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15445 if (err_code != NULL)
15448 EM_DEBUG_FUNC_END("ret [%d]", ret);
15452 /* Tasks --------------------------------------------------------------------------*/
15453 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)
15455 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);
15459 int err = EMAIL_ERROR_NONE;
15461 DB_STMT hStmt = NULL;
15462 char sql_query_string[QUERY_SIZE] = {0, };
15463 sqlite3 *local_db_handle = NULL;
15464 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
15465 char **result = NULL;
15467 if (input_task_parameter == NULL || output_task_id == NULL) {
15468 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15469 err = EMAIL_ERROR_INVALID_PARAM;
15473 local_db_handle = emstorage_get_db_connection();
15475 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, err);
15477 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
15478 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15479 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15481 if (NULL == result[1])
15484 task_id = atoi(result[1])+1;
15486 *output_task_id = task_id;
15488 sqlite3_free_table(result);
15491 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15492 "INSERT INTO mail_task_tbl VALUES "
15494 " ? " /* task_id */
15495 " , ? " /* task_type */
15496 " , ? " /* task_status */
15497 " , ? " /* task_priority */
15498 " , ? " /* task_parameter_length */
15499 " , ? " /* task_parameter */
15500 " , ? " /* date_time */
15503 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15504 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15505 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15507 EM_DEBUG_LOG(">>>> SQL STMT [%s] ", sql_query_string);
15510 _bind_stmt_field_data_int(hStmt, i++, task_id);
15511 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
15512 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
15513 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
15514 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
15515 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
15516 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
15518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15520 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15521 ("sqlite3_step fail:%d", rc));
15522 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15523 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15525 ret = (err == EMAIL_ERROR_NONE);
15526 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, err);
15530 if (hStmt != NULL) {
15531 rc = sqlite3_finalize(hStmt);
15532 if (rc != SQLITE_OK) {
15533 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
15534 err = EMAIL_ERROR_DB_FAILURE;
15538 EM_DEBUG_LOG("hStmt is NULL!!!");
15542 EM_DEBUG_FUNC_END("err [%d]", err);
15546 INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction)
15548 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
15549 int rc, ret = false;
15550 int err = EMAIL_ERROR_NONE;
15551 char sql_query_string[QUERY_SIZE] = {0, };
15552 sqlite3 *local_db_handle = NULL;
15555 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15556 err = EMAIL_ERROR_INVALID_PARAM;
15560 local_db_handle = emstorage_get_db_connection();
15562 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15564 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
15566 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15567 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15568 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15573 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15576 EM_DEBUG_FUNC_END("err [%d]", err);
15580 INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_type_t task_status, int transaction)
15582 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
15583 int rc, ret = false;
15584 int err = EMAIL_ERROR_NONE;
15585 char sql_query_string[QUERY_SIZE] = {0, };
15587 sqlite3 *local_db_handle = emstorage_get_db_connection();
15589 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15591 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15592 "UPDATE mail_task_tbl SET"
15593 " task_status = %d"
15594 " WHERE task_id = %d"
15597 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15598 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15599 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15604 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15608 EM_DEBUG_FUNC_END("err [%d]", err);
15612 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)
15614 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);
15615 int i = 0, count = 0, rc = -1;
15617 int field_index = 0;
15618 int err = EMAIL_ERROR_NONE;
15619 email_task_t *task_item_from_tbl = NULL;
15620 char sql_query_string[QUERY_SIZE] = {0, };
15621 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
15623 sqlite3 *local_db_handle = NULL;
15624 DB_STMT hStmt = NULL;
15626 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
15627 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
15629 local_db_handle = emstorage_get_db_connection();
15631 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
15632 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15634 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
15635 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15636 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15638 count = atoi(result[1]);
15639 sqlite3_free_table(result);
15641 EM_DEBUG_LOG("count = %d", rc);
15644 EM_DEBUG_EXCEPTION("no task found...");
15645 err = EMAIL_ERROR_TASK_NOT_FOUND;
15649 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);
15650 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15652 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15654 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
15655 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15656 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15658 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15659 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15660 ("sqlite3_step fail:%d", rc));
15662 if (rc == SQLITE_DONE) {
15663 EM_DEBUG_EXCEPTION("no task found...");
15664 err = EMAIL_ERROR_TASK_NOT_FOUND;
15669 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
15670 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
15671 err = EMAIL_ERROR_OUT_OF_MEMORY;
15675 for (i = 0; i < count; i++) {
15676 /* get recordset */
15679 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
15680 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
15681 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
15682 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
15683 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
15684 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
15686 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15687 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
15688 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15689 ("sqlite3_step fail:%d", rc));
15693 if (err == EMAIL_ERROR_NONE) {
15694 if (output_task_list)
15695 *output_task_list = task_item_from_tbl;
15696 *output_task_count = count;
15699 EM_SAFE_FREE(task_item_from_tbl);
15701 if (hStmt != NULL) {
15702 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15703 rc = sqlite3_finalize(hStmt);
15705 if (rc != SQLITE_OK) {
15706 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15707 err = EMAIL_ERROR_DB_FAILURE;
15713 EM_DEBUG_FUNC_END("err [%d]", err);
15716 /* Tasks --------------------------------------------------------------------------*/