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 %s", fields, input_conditional_clause, input_ordering_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 mailbox_id, char *server_mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
3057 EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", mailbox_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 (mailbox_id == 0)
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 mailbox_id = %d", server_mail_id, mailbox_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);
4594 int error = EMAIL_ERROR_NONE;
4595 char conditional_clause_string[QUERY_SIZE] = {0, };
4597 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
4598 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);
4599 if (err_code != NULL)
4600 *err_code = EMAIL_ERROR_INVALID_PARAM;
4604 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND UPPER(mailbox_name) LIKE UPPER('%s%%')", account_id, parent_mailbox_name);
4605 EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
4607 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4608 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4616 if (err_code != NULL)
4619 EM_DEBUG_FUNC_END("ret [%d]", ret);
4623 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)
4625 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);
4626 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
4627 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4629 if (err_code != NULL)
4630 *err_code = EMAIL_ERROR_INVALID_PARAM;
4636 int error = EMAIL_ERROR_NONE;
4637 char conditional_clause_string[QUERY_SIZE] = {0, };
4639 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
4640 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4642 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4643 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4651 if (err_code != NULL)
4654 EM_DEBUG_FUNC_END("ret [%d]", ret);
4658 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
4660 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
4662 int result_code = false;
4663 int error = EMAIL_ERROR_NONE;
4665 time_t current_time = 0;
4666 char sql_query_string[QUERY_SIZE] = {0, };
4667 sqlite3 *local_db_handle = NULL;
4669 if (!input_mailbox_id ) {
4670 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4671 return EMAIL_ERROR_INVALID_PARAM;
4674 time(¤t_time);
4676 local_db_handle = emstorage_get_db_connection();
4678 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
4680 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4681 "UPDATE mail_box_tbl SET"
4682 " last_sync_time = %d"
4683 " WHERE mailbox_id = %d"
4685 , input_mailbox_id);
4687 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4689 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4690 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4691 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4695 if(error == EMAIL_ERROR_NONE)
4698 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
4701 EM_DEBUG_FUNC_END("error [%d]", error);
4705 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)
4707 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);
4708 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
4710 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
4711 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
4713 if (err_code != NULL)
4714 *err_code = EMAIL_ERROR_INVALID_PARAM;
4719 int error = EMAIL_ERROR_NONE;
4720 int result_count = 0;
4721 char conditional_clause_string[QUERY_SIZE] = {0, };
4722 char *replaced_mailbox_name = NULL;
4724 if (strstr(mailbox_name, "'")) {
4725 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
4726 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
4728 replaced_mailbox_name = strdup(mailbox_name);
4731 if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
4732 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
4733 EM_DEBUG_EXCEPTION("malloc failed...");
4734 error = EMAIL_ERROR_OUT_OF_MEMORY;
4738 (*result_mailbox)->mailbox_id = 0;
4739 (*result_mailbox)->account_id = account_id;
4740 (*result_mailbox)->local_yn = 1;
4741 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
4742 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
4743 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
4744 (*result_mailbox)->deleted_flag = 0;
4745 (*result_mailbox)->modifiable_yn = 1;
4746 (*result_mailbox)->total_mail_count_on_server = 1;
4747 (*result_mailbox)->has_archived_mails = 0;
4748 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
4749 (*result_mailbox)->no_select = 0;
4754 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND mailbox_name = '%s'", account_id, replaced_mailbox_name);
4756 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);
4758 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4760 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4761 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4770 EM_SAFE_FREE(replaced_mailbox_name);
4772 if (err_code != NULL)
4775 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
4776 EM_DEBUG_FUNC_END("ret [%d]", ret);
4780 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)
4782 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);
4784 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !mailbox_name) {
4786 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4788 if (err_code != NULL)
4789 *err_code = EMAIL_ERROR_INVALID_PARAM;
4793 int rc, ret = false;
4794 int error = EMAIL_ERROR_NONE;
4795 emstorage_mailbox_tbl_t *p_data_tbl = NULL;
4796 emstorage_account_tbl_t *account = NULL;
4797 DB_STMT hStmt = NULL;
4798 char sql_query_string[QUERY_SIZE] = {0,};
4799 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 ";
4801 sqlite3 *local_db_handle = emstorage_get_db_connection();
4802 EMSTORAGE_START_READ_TRANSACTION(transaction);
4804 /* validate account */
4805 /* Check whether the account exists. */
4806 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
4807 EM_DEBUG_EXCEPTION(" emstorage_get_account_by_id failed - %d", error);
4812 emstorage_free_account(&account, 1, NULL);
4814 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);
4816 EM_DEBUG_LOG("query = [%s]", sql_query_string);
4819 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4820 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)));
4823 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4824 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
4826 if (rc == SQLITE_DONE) {
4827 EM_DEBUG_EXCEPTION(" no matched mailbox_name found...");
4828 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4833 if (!(p_data_tbl = (emstorage_mailbox_tbl_t*)malloc(sizeof(emstorage_mailbox_tbl_t)))) {
4834 EM_DEBUG_EXCEPTION(" malloc failed...");
4835 error = EMAIL_ERROR_OUT_OF_MEMORY;
4839 memset(p_data_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
4843 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
4844 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4845 _get_stmt_field_data_int(hStmt, &(p_data_tbl->local_yn), col_index++);
4846 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, col_index++);
4847 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->mailbox_type), col_index++);
4848 _get_stmt_field_data_string(hStmt, &(p_data_tbl->alias), 0, col_index++);
4849 _get_stmt_field_data_int(hStmt, &(p_data_tbl->deleted_flag), col_index++);
4850 _get_stmt_field_data_int(hStmt, &(p_data_tbl->modifiable_yn), col_index++);
4851 _get_stmt_field_data_int(hStmt, &(p_data_tbl->total_mail_count_on_server), col_index++);
4852 _get_stmt_field_data_int(hStmt, &(p_data_tbl->has_archived_mails), col_index++);
4853 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_slot_size), col_index++);
4854 _get_stmt_field_data_int(hStmt, &(p_data_tbl->no_select), col_index++);
4855 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->last_sync_time), col_index++);
4861 *mailbox_name = p_data_tbl;
4863 if (hStmt != NULL) {
4864 rc = sqlite3_finalize(hStmt);
4865 if (rc != SQLITE_OK) {
4866 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
4867 error = EMAIL_ERROR_DB_FAILURE;
4871 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4874 if (err_code != NULL)
4877 EM_DEBUG_FUNC_END("ret [%d]", ret);
4881 INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
4883 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
4885 if (input_mailbox_id <= 0 || !output_mailbox) {
4886 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4887 return EMAIL_ERROR_INVALID_PARAM;
4890 int ret = EMAIL_ERROR_NONE;
4891 int result_count = 0;
4892 char conditional_clause_string[QUERY_SIZE] = {0, };
4894 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE mailbox_id = %d", input_mailbox_id);
4896 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4898 if( (ret = emstorage_query_mailbox_tbl(conditional_clause_string, "", false, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4899 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
4905 EM_DEBUG_FUNC_END("ret [%d]", ret);
4909 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)
4911 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);
4912 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
4913 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
4914 if (err_code != NULL)
4915 *err_code = EMAIL_ERROR_INVALID_PARAM;
4919 int rc, ret = false;
4920 int error = EMAIL_ERROR_NONE;
4921 emstorage_account_tbl_t* account = NULL;
4922 DB_STMT hStmt = NULL;
4923 char sql_query_string[QUERY_SIZE] = {0, };
4925 sqlite3 *local_db_handle = emstorage_get_db_connection();
4926 EMSTORAGE_START_READ_TRANSACTION(transaction);
4928 /* Check whether the account exists. */
4929 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
4930 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
4935 emstorage_free_account(&account, 1, NULL);
4937 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);
4939 EM_DEBUG_LOG("query = [%s]", sql_query_string);
4942 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4944 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4945 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4948 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4949 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4950 ("sqlite3_step fail:%d", rc));
4952 if (rc == SQLITE_DONE) {
4953 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
4954 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4958 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
4963 if (hStmt != NULL) {
4964 rc = sqlite3_finalize(hStmt);
4965 if (rc != SQLITE_OK) {
4966 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
4967 error = EMAIL_ERROR_DB_FAILURE;
4971 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4974 if (err_code != NULL)
4977 EM_DEBUG_FUNC_END("ret [%d]", ret);
4981 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)
4983 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);
4984 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
4985 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4986 if (err_code != NULL)
4987 *err_code = EMAIL_ERROR_INVALID_PARAM;
4991 int rc, ret = false;
4992 int error = EMAIL_ERROR_NONE;
4993 emstorage_account_tbl_t* account = NULL;
4994 DB_STMT hStmt = NULL;
4995 char sql_query_string[QUERY_SIZE] = {0, };
4997 sqlite3 *local_db_handle = emstorage_get_db_connection();
4998 EMSTORAGE_START_READ_TRANSACTION(transaction);
5000 /* Check whether the account exists. */
5001 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5002 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5007 emstorage_free_account(&account, 1, NULL);
5009 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);
5011 EM_DEBUG_LOG("query = [%s]", sql_query_string);
5014 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5016 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5017 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5020 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5021 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5022 ("sqlite3_step fail:%d", rc));
5024 if (rc == SQLITE_DONE) {
5025 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5026 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5030 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5035 if (hStmt != NULL) {
5036 rc = sqlite3_finalize(hStmt);
5037 if (rc != SQLITE_OK) {
5038 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5039 error = EMAIL_ERROR_DB_FAILURE;
5043 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5046 if (err_code != NULL)
5049 EM_DEBUG_FUNC_END("ret [%d]", ret);
5053 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)
5055 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);
5056 int rc, ret = false;
5057 int error = EMAIL_ERROR_NONE;
5058 char sql_query_string[QUERY_SIZE] = {0, };
5059 char *replaced_mailbox_name = NULL;
5062 if (strstr(mailbox_name, "'")) {
5063 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5065 replaced_mailbox_name = strdup(mailbox_name);
5069 sqlite3 *local_db_handle = emstorage_get_db_connection();
5071 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5073 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5074 "UPDATE mail_box_tbl SET"
5075 " modifiable_yn = %d"
5076 " WHERE account_id = %d"
5077 " AND local_yn = %d"
5078 " AND mailbox_name = '%s'"
5082 , replaced_mailbox_name);
5083 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5084 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5085 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5090 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5093 EM_SAFE_FREE(replaced_mailbox_name);
5095 if (err_code != NULL)
5098 EM_DEBUG_FUNC_END("ret [%d]", ret);
5104 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)
5106 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);
5107 int rc, ret = false;
5108 int error = EMAIL_ERROR_NONE;
5109 char sql_query_string[QUERY_SIZE] = {0, };
5111 if (account_id <= 0 || input_mailbox_id <= 0) {
5112 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5114 if (err_code != NULL)
5115 *err_code = EMAIL_ERROR_INVALID_PARAM;
5118 sqlite3 *local_db_handle = emstorage_get_db_connection();
5120 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5122 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5123 "UPDATE mail_box_tbl SET"
5124 " total_mail_count_on_server = %d"
5125 " WHERE account_id = %d"
5126 " AND mailbox_id = %d"
5127 , total_count_on_server
5129 , input_mailbox_id);
5130 EM_DEBUG_LOG("query[%s]", sql_query_string);
5131 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5132 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5133 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5138 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5141 if (err_code != NULL)
5144 EM_DEBUG_FUNC_END("ret [%d]", ret);
5150 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)
5152 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);
5154 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5155 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);
5157 if (err_code != NULL)
5158 *err_code = EMAIL_ERROR_INVALID_PARAM;
5162 int rc, ret = false;
5163 int error = EMAIL_ERROR_NONE;
5164 char sql_query_string[QUERY_SIZE] = {0, };
5165 DB_STMT hStmt = NULL;
5168 sqlite3 *local_db_handle = emstorage_get_db_connection();
5169 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5171 if (local_yn != -1) {
5172 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5173 "UPDATE mail_box_tbl SET"
5175 ", mailbox_name = ?"
5176 ", mailbox_type = ?"
5178 ", deleted_flag = ?"
5179 ", modifiable_yn= ?"
5180 ", mail_slot_size= ?"
5181 ", total_mail_count_on_server = ?"
5182 " WHERE account_id = %d"
5183 " AND local_yn = %d"
5184 " AND mailbox_id = '%d'"
5187 , input_mailbox_id);
5190 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5191 "UPDATE mail_box_tbl SET"
5193 ", mailbox_name = ?"
5194 ", mailbox_type = ?"
5196 ", deleted_flag = ?"
5197 ", modifiable_yn= ?"
5198 ", mail_slot_size= ?"
5199 ", total_mail_count_on_server = ?"
5200 " WHERE account_id = %d"
5201 " AND mailbox_id = '%d'"
5203 , input_mailbox_id);
5208 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5209 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5210 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5212 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5213 _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);
5214 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5215 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5216 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5217 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5218 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5219 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5222 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5223 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5224 ("sqlite3_step fail:%d", rc));
5225 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5226 ("sqlite3_step fail:%d", rc));
5231 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5234 if (hStmt != NULL) {
5235 rc = sqlite3_finalize(hStmt);
5236 if (rc != SQLITE_OK) {
5237 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5238 error = EMAIL_ERROR_DB_FAILURE;
5242 if (err_code != NULL)
5245 EM_DEBUG_FUNC_END("ret [%d]", ret);
5250 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)
5252 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);
5254 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name) {
5255 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s]", account_id, local_yn, mailbox_name);
5257 if (err_code != NULL)
5258 *err_code = EMAIL_ERROR_INVALID_PARAM;
5262 int rc, ret = false;
5263 int error = EMAIL_ERROR_NONE;
5264 char sql_query_string[QUERY_SIZE] = {0, };
5265 char *replaced_mailbox_name = NULL;
5267 if (strstr(mailbox_name, "'")) {
5268 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5270 replaced_mailbox_name = strdup(mailbox_name);
5273 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5275 sqlite3 *local_db_handle = emstorage_get_db_connection();
5276 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5278 EM_DEBUG_LOG("emstorage_update_mailbox_type");
5280 DB_STMT hStmt_box_tbl = NULL;
5281 DB_STMT hStmt_mail_tbl = NULL;
5284 /* Update mail_box_tbl */
5285 if (local_yn != -1) {
5286 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5287 "UPDATE mail_box_tbl SET"
5289 " WHERE account_id = %d"
5290 " AND local_yn = %d"
5291 " AND mailbox_name = '%s'"
5294 , replaced_mailbox_name);
5297 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5298 "UPDATE mail_box_tbl SET"
5300 " WHERE account_id = %d"
5301 " AND mailbox_name = '%s'"
5303 , replaced_mailbox_name);
5306 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5308 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
5310 if(SQLITE_OK != rc) {
5311 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5312 error = EMAIL_ERROR_DB_FAILURE;
5316 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
5319 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
5321 if(rc == SQLITE_FULL) {
5322 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5323 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5327 if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
5328 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5329 error = EMAIL_ERROR_DB_FAILURE;
5334 /* Update mail_tbl */
5336 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5337 "UPDATE mail_tbl SET"
5339 " WHERE account_id = %d"
5340 " AND mailbox_name = '%s'"
5342 , replaced_mailbox_name);
5344 EM_DEBUG_LOG("SQL[%s]", sql_query_string);
5346 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
5347 if(SQLITE_OK != rc) {
5348 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5349 error = EMAIL_ERROR_DB_FAILURE;
5353 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
5355 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
5356 if(rc == SQLITE_FULL) {
5357 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5358 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5362 if(rc != SQLITE_ROW && rc != SQLITE_DONE){
5363 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5364 error = EMAIL_ERROR_DB_FAILURE;
5371 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5374 EM_SAFE_FREE(replaced_mailbox_name);
5376 if (hStmt_box_tbl != NULL) {
5377 rc = sqlite3_finalize(hStmt_box_tbl);
5378 if (rc != SQLITE_OK) {
5379 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5380 error = EMAIL_ERROR_DB_FAILURE;
5384 if (hStmt_mail_tbl != NULL) {
5385 rc = sqlite3_finalize(hStmt_mail_tbl);
5386 if (rc != SQLITE_OK) {
5387 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5388 error = EMAIL_ERROR_DB_FAILURE;
5392 if (err_code != NULL)
5395 EM_DEBUG_FUNC_END("ret [%d]", ret);
5399 INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox, int transaction)
5401 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);
5403 int rc, ret = false;
5404 int error = EMAIL_ERROR_NONE;
5405 char sql_query_string[QUERY_SIZE] = {0, };
5407 if (input_mailbox_id < 0) {
5408 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5409 return EMAIL_ERROR_INVALID_PARAM;
5412 sqlite3 *local_db_handle = emstorage_get_db_connection();
5414 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5416 EM_DEBUG_LOG("emstorage_update_mailbox_type");
5418 DB_STMT hStmt = NULL;
5421 /* Update mail_box_tbl */
5422 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5423 "UPDATE mail_box_tbl SET"
5425 " WHERE mailbox_id = %d"
5426 , input_mailbox_id);
5428 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5430 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5431 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5432 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5434 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
5436 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5437 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5438 ("sqlite3_step fail:%d", rc));
5439 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5440 ("sqlite3_step fail:%d", rc));
5442 if (hStmt != NULL) {
5443 rc = sqlite3_finalize(hStmt);
5444 if (rc != SQLITE_OK) {
5445 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5446 error = EMAIL_ERROR_DB_FAILURE;
5454 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5457 if (hStmt != NULL) {
5458 rc = sqlite3_finalize(hStmt);
5459 if (rc != SQLITE_OK) {
5460 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5461 error = EMAIL_ERROR_DB_FAILURE;
5465 EM_DEBUG_FUNC_END("error [%d]", error);
5469 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)
5471 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);
5473 int err = EMAIL_ERROR_NONE;
5476 int cur_mailbox_id_string = 0;
5477 int mailbox_id_string_buffer_length = 0;
5478 char sql_query_string[QUERY_SIZE] = {0, };
5479 char *mailbox_id_string_buffer = NULL;
5480 char *parameter_string = NULL;
5481 sqlite3 *local_db_handle = NULL;
5483 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
5484 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5485 return EMAIL_ERROR_INVALID_PARAM;
5488 local_db_handle = emstorage_get_db_connection();
5490 /* Generating mail id list string */
5491 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
5493 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
5495 if(!mailbox_id_string_buffer) {
5496 EM_DEBUG_EXCEPTION("em_malloc failed");
5497 err = EMAIL_ERROR_OUT_OF_MEMORY;
5501 for(i = 0; i < input_mailbox_id_count; i++)
5502 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]);
5504 if(EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
5505 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
5507 /* Generating notification parameter string */
5508 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
5510 if(!parameter_string) {
5511 EM_DEBUG_EXCEPTION("em_malloc failed");
5512 err = EMAIL_ERROR_OUT_OF_MEMORY;
5516 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
5518 /* Write query string */
5519 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);
5521 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
5524 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
5525 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5526 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5527 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5528 if (sqlite3_changes(local_db_handle) == 0)
5529 EM_DEBUG_LOG("no mail matched...");
5534 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, err);
5537 if (result && parameter_string && !emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
5538 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]", input_field_name, input_value);
5540 EM_SAFE_FREE(mailbox_id_string_buffer);
5541 EM_SAFE_FREE(parameter_string);
5543 EM_DEBUG_FUNC_END("err [%d]", err);
5547 INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
5549 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
5552 if (err_code != NULL)
5553 *err_code = EMAIL_ERROR_INVALID_PARAM;
5557 int rc, ret = false;
5558 int error = EMAIL_ERROR_NONE;
5559 DB_STMT hStmt = NULL;
5560 char sql_query_string[QUERY_SIZE] = {0,};
5561 char **result = NULL;
5562 time_t current_time;
5563 sqlite3 *local_db_handle = emstorage_get_db_connection();
5564 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5566 EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
5568 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5569 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5570 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5572 time(¤t_time);
5574 if (NULL == result[1])
5577 rc = atoi(result[1]) + 1;
5578 sqlite3_free_table(result);
5580 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
5582 mailbox_tbl->mailbox_id = rc;
5584 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5585 "INSERT INTO mail_box_tbl VALUES "
5586 "( ?" /* mailbox_id */
5587 ", ?" /* account_id */
5588 ", ?" /* local_yn */
5589 ", ?" /* mailbox_name */
5590 ", ?" /* mailbox_type */
5592 ", ?" /* deleted_flag */
5593 ", ?" /* modifiable_yn */
5594 ", ?" /* total_mail_count_on_server */
5595 ", ?" /* has_archived_mails */
5596 ", ?" /* mail_slot_size */
5597 ", ?" /* no_select */
5598 ", ? )");/* last_sync_time */
5601 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5602 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
5603 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5604 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5608 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
5609 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
5610 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
5611 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
5612 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
5613 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5614 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
5615 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
5616 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
5617 _bind_stmt_field_data_int(hStmt, col_index++, 0);
5618 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
5619 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
5620 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
5623 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5624 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5625 ("sqlite3_step fail:%dn", rc));
5630 if (hStmt != NULL) {
5631 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5633 rc = sqlite3_finalize(hStmt);
5634 if (rc != SQLITE_OK) {
5635 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
5636 error = EMAIL_ERROR_DB_FAILURE;
5640 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5643 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))
5644 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5646 if (err_code != NULL)
5649 EM_DEBUG_FUNC_END("ret [%d]", ret);
5653 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
5655 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
5657 if (account_id < FIRST_ACCOUNT_ID) {
5659 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
5661 if (err_code != NULL)
5662 *err_code = EMAIL_ERROR_INVALID_PARAM;
5665 int rc, ret = false;
5666 int error = EMAIL_ERROR_NONE;
5667 char sql_query_string[QUERY_SIZE] = {0,};
5669 sqlite3 *local_db_handle = emstorage_get_db_connection();
5670 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5673 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
5675 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5676 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5677 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5679 rc = sqlite3_changes(local_db_handle);
5681 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
5687 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5690 if (err_code != NULL)
5692 EM_DEBUG_FUNC_END("ret [%d]", ret);
5698 INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
5700 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);
5702 if (account_id < FIRST_ACCOUNT_ID) {
5703 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5705 if (err_code != NULL)
5706 *err_code = EMAIL_ERROR_INVALID_PARAM;
5710 int rc, ret = false;
5711 int error = EMAIL_ERROR_NONE;
5712 char sql_query_string[QUERY_SIZE] = {0, };
5714 sqlite3 *local_db_handle = emstorage_get_db_connection();
5715 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5718 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
5720 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);
5722 if (input_mailbox_id > 0) { /* 0 means all mailbox */
5723 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);
5726 EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
5727 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5728 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5729 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5731 rc = sqlite3_changes(local_db_handle);
5733 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
5734 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5741 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5744 if(error == EMAIL_ERROR_NONE) {
5745 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
5746 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5749 if (err_code != NULL)
5752 EM_DEBUG_FUNC_END("ret [%d]", ret);
5756 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
5758 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
5761 int error = EMAIL_ERROR_NONE;
5764 if (!mailbox_list || !*mailbox_list) {
5765 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
5767 error = EMAIL_ERROR_INVALID_PARAM;
5771 emstorage_mailbox_tbl_t* p = *mailbox_list;
5774 for (; i < count; i++) {
5775 EM_SAFE_FREE(p[i].mailbox_name);
5776 EM_SAFE_FREE(p[i].alias);
5779 EM_SAFE_FREE(p); *mailbox_list = NULL;
5785 if (err_code != NULL)
5788 EM_DEBUG_FUNC_END("ret [%d]", ret);
5792 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
5794 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);
5796 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
5797 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
5799 if (err_code != NULL)
5800 *err_code = EMAIL_ERROR_INVALID_PARAM;
5804 int rc = -1, ret = false;
5805 int error = EMAIL_ERROR_NONE;
5806 char sql_query_string[QUERY_SIZE] = {0, };
5807 char *replaced_mailbox_name = NULL;
5809 if (strstr(mailbox_name, "'")) {
5810 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5812 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5815 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5818 sqlite3 *local_db_handle = emstorage_get_db_connection();
5819 EMSTORAGE_START_READ_TRANSACTION(transaction);
5820 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);
5821 EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
5825 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5826 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5827 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5829 *count = atoi(result[1]);
5830 sqlite3_free_table(result);
5835 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5838 EM_SAFE_FREE(replaced_mailbox_name);
5840 if (err_code != NULL)
5843 EM_DEBUG_FUNC_END("ret [%d]", ret);
5849 INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
5851 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);
5853 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
5854 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
5856 if (err_code != NULL)
5857 *err_code = EMAIL_ERROR_INVALID_PARAM;
5861 int rc = -1, ret = false;
5862 int error = EMAIL_ERROR_NONE;
5863 char sql_query_string[QUERY_SIZE] = {0, };
5864 char *replaced_mailbox_name = NULL;
5866 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5868 sqlite3 *local_db_handle = emstorage_get_db_connection();
5869 EMSTORAGE_START_READ_TRANSACTION(transaction);
5872 if (strstr(mailbox_name, "'")) {
5873 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5875 replaced_mailbox_name = strdup(mailbox_name);
5878 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);
5881 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);
5886 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5887 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5888 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5890 *exist = atoi(result[1]);
5891 sqlite3_free_table(result);
5901 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5904 EM_SAFE_FREE(replaced_mailbox_name);
5906 if (err_code != NULL)
5909 EM_DEBUG_FUNC_END("ret [%d]", ret);
5913 INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
5915 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
5917 if (!mail || mail_id <= 0) {
5918 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
5919 if (err_code != NULL)
5920 *err_code = EMAIL_ERROR_INVALID_PARAM;
5924 int rc, ret = false, temp_rule;
5925 int error = EMAIL_ERROR_NONE;
5926 DB_STMT hStmt = NULL;
5927 char sql_query_string[QUERY_SIZE] = {0, };
5929 sqlite3 *local_db_handle = emstorage_get_db_connection();
5930 EMSTORAGE_START_READ_TRANSACTION(transaction);
5932 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
5934 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5935 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5937 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5938 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5941 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5942 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5943 ("sqlite3_step fail:%d", rc));
5945 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
5946 if (*mail == NULL) {
5947 error = EMAIL_ERROR_OUT_OF_MEMORY;
5948 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
5952 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
5954 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5955 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5956 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5957 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5958 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5959 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5960 _get_stmt_field_data_int(hStmt, &temp_rule, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5962 (*mail)->server_mail_status = 1;
5967 if (hStmt != NULL) {
5968 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5969 rc = sqlite3_finalize(hStmt);
5970 if (rc != SQLITE_OK) {
5971 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5972 error = EMAIL_ERROR_DB_FAILURE;
5976 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5979 if (err_code != NULL)
5982 EM_DEBUG_FUNC_END("ret [%d]", ret);
5986 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)
5988 EM_PROFILE_BEGIN(emStorageGetDownloadList);
5989 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);
5990 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
5991 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
5993 if (err_code != NULL)
5994 *err_code = EMAIL_ERROR_INVALID_PARAM;
5998 int rc, ret = false;
5999 int error = EMAIL_ERROR_NONE;
6001 DB_STMT hStmt = NULL;
6002 char sql_query_string[QUERY_SIZE] = {0, };
6004 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6007 sqlite3 *local_db_handle = emstorage_get_db_connection();
6008 EMSTORAGE_START_READ_TRANSACTION(transaction);
6011 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);
6013 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6015 EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
6019 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6020 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6021 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6022 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6025 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6026 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6027 ("sqlite3_step fail:%d", rc));
6030 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6031 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6032 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6033 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6035 sqlite3_free_table(result);
6037 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6043 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6044 EM_DEBUG_EXCEPTION(" malloc failed...");
6045 error = EMAIL_ERROR_OUT_OF_MEMORY;
6049 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
6051 for (i = 0; i < *count; ++i) {
6052 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6053 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6054 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6055 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6056 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].s_uid), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6057 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].data1), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6058 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].data2), 0, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6059 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag), FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6061 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6062 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6063 ("sqlite3_step fail:%d", rc));
6070 *read_mail_uid = p_data_tbl;
6071 else if (p_data_tbl)
6072 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6074 if (hStmt != NULL) {
6075 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6076 rc = sqlite3_finalize(hStmt);
6077 if (rc != SQLITE_OK) {
6078 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6080 error = EMAIL_ERROR_DB_FAILURE;
6084 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6087 if (err_code != NULL)
6090 EM_PROFILE_END(emStorageGetDownloadList);
6091 EM_DEBUG_FUNC_END("ret [%d]", ret);
6095 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)
6097 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);
6099 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6100 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);
6102 if (err_code != NULL)
6103 *err_code = EMAIL_ERROR_INVALID_PARAM;
6107 int rc, ret = false;
6108 int error = EMAIL_ERROR_NONE;
6109 DB_STMT hStmt = NULL;
6110 char sql_query_string[QUERY_SIZE] = {0, };
6111 char *replaced_mailbox_name = NULL;
6113 sqlite3 *local_db_handle = emstorage_get_db_connection();
6114 EMSTORAGE_START_READ_TRANSACTION(transaction);
6117 if (strstr(mailbox_name, "'")) {
6118 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6120 replaced_mailbox_name = strdup(mailbox_name);
6123 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6125 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6126 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6127 "WHERE account_id = %d "
6128 "AND mailbox_id = '%s' "
6129 "AND local_uid = %d "
6130 "AND mailbox_name = '%s' "
6132 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6135 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6136 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6137 "WHERE account_id = %d "
6138 "AND mailbox_id = '%s' "
6139 "AND local_uid = %d "
6141 account_id, mailbox_id, local_uid, uid);
6145 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6146 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6147 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6150 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6151 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6152 ("sqlite3_step fail:%d", rc));
6154 if (rc == SQLITE_DONE) {
6155 EM_DEBUG_EXCEPTION(" no matched mail found....");
6156 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6160 _get_stmt_field_data_int(hStmt, mail_size, 0);
6165 EM_SAFE_FREE(replaced_mailbox_name);
6167 if (hStmt != NULL) {
6168 rc = sqlite3_finalize(hStmt);
6169 if (rc != SQLITE_OK) {
6170 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6172 error = EMAIL_ERROR_DB_FAILURE;
6176 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6179 if (err_code != NULL)
6182 EM_DEBUG_FUNC_END("ret [%d]", ret);
6186 INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6188 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6190 if (!read_mail_uid) {
6191 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6192 if (err_code != NULL)
6193 *err_code = EMAIL_ERROR_INVALID_PARAM;
6197 int rc, rc2, ret = false;
6198 int error = EMAIL_ERROR_NONE;
6199 DB_STMT hStmt = NULL;
6200 char sql_query_string[QUERY_SIZE] = {0, };
6202 sqlite3 *local_db_handle = emstorage_get_db_connection();
6203 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6205 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6206 char **result = NULL;
6209 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6210 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6211 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6213 if (NULL==result[1]) rc = 1;
6214 else rc = atoi(result[1])+1;
6215 sqlite3_free_table(result);
6217 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6218 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6230 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6231 if (rc2 != SQLITE_OK) {
6232 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6233 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6235 error = EMAIL_ERROR_DB_FAILURE;
6239 EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
6240 EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
6241 EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
6242 EM_DEBUG_LOG("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
6243 EM_DEBUG_LOG("s_uid VALUE [%s] ", read_mail_uid->s_uid);
6244 EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->data1);
6245 EM_DEBUG_LOG("data2 VALUE [%s] ", read_mail_uid->data2);
6246 EM_DEBUG_LOG("flag VALUE [%d] ", read_mail_uid->flag);
6247 EM_DEBUG_LOG("rc VALUE [%d] ", rc);
6249 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6250 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6251 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6252 _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);
6253 _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);
6254 _bind_stmt_field_data_int(hStmt, DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->data1);
6255 _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);
6256 _bind_stmt_field_data_int(hStmt, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flag);
6257 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6260 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6261 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6262 ("sqlite3_step fail:%d", rc));
6263 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6264 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6270 if (hStmt != NULL) {
6271 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6273 rc = sqlite3_finalize(hStmt);
6274 if (rc != SQLITE_OK) {
6275 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6276 error = EMAIL_ERROR_DB_FAILURE;
6280 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6283 if (err_code != NULL)
6286 EM_DEBUG_FUNC_END("ret [%d]", ret);
6290 #ifdef __FEATURE_BODY_SEARCH__
6291 INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
6293 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
6296 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
6297 if (err_code != NULL)
6298 *err_code = EMAIL_ERROR_INVALID_PARAM;
6302 int rc, rc2, ret = false;
6303 int error = EMAIL_ERROR_NONE;
6304 DB_STMT hStmt = NULL;
6305 char sql_query_string[QUERY_SIZE] = {0, };
6307 sqlite3 *local_db_handle = emstorage_get_db_connection();
6308 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6310 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
6311 char **result = NULL;
6313 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6314 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6315 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6316 sqlite3_free_table(result);
6318 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6319 "INSERT INTO mail_text_tbl VALUES "
6325 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6326 if (rc2 != SQLITE_OK) {
6327 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6328 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
6330 error = EMAIL_ERROR_DB_FAILURE;
6334 EM_DEBUG_LOG("mail_id VALUE [%d] ", mail_text->mail_id);
6335 EM_DEBUG_LOG("account_id VALUE [%d] ", mail_text->account_id);
6336 EM_DEBUG_LOG("mailbox_id VALUE [%d] ", mail_text->mailbox_id);
6337 EM_DEBUG_LOG("body_text VALUE [%s] ", mail_text->body_text);
6339 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
6340 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
6341 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
6342 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
6344 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6345 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6346 ("sqlite3_step fail:%d", rc));
6347 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6348 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6353 if (hStmt != NULL) {
6354 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6356 rc = sqlite3_finalize(hStmt);
6357 if (rc != SQLITE_OK) {
6358 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6359 error = EMAIL_ERROR_DB_FAILURE;
6363 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6366 if (err_code != NULL)
6369 EM_DEBUG_FUNC_END("ret [%d]", ret);
6374 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)
6376 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);
6378 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
6379 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);
6381 if (err_code != NULL)
6382 *err_code = EMAIL_ERROR_INVALID_PARAM;
6386 int rc, ret = false;
6387 int error = EMAIL_ERROR_NONE;
6388 DB_STMT hStmt = NULL;
6389 char sql_query_string[QUERY_SIZE] = {0, };
6391 sqlite3 *local_db_handle = emstorage_get_db_connection();
6393 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6395 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6396 "UPDATE mail_read_mail_uid_tbl SET"
6400 ", mailbox_name = ?"
6405 " WHERE account_id = ?"
6406 " AND mailbox_id = ?"
6407 " AND local_uid = ?"
6408 " AND mailbox_name= ?"
6412 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6413 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6414 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6415 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6420 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
6421 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
6422 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
6423 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6424 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6425 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->data1);
6426 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6427 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flag);
6428 _bind_stmt_field_data_int(hStmt, i++, account_id);
6429 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
6430 _bind_stmt_field_data_int(hStmt, i++, local_uid);
6431 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6432 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6435 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6436 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6437 ("sqlite3_step fail:%d", rc));
6442 if (hStmt != NULL) {
6443 EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
6445 rc = sqlite3_finalize(hStmt);
6446 if (rc != SQLITE_OK) {
6447 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6449 error = EMAIL_ERROR_DB_FAILURE;
6453 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6456 if (err_code != NULL)
6459 EM_DEBUG_FUNC_END("ret [%d]", ret);
6463 INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
6465 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);
6467 if (account_id < FIRST_ACCOUNT_ID) {
6468 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
6470 if (err_code != NULL)
6471 *err_code = EMAIL_ERROR_INVALID_PARAM;
6475 int rc, ret = false;
6476 int error = EMAIL_ERROR_NONE;
6477 char sql_query_string[QUERY_SIZE] = {0, };
6478 char *replaced_mailbox_name = NULL;
6480 sqlite3 *local_db_handle = emstorage_get_db_connection();
6482 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6484 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
6486 if (mailbox_name) { /* NULL means all mailbox_name */
6487 if (strstr(mailbox_name, "'")) {
6488 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6490 replaced_mailbox_name = strdup(mailbox_name);
6493 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);
6496 if (uid) { /* NULL means all mail */
6497 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);
6500 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6501 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6502 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6509 EM_SAFE_FREE(replaced_mailbox_name);
6511 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6514 if (err_code != NULL)
6517 EM_DEBUG_FUNC_END("ret [%d]", ret);
6521 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
6523 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
6526 int error = EMAIL_ERROR_NONE;
6529 if (!read_mail_uid || !*read_mail_uid) {
6530 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
6532 error = EMAIL_ERROR_INVALID_PARAM;
6536 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
6539 for (i = 0; i < count; i++) {
6540 EM_SAFE_FREE(p[i].mailbox_name);
6541 EM_SAFE_FREE(p[i].s_uid);
6542 EM_SAFE_FREE(p[i].data2);
6545 EM_SAFE_FREE(p); *read_mail_uid = NULL;
6551 if (err_code != NULL)
6554 EM_DEBUG_FUNC_END("ret [%d]", ret);
6558 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(int account_id, int *count, int transaction, int *err_code)
6560 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
6563 EM_DEBUG_EXCEPTION("count[%p]", count);
6565 if (err_code != NULL)
6566 *err_code = EMAIL_ERROR_INVALID_PARAM;
6570 int rc = -1, ret = false;
6571 int error = EMAIL_ERROR_NONE;
6572 char sql_query_string[QUERY_SIZE] = {0, };
6574 sqlite3 *local_db_handle = emstorage_get_db_connection();
6575 EMSTORAGE_START_READ_TRANSACTION(transaction);
6577 if (account_id != ALL_ACCOUNT)
6578 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
6580 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
6584 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6585 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6586 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6588 *count = atoi(result[1]);
6589 sqlite3_free_table(result);
6594 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6597 if (err_code != NULL)
6600 EM_DEBUG_FUNC_END("ret [%d]", ret);
6604 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)
6606 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);
6608 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
6609 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);
6611 if (err_code != NULL)
6612 *err_code = EMAIL_ERROR_INVALID_PARAM;
6617 int error = EMAIL_ERROR_NONE;
6619 emstorage_rule_tbl_t* p_data_tbl = NULL;
6620 int i = 0, count = 0;
6621 DB_STMT hStmt = NULL;
6622 char sql_query_string[QUERY_SIZE] = {0, };
6626 sqlite3 *local_db_handle = emstorage_get_db_connection();
6627 EMSTORAGE_START_READ_TRANSACTION(transaction);
6629 if (account_id != ALL_ACCOUNT) {
6631 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
6633 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
6636 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
6638 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
6641 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6642 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6643 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6644 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6647 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6648 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6649 ("sqlite3_step fail:%d", rc));
6652 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
6653 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
6654 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6655 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6657 sqlite3_free_table(result);
6660 EM_DEBUG_LOG(" no matched rule found...");
6666 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
6667 EM_DEBUG_EXCEPTION(" malloc failed...");
6669 error = EMAIL_ERROR_OUT_OF_MEMORY;
6673 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
6675 for (i = 0; i < count; i++) {
6676 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6677 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6678 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
6679 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6680 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6681 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6682 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6683 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6685 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6686 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6687 ("sqlite3_step fail:%d", rc));
6694 EM_DEBUG_LOG("[%d] rules found.", count);
6697 *rule_list = p_data_tbl;
6698 *select_num = count;
6700 else if (p_data_tbl != NULL)
6701 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
6703 if (hStmt != NULL) {
6704 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6706 rc = sqlite3_finalize(hStmt);
6707 if (rc != SQLITE_OK) {
6708 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6709 error = EMAIL_ERROR_DB_FAILURE;
6714 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6717 if (err_code != NULL)
6720 EM_DEBUG_FUNC_END("ret [%d]", ret);
6724 INTERNAL_FUNC int emstorage_get_rule_by_id(int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
6726 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
6729 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
6731 if (err_code != NULL)
6732 *err_code = EMAIL_ERROR_INVALID_PARAM;
6736 emstorage_rule_tbl_t* p_data_tbl = NULL;
6737 int rc, ret = false;
6738 int error = EMAIL_ERROR_NONE;
6740 DB_STMT hStmt = NULL;
6741 char sql_query_string[QUERY_SIZE] = {0, };
6742 sqlite3 *local_db_handle = emstorage_get_db_connection();
6743 EMSTORAGE_START_READ_TRANSACTION(transaction);
6745 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
6747 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6748 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6749 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6752 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6753 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6754 ("sqlite3_step fail:%d", rc));
6756 if (rc == SQLITE_DONE) {
6757 EM_DEBUG_EXCEPTION(" no matched rule found...");
6758 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6762 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
6763 EM_DEBUG_EXCEPTION(" malloc failed...");
6764 error = EMAIL_ERROR_OUT_OF_MEMORY;
6768 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
6769 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6770 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6771 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
6772 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6773 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6774 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6775 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6776 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6784 if (hStmt != NULL) {
6785 EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
6787 rc = sqlite3_finalize(hStmt);
6788 if (rc != SQLITE_OK) {
6789 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6791 error = EMAIL_ERROR_DB_FAILURE;
6795 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6798 if (err_code != NULL)
6801 EM_DEBUG_FUNC_END("ret [%d]", ret);
6805 INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
6807 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
6809 if (!new_rule) { /* only global rule supported. */
6810 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
6812 if (err_code != NULL)
6813 *err_code = EMAIL_ERROR_INVALID_PARAM;
6817 int rc, ret = false;
6818 int error = EMAIL_ERROR_NONE;
6820 DB_STMT hStmt = NULL;
6821 char sql_query_string[QUERY_SIZE] = {0, };
6822 sqlite3 *local_db_handle = emstorage_get_db_connection();
6823 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6825 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6826 "UPDATE mail_rule_tbl SET"
6830 ", target_mailbox_id = ?"
6833 " WHERE account_id = ?"
6834 " AND rule_id = ?");
6837 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6838 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
6839 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6840 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6844 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
6845 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6846 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
6847 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
6848 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
6849 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
6850 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
6851 _bind_stmt_field_data_int(hStmt, i++, rule_id);
6854 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6855 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6856 ("sqlite3_step fail:%d", rc));
6861 if (hStmt != NULL) {
6862 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6864 rc = sqlite3_finalize(hStmt);
6865 if (rc != SQLITE_OK) {
6866 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6868 error = EMAIL_ERROR_DB_FAILURE;
6872 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6875 if (err_code != NULL)
6878 EM_DEBUG_FUNC_END("ret [%d]", ret);
6882 INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6884 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6888 EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6890 if (err_code != NULL)
6891 *err_code = EMAIL_ERROR_INVALID_PARAM;
6895 DB_STMT hStmt = NULL;
6896 char sql_query_string[QUERY_SIZE] = {0, };
6898 int error = EMAIL_ERROR_NONE;
6899 int rc, ret = false;
6901 sqlite3 *local_db_handle = emstorage_get_db_connection();
6902 EMSTORAGE_START_READ_TRANSACTION(transaction);
6904 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6905 "SELECT rule_id FROM mail_rule_tbl WHERE type = %d AND UPPER(value) = UPPER('%s')",
6906 rule->type, rule->value);
6909 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6910 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6911 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6914 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6915 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6916 ("sqlite3_step fail:%d", rc));
6918 if (rc == SQLITE_DONE) {
6919 EM_DEBUG_EXCEPTION(" no matched rule found...");
6920 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6928 if (hStmt != NULL) {
6929 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
6931 rc = sqlite3_finalize(hStmt);
6932 if (rc != SQLITE_OK) {
6933 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
6934 error = EMAIL_ERROR_DB_FAILURE;
6939 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6945 EM_DEBUG_FUNC_END("ret [%d]", ret);
6949 INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6951 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6953 if (!rule) { /* only global rule supported. */
6955 EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6957 if (err_code != NULL)
6958 *err_code = EMAIL_ERROR_INVALID_PARAM;
6962 int rc, rc_2, ret = false;
6963 int error = EMAIL_ERROR_NONE;
6964 DB_STMT hStmt = NULL;
6965 char sql_query_string[QUERY_SIZE] = {0, };
6968 sqlite3 *local_db_handle = emstorage_get_db_connection();
6970 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6974 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
6976 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6977 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6978 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6980 if (NULL==result[1])
6983 rc = atoi(result[1])+1;
6985 sqlite3_free_table(result);
6989 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6990 "INSERT INTO mail_rule_tbl VALUES "
6991 "( ?" /* account id */
6995 ", ?" /* action_type */
6996 ", ?" /* target_mailbox_id */
6998 ", ?)"); /* flag2 */
7000 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7001 if (rc_2 != SQLITE_OK) {
7002 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7003 error = EMAIL_ERROR_DB_FAILURE;
7007 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7008 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7009 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7010 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7011 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7012 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7013 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7014 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7016 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7017 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7018 ("sqlite3_step fail:%d", rc));
7023 if (hStmt != NULL) {
7024 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7026 rc = sqlite3_finalize(hStmt);
7027 if (rc != SQLITE_OK) {
7028 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
7030 error = EMAIL_ERROR_DB_FAILURE;
7034 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7037 if (err_code != NULL)
7040 EM_DEBUG_FUNC_END("ret [%d]", ret);
7044 INTERNAL_FUNC int emstorage_delete_rule(int rule_id, int transaction, int *err_code)
7046 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7048 if (rule_id <= 0) { /* only global rule supported. */
7049 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7051 if (err_code != NULL)
7052 *err_code = EMAIL_ERROR_INVALID_PARAM;
7056 int rc, ret = false;
7057 int error = EMAIL_ERROR_NONE;
7058 char sql_query_string[QUERY_SIZE] = {0, };
7060 sqlite3 *local_db_handle = emstorage_get_db_connection();
7061 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7063 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7065 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7066 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7067 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7068 rc = sqlite3_changes(local_db_handle);
7070 EM_DEBUG_EXCEPTION(" no matched rule found...");
7072 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7079 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7082 if (err_code != NULL)
7085 EM_DEBUG_FUNC_END("ret [%d]", ret);
7089 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7091 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7094 int error = EMAIL_ERROR_NONE;
7097 if (!rule_list || !*rule_list) {
7098 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7100 error = EMAIL_ERROR_INVALID_PARAM;
7104 emstorage_rule_tbl_t* p = *rule_list;
7107 for (; i < count; i++) {
7108 EM_SAFE_FREE(p[i].value);
7111 EM_SAFE_FREE(p); *rule_list = NULL;
7115 if (err_code != NULL)
7118 EM_DEBUG_FUNC_END("ret [%d]", ret);
7122 INTERNAL_FUNC int emstorage_get_mail_count(int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
7124 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);
7126 if (!total && !unseen) {
7127 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7128 if (err_code != NULL)
7129 *err_code = EMAIL_ERROR_INVALID_PARAM;
7133 int rc = -1, ret = false;
7134 int error = EMAIL_ERROR_NONE;
7135 DB_STMT hStmt = NULL;
7136 char sql_query_string[QUERY_SIZE] = {0, };
7138 char *replaced_mailbox_name = NULL;
7140 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7141 sqlite3 *local_db_handle = emstorage_get_db_connection();
7142 EMSTORAGE_START_READ_TRANSACTION(transaction);
7145 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7147 if (account_id != ALL_ACCOUNT) {
7148 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);
7150 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);
7152 else if (mailbox_id)
7153 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);
7155 #ifdef USE_GET_RECORD_COUNT_API
7158 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7159 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
7160 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7162 *total = atoi(result[1]);
7163 sqlite3_free_table(result);
7166 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7167 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7168 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7170 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7171 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7172 ("sqlite3_step fail:%d", rc));
7173 _get_stmt_field_data_int(hStmt, total, 0);
7174 #endif /* USE_GET_RECORD_COUNT_API */
7178 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7180 if (account_id != ALL_ACCOUNT)
7181 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);
7184 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);
7187 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)");
7190 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7191 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7192 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7194 *unseen = atoi(result[1]);
7195 sqlite3_free_table(result);
7203 #ifndef USE_PREPARED_QUERY_
7204 if (hStmt != NULL) {
7205 rc = sqlite3_finalize(hStmt);
7206 if (rc != SQLITE_OK) {
7207 EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
7208 error = EMAIL_ERROR_DB_FAILURE;
7213 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7216 EM_SAFE_FREE(replaced_mailbox_name);
7218 if (err_code != NULL)
7221 EM_DEBUG_FUNC_END("ret [%d]", ret);
7225 INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7227 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7229 if (mail_id <= 0 || !mail) {
7230 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7231 if (err_code != NULL)
7232 *err_code = EMAIL_ERROR_INVALID_PARAM;
7237 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7239 if (p_data_tbl == NULL) {
7240 EM_DEBUG_EXCEPTION("malloc failed...");
7241 if (err_code != NULL)
7242 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7246 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7247 DB_STMT hStmt = NULL;
7248 char sql_query_string[QUERY_SIZE] = {0, };
7250 int rc, ret = false;
7251 int error = EMAIL_ERROR_NONE;
7253 sqlite3 *local_db_handle = emstorage_get_db_connection();
7254 EMSTORAGE_START_READ_TRANSACTION(transaction);
7257 case RETRIEVE_SUMMARY:
7258 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7259 "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);
7262 case RETRIEVE_FIELDS_FOR_DELETE:
7263 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7264 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7267 case RETRIEVE_ACCOUNT:
7268 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7269 "SELECT account_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7273 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7274 "SELECT account_id, flags_seen_field, thread_id, mailbox_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7278 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7279 error = EMAIL_ERROR_INVALID_PARAM;
7283 EM_DEBUG_LOG("Query [%s]", sql_query_string);
7285 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7286 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7287 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7290 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7291 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7292 ("sqlite3_step fail:%d", rc));
7294 if (rc == SQLITE_DONE) {
7295 EM_DEBUG_EXCEPTION("no matched mail found...");
7296 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7300 case RETRIEVE_SUMMARY:
7301 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7302 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7303 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7304 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7305 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7306 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
7307 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
7308 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
7309 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
7310 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
7311 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
7312 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
7313 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
7316 case RETRIEVE_FIELDS_FOR_DELETE:
7317 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7318 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7319 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7320 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7321 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
7324 case RETRIEVE_ACCOUNT:
7325 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7329 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7330 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
7331 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
7332 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7341 else if (p_data_tbl != NULL)
7342 emstorage_free_mail(&p_data_tbl, 1, NULL);
7344 if (hStmt != NULL) {
7345 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7347 rc = sqlite3_finalize(hStmt);
7348 if (rc != SQLITE_OK) {
7349 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7350 error = EMAIL_ERROR_DB_FAILURE;
7355 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7358 if (err_code != NULL)
7361 EM_DEBUG_FUNC_END("ret [%d]", ret);
7365 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)
7367 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);
7370 int error = EMAIL_ERROR_NONE;
7371 int query_string_length = 0;
7372 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
7373 char **result = NULL;
7374 char *sql_query_string = NULL;
7375 emstorage_mail_tbl_t* p_data_tbl = NULL;
7376 sqlite3 *local_db_handle = NULL;
7378 if (number_of_mails <= 0 || !mail_ids) {
7379 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7380 if (err_code != NULL)
7381 *err_code = EMAIL_ERROR_INVALID_PARAM;
7385 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
7387 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
7388 sql_query_string = (char*)em_malloc(query_string_length);
7390 if (p_data_tbl == NULL || sql_query_string == NULL) {
7391 EM_DEBUG_EXCEPTION("malloc failed...");
7393 EM_SAFE_FREE(p_data_tbl);
7394 EM_SAFE_FREE(sql_query_string);
7396 if (err_code != NULL)
7397 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7401 local_db_handle = emstorage_get_db_connection();
7403 EMSTORAGE_START_READ_TRANSACTION(transaction);
7406 case RETRIEVE_SUMMARY:
7407 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7408 "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 (");
7412 case RETRIEVE_FIELDS_FOR_DELETE:
7413 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7414 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id in (");
7418 case RETRIEVE_ACCOUNT:
7419 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7420 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
7425 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7426 "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
7431 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7432 error = EMAIL_ERROR_INVALID_PARAM;
7436 for(i = 0; i < number_of_mails; i++)
7437 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
7438 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
7440 EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
7442 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
7443 if (SQLITE_OK != rc && -1 != rc) {
7444 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7445 error = EMAIL_ERROR_DB_FAILURE;
7449 if (rc == SQLITE_DONE) {
7450 EM_DEBUG_EXCEPTION("no matched mail found...");
7451 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7455 EM_DEBUG_LOG("item_count [%d]", item_count);
7457 if(number_of_mails != item_count) {
7458 EM_DEBUG_EXCEPTION("Can't find all emails");
7459 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7463 col_index = field_count;
7465 for(i = 0; i < item_count; i++) {
7467 case RETRIEVE_SUMMARY:
7468 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7469 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7470 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7471 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7472 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7473 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
7474 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
7475 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7476 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
7477 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
7478 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7479 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
7482 case RETRIEVE_FIELDS_FOR_DELETE:
7483 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7484 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7485 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7486 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7487 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7490 case RETRIEVE_ACCOUNT:
7491 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7495 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7496 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
7497 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7498 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7509 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
7512 sqlite3_free_table(result);
7514 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7517 EM_SAFE_FREE(sql_query_string);
7519 if (err_code != NULL)
7522 EM_DEBUG_FUNC_END("ret [%d]", ret);
7526 INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7528 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7530 if (mail_id <= 0 || !mail) {
7531 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7532 if (err_code != NULL)
7533 *err_code = EMAIL_ERROR_INVALID_PARAM;
7537 int ret = false, error = EMAIL_ERROR_NONE, count;
7538 char conditional_clause[QUERY_SIZE] = {0, };
7539 emstorage_mail_tbl_t* p_data_tbl = NULL;
7541 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7542 EM_DEBUG_LOG("query = [%s]", conditional_clause);
7544 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7545 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7554 else if (p_data_tbl != NULL)
7555 emstorage_free_mail(&p_data_tbl, 1, &error);
7557 if (err_code != NULL)
7560 EM_DEBUG_FUNC_END("ret [%d]", ret);
7564 #ifdef __FEATURE_BODY_SEARCH__
7565 INTERNAL_FUNC int emstorage_get_mail_text_by_id(int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
7567 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
7569 if (mail_id <= 0 || !mail_text) {
7570 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
7571 if (err_code != NULL)
7572 *err_code = EMAIL_ERROR_INVALID_PARAM;
7577 int error = EMAIL_ERROR_NONE;
7579 char conditional_clause[QUERY_SIZE] = {0, };
7580 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
7582 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7583 EM_DEBUG_LOG("query = [%s]", conditional_clause);
7585 if(!emstorage_query_mail_text_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7586 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7594 *mail_text = p_data_tbl;
7595 else if (p_data_tbl != NULL)
7596 emstorage_free_mail_text(&p_data_tbl, 1, &error);
7598 if (err_code != NULL)
7601 EM_DEBUG_FUNC_END("ret [%d]", ret);
7606 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)
7608 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);
7610 if (!search_handle || !searched) {
7611 if (err_code != NULL)
7612 *err_code = EMAIL_ERROR_INVALID_PARAM;
7613 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7614 EM_DEBUG_FUNC_END("false");
7618 emstorage_search_filter_t* p = search;
7619 int error = EMAIL_ERROR_NONE;
7620 DB_STMT hStmt = NULL;
7621 char sql_query_string[QUERY_SIZE] = {0, };
7622 int rc, ret = false;
7623 int and = false, mail_count = 0;
7624 char *replaced_mailbox_name = NULL;
7626 sqlite3 *local_db_handle = emstorage_get_db_connection();
7627 EMSTORAGE_START_READ_TRANSACTION(transaction);
7629 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
7631 if (account_id != ALL_ACCOUNT) {
7632 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);
7637 if (strstr(mailbox_name, "'")) {
7638 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7640 replaced_mailbox_name = strdup(mailbox_name);
7643 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);
7650 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
7651 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);
7654 else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
7655 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);
7658 else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
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_to LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7662 else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
7663 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);
7671 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");
7673 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7676 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7677 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7678 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7681 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7682 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7683 ("sqlite3_step fail:%d", rc));
7687 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
7688 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7689 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7691 sqlite3_free_table(result);
7697 *search_handle = (int)hStmt;
7698 *searched = mail_count;
7699 EM_DEBUG_LOG("mail_count [%d]", mail_count);
7702 if (hStmt != NULL) {
7703 rc = sqlite3_finalize(hStmt);
7704 if (rc != SQLITE_OK) {
7705 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7706 error = EMAIL_ERROR_DB_FAILURE;
7710 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7714 EM_SAFE_FREE(replaced_mailbox_name);
7716 if (err_code != NULL)
7719 EM_DEBUG_FUNC_END("ret [%d]", ret);
7723 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
7725 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
7727 if (search_handle == 0 || !data) {
7728 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
7730 if (err_code != NULL)
7731 *err_code = EMAIL_ERROR_INVALID_PARAM;
7735 emstorage_mail_tbl_t* p_data_tbl = NULL;
7736 DB_STMT hStmt = (DB_STMT)search_handle;
7737 int rc, ret = false;
7738 int error = EMAIL_ERROR_NONE;
7742 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
7745 case RETRIEVE_ENVELOPE:
7747 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
7748 EM_DEBUG_EXCEPTION(" em_malloc failed...");
7750 error = EMAIL_ERROR_OUT_OF_MEMORY;
7754 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7755 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7756 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
7757 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7758 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
7759 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
7760 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
7761 _get_stmt_field_data_int (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
7762 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
7763 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7764 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7765 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
7766 _get_stmt_field_data_char (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
7767 _get_stmt_field_data_int (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
7768 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
7769 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
7770 _get_stmt_field_data_int (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
7771 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
7772 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7773 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
7774 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
7775 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
7776 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
7777 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
7778 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
7779 _get_stmt_field_data_blob (hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
7781 if (type == RETRIEVE_ALL) {
7782 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7783 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7784 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
7785 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
7786 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
7787 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
7788 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
7789 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7790 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7791 _get_stmt_field_data_int (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
7792 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7796 if (p_data_tbl->body_download_status) {
7799 if (p_data_tbl->file_path_html) {
7800 if (stat(p_data_tbl->file_path_html, &buf) == -1)
7801 p_data_tbl->body_download_status = 0;
7803 else if (p_data_tbl->file_path_plain) {
7804 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
7805 p_data_tbl->body_download_status = 0;
7808 p_data_tbl->body_download_status = 0;
7811 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7814 case RETRIEVE_SUMMARY:
7815 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
7816 EM_DEBUG_EXCEPTION(" malloc failed...");
7818 error = EMAIL_ERROR_OUT_OF_MEMORY;
7822 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7824 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7825 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7826 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7827 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7828 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7830 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7833 case RETRIEVE_ADDRESS:
7834 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
7835 EM_DEBUG_EXCEPTION(" malloc failed...");
7836 error = EMAIL_ERROR_OUT_OF_MEMORY;
7840 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7841 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7842 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7843 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7844 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7852 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7853 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7854 ("sqlite3_step fail:%d", rc));
7860 if (err_code != NULL)
7863 EM_DEBUG_FUNC_END("ret [%d]", ret);
7867 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code)
7869 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
7871 int error = EMAIL_ERROR_NONE;
7872 int rc, ret = false;
7874 if (search_handle == 0) {
7875 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
7876 error = EMAIL_ERROR_INVALID_PARAM;
7880 DB_STMT hStmt = (DB_STMT)search_handle;
7882 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7884 rc = sqlite3_finalize(hStmt);
7885 if (rc != SQLITE_OK) {
7886 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
7887 error = EMAIL_ERROR_DB_FAILURE;
7893 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7896 if (err_code != NULL)
7899 EM_DEBUG_FUNC_END("ret [%d]", ret);
7903 INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7905 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7907 if (mail_id <= 0 || !mail) {
7908 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
7910 if (err_code != NULL)
7911 *err_code = EMAIL_ERROR_INVALID_PARAM;
7915 DB_STMT hStmt = NULL;
7916 char sql_query_string[QUERY_SIZE] = {0, };
7919 int error = EMAIL_ERROR_NONE;
7921 sqlite3 *local_db_handle = emstorage_get_db_connection();
7922 char mailbox_id_param_string[10] = {0,};
7924 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7926 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7927 "UPDATE mail_tbl SET"
7932 ", server_mail_status = ?"
7933 ", server_mailbox_name = ?"
7934 ", server_mail_id = ?"
7935 ", reference_mail_id = ?"
7936 ", full_address_from = ?"
7937 ", full_address_reply = ?" /* 10 */
7938 ", full_address_to = ?"
7939 ", full_address_cc = ?"
7940 ", full_address_bcc = ?"
7941 ", full_address_return = ?"
7943 ", body_download_status = ?"
7944 ", file_path_plain = ?"
7945 ", file_path_html = ?"
7947 ", flags_seen_field = ?"
7948 ", flags_deleted_field = ?"
7949 ", flags_flagged_field = ?"
7950 ", flags_answered_field = ?"
7951 ", flags_recent_field = ?"
7952 ", flags_draft_field = ?"
7953 ", flags_forwarded_field = ?"
7959 ", report_status = ?"
7960 ", preview_text = ?"
7962 ", scheduled_sending_time = ?"
7963 ", eas_data_length = ?"
7965 " WHERE mail_id = %d AND account_id != 0 "
7969 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7970 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7971 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7973 _bind_stmt_field_data_int (hStmt, i++, mail->mail_id);
7974 _bind_stmt_field_data_int (hStmt, i++, mail->account_id);
7975 _bind_stmt_field_data_int (hStmt, i++, mail->mailbox_id);
7976 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
7977 _bind_stmt_field_data_int (hStmt, i++, mail->server_mail_status);
7978 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
7979 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
7980 _bind_stmt_field_data_int (hStmt, i++, mail->reference_mail_id);
7981 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
7982 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
7983 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
7984 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
7985 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
7986 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
7987 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
7988 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
7989 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
7990 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
7991 _bind_stmt_field_data_int (hStmt, i++, mail->date_time);
7992 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
7993 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
7994 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
7995 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
7996 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
7997 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
7998 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
7999 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
8000 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
8001 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
8002 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
8003 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8004 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
8005 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8006 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
8007 _bind_stmt_field_data_int (hStmt, i++, mail->scheduled_sending_time);
8008 _bind_stmt_field_data_int (hStmt, i++, mail->eas_data_length);
8009 _bind_stmt_field_data_blob (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8011 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8012 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8013 ("sqlite3_step fail:%d", rc));
8015 rc = sqlite3_changes(local_db_handle);
8017 EM_DEBUG_EXCEPTION(" no matched mail found...");
8018 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8021 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8022 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8023 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8028 if (hStmt != NULL) {
8029 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
8030 rc = sqlite3_finalize(hStmt);
8031 if (rc != SQLITE_OK) {
8032 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
8033 error = EMAIL_ERROR_DB_FAILURE;
8037 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8040 if (err_code != NULL)
8043 EM_DEBUG_FUNC_END("ret [%d]", ret);
8048 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8052 INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int *err_code)
8054 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8056 EM_IF_NULL_RETURN_VALUE(err_code, false);
8059 int error = EMAIL_ERROR_NONE;
8061 char sql_query_string[QUERY_SIZE] = {0, };
8062 sqlite3 *local_db_handle = emstorage_get_db_connection();
8064 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8065 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);
8066 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8068 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8069 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8070 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8072 rc = sqlite3_changes(local_db_handle);
8074 EM_DEBUG_LOG(" No Matched Mail Exists ");
8075 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8082 if (err_code != NULL)
8085 EM_DEBUG_FUNC_END("ret [%d]", ret);
8089 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)
8091 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);
8093 int error = EMAIL_ERROR_NONE;
8096 int cur_mail_id_string = 0;
8097 int mail_id_string_buffer_length = 0;
8098 char sql_query_string[QUERY_SIZE] = {0, };
8099 char *mail_id_string_buffer = NULL;
8100 char *parameter_string = NULL;
8101 sqlite3 *local_db_handle = emstorage_get_db_connection();
8102 email_mail_attribute_type target_mail_attribute_type = 0;
8104 if (!mail_ids || !field_name || account_id == 0) {
8105 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8106 if (err_code != NULL)
8107 *err_code = EMAIL_ERROR_INVALID_PARAM;
8111 if( (error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8112 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8113 if (err_code != NULL)
8118 /* Generating mail id list string */
8119 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8121 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8123 if(!mail_id_string_buffer) {
8124 EM_DEBUG_EXCEPTION("em_malloc failed");
8125 error = EMAIL_ERROR_OUT_OF_MEMORY;
8129 for(i = 0; i < mail_ids_count; i++)
8130 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8132 if(EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8133 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8135 /* Generating notification parameter string */
8136 parameter_string = em_malloc(mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2);
8138 if(!parameter_string) {
8139 EM_DEBUG_EXCEPTION("em_malloc failed");
8140 error = EMAIL_ERROR_OUT_OF_MEMORY;
8144 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8146 /* Write query string */
8147 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);
8149 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
8152 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8153 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8154 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8155 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8156 if (sqlite3_changes(local_db_handle) == 0)
8157 EM_DEBUG_LOG("no mail matched...");
8162 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8165 if (ret && parameter_string && !emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8166 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8168 EM_SAFE_FREE(mail_id_string_buffer);
8169 EM_SAFE_FREE(parameter_string);
8171 if (err_code != NULL)
8174 EM_DEBUG_FUNC_END("error [%d]", error);
8178 #ifdef __FEATURE_BODY_SEARCH__
8179 INTERNAL_FUNC int emstorage_change_mail_text_field(int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
8181 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8183 if (mail_id <= 0 || !mail_text) {
8184 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
8185 if (err_code != NULL)
8186 *err_code = EMAIL_ERROR_INVALID_PARAM;
8191 int error = EMAIL_ERROR_NONE;
8192 DB_STMT hStmt = NULL;
8193 char sql_query_string[QUERY_SIZE] = {0, };
8198 sqlite3 *local_db_handle = emstorage_get_db_connection();
8199 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8201 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8202 "UPDATE mail_text_tbl SET"
8204 " WHERE mail_id = %d AND account_id != 0"
8206 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8208 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8209 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8210 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8213 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
8215 if (hStmt != NULL) {
8216 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8217 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8218 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8220 rc = sqlite3_changes(local_db_handle);
8222 EM_DEBUG_EXCEPTION(" no matched mail found...");
8223 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8232 if (hStmt != NULL) {
8233 rc = sqlite3_finalize(hStmt);
8234 if (rc != SQLITE_OK) {
8235 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8236 error = EMAIL_ERROR_DB_FAILURE;
8241 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8244 if (err_code != NULL)
8247 EM_DEBUG_FUNC_END("ret [%d]", ret);
8252 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)
8254 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8257 int error = EMAIL_ERROR_NONE;
8262 DB_STMT hStmt = NULL;
8263 char sql_query_string[QUERY_SIZE] = {0, };
8264 char mailbox_id_param_string[10] = {0,};
8265 sqlite3 *local_db_handle = NULL;
8267 if (mail_id <= 0 || !mail) {
8268 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
8269 if (err_code != NULL)
8270 *err_code = EMAIL_ERROR_INVALID_PARAM;
8274 local_db_handle = emstorage_get_db_connection();
8276 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8280 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8281 "UPDATE mail_tbl SET"
8282 " body_download_status = ?"
8283 ", file_path_plain = ?"
8284 ", file_path_html = ?"
8285 ", flags_seen_field = ?"
8286 ", flags_deleted_field = ?"
8287 ", flags_flagged_field = ?"
8288 ", flags_answered_field = ?"
8289 ", flags_recent_field = ?"
8290 ", flags_draft_field = ?"
8291 ", flags_forwarded_field = ?"
8293 ", attachment_count = ?"
8295 ", meeting_request_status = ? "
8296 ", message_class = ? "
8297 ", digest_type = ? "
8299 " WHERE mail_id = %d AND account_id != 0"
8303 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8304 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8305 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8308 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8309 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8310 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8311 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
8312 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
8313 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
8314 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
8315 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
8316 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
8317 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
8318 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8319 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8320 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8321 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
8322 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
8323 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
8324 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8327 case UPDATE_MAILBOX: {
8329 emstorage_mailbox_tbl_t *mailbox_tbl;
8331 if ((err = emstorage_get_mailbox_by_id(mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
8332 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
8336 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8337 "UPDATE mail_tbl SET"
8338 " mailbox_id = '%d'"
8339 ",mailbox_type = '%d'"
8340 " WHERE mail_id = %d AND account_id != 0"
8341 , mailbox_tbl->mailbox_id
8342 , mailbox_tbl->mailbox_type
8347 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8348 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8349 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8352 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
8354 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
8360 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8361 "UPDATE mail_tbl SET"
8362 " flags_seen_field = %d"
8363 ",flags_deleted_field = %d"
8364 ",flags_flagged_field = %d"
8365 ",flags_answered_field = %d"
8366 ",flags_recent_field = %d"
8367 ",flags_draft_field = %d"
8368 ",flags_forwarded_field = %d"
8369 " WHERE mail_id = %d AND account_id != 0"
8370 , mail->flags_seen_field
8371 , mail->flags_deleted_field
8372 , mail->flags_flagged_field
8373 , mail->flags_answered_field
8374 , mail->flags_recent_field
8375 , mail->flags_draft_field
8376 , mail->flags_forwarded_field
8378 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8381 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8382 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8383 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8387 case UPDATE_EXTRA_FLAG:
8388 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8389 "UPDATE mail_tbl SET"
8391 ", save_status = %d"
8392 ", lock_status = %d"
8393 ", report_status = %d"
8395 " WHERE mail_id = %d AND account_id != 0"
8399 , mail->report_status
8402 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8405 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8406 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8407 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8410 case UPDATE_STICKY_EXTRA_FLAG:
8411 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8412 "UPDATE mail_tbl SET"
8414 " WHERE mail_id = %d AND account_id != 0"
8417 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8420 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8421 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8422 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8426 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8427 "UPDATE mail_tbl SET"
8428 " full_address_from = ?"
8429 ", full_address_reply = ?"
8430 ", full_address_to = ?"
8431 ", full_address_cc = ?"
8432 ", full_address_bcc = ?"
8433 ", full_address_return = ?"
8435 ", file_path_plain = ?"
8437 ", flags_seen_field = ?"
8438 ", flags_deleted_field = ?"
8439 ", flags_flagged_field = ?"
8440 ", flags_answered_field = ?"
8441 ", flags_recent_field = ?"
8442 ", flags_draft_field = ?"
8443 ", flags_forwarded_field = ?"
8447 ", report_status = ?"
8449 ", file_path_html = ?"
8450 ", file_path_mime_entity = ?"
8452 ", preview_text = ?"
8453 ", body_download_status = ?"
8454 ", attachment_count = ?"
8455 ", inline_content_count = ?"
8456 ", meeting_request_status = ?"
8457 ", message_class = ?"
8460 ", scheduled_sending_time = ?"
8461 ", eas_data_length = ?"
8463 " WHERE mail_id = %d AND account_id != 0"
8467 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8468 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8469 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8470 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8472 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8473 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8474 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8475 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8476 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8477 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8478 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8479 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8480 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
8481 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
8482 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
8483 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
8484 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
8485 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
8486 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
8487 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
8488 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
8489 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
8490 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
8491 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
8492 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
8493 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8494 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8495 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
8496 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8497 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
8498 _bind_stmt_field_data_int (hStmt, i++, mail->attachment_count);
8499 _bind_stmt_field_data_int (hStmt, i++, mail->inline_content_count);
8500 _bind_stmt_field_data_int (hStmt, i++, mail->meeting_request_status);
8501 _bind_stmt_field_data_int (hStmt, i++, mail->message_class);
8502 _bind_stmt_field_data_int (hStmt, i++, mail->digest_type);
8503 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
8504 _bind_stmt_field_data_int (hStmt, i++, mail->scheduled_sending_time);
8505 _bind_stmt_field_data_int (hStmt, i++, mail->eas_data_length);
8506 _bind_stmt_field_data_blob (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8509 case UPDATE_DATETIME: {
8510 time_t now = time(NULL);
8512 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8513 "UPDATE mail_tbl SET"
8515 " WHERE mail_id = %d AND account_id != 0"
8519 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8520 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8521 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8525 case UPDATE_FROM_CONTACT_INFO:
8526 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
8527 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8528 "UPDATE mail_tbl SET"
8529 " email_address_sender = ?,"
8530 " WHERE mail_id = %d",
8535 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8536 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8537 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8539 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8542 case UPDATE_TO_CONTACT_INFO:
8543 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
8544 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8545 "UPDATE mail_tbl SET"
8546 " email_address_recipient = ?,"
8547 " WHERE mail_id = %d",
8552 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8553 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8554 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8556 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8559 case UPDATE_ALL_CONTACT_INFO:
8560 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
8561 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8562 "UPDATE mail_tbl SET"
8563 " email_address_sender = ?,"
8564 " email_address_recipient = ?,"
8565 " WHERE mail_id = %d",
8570 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8571 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8572 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8574 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8575 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8579 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
8580 case UPDATE_PARTIAL_BODY_DOWNLOAD:
8582 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8583 "UPDATE mail_tbl SET"
8584 " body_download_status = ?"
8585 ", file_path_plain = ?"
8586 ", file_path_html = ?"
8587 ", attachment_count = ?"
8588 ", inline_content_count = ?"
8590 " WHERE mail_id = %d"
8594 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8595 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8596 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8599 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8600 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8601 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8602 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8603 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
8604 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8609 case UPDATE_FILE_PATH:
8610 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8611 "UPDATE mail_tbl SET"
8612 ", file_path_plain = ?"
8613 ", file_path_html = ?"
8614 ", file_path_mime_entity = ?"
8615 " WHERE mail_id = %d"
8619 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8620 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8621 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8622 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8624 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8625 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8626 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8630 EM_DEBUG_LOG(" type[%d]", type);
8632 error = EMAIL_ERROR_INVALID_PARAM;
8636 if (hStmt != NULL) {
8638 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8639 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
8640 ("sqlite3_step fail:%d", rc));
8641 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8642 ("sqlite3_step fail:%d", rc));
8643 rc = sqlite3_changes(local_db_handle);
8645 EM_DEBUG_EXCEPTION(" no matched mail found...");
8647 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8652 if (mail->account_id == 0) {
8653 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
8654 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
8655 EM_DEBUG_EXCEPTION(" emstorage_get_mail_field_by_id failed - %d", error);
8659 mail->account_id = mail_for_account_tbl->account_id;
8660 if (mail_for_account_tbl)
8661 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
8668 if (hStmt != NULL) {
8669 rc = sqlite3_finalize(hStmt);
8670 if (rc != SQLITE_OK) {
8671 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8672 error = EMAIL_ERROR_DB_FAILURE;
8677 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8680 /*h.gahlaut@samsung.com: Moving publication of notification after committing transaction to DB */
8682 if (ret == true && move_flag != 1) {
8683 if (!emstorage_get_mailbox_id_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
8684 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_id_by_mailbox_type failed - %d", error);
8686 if (mail->mailbox_id == mailbox_id) {
8687 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8688 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
8689 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8692 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
8693 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
8694 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] ");
8698 if (err_code != NULL)
8701 EM_DEBUG_FUNC_END("ret [%d]", ret);
8704 INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
8706 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8708 int rc, ret = false;
8709 int error = EMAIL_ERROR_NONE;
8710 int latest_mail_id = 0;
8711 sqlite3 *local_db_handle = NULL;
8712 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
8713 char **result = NULL;
8715 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8716 _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
8717 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8719 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
8720 if (ret < 0 || latest_mail_id == 0) {
8721 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
8723 local_db_handle = emstorage_get_db_connection();
8725 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8726 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8727 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
8728 if (NULL == result[1])
8731 rc = atoi(result[1]) + 1;
8733 sqlite3_free_table(result);
8734 latest_mail_id = rc;
8739 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
8742 *mail_id = latest_mail_id;
8744 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8745 _unlockshm_mutex(&mapped_for_generating_mail_id);
8746 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8753 if (err_code != NULL)
8756 EM_DEBUG_FUNC_END("ret [%d]", ret);
8761 INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
8763 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
8764 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
8766 if (!mail_tbl_data) {
8767 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
8768 if (err_code != NULL)
8769 *err_code = EMAIL_ERROR_INVALID_PARAM;
8773 int rc, ret = false;
8774 int error = EMAIL_ERROR_NONE;
8775 char sql_query_string[QUERY_SIZE] = {0, };
8776 DB_STMT hStmt = NULL;
8777 sqlite3 *local_db_handle = emstorage_get_db_connection();
8779 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8782 /* increase unique id */
8783 char *sql = "SELECT max(rowid) FROM mail_tbl;";
8786 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8787 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8788 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
8790 if (NULL == result[1])
8793 rc = atoi(result[1])+1;
8795 sqlite3_free_table(result);
8797 mail_tbl_data->mail_id = rc;
8798 mail_tbl_data->thread_id = rc;
8801 if (mail_tbl_data->date_time == 0)
8802 mail_tbl_data->date_time = time(NULL);
8804 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8805 "INSERT INTO mail_tbl VALUES "
8807 ", ?" /* account_id */
8808 ", ?" /* mailbox_id */
8809 ", ?" /* mailbox_type */
8811 ", ?" /* date_time */
8812 ", ?" /* server_mail_status */
8813 ", ?" /* server_mailbox_name */
8814 ", ?" /* server_mail_id */
8815 ", ?" /* message_id */
8816 ", ?" /* reference_mail_id */
8817 ", ?" /* full_address_from */
8818 ", ?" /* full_address_reply */
8819 ", ?" /* full_address_to */
8820 ", ?" /* full_address_cc */
8821 ", ?" /* full_address_bcc */
8822 ", ?" /* full_address_return */
8823 ", ?" /* email_address_sender */
8824 ", ?" /* email_address_recipient */
8825 ", ?" /* alias_sender */
8826 ", ?" /* alias_recipient */
8827 ", ?" /* body_download_status */
8828 ", ?" /* file_path_plain */
8829 ", ?" /* file_path_html */
8830 ", ?" /* file_path_mime_entity */
8831 ", ?" /* mail_size */
8832 ", ?" /* flags_seen_field */
8833 ", ?" /* flags_deleted_field */
8834 ", ?" /* flags_flagged_field */
8835 ", ?" /* flags_answered_field */
8836 ", ?" /* flags_recent_field */
8837 ", ?" /* flags_draft_field */
8838 ", ?" /* flags_forwarded_field */
8839 ", ?" /* DRM_status */
8840 ", ?" /* priority */
8841 ", ?" /* save_status */
8842 ", ?" /* lock_status */
8843 ", ?" /* report_status */
8844 ", ?" /* attachment_count */
8845 ", ?" /* inline_content_count */
8846 ", ?" /* thread_id */
8847 ", ?" /* thread_item_count */
8848 ", ?" /* preview_text */
8849 ", ?" /* meeting_request_status */
8850 ", ?" /* message_class */
8851 ", ?" /* digest_type */
8852 ", ?" /* smime_type */
8853 ", ?" /* scheduled_sending_time */
8854 ", ?" /* eas_data_length */
8855 ", ?" /* eas_data */
8858 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8859 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8860 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8862 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
8863 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
8864 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
8865 _bind_stmt_field_data_int (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
8866 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8867 _bind_stmt_field_data_int (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
8868 _bind_stmt_field_data_int (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
8869 _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);
8870 _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);
8871 _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);
8872 _bind_stmt_field_data_int (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
8873 _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);
8874 _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);
8875 _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);
8876 _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);
8877 _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);
8878 _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);
8879 _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);
8880 _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);
8881 _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);
8882 _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);
8883 _bind_stmt_field_data_int (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
8884 _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);
8885 _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);
8886 _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);
8887 _bind_stmt_field_data_int (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
8888 _bind_stmt_field_data_int (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
8889 _bind_stmt_field_data_int (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
8890 _bind_stmt_field_data_int (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
8891 _bind_stmt_field_data_int (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
8892 _bind_stmt_field_data_int (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
8893 _bind_stmt_field_data_int (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
8894 _bind_stmt_field_data_int (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
8895 _bind_stmt_field_data_int (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
8896 _bind_stmt_field_data_int (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
8897 _bind_stmt_field_data_int (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
8898 _bind_stmt_field_data_int (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
8899 _bind_stmt_field_data_int (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
8900 _bind_stmt_field_data_int (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
8901 _bind_stmt_field_data_int (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
8902 _bind_stmt_field_data_int (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
8903 _bind_stmt_field_data_int (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
8904 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8905 _bind_stmt_field_data_int (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
8906 _bind_stmt_field_data_int (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
8907 _bind_stmt_field_data_int (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
8908 _bind_stmt_field_data_int (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
8909 _bind_stmt_field_data_int (hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
8910 _bind_stmt_field_data_int (hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
8911 _bind_stmt_field_data_blob (hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
8913 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8914 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8915 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8920 if (hStmt != NULL) {
8921 rc = sqlite3_finalize(hStmt);
8922 if (rc != SQLITE_OK) {
8923 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
8924 error = EMAIL_ERROR_DB_FAILURE;
8928 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8932 if (err_code != NULL)
8935 EM_PROFILE_END(profile_emstorage_add_mail);
8936 EM_DEBUG_FUNC_END("ret [%d]", ret);
8940 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)
8942 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);
8944 int rc, ret = false, i, cur_conditional_clause = 0;
8945 int error = EMAIL_ERROR_NONE;
8946 int target_account_id;
8947 char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
8948 emstorage_mailbox_tbl_t *result_mailbox = NULL;
8949 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
8950 char* target_mailbox_name = NULL;
8952 if (!mail_ids || input_mailbox_id <= 0) {
8953 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8954 if (err_code != NULL)
8955 *err_code = EMAIL_ERROR_INVALID_PARAM;
8959 sqlite3 *local_db_handle = emstorage_get_db_connection();
8961 if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
8962 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
8966 if (result_mailbox->mailbox_name) {
8967 if (strstr(result_mailbox->mailbox_name, "'")) {
8968 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
8970 target_mailbox_name = strdup(result_mailbox->mailbox_name);
8974 target_mailbox_type = result_mailbox->mailbox_type;
8975 target_account_id = result_mailbox->account_id;
8976 emstorage_free_mailbox(&result_mailbox, 1, NULL);
8978 cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
8980 for(i = 0; i < number_of_mails; i++)
8981 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8984 char *last_comma = rindex(conditional_clause, ',');
8985 if(last_comma) *last_comma = ')'; /* replace , with ) */
8987 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8989 /* Updating a mail_tbl */
8991 memset(sql_query_string, 0x00, QUERY_SIZE);
8992 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);
8993 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8995 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8996 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8997 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8999 /* Updating a mail_attachment_tbl */
9000 memset(sql_query_string, 0x00, QUERY_SIZE);
9001 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);
9002 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9004 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9005 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9006 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9008 /* Updating a mail_meeting_tbl */
9009 memset(sql_query_string, 0x00, QUERY_SIZE);
9010 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);
9011 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9013 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9014 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9015 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9017 #ifdef __FEATURE_BODY_SEARCH__
9018 /* Updating mail_text_tbl */
9019 memset(sql_query_string, 0x00, QUERY_SIZE);
9020 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);
9021 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9023 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9024 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9025 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9028 /* Updating a mail_read_mail_uid_tbl */
9029 memset(conditional_clause, 0x00, QUERY_SIZE);
9030 cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
9032 for(i = 0; i < number_of_mails; i++)
9033 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
9036 last_comma = rindex(conditional_clause, ',');
9037 if(last_comma) *last_comma = ')'; /* replace , with ) */
9039 memset(sql_query_string, 0x00, QUERY_SIZE);
9040 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);
9041 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9043 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9044 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9045 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9050 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9053 EM_SAFE_FREE(target_mailbox_name);
9055 if (err_code != NULL)
9058 EM_DEBUG_FUNC_END("ret [%d]", ret);
9062 INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
9064 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9067 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9068 if (err_code != NULL)
9069 *err_code = EMAIL_ERROR_INVALID_PARAM;
9073 int rc, ret = false;
9074 int error = EMAIL_ERROR_NONE;
9075 char sql_query_string[QUERY_SIZE] = {0, };
9077 sqlite3 *local_db_handle = emstorage_get_db_connection();
9078 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9080 memset(sql_query_string, 0x00, sizeof(sql_query_string));
9082 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9083 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9086 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9087 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9088 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9093 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9096 if (err_code != NULL)
9099 EM_DEBUG_FUNC_END("ret [%d]", ret);
9103 INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
9105 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
9107 int rc, ret = false, i, cur_sql_query_string = 0;
9108 int error = EMAIL_ERROR_NONE;
9109 char sql_query_string[QUERY_SIZE] = {0, };
9112 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9113 if (err_code != NULL)
9114 *err_code = EMAIL_ERROR_INVALID_PARAM;
9118 sqlite3 *local_db_handle = emstorage_get_db_connection();
9119 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9121 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
9123 for(i = 0; i < number_of_mails; i++)
9124 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
9127 char *last_comma = rindex(sql_query_string, ',');
9128 *last_comma = ')'; /* replace , with ) */
9130 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9132 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9133 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9134 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9136 #ifdef __FEATURE_BODY_SEARCH__
9137 /* delete mail_text from mail_text_tbl */
9138 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE mail_id in (");
9140 for(i = 0; i < number_of_mails; i++)
9141 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
9143 last_comma = rindex(sql_query_string, ',');
9144 *last_comma = ')'; /* replace , with ) */
9146 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9148 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9149 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9150 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9156 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9159 if (err_code != NULL)
9162 EM_DEBUG_FUNC_END("ret [%d]", ret);
9166 INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
9168 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
9170 if (account_id < FIRST_ACCOUNT_ID) {
9171 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
9173 if (err_code != NULL)
9174 *err_code = EMAIL_ERROR_INVALID_PARAM;
9178 int rc, ret = false;
9179 int error = EMAIL_ERROR_NONE;
9180 char sql_query_string[QUERY_SIZE] = {0, };
9182 sqlite3 *local_db_handle = emstorage_get_db_connection();
9183 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9185 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
9186 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9188 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9189 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9190 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9192 rc = sqlite3_changes(local_db_handle);
9194 EM_DEBUG_EXCEPTION(" no mail found...");
9195 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9198 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
9199 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
9200 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9202 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9203 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9204 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9206 rc = sqlite3_changes(local_db_handle);
9208 EM_DEBUG_EXCEPTION("no mail found...");
9209 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9212 #ifdef __FEATURE_BODY_SEARCH__
9213 /* Delete all mail_text in mail_text_tbl table based on account id */
9214 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
9215 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9217 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9218 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9219 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9221 rc = sqlite3_changes(local_db_handle);
9223 EM_DEBUG_EXCEPTION("no mail found...");
9224 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9228 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
9229 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
9235 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9238 if (err_code != NULL)
9241 EM_DEBUG_FUNC_END("ret [%d]", ret);
9245 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, int mailbox_id, int transaction, int *err_code)
9247 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
9249 if (account_id < FIRST_ACCOUNT_ID || mailbox_id == 0) {
9250 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%d]", account_id, mailbox_id);
9251 if (err_code != NULL)
9252 *err_code = EMAIL_ERROR_INVALID_PARAM;
9256 int rc, ret = false;
9257 int error = EMAIL_ERROR_NONE;
9258 char sql_query_string[QUERY_SIZE] = {0, };
9260 sqlite3 *local_db_handle = emstorage_get_db_connection();
9262 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9264 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = '%d'", account_id, mailbox_id);
9265 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9267 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9268 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9269 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9272 /* Delete Mails from mail_read_mail_uid_tbl */
9273 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);
9274 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9276 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9277 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9278 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9280 #ifdef __FEATURE_BODY_SEARCH__
9281 /* Delete Mails from mail_text_tbl */
9282 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);
9283 EM_DEBUG_LOG("Query [%s]", sql_query_string);
9285 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9286 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9287 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9290 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, mailbox_id , 0, 0))
9291 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
9296 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9299 if (err_code != NULL)
9302 EM_DEBUG_FUNC_END("ret [%d]", ret);
9306 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
9308 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
9311 if ((mail_list == NULL) || (*mail_list == NULL)) {
9312 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
9315 *err_code = EMAIL_ERROR_INVALID_PARAM;
9319 emstorage_mail_tbl_t* p = *mail_list;
9322 for (; i < count; i++, p++) {
9323 EM_SAFE_FREE(p->server_mailbox_name);
9324 EM_SAFE_FREE(p->server_mail_id);
9325 EM_SAFE_FREE(p->full_address_from);
9326 EM_SAFE_FREE(p->full_address_reply);
9327 EM_SAFE_FREE(p->full_address_to);
9328 EM_SAFE_FREE(p->full_address_cc);
9329 EM_SAFE_FREE(p->full_address_bcc);
9330 EM_SAFE_FREE(p->full_address_return);
9331 EM_SAFE_FREE(p->subject);
9332 EM_SAFE_FREE(p->file_path_plain);
9333 EM_SAFE_FREE(p->file_path_html);
9334 EM_SAFE_FREE(p->file_path_mime_entity);
9335 EM_SAFE_FREE(p->message_id);
9336 EM_SAFE_FREE(p->email_address_sender);
9337 EM_SAFE_FREE(p->email_address_recipient);
9338 EM_SAFE_FREE(p->preview_text);
9339 EM_SAFE_FREE(p->alias_sender);
9340 EM_SAFE_FREE(p->alias_recipient);
9341 EM_SAFE_FREE(p->eas_data);
9343 EM_SAFE_FREE(*mail_list);
9346 if (err_code != NULL)
9347 *err_code = EMAIL_ERROR_NONE;
9349 EM_DEBUG_FUNC_END();
9353 #ifdef __FEATURE_BODY_SEARCH__
9354 INTERNAL_FUNC int emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
9356 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
9359 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
9360 EM_DEBUG_EXCEPTION("mail_text_list[%p], count[%d]", mail_text_list, count);
9363 *err_code = EMAIL_ERROR_INVALID_PARAM;
9367 emstorage_mail_text_tbl_t *p = *mail_text_list;
9370 for (; i < count; i++, p++) {
9371 EM_SAFE_FREE(p->body_text);
9373 EM_SAFE_FREE(*mail_text_list);
9376 if (err_code != NULL)
9377 *err_code = EMAIL_ERROR_NONE;
9379 EM_DEBUG_FUNC_END();
9385 INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
9387 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
9389 if (mail_id <= 0 || !count) {
9390 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
9391 if (err_code != NULL)
9392 *err_code = EMAIL_ERROR_INVALID_PARAM;
9396 int rc = -1, ret = false;
9397 int error = EMAIL_ERROR_NONE;
9398 char sql_query_string[QUERY_SIZE] = {0, };
9400 sqlite3 *local_db_handle = emstorage_get_db_connection();
9401 EMSTORAGE_START_READ_TRANSACTION(transaction);
9403 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9407 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9408 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9409 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9411 *count = atoi(result[1]);
9412 sqlite3_free_table(result);
9418 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9421 if (err_code != NULL)
9424 EM_DEBUG_FUNC_END("ret [%d]", ret);
9428 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)
9430 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);
9432 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
9433 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9434 return EMAIL_ERROR_INVALID_PARAM;
9437 int error = EMAIL_ERROR_NONE;
9440 char **result = NULL;
9441 char sql_query_string[QUERY_SIZE] = {0, };
9442 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9443 DB_STMT hStmt = NULL;
9444 sqlite3 *local_db_handle = emstorage_get_db_connection();
9446 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
9447 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
9448 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9449 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9450 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9452 *output_attachment_count = atoi(result[1]);
9453 sqlite3_free_table(result);
9455 if(*output_attachment_count == 0) {
9456 error = EMAIL_ERROR_NONE;
9460 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
9463 EM_DEBUG_EXCEPTION("em_malloc failed...");
9464 error = EMAIL_ERROR_OUT_OF_MEMORY;
9468 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
9469 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
9471 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9472 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)));
9474 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9475 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9477 if (rc == SQLITE_DONE) {
9478 EM_DEBUG_EXCEPTION("no matched attachment found...");
9479 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9482 for (i = 0; i < *output_attachment_count; i++) {
9483 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9484 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9485 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9486 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9487 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9488 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9489 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9490 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9491 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9492 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9493 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9494 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9496 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
9497 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9498 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9503 if (error == EMAIL_ERROR_NONE)
9504 *output_attachment_list = p_data_tbl;
9505 else if (p_data_tbl != NULL)
9506 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
9509 rc = sqlite3_finalize(hStmt);
9510 if (rc != SQLITE_OK) {
9511 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
9512 error = EMAIL_ERROR_DB_FAILURE;
9516 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
9520 EM_DEBUG_FUNC_END("error [%d]", error);
9524 INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
9526 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
9528 if (attachment_id <= 0 || !attachment) {
9529 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
9530 if (err_code != NULL)
9531 *err_code = EMAIL_ERROR_INVALID_PARAM;
9535 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9536 int rc, ret = false;
9537 int error = EMAIL_ERROR_NONE;
9538 char sql_query_string[QUERY_SIZE] = {0, };
9540 sqlite3 *local_db_handle = emstorage_get_db_connection();
9541 EMSTORAGE_START_READ_TRANSACTION(transaction);
9543 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
9545 sqlite3_stmt* hStmt = NULL;
9547 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9548 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9550 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9551 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9554 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9555 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9556 ("sqlite3_step fail:%d", rc));
9558 if (rc == SQLITE_DONE) {
9559 EM_DEBUG_EXCEPTION("no matched attachment found...");
9560 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9564 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
9565 EM_DEBUG_EXCEPTION("malloc failed...");
9566 error = EMAIL_ERROR_OUT_OF_MEMORY;
9570 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9571 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9572 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9573 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9574 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9575 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9576 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9577 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9578 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9579 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9580 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9581 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9583 #ifdef __ATTACHMENT_OPTI__
9584 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9585 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
9592 *attachment = p_data_tbl;
9594 if (hStmt != NULL) {
9595 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
9597 rc = sqlite3_finalize(hStmt);
9598 if (rc != SQLITE_OK) {
9599 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9600 error = EMAIL_ERROR_DB_FAILURE;
9604 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9607 if (err_code != NULL)
9610 EM_DEBUG_FUNC_END("ret [%d]", ret);
9614 INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
9616 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);
9618 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
9619 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
9621 if (err_code != NULL)
9622 *err_code = EMAIL_ERROR_INVALID_PARAM;
9626 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9628 int rc, ret = false;
9629 int error = EMAIL_ERROR_NONE;
9630 char sql_query_string[QUERY_SIZE] = {0, };
9632 sqlite3 *local_db_handle = emstorage_get_db_connection();
9633 EMSTORAGE_START_READ_TRANSACTION(transaction);
9635 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));
9636 EM_DEBUG_LOG("query = [%s]", sql_query_string);
9638 DB_STMT hStmt = NULL;
9640 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9641 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9642 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9645 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9646 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9647 ("sqlite3_step fail:%d", rc));
9649 if (rc == SQLITE_DONE) {
9650 EM_DEBUG_EXCEPTION(" no matched attachment found...");
9651 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9655 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
9656 EM_DEBUG_EXCEPTION(" malloc failed...");
9657 error = EMAIL_ERROR_OUT_OF_MEMORY;
9661 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9662 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9663 p_data_tbl->attachment_name = cpy_str(p);
9664 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9665 p_data_tbl->attachment_path = cpy_str(p);
9666 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9667 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9668 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9669 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9670 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9671 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9672 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9673 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9674 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9675 p_data_tbl->attachment_mime_type = cpy_str(p);
9676 #ifdef __ATTACHMENT_OPTI__
9677 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9678 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9679 p_data_tbl->section= cpy_str(p);
9685 *attachment_tbl = p_data_tbl;
9687 if (hStmt != NULL) {
9688 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
9690 rc = sqlite3_finalize(hStmt);
9691 if (rc != SQLITE_OK) {
9692 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9693 error = EMAIL_ERROR_DB_FAILURE;
9697 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9700 if (err_code != NULL)
9703 EM_DEBUG_FUNC_END("ret [%d]", ret);
9707 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)
9709 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
9711 if (mail_id <= 0 || !attachment) {
9712 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
9713 if (err_code != NULL)
9714 *err_code = EMAIL_ERROR_INVALID_PARAM;
9718 int rc, ret = false;
9719 int error = EMAIL_ERROR_NONE;
9720 DB_STMT hStmt = NULL;
9721 char sql_query_string[QUERY_SIZE] = {0, };
9725 sqlite3 *local_db_handle = emstorage_get_db_connection();
9727 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9730 case UPDATE_MAILBOX:
9731 EM_DEBUG_LOG("UPDATE_MAILBOX");
9732 if (!attachment->mailbox_id) {
9733 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
9734 error = EMAIL_ERROR_INVALID_PARAM;
9737 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9738 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
9740 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9741 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9742 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9743 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9745 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
9748 case UPDATE_SAVENAME:
9749 EM_DEBUG_LOG("UPDATE_SAVENAME");
9750 if (!attachment->attachment_path) {
9751 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
9752 error = EMAIL_ERROR_INVALID_PARAM;
9756 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9757 "UPDATE mail_attachment_tbl SET"
9758 " attachment_size = ?"
9759 ", attachment_save_status = 1"
9760 ", attachment_path = ?"
9761 " WHERE mail_id = %d"
9762 " AND attachment_id = %d"
9763 , attachment->mail_id
9764 , attachment->attachment_id);
9767 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9768 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9769 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9770 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9772 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
9773 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9777 EM_DEBUG_LOG("type[%d]", type);
9778 error = EMAIL_ERROR_INVALID_PARAM;
9781 EM_DEBUG_LOG("query = [%s]", sql_query_string);
9783 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9784 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9785 ("sqlite3_step fail:%d", rc));
9789 if (hStmt != NULL) {
9790 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
9791 rc = sqlite3_finalize(hStmt);
9792 if (rc != SQLITE_OK) {
9793 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
9794 error = EMAIL_ERROR_DB_FAILURE;
9798 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9801 if (err_code != NULL)
9803 EM_DEBUG_FUNC_END("ret [%d]", ret);
9808 INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, int input_transaction)
9810 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);
9814 int error = EMAIL_ERROR_NONE;
9815 char sql_query_string[QUERY_SIZE] = {0, };
9816 sqlite3 *local_db_handle = NULL;
9818 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
9819 char *replaced_mailbox_name = NULL;
9820 char *replaced_alias = NULL;
9822 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
9823 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9824 return EMAIL_ERROR_INVALID_PARAM;
9827 if (strstr(input_new_mailbox_name, "'")) {
9828 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
9830 replaced_mailbox_name = strdup(input_new_mailbox_name);
9833 if (strstr(input_new_mailbox_alias, "'")) {
9834 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
9836 replaced_alias = strdup(input_new_mailbox_alias);
9839 local_db_handle = emstorage_get_db_connection();
9841 if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
9842 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
9845 account_id = old_mailbox_data->account_id;
9847 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
9849 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9850 "UPDATE mail_box_tbl SET"
9851 " mailbox_name = '%s'"
9853 " WHERE mailbox_id = %d"
9854 , replaced_mailbox_name
9856 , input_mailbox_id);
9858 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9860 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9861 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9863 if (sqlite3_changes(local_db_handle) == 0)
9864 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
9870 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
9873 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9874 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
9877 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
9878 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
9881 EM_SAFE_FREE(replaced_mailbox_name);
9882 EM_SAFE_FREE(replaced_alias);
9884 if (old_mailbox_data)
9885 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
9889 EM_DEBUG_FUNC_END("error [%d]", error);
9893 INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
9895 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
9896 int rc, ret = false;
9897 int error = EMAIL_ERROR_NONE;
9898 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9901 if (!attachment_no) {
9902 EM_DEBUG_EXCEPTION("Invalid attachment");
9903 error = EMAIL_ERROR_INVALID_PARAM;
9907 *attachment_no = -1;
9909 sqlite3 *local_db_handle = emstorage_get_db_connection();
9912 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9913 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9914 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9916 if (NULL == result[1])
9919 rc = atoi(result[1])+1;
9921 sqlite3_free_table(result);
9923 *attachment_no = rc;
9924 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
9930 if (err_code != NULL)
9933 EM_DEBUG_FUNC_END("ret [%d]", ret);
9937 INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
9939 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
9943 int rc, ret = false;
9944 int error = EMAIL_ERROR_NONE;
9945 DB_STMT hStmt = NULL;
9946 char sql_query_string[QUERY_SIZE] = {0, };
9947 sqlite3 *local_db_handle = emstorage_get_db_connection();
9949 if (!attachment_tbl) {
9950 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
9951 if (err_code != NULL)
9952 *err_code = EMAIL_ERROR_INVALID_PARAM;
9956 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9958 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9960 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9961 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9962 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9964 if (NULL==result[1]) rc = 1;
9965 else rc = atoi(result[1]) + 1;
9966 sqlite3_free_table(result);
9968 attachment_tbl->attachment_id = rc;
9970 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9971 "INSERT INTO mail_attachment_tbl VALUES "
9972 "( ?" /* attachment_id */
9973 ", ?" /* attachment_name */
9974 ", ?" /* attachment_path */
9975 ", ?" /* attachment_size */
9977 ", ?" /* account_id */
9978 ", ?" /* mailbox_id */
9979 ", ?" /* attachment_save_status */
9980 ", ?" /* attachment_drm_type */
9981 ", ?" /* attachment_drm_method */
9982 ", ?" /* attachment_inline_content_status */
9983 ", ?" /* attachment_mime_type */
9984 #ifdef __ATTACHMENT_OPTI__
9991 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9992 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9993 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9995 _bind_stmt_field_data_int (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
9996 _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);
9997 _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);
9998 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
9999 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
10000 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
10001 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
10002 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
10003 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
10004 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
10005 _bind_stmt_field_data_int (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
10006 _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);
10007 #ifdef __ATTACHMENT_OPTI__
10008 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
10009 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
10013 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10014 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10015 ("sqlite3_step fail:%d", rc));
10016 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10017 ("sqlite3_step fail:%d", rc));
10019 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10020 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
10022 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10024 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10025 ("sqlite3_exec fail:%d", rc));
10026 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10027 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10029 rc = sqlite3_changes(local_db_handle);
10031 EM_DEBUG_EXCEPTION(" no matched mail found...");
10032 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10039 if (hStmt != NULL) {
10040 rc = sqlite3_finalize(hStmt);
10041 if (rc != SQLITE_OK) {
10042 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
10043 error = EMAIL_ERROR_DB_FAILURE;
10047 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10050 if (err_code != NULL)
10053 EM_DEBUG_FUNC_END("ret [%d]", ret);
10057 INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
10059 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
10061 int rc, ret = false, field_idx = 0;
10062 int error = EMAIL_ERROR_NONE;
10063 DB_STMT hStmt = NULL;
10064 char sql_query_string[QUERY_SIZE] = {0, };
10066 if (!attachment_tbl) {
10067 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
10068 error = EMAIL_ERROR_INVALID_PARAM;
10072 sqlite3 *local_db_handle = emstorage_get_db_connection();
10074 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10076 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10077 "UPDATE mail_attachment_tbl SET "
10078 " attachment_name = ?"
10079 ", attachment_path = ?"
10080 ", attachment_size = ?"
10084 ", attachment_save_status = ?"
10085 ", attachment_drm_type = ?"
10086 ", attachment_drm_method = ?"
10087 ", attachment_inline_content_status = ? "
10088 ", attachment_mime_type = ? "
10089 " WHERE attachment_id = ?;");
10092 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10094 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10095 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10097 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10098 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10099 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_size);
10100 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mail_id);
10101 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->account_id);
10102 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mailbox_id);
10103 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
10104 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
10105 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
10106 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
10107 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
10108 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_id);
10111 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10112 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10113 ("sqlite3_step fail:%d", rc));
10114 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10115 ("sqlite3_step fail:%d", rc));
10117 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10118 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
10120 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10122 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10123 ("sqlite3_exec fail:%d", rc));
10124 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10125 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10127 rc = sqlite3_changes(local_db_handle);
10129 EM_DEBUG_EXCEPTION(" no matched mail found...");
10130 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10137 if (hStmt != NULL) {
10138 rc = sqlite3_finalize(hStmt);
10139 if (rc != SQLITE_OK) {
10140 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
10141 error = EMAIL_ERROR_DB_FAILURE;
10145 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10148 if (err_code != NULL)
10151 EM_DEBUG_FUNC_END("ret [%d]", ret);
10155 INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
10157 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
10159 if (attachment_id < 0) {
10160 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
10161 if (err_code != NULL)
10162 *err_code = EMAIL_ERROR_INVALID_PARAM;
10166 int rc, ret = false;
10167 int error = EMAIL_ERROR_NONE;
10168 char sql_query_string[QUERY_SIZE] = {0, };
10170 sqlite3 *local_db_handle = emstorage_get_db_connection();
10172 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10174 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10176 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10177 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10178 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10183 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10189 EM_DEBUG_FUNC_END("ret [%d]", ret);
10193 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
10195 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10196 int rc, ret = false;
10197 int error = EMAIL_ERROR_NONE;
10198 char sql_query_string[QUERY_SIZE] = {0, };
10199 sqlite3 *local_db_handle = NULL;
10201 if (mail_id <= 0) {
10202 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10203 if (err_code != NULL)
10204 *err_code = EMAIL_ERROR_INVALID_PARAM;
10208 local_db_handle = emstorage_get_db_connection();
10210 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10212 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10214 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10215 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10216 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10221 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10227 EM_DEBUG_FUNC_END("ret [%d]", ret);
10231 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
10233 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
10235 int error = EMAIL_ERROR_NONE;
10236 int rc, ret = false;
10237 char sql_query_string[QUERY_SIZE] = {0, };
10238 char *replaced_mailbox = NULL;
10240 sqlite3 *local_db_handle = emstorage_get_db_connection();
10242 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10244 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
10246 if (account_id != ALL_ACCOUNT) /* '0' means all account */
10247 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);
10249 if (mailbox) /* NULL means all mailbox_name */ {
10250 if (strstr(mailbox, "'")) {
10251 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
10253 replaced_mailbox = strdup(mailbox);
10256 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);
10257 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
10259 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10260 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10261 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10266 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10269 if (err_code != NULL)
10272 EM_DEBUG_FUNC_END("ret [%d]", ret);
10276 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
10278 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
10281 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
10282 EM_DEBUG_EXCEPTION(" attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
10283 if (err_code != NULL)
10284 *err_code = EMAIL_ERROR_INVALID_PARAM;
10288 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
10291 for (i = 0; i < count; i++) {
10292 EM_SAFE_FREE(p[i].attachment_name);
10293 EM_SAFE_FREE(p[i].attachment_path);
10294 EM_SAFE_FREE(p[i].attachment_mime_type);
10295 #ifdef __ATTACHMENT_OPTI__
10296 EM_SAFE_FREE(p[i].section);
10301 *attachment_tbl_list = NULL;
10304 if (err_code != NULL)
10305 *err_code = EMAIL_ERROR_NONE;
10306 EM_DEBUG_FUNC_END();
10312 INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
10314 EM_PROFILE_BEGIN(emStorageBeginTransaction);
10317 ENTER_CRITICAL_SECTION(_transactionBeginLock);
10319 /* wait for the trnasaction authority to be changed. */
10320 while (g_transaction) {
10321 EM_DEBUG_LOG(">>>>>>>> Wait for the transaction authority to be changed");
10325 /* take the transaction authority. */
10326 g_transaction = true;
10328 LEAVE_CRITICAL_SECTION(_transactionBeginLock);
10330 sqlite3 *local_db_handle = emstorage_get_db_connection();
10332 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
10333 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10334 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10336 if (ret == false) {
10337 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
10338 ENTER_CRITICAL_SECTION(_transactionEndLock);
10339 g_transaction = false;
10340 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10343 EM_PROFILE_END(emStorageBeginTransaction);
10344 EM_DEBUG_FUNC_END("ret [%d]", ret);
10348 INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
10350 EM_DEBUG_FUNC_BEGIN();
10352 sqlite3 *local_db_handle = emstorage_get_db_connection();
10354 ENTER_CRITICAL_SECTION(_transactionEndLock);
10357 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
10358 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10359 /* release the transaction authority. */
10360 g_transaction = false;
10362 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10363 if (ret == false && err_code != NULL)
10364 *err_code = EMAIL_ERROR_DB_FAILURE;
10366 EM_DEBUG_FUNC_END("ret [%d]", ret);
10370 INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
10372 EM_DEBUG_FUNC_BEGIN();
10374 sqlite3 *local_db_handle = emstorage_get_db_connection();
10377 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
10378 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10379 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10381 ENTER_CRITICAL_SECTION(_transactionEndLock);
10383 /* release the transaction authority. */
10384 g_transaction = false;
10386 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10388 if (ret == false && err_code != NULL)
10389 *err_code = EMAIL_ERROR_DB_FAILURE;
10391 EM_DEBUG_FUNC_END("ret [%d]", ret);
10395 INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
10397 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
10399 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
10401 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
10403 if (err_code != NULL)
10404 *err_code = EMAIL_ERROR_INVALID_PARAM;
10410 int error = EMAIL_ERROR_NONE;
10411 int mail_count = 0;
10413 if (!emstorage_get_mail_count(account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
10414 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
10419 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
10420 if (mail_count >= mailbox->mail_slot_size)
10431 if (err_code != NULL)
10434 EM_DEBUG_FUNC_END("ret [%d]", ret);
10438 INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
10440 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
10442 int rc, ret = false;
10443 int error = EMAIL_ERROR_NONE;
10444 char sql_query_string[QUERY_SIZE] = {0, };
10446 const email_db_object_t* tables = _g_db_tables;
10447 const email_db_object_t* indexes = _g_db_indexes;
10449 sqlite3 *local_db_handle = emstorage_get_db_connection();
10450 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10452 if (!emstorage_delete_dir(MAILHOME, &error)) {
10453 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
10458 mkdir(MAILHOME, DIRECTORY_PERMISSION);
10459 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10461 /* first clear index. */
10462 while (indexes->object_name) {
10463 if (indexes->data_flag) {
10464 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
10465 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10466 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10467 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10472 while (tables->object_name) {
10473 if (tables->data_flag) {
10474 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
10475 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10476 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10477 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10485 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10488 if (err_code != NULL)
10491 EM_DEBUG_FUNC_END("ret [%d]", ret);
10494 /*======================= DB File Utils =============================================*/
10495 #include <dirent.h>
10496 #include <sys/types.h>
10497 #define DIR_SEPERATOR "/"
10499 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
10501 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
10502 char delims[] = "/";
10503 char *result = NULL;
10504 gchar **token = NULL;
10506 token = g_strsplit_set(file_name, delims, 1);
10508 if (token && token[0]) {
10509 EM_DEBUG_LOG(">>>> Directory_name [ %s ]", token[0]);
10510 result = EM_SAFE_STRDUP(token[0]);
10512 EM_DEBUG_LOG(">>>> No Need to create Directory");
10519 INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10521 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);
10522 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
10525 int error = EMAIL_ERROR_NONE;
10526 char *dir_name = NULL;
10527 char create_dir[1024]={0};
10528 char *temp_file = NULL;
10530 if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
10531 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);
10532 error = EMAIL_ERROR_INVALID_PARAM;
10536 sprintf(name_buf, "%s", MAILHOME);
10537 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, account_id);
10540 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10543 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10546 temp_file = EM_SAFE_STRDUP(fname);
10547 if (strstr(temp_file, "/")) {
10548 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
10553 sprintf(create_dir, "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
10554 EM_DEBUG_LOG(">>>>> DIR PATH [ %s ]", create_dir);
10555 mkdir(create_dir, DIRECTORY_PERMISSION);
10556 EM_SAFE_FREE(dir_name);
10560 EM_DEBUG_LOG(">>>>> fname [ %s ]", fname);
10561 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s", DIR_SEPERATOR, fname);
10564 EM_DEBUG_LOG(">>>>> name_buf [ %s ]", name_buf);
10569 EM_SAFE_FREE(temp_file);
10571 if (err_code != NULL)
10574 EM_PROFILE_END(profile_emstorage_get_save_name);
10575 EM_DEBUG_FUNC_END("ret [%d]", ret);
10579 INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10581 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);
10583 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
10584 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);
10585 if (err_code != NULL)
10586 *err_code = EMAIL_ERROR_INVALID_PARAM;
10590 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10593 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10598 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10603 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
10605 EM_DEBUG_FUNC_END();
10609 INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
10611 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
10612 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
10614 int error = EMAIL_ERROR_NONE;
10618 if (account_id >= FIRST_ACCOUNT_ID) {
10619 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10621 if (stat(buf, &sbuf) == 0) {
10622 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10623 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10624 error = EMAIL_ERROR_SYSTEM_FAILURE;
10629 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10630 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10631 EM_DEBUG_EXCEPTION("mkdir failed l(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10632 error = EMAIL_ERROR_SYSTEM_FAILURE;
10639 if (account_id < FIRST_ACCOUNT_ID) {
10640 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10641 error = EMAIL_ERROR_INVALID_PARAM;
10645 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf), "%s%d", DIR_SEPERATOR, mail_id);
10647 if (stat(buf, &sbuf) == 0) {
10648 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10649 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10651 error = EMAIL_ERROR_SYSTEM_FAILURE;
10656 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10657 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10658 EM_DEBUG_EXCEPTION("mkdir failed l (Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10659 error = EMAIL_ERROR_SYSTEM_FAILURE;
10666 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
10667 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10669 error = EMAIL_ERROR_INVALID_PARAM;
10673 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
10675 if (stat(buf, &sbuf) == 0) {
10676 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10677 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10679 error = EMAIL_ERROR_SYSTEM_FAILURE;
10684 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10685 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10686 error = EMAIL_ERROR_SYSTEM_FAILURE;
10695 if (err_code != NULL)
10698 EM_PROFILE_END(profile_emcore_save_create_dir);
10699 EM_DEBUG_FUNC_END("ret [%d]", ret);
10703 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10705 EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
10706 EM_DEBUG_LOG("Using the fsync function");
10708 int error = EMAIL_ERROR_NONE;
10709 struct stat st_buf;
10718 if (!src_file || !dst_file) {
10719 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10721 error = EMAIL_ERROR_INVALID_PARAM;
10725 if (stat(src_file, &st_buf) < 0) {
10726 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
10728 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
10732 buf_size = st_buf.st_size;
10733 EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
10734 buf = (char *)calloc(1, buf_size+1);
10737 EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
10738 error = EMAIL_ERROR_OUT_OF_MEMORY;
10743 if ((fp_src = open(src_file, O_RDONLY))<0) { /*prevent 24474*/
10744 EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed [ %d ] - Error [ %s ]", src_file, errno, strerror(errno));
10745 error = EMAIL_ERROR_SYSTEM_FAILURE;
10749 if ((fp_dst = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) { /*prevent 24474*/
10750 EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s Failed [ %d ] - Error [ %s ]", dst_file, errno, strerror(errno));
10751 error = EMAIL_ERROR_SYSTEM_FAILURE;
10755 while ((nread = read(fp_src, buf, buf_size)) > 0) {
10756 if (nread > 0 && nread <= buf_size) {
10757 EM_DEBUG_LOG("Nread Value [%d]", nread);
10758 if ((nwritten = write(fp_dst, buf, nread)) != nread) {
10759 EM_DEBUG_EXCEPTION("fwrite failed...[%d] : [%s]", errno, strerror(errno));
10760 error = EMAIL_ERROR_UNKNOWN;
10763 EM_DEBUG_LOG("NWRITTEN [%d]", nwritten);
10771 if (fp_src>0) /*prevent 24474*/
10774 if (fp_dst>0) { /*prevent 24474*/
10776 EM_DEBUG_LOG("Before fsync");
10782 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
10785 if (err_code != NULL)
10787 EM_DEBUG_FUNC_END("ret [%d]", ret);
10790 /* create Directory if user has deleted [deepam.p@samsung.com] */
10791 INTERNAL_FUNC void emstorage_create_dir_if_delete()
10793 EM_DEBUG_FUNC_BEGIN();
10795 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
10796 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
10797 mkdir(MAILHOME, DIRECTORY_PERMISSION);
10798 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10800 EM_DEBUG_FUNC_END();
10802 static int _get_temp_file_name(char **filename, int *err_code)
10804 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
10807 int error = EMAIL_ERROR_NONE;
10809 if (filename == NULL) {
10810 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
10811 error = EMAIL_ERROR_INVALID_PARAM;
10815 char tempname[512] = {0x00, };
10818 gettimeofday(&tv, NULL);
10821 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
10823 char *p = EM_SAFE_STRDUP(tempname);
10825 EM_DEBUG_EXCEPTION(" strdup failed...");
10826 error = EMAIL_ERROR_OUT_OF_MEMORY;
10835 if (err_code != NULL)
10838 EM_DEBUG_FUNC_END("ret [%d]", ret);
10842 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
10844 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
10846 EM_IF_NULL_RETURN_VALUE(file_path, false);
10847 EM_IF_NULL_RETURN_VALUE(char_set, false);
10848 EM_IF_NULL_RETURN_VALUE(err_code, false);
10852 struct stat st_buf;
10854 char *low_char_set = NULL;
10855 char *match_str = NULL;
10858 int error = EMAIL_ERROR_NONE;
10859 int data_count_to_written = 0;
10860 char *temp_file_name = NULL;
10863 FILE* fp_src = NULL;
10864 FILE* fp_dest = NULL;
10868 if (stat(file_path, &st_buf) < 0) {
10869 EM_DEBUG_EXCEPTION(" stat(\"%s\") failed...", file_path);
10871 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
10875 buf_size = st_buf.st_size;
10877 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
10879 buf = (char *)calloc(1, buf_size+1);
10882 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
10886 if (!(fp_src = fopen(file_path, "rb"))) {
10887 EM_DEBUG_EXCEPTION(" file_path fopen failed - %s", file_path);
10889 error = EMAIL_ERROR_SYSTEM_FAILURE;
10893 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
10894 if (nread > 0 && nread <= buf_size) {
10895 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
10898 * 1.Add check for whether content type is there.
10899 * 2. If not based on the character set, Append it in File
10902 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
10904 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
10906 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
10908 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
10910 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
10912 EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
10914 match_str = strstr(buf, CONTENT_TYPE_DATA);
10915 EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
10917 if (match_str == NULL) {
10918 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
10919 if (fp_src !=NULL) {
10920 fclose(fp_src);fp_src = NULL;
10922 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
10923 EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
10924 buf1 = (char *)calloc(1, data_count_to_written);
10925 EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
10928 EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
10929 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
10931 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
10933 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
10935 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
10938 /* 1. Create a temporary file name */
10939 if (!_get_temp_file_name(&temp_file_name, &err)) {
10940 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
10941 if (err_code != NULL) *err_code = err;
10942 EM_SAFE_FREE(temp_file_name);
10945 EM_DEBUG_LOG(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
10947 /* Open the Temp file in Append mode */
10948 if (!(fp_dest = fopen(temp_file_name, "ab"))) {
10949 EM_DEBUG_EXCEPTION(" fopen failed - %s", temp_file_name);
10950 error = EMAIL_ERROR_SYSTEM_FAILURE;
10954 /* 2. write the Latest data */
10955 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
10957 if (nwritten > 0) {
10958 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
10960 /* 3. Append old data */
10961 nwritten = fwrite(buf, nread-1, 1, fp_dest);
10963 if (nwritten <= 0) {
10964 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
10965 error = EMAIL_ERROR_SYSTEM_FAILURE;
10969 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
10971 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
10972 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
10979 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
10980 error = EMAIL_ERROR_SYSTEM_FAILURE;
10987 EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
10997 EM_SAFE_FREE(buf1);
10998 EM_SAFE_FREE(low_char_set);
11000 if (fp_src != NULL) {
11005 if (fp_dest != NULL) {
11009 EM_DEBUG_FUNC_END("ret [%d]", ret);
11014 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11016 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
11019 int error = EMAIL_ERROR_NONE;
11021 if (src_file == NULL || dst_file == NULL) {
11022 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11023 error = EMAIL_ERROR_INVALID_PARAM;
11027 EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
11029 if (strcmp(src_file, dst_file) != 0) {
11030 if (rename(src_file, dst_file) != 0) {
11031 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() */
11032 /* does not work across different mount points, even if the same file system is mounted on both.) */
11033 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
11034 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
11035 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
11039 EM_DEBUG_LOG("src[%s] removed", src_file);
11043 if (errno == ENOENT) {
11044 struct stat temp_file_stat;
11045 if (stat(src_file, &temp_file_stat) < 0)
11046 EM_DEBUG_EXCEPTION("no src file found [%s]", src_file);
11047 if (stat(dst_file, &temp_file_stat) < 0)
11048 EM_DEBUG_EXCEPTION("no dst file found [%s]", src_file);
11050 EM_DEBUG_EXCEPTION("no file found [%d]", errno);
11051 error = EMAIL_ERROR_FILE_NOT_FOUND;
11056 EM_DEBUG_EXCEPTION("rename failed [%d]", errno);
11057 error = EMAIL_ERROR_SYSTEM_FAILURE;
11064 EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
11070 if (err_code != NULL)
11073 EM_DEBUG_FUNC_END("ret [%d]", ret);
11077 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
11079 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
11082 int error = EMAIL_ERROR_NONE;
11084 if (src_file == NULL) {
11085 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
11087 error = EMAIL_ERROR_INVALID_PARAM;
11091 if (remove(src_file) != 0) {
11092 if (errno != ENOENT) {
11093 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
11095 error = EMAIL_ERROR_SYSTEM_FAILURE;
11099 EM_DEBUG_EXCEPTION(" no file found...");
11101 error = EMAIL_ERROR_FILE_NOT_FOUND;
11108 if (err_code != NULL)
11111 EM_DEBUG_FUNC_END("ret [%d]", ret);
11116 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
11118 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
11120 if (src_dir == NULL) {
11121 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
11123 if (err_code != NULL)
11124 *err_code = EMAIL_ERROR_INVALID_PARAM;
11128 int error = EMAIL_ERROR_NONE;
11135 dirp = opendir(src_dir);
11137 if (dirp == NULL) {
11138 if (errno == ENOENT) {
11139 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
11140 if (err_code != NULL)
11141 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11145 EM_DEBUG_EXCEPTION("opendir failed - %d", errno);
11146 if (err_code != NULL)
11147 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11152 while ((dp=readdir(dirp))) {
11153 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
11156 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
11158 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
11159 /* check directory */
11160 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
11161 /* recursive call */
11162 if (!emstorage_delete_dir(buf, &error)) {
11164 if (err_code != NULL)
11170 if (remove(buf) < 0) {
11171 EM_DEBUG_EXCEPTION("remove failed - %s", buf);
11173 if (err_code != NULL)
11174 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11180 EM_DEBUG_EXCEPTION("content does not exist...");
11185 EM_DEBUG_LOG("remove direcotory [%s]", src_dir);
11187 /* EM_DEBUG_FUNC_BEGIN(); */
11189 if (remove(src_dir) < 0) {
11190 EM_DEBUG_EXCEPTION("remove failed [%s]", src_dir);
11191 if (err_code != NULL)
11192 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11196 if (err_code != NULL)
11202 /* faizan.h@samsung.com */
11203 INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
11205 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
11206 int rc, ret = false;
11207 int error = EMAIL_ERROR_NONE;
11208 char sql_query_string[QUERY_SIZE] = {0, };
11210 int transaction = true;
11212 if (!old_server_uid || !new_server_uid) {
11213 EM_DEBUG_EXCEPTION("Invalid parameters");
11214 error = EMAIL_ERROR_INVALID_PARAM;
11217 sqlite3 *local_db_handle = emstorage_get_db_connection();
11219 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11221 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11222 "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
11223 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
11225 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11226 ("sqlite3_exec fail:%d", rc));
11227 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11228 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11233 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11236 if (err_code != NULL)
11239 EM_DEBUG_FUNC_END("ret [%d]", ret);
11244 INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
11246 EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
11248 int rc, ret = false;
11249 int error = EMAIL_ERROR_NONE;
11250 char sql_query_string[QUERY_SIZE] = {0, };
11252 int transaction = true;
11254 if (!mail_id || !new_server_uid || !mbox_name) {
11255 EM_DEBUG_EXCEPTION("Invalid parameters");
11256 error = EMAIL_ERROR_INVALID_PARAM;
11259 sqlite3 *local_db_handle = emstorage_get_db_connection();
11260 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11263 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11264 "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);
11265 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
11266 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11267 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11272 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11275 if (err_code != NULL)
11278 EM_DEBUG_FUNC_END("ret [%d]", ret);
11284 int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
11286 EM_DEBUG_FUNC_BEGIN();
11288 if ((!mail_id) ||(account_id <= 0 && account_id != -1)) {
11289 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
11290 if (err_code != NULL)
11291 *err_code = EMAIL_ERROR_INVALID_PARAM;
11297 int error = EMAIL_ERROR_NONE;
11298 DB_STMT hStmt = NULL;
11301 int transaction = false;
11302 char sql_query_string[QUERY_SIZE] = {0, };
11304 sqlite3 *local_db_handle = emstorage_get_db_connection();
11305 EMSTORAGE_START_READ_TRANSACTION(transaction);
11307 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11309 if (account_id == -1)
11310 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
11312 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);
11314 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11315 EM_DEBUG_LOG(" sqlite3_prepare hStmt = %p", hStmt);
11316 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11317 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11319 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11320 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11321 ("sqlite3_step fail:%d", rc));
11325 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11326 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11327 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11329 sqlite3_free_table(result);
11331 EM_DEBUG_EXCEPTION("no Mails found...");
11333 error= EMAIL_ERROR_MAIL_NOT_FOUND;
11337 _get_stmt_field_data_int(hStmt, &mailid, 0);
11338 EM_DEBUG_LOG("mailid [%d]", mailid);
11343 if (hStmt != NULL) {
11344 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11346 rc = sqlite3_finalize(hStmt);
11347 if (rc != SQLITE_OK) {
11348 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11350 error = EMAIL_ERROR_DB_FAILURE;
11353 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11356 if (mail_id != NULL)
11359 if (err_code != NULL)
11362 EM_DEBUG_FUNC_END("ret [%d]", ret);
11366 int setting_system_command(const char *command)
11369 int pid = 0, status = 0;
11370 char *const environ[] = { NULL };
11385 argv[2] = (char *)command;
11388 execve("/bin/sh", argv, environ);
11392 if (waitpid(pid, &status, 0) == -1) {
11393 if (errno != EINTR)
11405 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
11407 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
11409 if (!total_usage) {
11410 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
11412 if (err_code != NULL)
11413 *err_code = EMAIL_ERROR_INVALID_PARAM;
11418 int error = EMAIL_ERROR_NONE;
11419 char syscmd[256] = {0, };
11420 char line[256] = {0, };
11421 char *line_from_file = NULL;
11423 unsigned long total_diskusage = 0;
11425 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
11426 if (setting_system_command(syscmd) == -1) {
11427 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11429 error = EMAIL_ERROR_SYSTEM_FAILURE;
11433 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
11434 EM_DEBUG_LOG(" cmd : %s", syscmd);
11435 if (setting_system_command(syscmd) == -1) {
11436 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
11438 error = EMAIL_ERROR_SYSTEM_FAILURE;
11442 fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
11444 perror(SETTING_MEMORY_TEMP_FILE_PATH);
11446 error = EMAIL_ERROR_SYSTEM_FAILURE;
11450 line_from_file = fgets(line, sizeof(line), fp);
11452 if(line_from_file == NULL) {
11453 EM_DEBUG_EXCEPTION("fgets failed");
11454 error = EMAIL_ERROR_SYSTEM_FAILURE;
11457 total_diskusage = strtoul(line, NULL, 10);
11459 memset(syscmd, 0, sizeof(syscmd));
11460 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
11461 if (setting_system_command(syscmd) == -1) {
11462 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11463 error = EMAIL_ERROR_SYSTEM_FAILURE;
11467 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
11472 if (err_code != NULL)
11476 *total_usage = total_diskusage;
11480 if(fp) fclose(fp); /* prevent 32730 */
11482 EM_DEBUG_FUNC_END("ret [%d]", ret);
11487 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)
11489 DB_STMT hStmt = NULL;
11491 int error = EMAIL_ERROR_NONE;
11493 char sql_query_string[QUERY_SIZE] = {0, };
11495 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11496 "INSERT INTO mail_tbl VALUES "
11497 "( ?" /* mail_id */
11498 ", ?" /* account_id */
11499 ", ?" /* mail_size */
11500 ", ?" /* server_mail_status */
11501 ", ?" /* server_mailbox_name */
11502 ", ?" /* server_mail_id */
11503 ", ?" /* reference_mail_id */
11504 ", ?" /* full_address_from */
11505 ", ?" /* full_address_reply */
11506 ", ?" /* full_address_to */
11507 ", ?" /* full_address_cc */
11508 ", ?" /* full_address_bcc */
11509 ", ?" /* full_address_return */
11510 ", ?" /* subject */
11511 ", ?" /* body_download_status */
11512 ", ?" /* file_path_plain */
11513 ", ?" /* file_path_html */
11514 ", ?" /* date_time */
11515 ", ?" /* flags_seen_field */
11516 ", ?" /* flags_deleted_field */
11517 ", ?" /* flags_flagged_field */
11518 ", ?" /* flags_answered_field */
11519 ", ?" /* flags_recent_field */
11520 ", ?" /* flags_draft_field */
11521 ", ?" /* flags_forwarded_field */
11522 ", ?" /* DRM_status */
11523 ", ?" /* priority */
11524 ", ?" /* save_status */
11525 ", ?" /* lock_status */
11526 ", ?" /* message_id */
11527 ", ?" /* report_status */
11528 ", ?" /* email_address_sender */
11529 ", ?" /* email_address_recipient */
11530 ", ?" /* attachment_count */
11531 ", ?" /* inline_content_count */
11532 ", ?" /* preview_text */
11533 ", ?" /* thread_id */
11534 ", ?" /* mailbox_type */
11535 ", ?" /* alias_sender */
11536 ", ?" /* alias_recipient */
11537 ", ?" /* thread_item_count */
11538 ", ?" /* meeting_request_status */
11539 ", ?" /* message_class */
11540 ", ?" /* digest_type */
11541 ", ?" /* smime_type */
11542 ", ?" /* scheduled_sending_time */
11543 ", ?" /* eas_data_length */
11544 ", ?" /* eas_data */
11547 int transaction = true;
11548 sqlite3 *local_db_handle = emstorage_get_db_connection();
11550 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11553 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11554 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11555 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11557 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
11558 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
11559 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
11560 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
11561 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
11562 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
11563 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
11564 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
11565 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
11566 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
11567 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
11568 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
11569 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
11570 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
11571 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
11572 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
11573 _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);
11574 _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);
11575 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11576 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11577 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
11578 _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);
11579 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
11580 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
11581 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
11582 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
11583 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
11584 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
11585 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
11586 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
11587 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
11588 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
11589 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
11590 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
11591 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
11592 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
11593 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11594 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
11595 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11596 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
11597 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
11598 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
11599 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
11600 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
11601 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
11602 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
11603 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
11604 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
11606 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11607 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11608 ("sqlite3_step fail:%d", rc));
11609 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11610 ("sqlite3_step fail:%d", rc));
11614 if (hStmt != NULL) {
11615 rc = sqlite3_finalize(hStmt);
11616 if (rc != SQLITE_OK) {
11617 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11618 error = EMAIL_ERROR_DB_FAILURE;
11622 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11625 if (err_code != NULL)
11628 EM_DEBUG_FUNC_END("ret [%d]", ret);
11633 INTERNAL_FUNC int emstorage_get_max_mail_count()
11635 return EMAIL_MAIL_MAX_COUNT;
11638 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
11640 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)
11642 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);
11643 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
11644 int rc = 0, query_size = 0, query_size_account = 0;
11645 int account_id = 0;
11646 int err_code = EMAIL_ERROR_NONE;
11647 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
11648 time_t latest_date_time = 0;
11649 time_t date_time = 0;
11650 char *subject = NULL;
11651 char *sql_query_string = NULL, *sql_account = NULL;
11652 char *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
11653 char *sql_format_account = " AND account_id = %d ";
11654 char *sql_format_order_by = " ORDER BY date_time DESC ";
11655 char **result = NULL;
11656 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
11657 sqlite3 *local_db_handle = emstorage_get_db_connection();
11659 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
11660 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
11661 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
11662 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
11664 account_id = mail_tbl->account_id;
11665 subject = mail_tbl->subject;
11666 date_time = mail_tbl->date_time;
11668 EM_DEBUG_LOG("subject : %s", subject);
11670 if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
11671 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
11672 err_code = EMAIL_ERROR_UNKNOWN;
11673 result_thread_id = -1;
11677 if (EM_SAFE_STRLEN(stripped_subject) < 2) {
11678 result_thread_id = -1;
11682 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
11684 if (account_id > 0) {
11685 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
11686 sql_account = malloc(query_size_account);
11687 if (sql_account == NULL) {
11688 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
11689 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
11692 snprintf(sql_account, query_size_account, sql_format_account, account_id);
11695 /* prevent 34362 */
11696 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
11697 sql_query_string = malloc(query_size);
11699 if (sql_query_string == NULL) {
11700 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
11701 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
11705 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
11707 if (account_id > 0)
11708 strcat(sql_query_string, sql_account);
11710 strcat(sql_query_string, sql_format_order_by);
11711 strcat(sql_query_string, ";");
11713 EM_DEBUG_LOG("Query : %s", sql_query_string);
11715 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11717 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11718 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11720 EM_DEBUG_LOG("Result rows count : %d", count);
11723 result_thread_id = -1;
11725 _get_table_field_data_int (result, &result_thread_id, 3);
11726 _get_table_field_data_time_t(result, &latest_date_time, 4);
11727 _get_table_field_data_int (result, &latest_mail_id_in_thread, 5);
11729 if (latest_date_time < mail_tbl->date_time)
11730 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
11732 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
11733 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
11737 *thread_id = result_thread_id;
11738 *thread_item_count = count;
11740 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
11741 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
11742 EM_DEBUG_LOG("err_code : %d", err_code);
11744 EM_SAFE_FREE(sql_account);
11745 EM_SAFE_FREE(sql_query_string);
11747 sqlite3_free_table(result);
11749 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
11755 INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
11757 EM_DEBUG_FUNC_BEGIN();
11759 int count = 0, ret = false;
11760 int error = EMAIL_ERROR_NONE;
11761 emstorage_mail_tbl_t *p_data_tbl = NULL;
11762 char conditional_clause[QUERY_SIZE] = {0, };
11764 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
11766 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
11767 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
11769 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
11770 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
11775 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
11781 *mail_tbl = p_data_tbl;
11782 else if (p_data_tbl != NULL)
11783 emstorage_free_mail(&p_data_tbl, 1, NULL);
11785 if (err_code != NULL)
11788 EM_DEBUG_FUNC_END("ret [%d]", ret);
11793 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)
11795 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]"
11796 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
11798 if ((!sender_list) ||(!sender_count)) {
11799 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11800 if (err_code != NULL)
11801 *err_code = EMAIL_ERROR_INVALID_PARAM;
11805 int rc = -1, ret = false;
11806 int error = EMAIL_ERROR_NONE;
11808 int i, col_index = 0;
11809 int read_count = 0;
11810 email_sender_list_t *p_sender_list = NULL;
11811 char sql_query_string[QUERY_SIZE] = {0, };
11812 char **result = NULL;
11813 sqlite3 *local_db_handle = emstorage_get_db_connection();
11815 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11816 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
11821 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);
11822 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
11823 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) ");
11826 /* '0' (ALL_ACCOUNT) means all account */
11827 if (account_id > ALL_ACCOUNT)
11828 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);
11830 if (search_value) {
11831 switch (search_type) {
11832 case EMAIL_SEARCH_FILTER_SUBJECT:
11833 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11834 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
11836 case EMAIL_SEARCH_FILTER_SENDER:
11837 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11838 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11839 ") ", search_value);
11841 case EMAIL_SEARCH_FILTER_RECIPIENT:
11842 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11843 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11844 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11845 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11846 ") ", search_value, search_value, search_value);
11848 case EMAIL_SEARCH_FILTER_ALL:
11849 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11850 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
11851 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11852 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11853 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11854 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11857 ")", search_value, search_value, search_value, search_value, search_value);
11863 /* sorting option is not available now. The order of sender list is ascending order by display name */
11864 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
11865 "GROUP BY email_address_sender "
11866 "ORDER BY UPPER(alias_sender) ");
11868 EM_DEBUG_LOG("query[%s]", sql_query_string);
11870 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
11871 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11872 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11874 EM_DEBUG_LOG("Count of Sender [%d]", count);
11876 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
11877 EM_DEBUG_EXCEPTION("em_malloc failed...");
11878 error = EMAIL_ERROR_OUT_OF_MEMORY;
11884 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
11885 for (i = 0; i < count; i++) {
11886 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
11887 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
11888 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
11889 _get_table_field_data_int(result, &(read_count), col_index++);
11890 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
11892 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
11894 sqlite3_free_table(result);
11901 *sender_list = p_sender_list;
11902 *sender_count = count;
11903 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
11908 if (err_code != NULL)
11911 EM_DEBUG_FUNC_END("ret [%d]", ret);
11915 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
11917 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
11919 int err = EMAIL_ERROR_NONE;
11922 if (!sender_list || !*sender_list) {
11923 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
11924 err = EMAIL_ERROR_INVALID_PARAM;
11928 email_sender_list_t* p = *sender_list;
11931 for (; i < count; i++) {
11932 EM_SAFE_FREE(p[i].address);
11933 EM_SAFE_FREE(p[i].display_name);
11937 *sender_list = NULL;
11944 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
11946 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
11948 int err = EMAIL_ERROR_NONE;
11949 email_address_info_t *p_address_info = NULL;
11950 GList *list = NULL;
11951 GList *node = NULL;
11954 if (!address_info_list || !*address_info_list) {
11955 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
11956 err = EMAIL_ERROR_INVALID_PARAM;
11960 /* delete GLists */
11961 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
11963 case EMAIL_ADDRESS_TYPE_FROM:
11964 list = (*address_info_list)->from;
11966 case EMAIL_ADDRESS_TYPE_TO:
11967 list = (*address_info_list)->to;
11969 case EMAIL_ADDRESS_TYPE_CC:
11970 list = (*address_info_list)->cc;
11972 case EMAIL_ADDRESS_TYPE_BCC:
11973 list = (*address_info_list)->bcc;
11977 /* delete dynamic-allocated memory for each item */
11978 node = g_list_first(list);
11979 while (node != NULL) {
11980 p_address_info = (email_address_info_t*)node->data;
11981 EM_SAFE_FREE(p_address_info->address);
11982 EM_SAFE_FREE(p_address_info->display_name);
11983 EM_SAFE_FREE(node->data);
11985 node = g_list_next(node);
11989 EM_SAFE_FREE(*address_info_list);
11990 *address_info_list = NULL;
11992 EM_DEBUG_FUNC_END("err [%d]", err);
11995 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
11997 INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
11999 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
12001 if (!local_activity || !activity_id) {
12002 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
12003 if (err_code != NULL)
12004 *err_code = EMAIL_ERROR_INVALID_PARAM;
12010 int error = EMAIL_ERROR_NONE;
12013 char sql_query_string[QUERY_SIZE] = {0, };
12014 DB_STMT hStmt = NULL;
12015 sqlite3 *local_db_handle = emstorage_get_db_connection();
12016 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12017 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12018 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12019 "INSERT INTO mail_partial_body_activity_tbl VALUES "
12021 "? " /* Account ID */
12022 ",?" /* Local Mail ID */
12023 ",?" /* Server mail ID */
12024 ",?" /* Activity ID */
12025 ",?" /* Activity type*/
12026 ",?" /* Mailbox ID*/
12027 ",?" /* Mailbox name*/
12030 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
12031 char **result = NULL;
12034 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
12036 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12037 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
12039 if (NULL==result[1]) rc = 1;
12040 else rc = atoi(result[1])+1;
12041 sqlite3_free_table(result);
12044 *activity_id = local_activity->activity_id = rc;
12046 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
12047 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
12049 if (local_activity->mailbox_id)
12050 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
12053 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12054 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12055 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12057 EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
12059 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
12060 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
12061 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
12062 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
12063 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
12064 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
12065 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
12068 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12070 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12071 ("sqlite3_step fail:%d", rc));
12072 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12073 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
12079 if (hStmt != NULL) {
12080 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12082 rc = sqlite3_finalize(hStmt);
12084 if (rc != SQLITE_OK) {
12085 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12086 error = EMAIL_ERROR_DB_FAILURE;
12090 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12092 if (err_code != NULL)
12095 EM_DEBUG_FUNC_END("ret [%d]", ret);
12100 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
12102 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
12104 if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
12105 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
12106 if (err_code != NULL)
12107 *err_code = EMAIL_ERROR_INVALID_PARAM;
12112 int error = EMAIL_ERROR_NONE;
12114 int i = 0, rc = -1;
12115 int *mbox_list = NULL;
12116 DB_STMT hStmt = NULL;
12117 char sql_query_string[QUERY_SIZE] = {0, };
12119 sqlite3 *local_db_handle = emstorage_get_db_connection();
12121 EMSTORAGE_START_READ_TRANSACTION(transaction);
12123 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12124 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);
12127 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12128 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12129 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12131 *count = atoi(result[1]);
12132 sqlite3_free_table(result);
12135 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
12136 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
12140 EM_DEBUG_LOG("Mailbox count = %d", *count);
12142 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12144 /* 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); */
12145 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);
12147 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12150 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12152 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12153 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12154 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12157 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12158 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12159 ("sqlite3_step fail:%d", rc));
12161 if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
12162 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12163 error = EMAIL_ERROR_OUT_OF_MEMORY;
12167 memset(mbox_list, 0x00, sizeof(int) * (*count));
12169 for (i = 0; i < (*count); i++) {
12170 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
12171 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12172 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
12173 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12174 ("sqlite3_step fail:%d", rc));
12175 EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
12182 *mailbox_list = mbox_list;
12184 EM_SAFE_FREE(mbox_list);
12186 if (hStmt != NULL) {
12187 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12189 rc = sqlite3_finalize(hStmt);
12191 if (rc != SQLITE_OK) {
12192 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
12193 error = EMAIL_ERROR_DB_FAILURE;
12197 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12199 if (err_code != NULL)
12201 EM_DEBUG_FUNC_END("ret [%d]", ret);
12206 INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
12208 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
12210 if (NULL == &account_list || NULL == count) {
12211 EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
12212 if (err_code != NULL)
12213 *err_code = EMAIL_ERROR_INVALID_PARAM;
12218 int error = EMAIL_ERROR_NONE;
12221 int i = 0, rc = -1;
12222 int *result_account_list = NULL;
12223 DB_STMT hStmt = NULL;
12224 char sql_query_string[QUERY_SIZE] = {0, };
12226 sqlite3 *local_db_handle = emstorage_get_db_connection();
12228 EMSTORAGE_START_READ_TRANSACTION(transaction);
12231 sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
12234 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
12235 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12236 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
12238 *count = atoi(result[1]);
12239 sqlite3_free_table(result);
12242 EM_DEBUG_EXCEPTION("no account found...");
12243 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
12248 EM_DEBUG_LOG("Account count [%d]", *count);
12250 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12252 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
12254 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12257 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12259 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
12260 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12261 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12264 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12265 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12266 ("sqlite3_step fail:%d", rc));
12268 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
12269 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12270 error = EMAIL_ERROR_OUT_OF_MEMORY;
12274 memset(result_account_list, 0x00, sizeof(int) * (*count));
12276 for (i = 0; i < (*count); i++) {
12277 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
12279 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12280 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12281 ("sqlite3_step fail:%d", rc));
12282 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
12289 *account_list = result_account_list;
12291 EM_SAFE_FREE(result_account_list);
12293 if (hStmt != NULL) {
12294 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12296 rc = sqlite3_finalize(hStmt);
12298 if (rc != SQLITE_OK) {
12299 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12300 error = EMAIL_ERROR_DB_FAILURE;
12304 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12306 if (err_code != NULL)
12308 EM_DEBUG_FUNC_END("ret [%d]", ret);
12314 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)
12316 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
12318 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
12319 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);
12321 if (err_code != NULL)
12322 *err_code = EMAIL_ERROR_INVALID_PARAM;
12329 int error = EMAIL_ERROR_NONE;
12331 DB_STMT hStmt = NULL;
12332 email_event_partial_body_thd* event_list = NULL;
12333 char sql_query_string[QUERY_SIZE] = {0, };
12335 sqlite3 *local_db_handle = emstorage_get_db_connection();
12337 EMSTORAGE_START_READ_TRANSACTION(transaction);
12339 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12340 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);
12343 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12344 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12345 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12347 *count = atoi(result[1]);
12348 sqlite3_free_table(result);
12350 EM_DEBUG_LOG("Query = [%s]", sql_query_string);
12353 EM_DEBUG_EXCEPTION("No matched activity found in mail_partial_body_activity_tbl");
12355 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12359 EM_DEBUG_LOG("Activity Count = %d", *count);
12361 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12362 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);
12364 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12367 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12369 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12370 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12371 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12374 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12375 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12376 ("sqlite3_step fail:%d", rc));
12378 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
12379 EM_DEBUG_EXCEPTION("Malloc failed");
12381 error = EMAIL_ERROR_OUT_OF_MEMORY;
12384 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
12386 for (i=0; i < (*count); i++) {
12387 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12388 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12389 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12390 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12391 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12392 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12393 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12395 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12396 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
12397 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12398 ("sqlite3_step fail:%d", rc));
12400 event_list[i].event_type = 0;
12407 *event_start = event_list;
12409 EM_SAFE_FREE(event_list);
12412 if (hStmt != NULL) {
12413 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12415 rc = sqlite3_finalize(hStmt);
12417 if (rc != SQLITE_OK) {
12418 EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
12419 error = EMAIL_ERROR_DB_FAILURE;
12423 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12425 if (err_code != NULL)
12428 EM_DEBUG_FUNC_END("ret [%d]", ret);
12434 INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
12436 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);
12439 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
12440 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);
12442 if (err_code != NULL)
12443 *err_code = EMAIL_ERROR_INVALID_PARAM;
12449 int error = EMAIL_ERROR_NONE;
12450 char sql_query_string[QUERY_SIZE] = {0, };
12452 sqlite3 *local_db_handle = emstorage_get_db_connection();
12454 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12455 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12457 if (activity_id == 0)
12458 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);
12460 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);
12462 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12464 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12465 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12466 ("sqlite3_exec fail:%d", rc));
12467 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12468 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12470 /* validate activity existence */
12471 rc = sqlite3_changes(local_db_handle);
12473 EM_DEBUG_EXCEPTION("No matching activity found");
12474 error = EMAIL_ERROR_DATA_NOT_FOUND;
12483 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12486 if (err_code != NULL)
12489 EM_DEBUG_FUNC_END("ret [%d]", ret);
12493 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
12495 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12497 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
12498 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12499 if (err_code != NULL)
12500 *err_code = EMAIL_ERROR_INVALID_PARAM;
12505 int error = EMAIL_ERROR_NONE;
12506 char sql_query_string[QUERY_SIZE] = {0, };
12508 DB_STMT hStmt = NULL;
12510 sqlite3 *local_db_handle = emstorage_get_db_connection();
12512 EMSTORAGE_START_READ_TRANSACTION(transaction);
12513 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12515 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);
12517 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12520 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12521 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
12522 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12523 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12526 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12527 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12528 ("sqlite3_step fail:%d", rc));
12530 _get_stmt_field_data_int(hStmt, activity_count, 0);
12532 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12538 if (hStmt != NULL) {
12539 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12540 rc = sqlite3_finalize(hStmt);
12542 if (rc != SQLITE_OK) {
12543 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12544 error = EMAIL_ERROR_DB_FAILURE;
12546 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12549 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12552 if (err_code != NULL)
12555 EM_DEBUG_FUNC_END("ret [%d]", ret);
12560 INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
12562 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
12564 if (NULL == activity_count || NULL == err_code) {
12565 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
12566 if (err_code != NULL)
12567 *err_code = EMAIL_ERROR_INVALID_PARAM;
12572 int error = EMAIL_ERROR_NONE;
12573 DB_STMT hStmt = NULL;
12574 char sql_query_string[QUERY_SIZE] = {0, };
12576 sqlite3 *local_db_handle = emstorage_get_db_connection();
12578 EMSTORAGE_START_READ_TRANSACTION(transaction);
12579 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12581 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
12583 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12586 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12587 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
12588 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12589 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12592 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12593 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12594 ("sqlite3_step fail:%d", rc));
12596 _get_stmt_field_data_int(hStmt, activity_count, 0);
12598 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12605 if (hStmt != NULL) {
12606 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12608 rc = sqlite3_finalize(hStmt);
12610 if (rc != SQLITE_OK) {
12611 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12612 error = EMAIL_ERROR_DB_FAILURE;
12614 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12617 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12619 if (err_code != NULL)
12622 EM_DEBUG_FUNC_END("ret [%d]", ret);
12626 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
12628 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12629 if (account_id < FIRST_ACCOUNT_ID) {
12630 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12631 if (err_code != NULL)
12632 *err_code = EMAIL_ERROR_INVALID_PARAM;
12638 int error = EMAIL_ERROR_NONE;
12639 char sql_query_string[QUERY_SIZE] = {0, };
12641 sqlite3 *local_db_handle = emstorage_get_db_connection();
12642 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12643 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12644 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
12646 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12648 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12649 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12650 ("sqlite3_exec fail:%d", rc));
12651 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12652 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12655 rc = sqlite3_changes(local_db_handle);
12657 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
12658 error = EMAIL_ERROR_DATA_NOT_FOUND;
12667 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12669 if (err_code != NULL)
12672 EM_DEBUG_FUNC_END("ret [%d]", ret);
12676 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
12677 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*/
12679 INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12681 EM_DEBUG_FUNC_BEGIN("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12683 int rc = -1, ret = false;
12684 int error = EMAIL_ERROR_NONE;
12685 char sql_query_string[QUERY_SIZE] = {0, };
12687 int transaction = true;
12689 if (!old_server_uid || !new_server_uid || !mbox_name) {
12690 EM_DEBUG_EXCEPTION("Invalid parameters");
12691 error = EMAIL_ERROR_INVALID_PARAM;
12695 sqlite3 *local_db_handle = emstorage_get_db_connection();
12696 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12697 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12698 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12699 "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);
12701 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12703 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12704 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12705 ("sqlite3_exec fail:%d", rc));
12706 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12707 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12709 rc = sqlite3_changes(local_db_handle);
12711 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12712 error = EMAIL_ERROR_DATA_NOT_FOUND;
12720 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12722 if (err_code != NULL)
12725 EM_DEBUG_FUNC_END("ret [%d]", ret);
12730 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
12732 EM_DEBUG_FUNC_BEGIN("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
12735 int error = EMAIL_ERROR_NONE;
12737 FILE* fp_dst = NULL;
12739 if (!data_string || !dst_file_name) {
12740 EM_DEBUG_EXCEPTION("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
12741 error = EMAIL_ERROR_INVALID_PARAM;
12745 fp_dst = fopen(dst_file_name, "w");
12748 EM_DEBUG_EXCEPTION("fopen failed - %s", dst_file_name);
12750 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
12752 error = EMAIL_ERROR_SYSTEM_FAILURE;
12756 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
12757 EM_DEBUG_EXCEPTION("fwrite failed...");
12758 error = EMAIL_ERROR_UNKNOWN;
12766 if (fp_dst != NULL)
12769 if (err_code != NULL)
12772 EM_DEBUG_FUNC_END("ret [%d]", ret);
12776 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
12780 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
12782 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)
12784 EM_DEBUG_FUNC_BEGIN();
12787 int error = EMAIL_ERROR_NONE;
12788 char sql_query_string[QUERY_SIZE] = {0, };
12790 int transaction = true;
12792 if (!old_server_uid || !new_server_uid || !mbox_name) {
12793 EM_DEBUG_EXCEPTION("Invalid parameters");
12794 error = EMAIL_ERROR_INVALID_PARAM;
12798 EM_DEBUG_LOG("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12800 sqlite3 *local_db_handle = emstorage_get_db_connection();
12801 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12804 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12805 "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);
12807 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12809 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12810 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12811 ("sqlite3_exec fail:%d", rc));
12812 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12813 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12816 rc = sqlite3_changes(local_db_handle);
12819 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12820 error = EMAIL_ERROR_DATA_NOT_FOUND;
12829 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12832 if (err_code != NULL)
12835 EM_DEBUG_FUNC_END("ret [%d]", ret);
12842 * @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);
12843 * Prepare an array of mail_id and corresponding server mail id.
12845 *@author h.gahlaut@samsung.com
12846 * @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)
12847 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
12848 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
12849 * @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
12850 * @param[out] err_code Returns the error code.
12851 * @remarks An Example of Query to be exexuted in this API:
12852 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
12853 * @return This function returns true on success or false on failure.
12856 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)
12858 EM_DEBUG_FUNC_BEGIN();
12859 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
12861 int error = EMAIL_ERROR_NONE;
12863 email_id_set_t* p_id_set = NULL;
12865 const int buf_size = QUERY_SIZE;
12866 char sql_query_string[QUERY_SIZE] = {0, };
12867 int space_left_in_query_buffer = buf_size;
12870 char *server_mail_id = NULL;
12871 char **result = NULL;
12875 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
12876 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]", mail_ids, idset, id_set_count);
12877 error = EMAIL_ERROR_INVALID_PARAM;
12881 sqlite3 *local_db_handle = emstorage_get_db_connection();
12883 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);
12885 EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
12887 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12888 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12889 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12890 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12892 EM_DEBUG_LOG(" Count of mails [%d ]", count);
12895 EM_DEBUG_EXCEPTION("Can't find proper mail");
12896 error = EMAIL_ERROR_DATA_NOT_FOUND;
12901 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
12902 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12903 error = EMAIL_ERROR_OUT_OF_MEMORY;
12909 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
12910 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
12911 for (i = 0; i < count; i++) {
12912 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
12913 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
12914 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
12915 EM_SAFE_FREE(server_mail_id);
12917 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
12918 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
12920 sqlite3_free_table(result);
12929 *id_set_count = count;
12930 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
12933 EM_SAFE_FREE(p_id_set);
12937 if (err_code != NULL)
12940 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
12942 EM_DEBUG_FUNC_END("ret [%d]", ret);
12950 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
12952 EM_DEBUG_FUNC_BEGIN();
12953 int rc, ret = true, transaction = true;
12954 int error = EMAIL_ERROR_NONE;
12955 char sql_query_string[QUERY_SIZE] = {0, };
12957 sqlite3 *local_db_handle = emstorage_get_db_connection();
12958 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12960 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
12962 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12963 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12964 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12966 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
12968 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12969 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12970 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12973 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
12975 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12976 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12977 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12983 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12987 EM_DEBUG_FUNC_END("ret [%d]", ret);
12991 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)
12993 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);
12995 if ((account_id <= 0) || (dest_mailbox_id <= 0) || (!rule) || (!rule->value)|| (!filtered_mail_id_list) || (!count_of_mails)) {
12996 EM_DEBUG_EXCEPTION("Invalid Parameter");
12998 if (err_code != NULL)
12999 *err_code = EMAIL_ERROR_INVALID_PARAM;
13003 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
13004 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
13005 char **result = NULL, *where_pararaph = NULL;
13006 char sql_query_string[QUERY_SIZE] = {0, };
13008 sqlite3 *local_db_handle = emstorage_get_db_connection();
13010 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
13012 EM_DEBUG_LOG("rule->value [%s]", rule->value);
13014 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 100;
13015 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
13017 if (where_pararaph == NULL) {
13018 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
13019 error = EMAIL_ERROR_OUT_OF_MEMORY;
13023 memset(where_pararaph, 0, sizeof(char) * where_pararaph_length);
13025 EM_DEBUG_LOG("rule->type [%d], rule->flag2[%d]", rule->type, rule->flag2);
13027 if (rule->type == EMAIL_FILTER_FROM) {
13028 if (rule->flag2 == RULE_TYPE_INCLUDES)
13029 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);
13030 else /* RULE_TYPE_EXACTLY */
13031 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);
13033 else if (rule->type == EMAIL_FILTER_SUBJECT) {
13034 if (rule->flag2 == RULE_TYPE_INCLUDES)
13035 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);
13036 else /* RULE_TYPE_EXACTLY */
13037 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject = \'%s\'", account_id, rule->value);
13040 error = EMAIL_ERROR_INVALID_PARAM;
13041 EM_DEBUG_EXCEPTION("rule->type is invald");
13045 /* prevent 34361 */
13046 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13047 strcat(sql_query_string, where_pararaph);
13049 EM_DEBUG_LOG("query[%s]", sql_query_string);
13051 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
13052 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13053 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13054 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13056 EM_DEBUG_LOG("Count of mails [%d]", count);
13059 mail_list = malloc(sizeof(int) * count);
13060 if (mail_list == NULL) {
13061 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
13062 error = EMAIL_ERROR_OUT_OF_MEMORY;
13068 for (i = 0; i < count; i++)
13069 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
13071 memset(sql_query_string, 0x00, QUERY_SIZE);
13072 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
13074 /* prevent 34361 */
13075 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13076 strcat(sql_query_string, where_pararaph);
13078 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13079 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13080 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13082 #ifdef __FEATURE_BODY_SEARCH__
13083 /* Updating mail_text_tbl */
13084 memset(sql_query_string, 0x00, QUERY_SIZE);
13085 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
13086 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13087 strcat(sql_query_string, where_pararaph);
13088 EM_DEBUG_LOG("Query [%s]", sql_query_string);
13090 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13091 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13092 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13101 *filtered_mail_id_list = mail_list;
13103 EM_SAFE_FREE(mail_list);
13105 sqlite3_free_table(result);
13110 EM_SAFE_FREE(where_pararaph);
13112 if (ret && count_of_mails)
13113 *count_of_mails = count;
13115 if (err_code != NULL)
13118 EM_DEBUG_FUNC_END("ret [%d]", ret);
13122 #define EMAIL_SLOT_UNIT 25
13124 INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
13126 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);
13127 int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
13128 int where_pararaph_length = 0;
13129 char *where_pararaph = NULL;
13130 char sql_query_string[QUERY_SIZE] = {0, };
13132 sqlite3 *local_db_handle = emstorage_get_db_connection();
13134 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13136 if (new_slot_size > 0)
13137 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
13138 else if (new_slot_size == 0)
13139 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
13141 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
13145 where_pararaph_length = 80;
13147 where_pararaph_length = 50;
13149 if (new_slot_size == 0)
13150 where_pararaph_length += 70;
13152 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
13153 if (where_pararaph == NULL) {
13154 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
13155 err = EMAIL_ERROR_OUT_OF_MEMORY;
13158 memset(where_pararaph, 0x00, where_pararaph_length);
13160 if (account_id > ALL_ACCOUNT) {
13163 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
13165 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
13168 if (new_slot_size == 0)
13169 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"));
13171 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
13172 strcat(sql_query_string, where_pararaph);
13174 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
13175 err = EMAIL_ERROR_OUT_OF_MEMORY;
13179 EM_DEBUG_LOG("query[%s]", sql_query_string);
13181 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13182 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13183 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13189 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13193 EM_SAFE_FREE(where_pararaph);
13195 if (err_code != NULL)
13198 EM_DEBUG_FUNC_END("ret [%d]", ret);
13202 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)
13204 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);
13206 if (!meeting_req || meeting_req->mail_id <= 0) {
13208 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
13210 if (err_code != NULL)
13211 *err_code = EMAIL_ERROR_INVALID_PARAM;
13218 int error = EMAIL_ERROR_NONE;
13219 DB_STMT hStmt = NULL;
13220 char sql_query_string[QUERY_SIZE] = {0, };
13222 time_t temp_unix_time = 0;
13224 sqlite3 *local_db_handle = emstorage_get_db_connection();
13225 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13227 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13228 "INSERT INTO mail_meeting_tbl VALUES "
13229 "( ?" /* mail_id */
13230 ", ?" /* account_id */
13231 ", ?" /* mailbox_id */
13232 ", ?" /* meeting_response */
13233 ", ?" /* start_time */
13234 ", ?" /* end_time */
13235 ", ?" /* location */
13236 ", ?" /* global_object_id */
13238 ", ?" /* standard_name */
13239 ", ?" /* standard_time_start_date */
13240 ", ?" /* standard_biad */
13241 ", ?" /* daylight_name */
13242 ", ?" /* daylight_time_start_date */
13243 ", ?" /* daylight_bias */
13246 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13247 if (rc != SQLITE_OK) {
13248 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
13249 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
13251 error = EMAIL_ERROR_DB_FAILURE;
13257 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13258 EM_DEBUG_LOG(">>>>> account_id[%d]", account_id);
13259 EM_DEBUG_LOG(">>>>> mailbox_name[%s]", mailbox_name);
13260 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13261 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13262 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13263 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13264 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13265 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13266 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13267 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13268 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13269 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13270 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13271 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13273 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
13274 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
13275 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
13276 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13278 temp_unix_time = timegm(&(meeting_req->start_time));
13279 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13280 temp_unix_time = timegm(&(meeting_req->end_time));
13281 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13283 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13284 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13286 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13287 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13288 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13289 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13290 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13292 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13293 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13294 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13295 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13298 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13299 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13300 ("sqlite3_step fail:%d", rc));
13301 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13302 ("sqlite3_step fail:%d", rc));
13307 if (hStmt != NULL) {
13308 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13310 rc = sqlite3_finalize(hStmt);
13311 if (rc != SQLITE_OK) {
13312 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13313 error = EMAIL_ERROR_DB_FAILURE;
13317 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13320 if (err_code != NULL)
13323 EM_DEBUG_FUNC_END("ret [%d]", ret);
13327 INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
13329 EM_DEBUG_FUNC_BEGIN();
13334 int error = EMAIL_ERROR_NONE;
13335 email_meeting_request_t *p_temp_meeting_req = NULL;
13337 time_t temp_unix_time;
13339 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
13341 DB_STMT hStmt = NULL;
13342 char sql_query_string[QUERY_SIZE] = {0, };
13344 sqlite3 *local_db_handle = emstorage_get_db_connection();
13345 EMSTORAGE_START_READ_TRANSACTION(transaction);
13347 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13348 "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 "
13349 " FROM mail_meeting_tbl "
13350 " WHERE mail_id = %d", mail_id);
13351 EM_DEBUG_LOG("sql : %s ", sql_query_string);
13354 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13356 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13357 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13360 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13361 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13362 ("sqlite3_step fail:%d", rc));
13364 if (rc == SQLITE_DONE) {
13365 EM_DEBUG_EXCEPTION(" no Meeting request found...");
13368 error= EMAIL_ERROR_DATA_NOT_FOUND;
13372 if (!(p_temp_meeting_req = (email_meeting_request_t*)malloc(sizeof(email_meeting_request_t)))) {
13373 EM_DEBUG_EXCEPTION(" malloc failed...");
13374 error = EMAIL_ERROR_OUT_OF_MEMORY;
13378 memset(p_temp_meeting_req, 0x00, sizeof(email_meeting_request_t));
13382 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->mail_id), col_index++);
13383 _get_stmt_field_data_int(hStmt, (int *)&(p_temp_meeting_req->meeting_response), col_index++);
13384 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13385 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->start_time));
13386 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13387 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->end_time));
13388 _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->location, 1, col_index++);
13389 _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->global_object_id, 1, col_index++);
13390 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.offset_from_GMT), col_index++);
13392 _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++);
13393 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13394 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.standard_time_start_date));
13395 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.standard_bias), col_index++);
13397 _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++);
13398 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13399 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.daylight_time_start_date));
13400 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.daylight_bias), col_index++);
13403 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", p_temp_meeting_req->mail_id);
13404 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", p_temp_meeting_req->meeting_response);
13405 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(p_temp_meeting_req->start_time)));
13406 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(p_temp_meeting_req->end_time)));
13407 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", p_temp_meeting_req->location);
13408 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", p_temp_meeting_req->global_object_id);
13409 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", p_temp_meeting_req->time_zone.offset_from_GMT);
13410 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", p_temp_meeting_req->time_zone.standard_name);
13411 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.standard_time_start_date)));
13412 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", p_temp_meeting_req->time_zone.standard_bias);
13413 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", p_temp_meeting_req->time_zone.daylight_name);
13414 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.daylight_time_start_date)));
13415 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", p_temp_meeting_req->time_zone.daylight_bias);
13421 *meeting_req = p_temp_meeting_req;
13423 EM_SAFE_FREE(p_temp_meeting_req);
13426 if (hStmt != NULL) {
13427 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13429 rc = sqlite3_finalize(hStmt);
13431 if (rc != SQLITE_OK) {
13432 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
13433 error = EMAIL_ERROR_DB_FAILURE;
13437 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13440 if (err_code != NULL)
13443 EM_DEBUG_FUNC_END("ret [%d]", ret);
13448 INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
13450 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
13453 int error = EMAIL_ERROR_NONE;
13455 DB_STMT hStmt = NULL;
13456 char sql_query_string[QUERY_SIZE] = {0, };
13457 time_t temp_unix_time = 0;
13459 if (!meeting_req) {
13460 EM_DEBUG_EXCEPTION("Invalid Parameter!");
13461 error = EMAIL_ERROR_INVALID_PARAM;
13465 sqlite3 *local_db_handle = emstorage_get_db_connection();
13466 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13468 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13469 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13470 "UPDATE mail_meeting_tbl "
13472 /* " account_id = ?, " // not update here, this can be changed when move or copy */
13473 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
13474 " meeting_response = ?, "
13475 " start_time = ?, "
13478 " global_object_id = ?, "
13480 " standard_name = ?, "
13481 " standard_time_start_date = ?, "
13482 " standard_bias = ?, "
13483 " daylight_name = ?, "
13484 " daylight_time_start_date = ?, "
13485 " daylight_bias = ? "
13486 "WHERE mail_id = %d",
13487 meeting_req->mail_id);
13489 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
13491 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13492 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13493 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13495 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13496 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13497 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13498 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13499 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13500 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13501 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13502 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13503 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13504 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13505 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13506 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13507 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13511 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13512 temp_unix_time = timegm(&(meeting_req->start_time));
13513 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13514 temp_unix_time = timegm(&(meeting_req->end_time));
13515 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13516 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13517 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13518 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13519 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13520 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13521 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13522 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13523 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13524 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_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.daylight_bias);
13529 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13530 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13531 ("sqlite3_step fail:%d", rc));
13536 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13538 if (hStmt != NULL) {
13539 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13541 rc = sqlite3_finalize(hStmt);
13542 if (rc != SQLITE_OK) {
13543 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
13545 error = EMAIL_ERROR_DB_FAILURE;
13551 if (err_code != NULL)
13554 EM_DEBUG_FUNC_END("ret [%d]", ret);
13558 INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
13560 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);
13562 if (account_id < ALL_ACCOUNT || mail_id < 0) {
13563 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
13565 if (err_code != NULL)
13566 *err_code = EMAIL_ERROR_INVALID_PARAM;
13572 int error = EMAIL_ERROR_NONE;
13574 char sql_query_string[QUERY_SIZE] = {0, };
13576 sqlite3 *local_db_handle = emstorage_get_db_connection();
13577 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13579 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
13581 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
13582 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);
13586 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);
13589 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
13590 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);
13593 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13594 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13595 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13600 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13606 EM_DEBUG_FUNC_END("ret [%d]", ret);
13611 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
13613 EM_DEBUG_FUNC_BEGIN();
13615 if (!meeting_req ) {
13616 EM_DEBUG_EXCEPTION("NULL PARAM");
13620 EM_SAFE_FREE(meeting_req->location);
13621 EM_SAFE_FREE(meeting_req->global_object_id);
13623 EM_DEBUG_FUNC_END();
13627 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)
13629 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);
13630 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
13631 char sql_query_string[QUERY_SIZE] = {0, };
13632 char **result = NULL;
13633 int rc = -1, ret = false;
13634 int error = EMAIL_ERROR_NONE;
13635 int counter = 0, col_index = 0;
13636 int result_mail_id_count = 0;
13637 int *result_mail_id_list = NULL;
13639 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
13640 EM_DEBUG_EXCEPTION("Invalid Parameter");
13641 error = EMAIL_ERROR_INVALID_PARAM;
13645 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);
13647 EM_DEBUG_LOG("query[%s].", sql_query_string);
13649 sqlite3 *local_db_handle = emstorage_get_db_connection();
13650 EMSTORAGE_START_READ_TRANSACTION(transaction);
13652 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
13653 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13654 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13656 if (!result_mail_id_count) {
13657 EM_DEBUG_LOG("No mail found...");
13659 error= EMAIL_ERROR_MAIL_NOT_FOUND;
13663 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
13665 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
13666 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
13667 error = EMAIL_ERROR_OUT_OF_MEMORY;
13668 sqlite3_free_table(result);
13672 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
13676 for (counter = 0; counter < result_mail_id_count; counter++)
13677 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
13682 EM_DEBUG_LOG("finish off [%d]", ret);
13685 sqlite3_free_table(result);
13688 *mail_id_list = result_mail_id_list;
13689 *mail_id_count = result_mail_id_count;
13692 EM_SAFE_FREE(result_mail_id_list);
13694 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13697 if (err_code != NULL)
13700 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
13701 EM_DEBUG_FUNC_END("ret [%d]", ret);
13706 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
13708 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
13710 int rc = -1, ret = false;
13711 int err = EMAIL_ERROR_NONE;
13712 char sql_query_string[QUERY_SIZE] = {0, };
13714 int result_count = 0;
13716 if (mail_id == 0 || thread_id == NULL) {
13717 EM_DEBUG_EXCEPTION("Invalid Parameter");
13718 if (err_code != NULL)
13719 *err_code = EMAIL_ERROR_INVALID_PARAM;
13723 sqlite3 *local_db_handle = emstorage_get_db_connection();
13725 memset(sql_query_string, 0, QUERY_SIZE);
13726 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
13728 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13729 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13730 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13731 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13733 if (!result_count) {
13734 EM_DEBUG_EXCEPTION("No mail found...");
13736 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13737 /* sqlite3_free_table(result); */
13741 _get_table_field_data_int(result, thread_id, 1);
13743 sqlite3_free_table(result);
13751 if (err_code != NULL)
13754 EM_DEBUG_FUNC_END("ret [%d]", ret);
13759 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)
13761 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);
13763 int rc = -1, ret = false;
13764 int err = EMAIL_ERROR_NONE;
13765 char sql_query_string[QUERY_SIZE] = {0, };
13766 char **result = NULL;
13767 int result_count = 0;
13769 if (thread_id == 0) {
13770 EM_DEBUG_EXCEPTION("Invalid Parameter");
13771 if (err_code != NULL)
13772 *err_code = EMAIL_ERROR_INVALID_PARAM;
13776 sqlite3 *local_db_handle = emstorage_get_db_connection();
13778 if (thread_item_count == 0 || latest_mail_id == 0) {
13779 memset(sql_query_string, 0, QUERY_SIZE);
13780 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);
13782 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13783 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13784 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13785 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13786 EM_DEBUG_LOG("result_count[%d]", result_count);
13787 if (result_count == 0) {
13788 EM_DEBUG_EXCEPTION("No mail found...");
13790 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13791 sqlite3_free_table(result);
13795 _get_table_field_data_int(result, &latest_mail_id, 2);
13796 _get_table_field_data_int(result, &thread_item_count, 3);
13798 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
13799 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
13801 sqlite3_free_table(result);
13804 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13806 /* if (thread_item_count > 1) */
13808 memset(sql_query_string, 0, QUERY_SIZE);
13809 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);
13810 EM_DEBUG_LOG("query[%s]", sql_query_string);
13812 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13813 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13814 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13817 memset(sql_query_string, 0, QUERY_SIZE);
13818 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);
13819 EM_DEBUG_LOG("query[%s]", sql_query_string);
13821 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13822 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13823 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13829 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13833 if (err_code != NULL)
13836 EM_DEBUG_FUNC_END("ret [%d]", ret);
13841 INTERNAL_FUNC void emstorage_flush_db_cache()
13843 sqlite3_release_memory(-1);
13846 #ifdef __FEATURE_LOCAL_ACTIVITY__
13848 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
13851 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13853 EM_DEBUG_FUNC_BEGIN();
13855 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13857 int rc = -1, ret = false;
13858 int error = EMAIL_ERROR_NONE;
13859 DB_STMT hStmt = NULL;
13860 char sql_query_string[8192] = { 0x00, };
13863 if (!local_activity) {
13864 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13865 if (err_code != NULL)
13866 *err_code = EMAIL_ERROR_INVALID_PARAM;
13870 sqlite3 *local_db_handle = emstorage_get_db_connection();
13871 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
13872 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13874 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
13876 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
13877 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
13878 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
13879 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
13880 EM_DEBUG_LOG(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
13881 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
13882 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
13884 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13887 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13888 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13889 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13891 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13896 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13897 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13898 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13899 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13900 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
13901 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13902 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13905 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13907 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13908 ("sqlite3_step fail:%d", rc));
13909 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13910 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13916 if (hStmt != NULL) {
13917 rc = sqlite3_finalize(hStmt);
13918 if (rc != SQLITE_OK) {
13919 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13921 error = EMAIL_ERROR_DB_FAILURE;
13925 EM_DEBUG_LOG(" >>>>>>>>>> hStmt is NULL!!!");
13928 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13932 if (err_code != NULL)
13935 EM_DEBUG_FUNC_END("ret [%d]", ret);
13940 * emstorage_get_activity - Get the Local activity Information
13944 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)
13946 EM_DEBUG_FUNC_BEGIN();
13948 int i = 0, count = 0, rc = -1, ret = false;
13949 int error = EMAIL_ERROR_NONE;
13950 emstorage_activity_tbl_t *p_activity_tbl = NULL;
13951 char sql_query_string[1024] = {0x00, };
13952 char **result = NULL;
13955 EM_IF_NULL_RETURN_VALUE(activity_list, false);
13956 EM_IF_NULL_RETURN_VALUE(select_num, false);
13959 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
13960 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
13961 if (err_code != NULL)
13962 *err_code = EMAIL_ERROR_INVALID_PARAM;
13966 EMSTORAGE_START_READ_TRANSACTION(transaction);
13968 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13970 if (activityid == ALL_ACTIVITIES) {
13971 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
13974 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);
13977 EM_DEBUG_LOG("Query [%s]", sql_query_string);
13981 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13982 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13983 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13989 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
13990 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13991 error = EMAIL_ERROR_OUT_OF_MEMORY;
13996 for (i = 0; i < count; i++) {
13997 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13998 if (result[col_index])
13999 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
14001 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14002 if (result[col_index])
14003 p_activity_tbl[i].account_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].mail_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].activity_type = atoi(result[col_index++]);
14014 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14015 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14016 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
14020 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14021 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14022 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
14026 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14027 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14028 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
14035 sqlite3_free_table(result);
14043 *activity_list = p_activity_tbl;
14044 *select_num = count;
14045 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
14047 else if (p_activity_tbl != NULL) {
14048 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
14051 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14052 if (err_code != NULL)
14055 EM_DEBUG_FUNC_END("ret [%d]", ret);
14060 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
14063 EM_DEBUG_FUNC_BEGIN();
14066 int err = EMAIL_ERROR_NONE;
14069 char **result = NULL;
14071 if (NULL == activity_id)
14073 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
14075 err = EMAIL_ERROR_INVALID_PARAM;
14079 /* increase unique id */
14081 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
14083 /* 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); */
14084 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14085 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14087 if (NULL==result[1])
14090 rc = atoi(result[1])+1;
14095 sqlite3_free_table(result);
14101 if (NULL != err_code) {
14105 EM_DEBUG_FUNC_END("ret [%d]", ret);
14110 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)
14113 EM_DEBUG_FUNC_BEGIN();
14115 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);
14117 if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0) {
14118 if (err_code != NULL)
14119 *err_code = EMAIL_ERROR_INVALID_PARAM;
14124 int error = EMAIL_ERROR_NONE;
14125 int i = 0, rc = -1, count = 0;
14126 char sql_query_string[1024] = {0x00, };
14127 int *activity_ids = NULL;
14129 char **result = NULL;
14131 EMSTORAGE_START_READ_TRANSACTION(transaction);
14133 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14135 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);
14137 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
14140 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
14141 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14142 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14146 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
14148 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
14149 EM_DEBUG_EXCEPTION(" em_malloc failed...");
14150 error = EMAIL_ERROR_OUT_OF_MEMORY;
14154 for (i = 0; i < count; i++) {
14155 activity_ids[i] = atoi(result[col_index]);
14157 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
14166 *activity_id_count = count;
14167 *activity_id_list = activity_ids;
14170 else if (activity_ids != NULL) /* Prevent defect - 216566 */
14171 EM_SAFE_FREE(activity_ids);
14174 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14175 if (err_code != NULL) {
14179 EM_DEBUG_FUNC_END("ret [%d]", ret);
14183 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
14185 EM_DEBUG_FUNC_BEGIN();
14187 int error = EMAIL_ERROR_NONE;
14190 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
14192 if (NULL == activity_id_list) {
14193 error = EMAIL_ERROR_INVALID_PARAM;
14197 free(activity_id_list);
14198 activity_id_list = NULL;
14206 if (NULL != error_code) {
14207 *error_code = error;
14210 EM_DEBUG_FUNC_END("ret [%d]", ret);
14215 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
14216 * or based on server mail id
14219 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
14221 EM_DEBUG_FUNC_BEGIN();
14224 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
14226 if (!local_activity) {
14227 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
14228 if (err_code != NULL)
14229 *err_code = EMAIL_ERROR_INVALID_PARAM;
14233 int rc = -1, ret = false; /* Prevent_FIX */
14234 int err = EMAIL_ERROR_NONE;
14236 int query_where = 0;
14237 char sql_query_string[8192] = { 0x00, };
14238 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14240 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14242 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
14244 EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
14246 if (local_activity->account_id) {
14247 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14248 " WHERE account_id = %d ", local_activity->account_id);
14253 if (local_activity->server_mailid) {
14254 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14255 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
14261 if (local_activity->mail_id) {
14262 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
14264 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14265 " %s %s mail_id = %d ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
14272 if (local_activity->activity_type > 0) {
14273 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14274 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
14277 if (local_activity->activity_id > 0) {
14278 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14279 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
14283 EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
14285 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
14286 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14287 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14289 rc = sqlite3_changes(local_db_handle);
14291 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
14292 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
14298 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14299 if (err_code != NULL)
14301 EM_DEBUG_FUNC_END("ret [%d]", ret);
14306 * emstorage_free_local_activity - Free the Local Activity data
14308 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
14310 EM_DEBUG_FUNC_BEGIN();
14312 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
14315 int error = EMAIL_ERROR_INVALID_PARAM;
14318 if (!local_activity_list || !*local_activity_list) {
14319 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
14321 error = EMAIL_ERROR_INVALID_PARAM;
14325 emstorage_activity_tbl_t* p = *local_activity_list;
14328 for (; i < count; i++) {
14329 EM_SAFE_FREE(p[i].dest_mbox);
14330 EM_SAFE_FREE(p[i].src_mbox);
14331 EM_SAFE_FREE(p[i].server_mailid);
14334 free(p); *local_activity_list = NULL;
14341 if (err_code != NULL)
14344 EM_DEBUG_FUNC_END("ret [%d]", ret);
14348 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
14351 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
14353 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
14355 int ret = EMAIL_ERROR_NONE;
14356 char key_value_string[QUERY_SIZE] = { 0, };
14357 char *temp_key_value_1 = NULL;
14358 char *temp_key_value_2 = NULL;
14360 if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
14361 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14362 return EMAIL_ERROR_INVALID_PARAM;
14365 switch(input_list_filter_rule->target_attribute) {
14366 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID :
14367 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID :
14368 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID :
14369 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE :
14370 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS :
14371 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID :
14372 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS :
14373 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14374 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14375 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE :
14376 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD :
14377 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD :
14378 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD :
14379 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD :
14380 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD :
14381 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD :
14382 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD :
14383 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS :
14384 case EMAIL_MAIL_ATTRIBUTE_PRIORITY :
14385 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS :
14386 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS :
14387 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS :
14388 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT :
14389 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT :
14390 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID :
14391 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT :
14392 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS :
14393 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS :
14394 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE :
14395 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE :
14396 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
14399 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14400 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14401 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14402 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14403 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14404 case EMAIL_MAIL_ATTRIBUTE_FROM :
14405 case EMAIL_MAIL_ATTRIBUTE_TO :
14406 case EMAIL_MAIL_ATTRIBUTE_CC :
14407 case EMAIL_MAIL_ATTRIBUTE_BCC :
14408 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14409 if(input_list_filter_rule->key_value.string_type_value == NULL) {
14410 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
14411 ret = EMAIL_ERROR_INVALID_PARAM;
14415 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
14417 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
14418 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
14420 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
14421 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
14423 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
14426 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME :
14427 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
14431 ret = EMAIL_ERROR_INVALID_PARAM;
14432 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14436 if(ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
14437 *output_key_value_string = strdup(key_value_string);
14442 EM_SAFE_FREE(temp_key_value_1);
14443 EM_SAFE_FREE(temp_key_value_2);
14445 EM_DEBUG_FUNC_END("ret [%d]", ret);
14449 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14450 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
14452 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
14454 if (index <= 0 || !cert_password_file_name) {
14455 EM_DEBUG_EXCEPTION("Invalid parameter");
14456 return EMAIL_ERROR_INVALID_PARAM;
14459 sprintf(cert_password_file_name, ".email_cert_%d", index);
14461 EM_DEBUG_FUNC_END();
14462 return EMAIL_ERROR_NONE;
14466 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
14468 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14470 int ret = EMAIL_ERROR_NONE;
14472 int length_field_name = 0;
14473 int length_value = 0;
14474 char result_rule_string[QUERY_SIZE] = { 0 , };
14475 char *mod_field_name_string = NULL;
14476 char *mod_value_string = NULL;
14477 char *temp_field_name_string = NULL;
14478 char *temp_key_value_string = NULL;
14480 if(input_list_filter_rule == NULL || output_string == NULL) {
14481 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14482 return EMAIL_ERROR_INVALID_PARAM;
14485 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
14487 if(temp_field_name_string == NULL) {
14488 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14489 return EMAIL_ERROR_INVALID_PARAM;
14492 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) {
14493 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
14494 return EMAIL_ERROR_INVALID_PARAM;
14497 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
14498 length_value = EM_SAFE_STRLEN(temp_key_value_string);
14500 switch(input_list_filter_rule->target_attribute) {
14501 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14502 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14503 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14504 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14505 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14506 case EMAIL_MAIL_ATTRIBUTE_FROM :
14507 case EMAIL_MAIL_ATTRIBUTE_TO :
14508 case EMAIL_MAIL_ATTRIBUTE_CC :
14509 case EMAIL_MAIL_ATTRIBUTE_BCC :
14510 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14518 if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
14519 length_field_name += strlen("UPPER() ");
14520 length_value += strlen("UPPER() ");
14521 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
14522 mod_value_string = em_malloc(sizeof(char) * length_value);
14523 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
14524 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
14525 EM_SAFE_FREE(temp_key_value_string);
14528 mod_field_name_string = strdup(temp_field_name_string);
14529 mod_value_string = temp_key_value_string;
14532 switch (input_list_filter_rule->rule_type) {
14533 case EMAIL_LIST_FILTER_RULE_EQUAL :
14534 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14536 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
14537 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
14539 case EMAIL_LIST_FILTER_RULE_LESS_THAN :
14540 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
14542 case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
14543 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
14545 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
14546 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
14548 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
14549 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
14551 case EMAIL_LIST_FILTER_RULE_INCLUDE :
14552 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
14554 case EMAIL_LIST_FILTER_RULE_IN :
14555 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
14557 case EMAIL_LIST_FILTER_RULE_NOT_IN :
14558 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
14561 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14562 ret = EMAIL_ERROR_INVALID_PARAM;
14566 *output_string = strdup(result_rule_string);
14569 EM_SAFE_FREE(mod_field_name_string);
14570 EM_SAFE_FREE(mod_value_string);
14572 EM_DEBUG_FUNC_END("ret [%d]", ret);
14576 static int _make_filter_fts_rule_string(email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
14578 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14580 int ret = EMAIL_ERROR_NONE;
14581 char *field_name_string = NULL;
14582 char key_value_string[QUERY_SIZE] = {0,};
14583 char result_rule_string[QUERY_SIZE] = {0,};
14588 int error = EMAIL_ERROR_NONE;
14589 char **result = NULL;
14590 char sql_query_string[QUERY_SIZE] = {0,};
14591 char sql_query_string2[QUERY_SIZE] = {0,};
14592 sqlite3 *local_db_handle = NULL;
14594 if(input_list_filter_rule == NULL || output_string == NULL) {
14595 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14596 return EMAIL_ERROR_INVALID_PARAM;
14599 field_name_string = EM_SAFE_STRDUP("body_text");
14600 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
14602 switch (input_list_filter_rule->rule_type) {
14604 case EMAIL_LIST_FILTER_RULE_INCLUDE:
14605 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
14608 case EMAIL_LIST_FILTER_RULE_MATCH:
14609 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
14613 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14614 ret = EMAIL_ERROR_INVALID_PARAM;
14618 local_db_handle = emstorage_get_db_connection();
14619 EMSTORAGE_START_READ_TRANSACTION(true);
14620 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
14622 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14623 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF;},
14624 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14629 EM_DEBUG_EXCEPTION("No mail found...");
14631 error= EMAIL_ERROR_MAIL_NOT_FOUND;
14632 *output_string = strdup("mail_id IN ( ) ");
14636 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
14638 int *mail_ids = NULL;
14639 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
14640 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
14641 error = EMAIL_ERROR_OUT_OF_MEMORY;
14645 for (i = 0; i < count; i++) {
14646 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
14647 EM_DEBUG_LOG(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
14650 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
14651 EM_PROFILE_END(emstorage_query_mail_list_loop);
14652 sqlite3_free_table(result);
14653 EMSTORAGE_FINISH_READ_TRANSACTION(true);
14655 sqlite3_release_memory(-1);
14660 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "mail_id IN ( ");
14661 for (i = 0; i < count-1; i++) {
14662 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "%d, ", mail_ids[i]);
14664 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "%d ) ", mail_ids[count-1]);
14666 *output_string = strdup(sql_query_string2);
14669 EM_SAFE_FREE(field_name_string);
14670 EM_DEBUG_FUNC_END("ret [%d]", ret);
14674 static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
14675 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
14677 char result_rule_string[QUERY_SIZE] = { 0 , };
14678 int ret = EMAIL_ERROR_NONE;
14680 if(input_sorting_rule->force_boolean_check) {
14681 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14684 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14686 switch (input_sorting_rule->sort_order) {
14687 case EMAIL_SORT_ORDER_ASCEND :
14688 EM_SAFE_STRCAT(result_rule_string, " ASC ");
14691 case EMAIL_SORT_ORDER_DESCEND :
14692 EM_SAFE_STRCAT(result_rule_string, " DESC ");
14696 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
14697 ret = EMAIL_ERROR_INVALID_PARAM;
14701 *output_string = strdup(result_rule_string);
14704 EM_DEBUG_FUNC_END("ret [%d]", ret);
14708 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)
14710 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);
14711 int ret = EMAIL_ERROR_NONE;
14713 int string_offset = 0;
14714 char conditional_clause_string[QUERY_SIZE] = {0, };
14715 char *result_string_for_a_item = NULL;
14717 if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
14718 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14719 return EMAIL_ERROR_INVALID_PARAM;
14722 if(input_filter_count > 0) {
14723 strcpy(conditional_clause_string, " WHERE ");
14725 for ( i = 0; i < input_filter_count; i++) {
14726 switch (input_filter_list[i].list_filter_item_type) {
14727 case EMAIL_LIST_FILTER_ITEM_RULE :
14728 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
14729 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
14732 case EMAIL_LIST_FILTER_ITEM_RULE_FTS :
14733 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
14734 _make_filter_fts_rule_string(&(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
14737 case EMAIL_LIST_FILTER_ITEM_OPERATOR :
14738 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
14739 switch(input_filter_list[i].list_filter_item.operator_type) {
14740 case EMAIL_LIST_FILTER_OPERATOR_AND :
14741 result_string_for_a_item = strdup("AND ");
14743 case EMAIL_LIST_FILTER_OPERATOR_OR :
14744 result_string_for_a_item = strdup("OR ");
14746 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
14747 result_string_for_a_item = strdup(" (");
14749 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
14750 result_string_for_a_item = strdup(") ");
14756 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
14757 ret = EMAIL_ERROR_INVALID_PARAM;
14761 if(result_string_for_a_item == NULL) {
14762 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
14763 ret = EMAIL_ERROR_INVALID_PARAM;
14767 if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= QUERY_SIZE) { /* prevent 34364 */
14768 EM_DEBUG_EXCEPTION("Query is too long");
14769 ret = EMAIL_ERROR_DATA_TOO_LONG;
14772 strcat(conditional_clause_string, result_string_for_a_item);
14773 EM_SAFE_FREE(result_string_for_a_item);
14777 if(input_sorting_rule_count > 0) {
14778 strcat(conditional_clause_string, "ORDER BY ");
14780 for ( i = 0; i < input_sorting_rule_count; i++) {
14781 if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
14782 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
14786 strcat(conditional_clause_string, ", ");
14787 strcat(conditional_clause_string, result_string_for_a_item);
14788 EM_SAFE_FREE(result_string_for_a_item);
14792 if (input_start_index != -1 && input_limit_count != -1) {
14793 string_offset = strlen(conditional_clause_string);
14794 SNPRINTF_OFFSET(conditional_clause_string, string_offset, QUERY_SIZE, " LIMIT %d, %d", input_start_index, input_limit_count);
14797 *output_conditional_clause = strdup(conditional_clause_string);
14800 EM_SAFE_FREE(result_string_for_a_item);
14802 EM_DEBUG_FUNC_END("ret [%d]", ret);
14806 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
14808 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
14809 int err = EMAIL_ERROR_NONE;
14811 email_list_filter_t *temp_filter_list = NULL;
14813 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
14815 for ( i = 0; i < input_filter_count; i++) {
14816 temp_filter_list = (*input_filter_list) + i;
14817 if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
14818 switch(temp_filter_list->list_filter_item.rule.target_attribute) {
14819 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14820 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14821 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14822 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14823 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14824 case EMAIL_MAIL_ATTRIBUTE_FROM :
14825 case EMAIL_MAIL_ATTRIBUTE_TO :
14826 case EMAIL_MAIL_ATTRIBUTE_CC :
14827 case EMAIL_MAIL_ATTRIBUTE_BCC :
14828 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14829 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14830 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14831 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
14839 free(*input_filter_list);
14840 *input_filter_list = NULL;
14842 EM_DEBUG_FUNC_END("err [%d]", err);
14846 INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14848 EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14850 if (!certificate) {
14851 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14852 if (err_code != NULL)
14853 *err_code = EMAIL_ERROR_INVALID_PARAM;
14858 int rc = -1, ret = false;
14859 int error = EMAIL_ERROR_NONE;
14860 DB_STMT hStmt = NULL;
14861 char sql_query_string[QUERY_SIZE] = {0, };
14862 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14863 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14865 sqlite3 *local_db_handle = emstorage_get_db_connection();
14867 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14869 char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
14870 char **result = NULL;
14872 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14873 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14874 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14875 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14877 if (NULL==result[1]) rc = 1;
14878 else rc = atoi(result[1])+1;
14879 sqlite3_free_table(result);
14881 certificate->certificate_id = rc;
14882 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14883 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14884 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14888 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14889 "INSERT INTO mail_certificate_tbl VALUES "
14891 " ? " /* Index of certificate */
14892 " , ? " /* Select the account */
14893 " , ? " /* Year of issue */
14894 " , ? " /* Month of issue */
14895 " , ? " /* Day of issue */
14896 " , ? " /* Year of expiration */
14897 " , ? " /* Month of expiration */
14898 " , ? " /* Day of expiration */
14899 " , ? " /* Organization of issue */
14900 " , ? " /* Email address */
14901 " , ? " /* Subject of certificate */
14902 " , ? " /* Name of saved certificate */
14906 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
14907 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14908 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14909 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14911 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
14912 _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
14913 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
14914 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
14915 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
14916 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
14917 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
14918 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
14919 _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);
14920 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
14921 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
14922 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
14923 /* rc = sqlite3_step(hStmt); */
14924 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14926 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14927 ("sqlite3_step fail:%d", rc));
14928 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14929 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
14930 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14931 if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14932 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
14933 error = EMAIL_ERROR_SYSTEM_FAILURE;
14939 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14941 if (!emcore_notify_storage_event(NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
14942 EM_DEBUG_EXCEPTION("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
14946 if (hStmt != NULL) {
14947 rc = sqlite3_finalize(hStmt);
14948 if (rc != SQLITE_OK) {
14949 EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
14950 error = EMAIL_ERROR_DB_FAILURE;
14954 EM_DEBUG_LOG("hStmt is NULL!!!");
14956 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14959 if (err_code != NULL)
14962 EM_DEBUG_FUNC_END("ret [%d]", ret);
14966 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
14968 EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
14971 int error = EMAIL_ERROR_NONE;
14974 if (!certificate_list || !*certificate_list) {
14975 EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
14976 error = EMAIL_ERROR_INVALID_PARAM;
14980 emstorage_certificate_tbl_t *p = *certificate_list;
14983 for (; i < count; i++) {
14984 EM_SAFE_FREE(p[i].issue_organization_name);
14985 EM_SAFE_FREE(p[i].email_address);
14986 EM_SAFE_FREE(p[i].subject_str);
14987 EM_SAFE_FREE(p[i].filepath);
14988 EM_SAFE_FREE(p[i].password);
14992 *certificate_list = NULL;
14998 if (err_code != NULL)
15001 EM_DEBUG_FUNC_END("ret [%d]", ret);
15005 INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
15007 EM_DEBUG_FUNC_BEGIN();
15009 int i = 0, count = 0, rc = -1, ret = false;
15010 int error = EMAIL_ERROR_NONE;
15011 emstorage_certificate_tbl_t *p_data_tbl = NULL;
15013 DB_STMT hStmt = NULL;
15015 if (!select_num || !certificate_list) {
15016 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
15017 if (err_code != NULL)
15018 *err_code = EMAIL_ERROR_INVALID_PARAM;
15022 sqlite3 *local_db_handle = emstorage_get_db_connection();
15023 EMSTORAGE_START_READ_TRANSACTION(transaction);
15025 char sql_query_string[QUERY_SIZE] = {0, };
15026 char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
15029 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
15030 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
15031 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15032 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15034 count = atoi(result[1]);
15035 sqlite3_free_table(result);
15038 EM_DEBUG_EXCEPTION("no account found...");
15039 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15043 EM_DEBUG_LOG("count = %d", rc);
15044 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
15046 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
15047 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15049 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
15050 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15051 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15053 /* rc = sqlite3_step(hStmt); */
15054 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15055 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15056 ("sqlite3_step fail:%d", rc));
15057 if (rc == SQLITE_DONE) {
15058 EM_DEBUG_EXCEPTION("no account found...");
15060 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15066 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count))) {
15067 EM_DEBUG_EXCEPTION("malloc failed...");
15068 error = EMAIL_ERROR_OUT_OF_MEMORY;
15071 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
15072 for (i = 0; i < count; i++) {
15073 /* get recordset */
15074 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15075 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15076 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15077 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15078 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15079 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15080 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15081 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15082 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15083 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15084 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15085 if (with_password == true) {
15086 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15087 /* get password from the secure storage */
15088 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15090 EM_SAFE_FREE(p_data_tbl[i].password);
15092 /* get password file name */
15093 if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15094 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15098 /* read password from secure storage */
15099 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
15100 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
15103 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl[i].password);
15107 /* rc = sqlite3_step(hStmt); */
15108 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15109 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
15110 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15111 ("sqlite3_step fail:%d", rc));
15118 *certificate_list = p_data_tbl;
15119 *select_num = count;
15120 EM_DEBUG_LOG("COUNT : %d", count);
15122 else if (p_data_tbl != NULL)
15123 emstorage_free_certificate(&p_data_tbl, count, NULL);
15124 if (hStmt != NULL) {
15125 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15127 rc = sqlite3_finalize(hStmt);
15129 if (rc != SQLITE_OK) {
15130 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15131 error = EMAIL_ERROR_DB_FAILURE;
15135 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15138 if (err_code != NULL)
15141 EM_DEBUG_FUNC_END("ret [%d]", ret);
15145 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)
15147 EM_DEBUG_FUNC_BEGIN("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
15149 if (!certificate) {
15150 EM_DEBUG_EXCEPTION("email_address[%s], certificate[%p]", email_address, certificate);
15151 if (err_code != NULL)
15152 *err_code = EMAIL_ERROR_INVALID_PARAM;
15157 int error = EMAIL_ERROR_NONE;
15158 emstorage_certificate_tbl_t *p_data_tbl = NULL;
15159 DB_STMT hStmt = NULL;
15160 char sql_query_string[QUERY_SIZE] = {0, };
15163 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15164 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15166 sqlite3 *local_db_handle = emstorage_get_db_connection();
15167 EMSTORAGE_START_READ_TRANSACTION(transaction);
15169 /* Make query string */
15170 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
15171 sql_len = EM_SAFE_STRLEN(sql_query_string);
15173 /* dummy value, FROM WHERE clause */
15174 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
15177 EM_DEBUG_LOG("query = [%s]", sql_query_string);
15179 /* execute a sql and count rows */
15180 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15181 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15182 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15184 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15185 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15186 ("sqlite3_step fail:%d", rc));
15188 if (rc == SQLITE_DONE) {
15189 EM_DEBUG_EXCEPTION("no matched certificate found...");
15190 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15194 /* Assign query result to structure */
15195 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
15196 EM_DEBUG_EXCEPTION("malloc failed...");
15197 error = EMAIL_ERROR_OUT_OF_MEMORY;
15201 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15202 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15203 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15204 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15205 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15206 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15207 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15208 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15209 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15210 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15211 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15212 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15214 if (with_password) {
15215 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15216 /* get password file name */
15217 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15218 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15222 /* read password from secure storage */
15223 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15224 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
15227 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
15234 *certificate = p_data_tbl;
15236 if (hStmt != NULL) {
15237 rc = sqlite3_finalize(hStmt);
15238 if (rc != SQLITE_OK) {
15239 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15240 error = EMAIL_ERROR_DB_FAILURE;
15244 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15247 if (err_code != NULL)
15250 EM_DEBUG_FUNC_END("ret [%d]", ret);
15254 INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
15256 EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
15258 if (!certificate) {
15259 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
15260 if (err_code != NULL)
15261 *err_code = EMAIL_ERROR_INVALID_PARAM;
15266 int error = EMAIL_ERROR_NONE;
15267 emstorage_certificate_tbl_t *p_data_tbl = NULL;
15268 DB_STMT hStmt = NULL;
15269 char sql_query_string[QUERY_SIZE] = {0, };
15272 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15273 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15275 sqlite3 *local_db_handle = emstorage_get_db_connection();
15276 EMSTORAGE_START_READ_TRANSACTION(transaction);
15278 /* Make query string */
15279 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
15280 sql_len = EM_SAFE_STRLEN(sql_query_string);
15282 /* dummy value, FROM WHERE clause */
15283 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
15286 EM_DEBUG_LOG("query = [%s]", sql_query_string);
15288 /* execute a sql and count rows */
15289 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15290 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15291 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15293 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15294 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15295 ("sqlite3_step fail:%d", rc));
15297 if (rc == SQLITE_DONE) {
15298 EM_DEBUG_EXCEPTION("no matched certificate found...");
15299 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15303 /* Assign query result to structure */
15304 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
15305 EM_DEBUG_EXCEPTION("malloc failed...");
15306 error = EMAIL_ERROR_OUT_OF_MEMORY;
15310 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15312 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15313 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15314 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15315 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15316 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15317 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15318 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15319 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15320 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15321 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15322 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15324 if (with_password) {
15325 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15326 /* get password file name */
15327 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15328 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15332 /* read password from secure storage */
15333 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15334 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
15337 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
15344 *certificate = p_data_tbl;
15346 if (hStmt != NULL) {
15347 rc = sqlite3_finalize(hStmt);
15348 if (rc != SQLITE_OK) {
15349 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15350 error = EMAIL_ERROR_DB_FAILURE;
15354 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15357 if (err_code != NULL)
15360 EM_DEBUG_FUNC_END("ret [%d]", ret);
15364 INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
15366 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
15368 if (certificate_id < 1) {
15369 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15371 if (err_code != NULL)
15372 *err_code = EMAIL_ERROR_INVALID_PARAM;
15376 int rc = -1, ret = false;
15377 int error = EMAIL_ERROR_NONE;
15378 sqlite3 *local_db_handle = emstorage_get_db_connection();
15379 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15381 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
15382 DB_STMT hStmt = NULL;
15383 char sql_query_string[QUERY_SIZE] = {0, };
15384 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15385 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15386 /* get password file name */
15387 if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15388 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15392 /* delete from db */
15393 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15394 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
15396 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15397 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15398 ("sqlite3_exec fail:%d", rc));
15399 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15400 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15402 /* validate account existence */
15403 rc = sqlite3_changes(local_db_handle);
15405 EM_DEBUG_EXCEPTION(" no matched certificate found...");
15406 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15409 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15410 /* delete from secure storage */
15411 if (ssm_delete_file(cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15412 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
15413 error = EMAIL_ERROR_SYSTEM_FAILURE;
15421 if (hStmt != NULL) {
15422 rc = sqlite3_finalize(hStmt);
15423 if (rc != SQLITE_OK) {
15424 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15425 error = EMAIL_ERROR_DB_FAILURE;
15429 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15432 if (err_code != NULL)
15435 EM_DEBUG_FUNC_END("ret [%d]", ret);
15439 INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
15441 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
15443 if (certificate_id < 1) {
15444 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15446 if (err_code != NULL)
15447 *err_code = EMAIL_ERROR_INVALID_PARAM;
15451 int error = EMAIL_ERROR_NONE;
15452 int rc, ret = false;
15454 DB_STMT hStmt = NULL;
15455 char sql_query_string[QUERY_SIZE] = {0, };
15456 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15457 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15459 sqlite3 *local_db_handle = emstorage_get_db_connection();
15460 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15462 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15463 "UPDATE mail_certificate_tbl SET"
15465 ", issue_month = ?" /* Index of certificate */
15466 ", issue_day = ?" /* Select the account */
15467 ", expiration_year = ?" /* Year of issue */
15468 ", expiration_month = ?" /* Month of issue */
15469 ", expiration_day = ?" /* Day of issue */
15470 ", issue_organization_name = ?" /* Year of expiration */
15471 ", email_address = ?" /* Month of expiration */
15472 ", subject_str = ?" /* Day of expiration */
15473 ", filepath = ?" /* Organization of issue */
15475 " WHERE certificate_id = ?");
15477 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15478 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
15479 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15480 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15482 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
15483 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
15484 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
15485 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
15486 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
15487 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
15488 _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);
15489 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
15490 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
15491 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
15493 /* rc = sqlite3_step(hStmt); */
15494 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15495 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15496 ("sqlite3_step fail:%d", rc));
15497 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15498 ("sqlite3_step fail:%d", rc));
15500 /* validate account existence */
15501 rc = sqlite3_changes(local_db_handle);
15503 EM_DEBUG_EXCEPTION(" no matched account found...");
15505 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15509 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15510 /* get password file name */
15511 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15512 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15516 /* save passwords to the secure storage */
15517 if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15518 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
15519 error = EMAIL_ERROR_SYSTEM_FAILURE;
15523 if (!emcore_notify_storage_event(NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
15524 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
15530 if (hStmt != NULL) {
15531 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
15533 rc = sqlite3_finalize(hStmt);
15534 if (rc != SQLITE_OK) {
15535 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15536 error = EMAIL_ERROR_DB_FAILURE;
15540 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15544 if (err_code != NULL)
15547 EM_DEBUG_FUNC_END("ret [%d]", ret);
15551 /* Tasks --------------------------------------------------------------------------*/
15552 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)
15554 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);
15558 int err = EMAIL_ERROR_NONE;
15560 DB_STMT hStmt = NULL;
15561 char sql_query_string[QUERY_SIZE] = {0, };
15562 sqlite3 *local_db_handle = NULL;
15563 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
15564 char **result = NULL;
15566 if (input_task_parameter == NULL || output_task_id == NULL) {
15567 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15568 err = EMAIL_ERROR_INVALID_PARAM;
15572 local_db_handle = emstorage_get_db_connection();
15574 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, err);
15576 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
15577 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15578 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15580 if (NULL == result[1])
15583 task_id = atoi(result[1])+1;
15585 *output_task_id = task_id;
15587 sqlite3_free_table(result);
15590 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15591 "INSERT INTO mail_task_tbl VALUES "
15593 " ? " /* task_id */
15594 " , ? " /* task_type */
15595 " , ? " /* task_status */
15596 " , ? " /* task_priority */
15597 " , ? " /* task_parameter_length */
15598 " , ? " /* task_parameter */
15599 " , ? " /* date_time */
15602 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15603 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15604 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15606 EM_DEBUG_LOG(">>>> SQL STMT [%s] ", sql_query_string);
15609 _bind_stmt_field_data_int(hStmt, i++, task_id);
15610 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
15611 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
15612 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
15613 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
15614 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
15615 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
15617 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15619 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15620 ("sqlite3_step fail:%d", rc));
15621 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15622 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15624 ret = (err == EMAIL_ERROR_NONE);
15625 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, err);
15629 if (hStmt != NULL) {
15630 rc = sqlite3_finalize(hStmt);
15631 if (rc != SQLITE_OK) {
15632 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
15633 err = EMAIL_ERROR_DB_FAILURE;
15637 EM_DEBUG_LOG("hStmt is NULL!!!");
15641 EM_DEBUG_FUNC_END("err [%d]", err);
15645 INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction)
15647 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
15648 int rc, ret = false;
15649 int err = EMAIL_ERROR_NONE;
15650 char sql_query_string[QUERY_SIZE] = {0, };
15651 sqlite3 *local_db_handle = NULL;
15654 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15655 err = EMAIL_ERROR_INVALID_PARAM;
15659 local_db_handle = emstorage_get_db_connection();
15661 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15663 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
15665 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15666 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15667 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15672 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15675 EM_DEBUG_FUNC_END("err [%d]", err);
15679 INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_type_t task_status, int transaction)
15681 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
15682 int rc, ret = false;
15683 int err = EMAIL_ERROR_NONE;
15684 char sql_query_string[QUERY_SIZE] = {0, };
15686 sqlite3 *local_db_handle = emstorage_get_db_connection();
15688 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15690 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15691 "UPDATE mail_task_tbl SET"
15692 " task_status = %d"
15693 " WHERE task_id = %d"
15696 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15697 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15698 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15703 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15707 EM_DEBUG_FUNC_END("err [%d]", err);
15711 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)
15713 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);
15714 int i = 0, count = 0, rc = -1;
15716 int field_index = 0;
15717 int err = EMAIL_ERROR_NONE;
15718 email_task_t *task_item_from_tbl = NULL;
15719 char sql_query_string[QUERY_SIZE] = {0, };
15720 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
15722 sqlite3 *local_db_handle = NULL;
15723 DB_STMT hStmt = NULL;
15725 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
15726 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
15728 local_db_handle = emstorage_get_db_connection();
15730 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
15731 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15733 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
15734 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15735 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15737 count = atoi(result[1]);
15738 sqlite3_free_table(result);
15740 EM_DEBUG_LOG("count = %d", rc);
15743 EM_DEBUG_EXCEPTION("no task found...");
15744 err = EMAIL_ERROR_TASK_NOT_FOUND;
15748 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);
15749 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15751 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15753 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
15754 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15755 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15757 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15758 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15759 ("sqlite3_step fail:%d", rc));
15761 if (rc == SQLITE_DONE) {
15762 EM_DEBUG_EXCEPTION("no task found...");
15763 err = EMAIL_ERROR_TASK_NOT_FOUND;
15768 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
15769 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
15770 err = EMAIL_ERROR_OUT_OF_MEMORY;
15774 for (i = 0; i < count; i++) {
15775 /* get recordset */
15778 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
15779 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
15780 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
15781 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
15782 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
15783 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
15785 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15786 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
15787 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15788 ("sqlite3_step fail:%d", rc));
15792 if (err == EMAIL_ERROR_NONE) {
15793 if (output_task_list)
15794 *output_task_list = task_item_from_tbl;
15795 *output_task_count = count;
15798 EM_SAFE_FREE(task_item_from_tbl);
15800 if (hStmt != NULL) {
15801 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15802 rc = sqlite3_finalize(hStmt);
15804 if (rc != SQLITE_OK) {
15805 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15806 err = EMAIL_ERROR_DB_FAILURE;
15812 EM_DEBUG_FUNC_END("err [%d]", err);
15815 /* Tasks --------------------------------------------------------------------------*/