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>
49 #include <tzplatform_config.h>
55 #include "email-internal-types.h"
56 #include "email-convert.h"
57 #include "email-core-utils.h"
58 #include "email-utilities.h"
59 #include "email-storage.h"
60 #include "email-debug-log.h"
61 #include "email-types.h"
62 #include "email-convert.h"
63 #include "email-core-signal.h"
64 #include "email-core-event.h"
65 #include "email-core-container.h"
66 #include "email-core-key-manager.h"
68 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
69 #define CONTENT_DATA "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
70 #define CONTENT_TYPE_DATA "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
72 #define FLAG_SEEN 0x01
73 #define FLAG_DELETED 0x02
74 #define FLAG_FLAGGED 0x04
75 #define FLAG_ANSWERED 0x08
77 #define FLAG_DRAFT 0x20
81 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL 256
82 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL 128
83 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL 1027
84 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL 256
86 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
87 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
88 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
89 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
90 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
91 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
92 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL 50
93 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
94 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL 30
95 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
96 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
97 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
98 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL 256
99 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL 256
100 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL 128
101 #define ALIAS_LEN_IN_MAIL_BOX_TBL 128
102 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
103 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
104 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
105 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL 256
106 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL 256
107 #define VALUE_LEN_IN_MAIL_RULE_TBL 256
108 #define VALUE2_LEN_IN_MAIL_RULE_TBL 256
109 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL 128
110 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 128
111 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL 256
112 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 256
113 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL 256
114 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
115 #define MAILBOX_LEN_IN_MAIL_TBL 128
116 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL 128
117 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL 128
118 #define FROM_LEN_IN_MAIL_TBL 256
119 #define SENDER_LEN_IN_MAIL_TBL 256
120 #define REPLY_TO_LEN_IN_MAIL_TBL 256
121 #define TO_LEN_IN_MAIL_TBL 3999
122 #define CC_LEN_IN_MAIL_TBL 3999
123 #define BCC_LEN_IN_MAIL_TBL 3999
124 #define RETURN_PATH_LEN_IN_MAIL_TBL 3999
125 #define SUBJECT_LEN_IN_MAIL_TBL 1027
126 #define THREAD_TOPIC_LEN_IN_MAIL_TBL 256
127 #define TEXT_1_LEN_IN_MAIL_TBL 256
128 #define TEXT_2_LEN_IN_MAIL_TBL 256
129 #define MIME_ENTITY_LEN_IN_MAIL_TBL 256
130 #define DATETIME_LEN_IN_MAIL_TBL 128
131 #define MESSAGE_ID_LEN_IN_MAIL_TBL 256
132 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
133 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
134 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
135 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
136 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL 128
137 #define LOCATION_LEN_IN_MAIL_MEETING_TBL 1024
138 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL 512
139 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL 32
140 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL 32
141 #define PREVIEWBODY_LEN_IN_MAIL_TBL 512
142 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
144 /* this define is used for query to change data (delete, insert, update) */
145 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
147 if (transaction_flag) {\
148 if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) {\
149 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(multi_user_name, transaction_flag, result_code, error_code) \
158 if (transaction_flag) {\
159 if (result_code == true) {\
160 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {\
161 EM_DEBUG_EXCEPTION("emstorage_commit_transaction error");\
162 error_code = EMAIL_ERROR_DB_FAILURE;\
163 result_code = false;\
167 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) {\
168 EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error");\
169 error_code = EMAIL_ERROR_DB_FAILURE;\
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 _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
192 /* for safety secure-storage operation */
193 static pthread_mutex_t _ss_handle_lock = PTHREAD_MUTEX_INITIALIZER;
195 #define _MULTIPLE_DB_HANDLE
197 #ifdef _MULTIPLE_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] = {{NULL, 0, 0}, };
212 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
214 EM_DEBUG_FUNC_BEGIN();
216 pthread_t current_thread_id = THREAD_SELF();
217 sqlite3 *result_db_handle = NULL;
219 ENTER_CRITICAL_SECTION(_db_handle_lock);
220 for (i = 0; i < MAX_DB_CLIENT; i++) {
221 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id))
223 if (_db_handle_list[i].user_name) {
224 if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name ? multi_user_name : "root"))
227 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
228 result_db_handle = _db_handle_list[i].db_handle;
232 LEAVE_CRITICAL_SECTION(_db_handle_lock);
234 if (!result_db_handle)
235 EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
238 return result_db_handle;
241 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
243 EM_DEBUG_FUNC_BEGIN();
244 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
245 pthread_t current_thread_id = THREAD_SELF();
247 ENTER_CRITICAL_SECTION(_db_handle_lock);
248 for (i = 0; i < MAX_DB_CLIENT; i++) {
249 if (_db_handle_list[i].thread_id == 0) {
250 _db_handle_list[i].thread_id = current_thread_id;
251 _db_handle_list[i].db_handle = db_handle;
252 /* Only distinguished container and host */
253 _db_handle_list[i].user_name = strdup(multi_user_name ? multi_user_name : "root");
254 EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
255 error_code = EMAIL_ERROR_NONE;
259 LEAVE_CRITICAL_SECTION(_db_handle_lock);
261 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
262 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
264 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
268 int emstorage_remove_db_handle()
270 EM_DEBUG_FUNC_BEGIN();
271 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
272 ENTER_CRITICAL_SECTION(_db_handle_lock);
273 for (i = 0; i < MAX_DB_CLIENT; i++)
275 if (_db_handle_list[i].thread_id == THREAD_SELF())
277 _db_handle_list[i].thread_id = 0;
278 _db_handle_list[i].db_handle = NULL;
279 EM_SAFE_FREE(_db_handle_list[i].user_name);
281 EM_DEBUG_LOG("index [%d]", i);
282 error_code = EMAIL_ERROR_NONE;
286 LEAVE_CRITICAL_SECTION(_db_handle_lock);
288 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
289 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
291 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
295 int emstorage_reset_db_handle_list()
297 EM_DEBUG_FUNC_BEGIN();
300 ENTER_CRITICAL_SECTION(_db_handle_lock);
301 for (i = 0; i < MAX_DB_CLIENT; i++)
303 _db_handle_list[i].thread_id = 0;
304 _db_handle_list[i].db_handle = NULL;
305 EM_SAFE_FREE(_db_handle_list[i].user_name);
307 LEAVE_CRITICAL_SECTION(_db_handle_lock)
310 return EMAIL_ERROR_NONE;
314 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
316 return emstorage_db_open(multi_user_name, NULL);
319 #else /* _MULTIPLE_DB_HANDLE */
321 sqlite3 *_db_handle = NULL;
323 sqlite3 *emstorage_get_db_connection()
325 if (NULL == _db_handle)
326 emstorage_db_open(NULL);
329 #endif /* _MULTIPLE_DB_HANDLE */
331 /* ------------------------------------------------------------------------------ */
332 /* Mutex using shared memory */
335 pthread_mutex_t mutex;
339 mmapped_t *mapped_for_db_lock = NULL;
340 int shm_fd_for_db_lock = 0;
342 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
343 mmapped_t *mapped_for_generating_mail_id = NULL;
344 int shm_fd_for_generating_mail_id = 0;
345 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
347 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
348 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
350 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
351 return_value = function_call;\
352 _unlockshm_mutex(mapped_for_db_lock);\
355 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
356 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
357 { return_value = function_call; }
358 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
360 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
362 EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
363 int error = EMAIL_ERROR_NONE;
365 DB_STMT db_statement = NULL;
367 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
368 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
369 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
371 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
372 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
373 ("sqlite3_step failed [%d] [%s]", rc, query_string));
377 if (db_statement != NULL) {
378 rc = sqlite3_finalize(db_statement);
379 if (rc != SQLITE_OK) {
380 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
381 error = EMAIL_ERROR_DB_FAILURE;
388 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
390 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
391 char errno_buf[ERRNO_BUF_SIZE] = {0};
394 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
395 return EMAIL_ERROR_INVALID_PARAM;
398 int fd = shm_open (shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
400 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
401 return EMAIL_ERROR_SYSTEM_FAILURE;
405 EM_DEBUG_LOG("** Create SHM FILE **");
406 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
407 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
408 return EMAIL_ERROR_SYSTEM_FAILURE;
411 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
412 if (m == MAP_FAILED) {
413 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
415 return EMAIL_ERROR_SYSTEM_FAILURE;
420 pthread_mutexattr_t mattr;
421 pthread_mutexattr_init(&mattr);
422 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
423 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
424 pthread_mutex_init(&(m->mutex), &mattr);
425 pthread_mutexattr_destroy(&mattr);
427 pthread_mutex_destroy(&(m->mutex));
428 munmap(m, sizeof(mmapped_t));
432 return EMAIL_ERROR_NONE;
435 int emstorage_shm_file_destroy(const char *shm_file_name)
437 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
438 char errno_buf[ERRNO_BUF_SIZE] = {0};
441 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
442 return EMAIL_ERROR_INVALID_PARAM;
445 if (shm_unlink(shm_file_name) != 0)
446 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
448 return EMAIL_ERROR_NONE;
451 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
453 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
454 char errno_buf[ERRNO_BUF_SIZE] = {0};
456 if(!shm_file_name || !param_shm_fd || !param_mapped) {
457 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
458 return EMAIL_ERROR_INVALID_PARAM;
461 if (!(*param_mapped)) {
462 EM_DEBUG_LOG("** mapping begin **");
463 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
464 *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
465 if ((*param_shm_fd) == -1) {
466 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
467 return EMAIL_ERROR_SYSTEM_FAILURE;
471 fchmod((*param_shm_fd), 0666);
472 EM_DEBUG_LOG("** Create SHM FILE **");
473 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
474 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
475 return EMAIL_ERROR_SYSTEM_FAILURE;
478 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
479 if (tmp == MAP_FAILED) {
480 EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
481 return EMAIL_ERROR_SYSTEM_FAILURE;
486 pthread_mutexattr_t mattr;
487 pthread_mutexattr_init(&mattr);
488 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
489 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
490 pthread_mutex_init(&(tmp->mutex), &mattr);
491 pthread_mutexattr_destroy(&mattr);
497 return EMAIL_ERROR_NONE;
500 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
502 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
505 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
506 return EMAIL_ERROR_INVALID_PARAM;
509 struct timespec abs_time;
510 clock_gettime(CLOCK_REALTIME, &abs_time);
511 abs_time.tv_sec += sec;
512 char errno_buf[ERRNO_BUF_SIZE] = {0};
514 int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
516 if (err == EOWNERDEAD) {
517 err = pthread_mutex_consistent(&(param_mapped->mutex));
518 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
521 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
526 return EMAIL_ERROR_NONE;
529 void _unlockshm_mutex(mmapped_t *param_mapped)
531 EM_DEBUG_FUNC_BEGIN();
532 pthread_mutex_unlock(&(param_mapped->mutex));
535 /* ------------------------------------------------------------------------------ */
538 static int _open_counter = 0;
540 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
541 static int _read_password_from_secure_storage(char *file_name, char **password);
543 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
544 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
548 const char *object_name;
549 unsigned int data_flag;
552 static const email_db_object_t _g_db_tables[] =
554 { "mail_read_mail_uid_tbl", 1},
556 { "mail_attachment_tbl", 1},
560 static const email_db_object_t _g_db_indexes[] =
562 { "mail_read_mail_uid_idx1", 1},
564 { "mail_attachment_idx1", 1},
570 CREATE_TABLE_MAIL_ACCOUNT_TBL,
571 CREATE_TABLE_MAIL_BOX_TBL,
572 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
573 CREATE_TABLE_MAIL_RULE_TBL,
574 CREATE_TABLE_MAIL_TBL,
575 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
576 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
577 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
579 CREATE_TABLE_DUMMY_INDEX1,
581 CREATE_TABLE_MAIL_MEETING_TBL,
582 #ifdef __FEATURE_LOCAL_ACTIVITY__
583 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
585 CREATE_TABLE_DUMMY_INDEX2,
587 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
588 CREATE_TABLE_MAIL_TASK_TBL,
589 #ifdef __FEATURE_BODY_SEARCH__
590 CREATE_TABLE_MAIL_TEXT_TBL,
592 CREATE_TABLE_DUMMY_INDEX3,
595 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
596 CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
598 CREATE_TABLE_DUMMY_INDEX4,
602 CREATE_TABLE_MAIL_ACCOUNT_IDX,
603 CREATE_TABLE_MAIL_BOX_IDX,
604 CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
605 CREATE_TABLE_MAIL_IDX,
606 CREATE_TABLE_MAIL_ATTACHMENT_IDX,
607 CREATE_TABLE_MAIL_MEETING_IDX,
608 CREATE_TABLE_MAIL_TASK_IDX,
609 CREATE_TABLE_MAIL_DATETIME_IDX,
610 CREATE_TABLE_MAIL_THREAD_IDX,
616 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
617 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
618 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
623 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
624 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
625 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
626 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
627 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
628 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
629 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
630 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
631 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
632 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
633 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
644 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
645 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
646 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
647 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
648 ALIAS_IDX_IN_MAIL_BOX_TBL,
649 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
650 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
651 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
652 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
653 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
658 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
659 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
660 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
661 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
662 SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663 RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664 SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665 FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
666 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
667 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
670 #ifdef __FEATURE_BODY_SEARCH__
673 MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
674 ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
675 MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
676 BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
677 FIELD_COUNT_OF_MAIL_TEXT_TBL,
683 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
684 RULE_ID_IDX_IN_MAIL_RULE_TBL,
685 FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
686 TYPE_IDX_IN_MAIL_RULE_TBL,
687 VALUE_IDX_IN_MAIL_RULE_TBL,
688 VALUE2_IDX_IN_MAIL_RULE_TBL,
689 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
690 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
691 FLAG1_IDX_IN_MAIL_RULE_TBL,
692 FLAG2_IDX_IN_MAIL_RULE_TBL,
697 MAIL_ID_IDX_IN_MAIL_TBL = 0,
698 ACCOUNT_ID_IDX_IN_MAIL_TBL,
699 MAILBOX_ID_IDX_IN_MAIL_TBL,
700 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
701 SUBJECT_IDX_IN_MAIL_TBL,
702 DATETIME_IDX_IN_MAIL_TBL,
703 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
704 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
705 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
706 MESSAGE_ID_IDX_IN_MAIL_TBL,
707 REFERENCE_ID_IDX_IN_MAIL_TBL,
708 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
709 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
710 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
711 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
712 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
713 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
714 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
715 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
716 ALIAS_SENDER_IDX_IN_MAIL_TBL,
717 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
718 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
719 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
720 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
721 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
722 MAIL_SIZE_IDX_IN_MAIL_TBL,
723 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
724 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
725 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
726 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
727 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
728 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
729 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
730 DRM_STATUS_IDX_IN_MAIL_TBL,
731 PRIORITY_IDX_IN_MAIL_TBL,
732 SAVE_STATUS_IDX_IN_MAIL_TBL,
733 LOCK_STATUS_IDX_IN_MAIL_TBL,
734 REPORT_STATUS_IDX_IN_MAIL_TBL,
735 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
736 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
737 THREAD_ID_IDX_IN_MAIL_TBL,
738 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
739 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
740 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
741 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
742 DIGEST_TYPE_IDX_IN_MAIL_TBL,
743 SMIME_TYPE_IDX_IN_MAIL_TBL,
744 SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
745 REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
746 TAG_ID_IDX_IN_MAIL_TBL,
747 REPLIED_TIME_IDX_IN_MAIL_TBL,
748 FORWARDED_TIME_IDX_IN_MAIL_TBL,
749 DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
750 EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
751 EAS_DATA_IDX_IN_MAIL_TBL,
752 USER_NAME_IDX_IN_MAIL_TBL,
753 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
758 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
759 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
760 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
761 CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
762 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
763 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
764 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
765 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
766 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
767 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
768 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
769 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
770 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
771 #ifdef __ATTACHMENT_OPTI__
772 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
773 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
778 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
779 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
780 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
785 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
788 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
789 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
790 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
791 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
795 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
798 ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
799 STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
800 ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801 MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
802 SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
803 MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
804 MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
808 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
809 typedef struct _em_mail_id_list {
811 struct _em_mail_id_list *next;
814 static char *g_test_query[] = {
815 /* 1. select mail_account_tbl */
818 " incoming_server_type, "
819 " incoming_server_address, "
820 " user_email_address, "
821 " incoming_server_user_name, "
822 " incoming_server_password, "
824 " incoming_server_port_number, "
825 " incoming_server_secure_connection, "
826 " incoming_server_authentication_method,"
827 " outgoing_server_type, "
828 " outgoing_server_address, "
829 " outgoing_server_port_number, "
830 " outgoing_server_need_authentication, "
831 " outgoing_server_secure_connection, "
832 " outgoing_server_user_name, "
833 " outgoing_server_password, "
838 " keep_mails_on_pop_server_after_download, "
839 " auto_resend_times, "
840 " outgoing_server_size_limit, "
841 " wifi_auto_download, "
843 " incoming_server_requires_apop,"
845 " is_preset_account, "
849 " req_delivery_receipt, "
850 " req_read_receipt, "
854 " display_name_from, "
856 " forward_with_files, "
860 ", add_my_address_to_bcc"
861 ", notification_status "
863 ", display_content_status "
864 ", default_ringtone_status "
865 ", alert_ringtone_path "
874 " FROM mail_account_tbl",
875 /* 2. select mail_box_tbl */
885 " total_mail_count_on_server, "
886 " has_archived_mails, "
890 " FROM mail_box_tbl ",
891 /* 3. select mail_read_mail_uid_tbl */
900 " flags_seen_field, "
902 " FROM mail_read_mail_uid_tbl ",
903 /* 4. select mail_rule_tbl */
912 " target_mailbox_id, "
915 " FROM mail_rule_tbl ",
916 /* 5. select mail_tbl */
925 " server_mail_status, "
926 " server_mailbox_name, "
929 " reference_mail_id, "
930 " full_address_from, "
931 " full_address_reply, "
934 " full_address_bcc, "
935 " full_address_return, "
936 " email_address_sender, "
937 " email_address_recipient, "
940 " body_download_status, "
943 " file_path_mime_entity, "
945 " flags_seen_field ,"
946 " flags_deleted_field ,"
947 " flags_flagged_field ,"
948 " flags_answered_field ,"
949 " flags_recent_field ,"
950 " flags_draft_field ,"
951 " flags_forwarded_field,"
957 " attachment_count, "
958 " inline_content_count, "
960 " thread_item_count, "
962 " meeting_request_status, "
967 /* 6. select mail_attachment_tbl */
976 " attachment_save_status, "
977 " attachment_drm_type, "
978 " attachment_drm_method, "
979 " attachment_inline_content_status, "
980 " attachment_mime_type "
981 " FROM mail_attachment_tbl ",
983 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
993 " FROM mail_partial_body_activity_tbl ",
1000 " meeting_response, "
1004 " global_object_id, "
1007 " standard_time_start_date, "
1010 " daylight_time_start_date, "
1012 " FROM mail_meeting_tbl ",
1014 #ifdef __FEATURE_LOCAL_ACTIVITY__
1023 " FROM mail_local_activity_tbl ",
1030 " expiration_year, "
1031 " expiration_month, "
1033 " issue_organization_name, "
1038 " FROM mail_certificate_tbl ",
1044 " task_parameter_length, "
1045 " task_parameter , "
1047 " FROM mail_task_tbl ",
1048 #ifdef __FEATURE_BODY_SEARCH__
1054 " FROM mail_text_tbl ",
1056 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1064 " multi_user_name, "
1065 " FROM mail_auto_download_activity_tbl ",
1070 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1072 static int _get_table_field_data_char(char **table, char *buf, int index)
1074 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1075 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1079 if (table[index] != NULL) {
1080 *buf = (char)atoi(table[index]);
1084 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1090 static int _get_table_field_data_int(char **table, int *buf, int index)
1092 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1093 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1097 if (table[index] != NULL) {
1098 *buf = atoi(table[index]);
1102 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1108 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
1110 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1111 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1115 if (table[index] != NULL) {
1116 *buf = (time_t)atol(table[index]);
1120 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1126 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1130 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1131 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1135 char *pTemp = table[index];
1140 sLen = EM_SAFE_STRLEN(pTemp);
1142 *buf = (char *) em_malloc(sLen + 1);
1144 EM_DEBUG_EXCEPTION("malloc is failed");
1147 strncpy(*buf, pTemp, sLen);
1152 #ifdef _PRINT_STORAGE_LOG_
1154 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1156 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1164 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1166 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1167 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1171 char *pTemp = table[index];
1176 memset(buf, 0, buffer_size);
1177 strncpy(buf, pTemp, buffer_size - 1);
1179 #ifdef _PRINT_STORAGE_LOG_
1181 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1183 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1189 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1191 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1192 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1196 char *temp_buffer = table[index];
1198 if (temp_buffer == NULL)
1201 *buffer = malloc(buffer_size);
1202 if(*buffer == NULL) {
1203 EM_DEBUG_EXCEPTION("allocation failed.");
1206 memset(*buffer, 0, buffer_size);
1207 memcpy(*buffer, temp_buffer, buffer_size);
1209 #ifdef _PRINT_STORAGE_LOG_
1211 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1213 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1219 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1221 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1222 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1226 if (sqlite3_column_text(hStmt, index) != NULL) {
1227 *buf = (char)sqlite3_column_int(hStmt, index);
1228 #ifdef _PRINT_STORAGE_LOG_
1229 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1234 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1239 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1241 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1242 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1246 if (sqlite3_column_text(hStmt, index) != NULL) {
1247 *buf = sqlite3_column_int(hStmt, index);
1248 #ifdef _PRINT_STORAGE_LOG_
1249 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1254 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1259 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1261 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1262 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1266 if (sqlite3_column_text(hStmt, index) != NULL) {
1267 *buf = (time_t)sqlite3_column_int(hStmt, index);
1268 #ifdef _PRINT_STORAGE_LOG_
1269 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1274 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1278 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1280 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1281 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1286 sLen = sqlite3_column_bytes(hStmt, index);
1288 #ifdef _PRINT_STORAGE_LOG_
1289 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1293 *buf = (char *) em_malloc(sLen + 1);
1295 EM_DEBUG_EXCEPTION("em_malloc failed");
1299 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1304 #ifdef _PRINT_STORAGE_LOG_
1306 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1308 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1314 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1316 if( !hStmt || !buf || (index < 0)) { /*prevent 39618*/
1317 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1322 sLen = sqlite3_column_bytes(hStmt, index);
1324 #ifdef _PRINT_STORAGE_LOG_
1325 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1329 *buf = (char *) em_malloc(sLen);
1331 EM_DEBUG_EXCEPTION("em_malloc failed");
1335 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1342 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1344 if (!hStmt || !buf || (index < 0)) { /*prevent 39620*/
1345 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1350 sLen = sqlite3_column_bytes(hStmt, index);
1352 #ifdef _PRINT_STORAGE_LOG_
1353 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1357 memset(buf, 0, buffer_size);
1358 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1363 #ifdef _PRINT_STORAGE_LOG_
1364 EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1370 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1372 if ((hStmt == NULL) || (index < 0)) {
1373 EM_DEBUG_EXCEPTION("index[%d]", index);
1377 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1379 if (ret != SQLITE_OK) {
1380 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1387 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1389 if ((hStmt == NULL) || (index < 0)) {
1390 EM_DEBUG_EXCEPTION("index[%d]", index);
1394 int ret = sqlite3_bind_int(hStmt, index+1, value);
1396 if (ret != SQLITE_OK) {
1397 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1404 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1406 if ((hStmt == NULL) || (index < 0)) {
1407 EM_DEBUG_EXCEPTION("index[%d]", index);
1411 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1413 if (ret != SQLITE_OK) {
1414 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1421 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1423 if ((hStmt == NULL) || (index < 0)) {
1424 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1428 #ifdef _PRINT_STORAGE_LOG_
1429 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1434 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1436 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1438 if (ret != SQLITE_OK) {
1439 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1445 /* destroy function for sqlite3_bind_text */
1446 void _bind_stmt_free_string(void* arg)
1448 EM_DEBUG_FUNC_BEGIN();
1449 char* p = (char*) arg;
1451 EM_DEBUG_FUNC_END();
1454 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1456 if ((hStmt == NULL) || (index < 0)) {
1457 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1461 #ifdef _PRINT_STORAGE_LOG_
1462 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1466 if (value != NULL) {
1467 if (strlen(value) <= max_len)
1468 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1470 char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1472 EM_DEBUG_EXCEPTION("em_malloc failed");
1475 snprintf(buf, max_len-1, "%s", value);
1476 ret = sqlite3_bind_text(hStmt, index+1, buf, -1, _bind_stmt_free_string);
1479 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1481 if (ret != SQLITE_OK) {
1482 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1489 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1491 if ((hStmt == NULL) || (index < 0)) {
1492 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1496 #ifdef _PRINT_STORAGE_LOG_
1497 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1502 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1504 ret = sqlite3_bind_null(hStmt, index+1);
1506 if (ret != SQLITE_OK) {
1507 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1514 static int _delete_temp_file(const char *path)
1516 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1519 struct dirent *entry = NULL;
1521 char buf[1024] = {0x00, };
1523 if ((dp = opendir(path)) == NULL) {
1524 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1528 while ((entry = readdir(dp)) != NULL) {
1529 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1534 EM_DEBUG_FUNC_END();
1538 char *cpy_str(char *src)
1543 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1544 EM_DEBUG_EXCEPTION("mailoc failed...");
1547 strncpy(p, src, EM_SAFE_STRLEN(src));
1553 static void _emstorage_close_once(void)
1555 EM_DEBUG_FUNC_BEGIN();
1557 EM_DEBUG_FUNC_END();
1560 INTERNAL_FUNC int emstorage_close(int *err_code)
1562 EM_DEBUG_FUNC_BEGIN();
1565 int error = EMAIL_ERROR_NONE;
1567 if (!emstorage_db_close(NULL, &error))
1569 if (--_open_counter == 0)
1570 _emstorage_close_once();
1574 if (err_code != NULL)
1577 EM_DEBUG_FUNC_END("ret [%d]", ret);
1581 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1583 EM_DEBUG_FUNC_BEGIN();
1585 int error = EMAIL_ERROR_NONE;
1587 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1588 char buf[MAX_PATH] = {0};
1589 char *prefix_path = NULL;
1591 error = emcore_get_container_path(multi_user_name, &prefix_path);
1592 if (error != EMAIL_ERROR_NONE) {
1593 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1597 memset(buf, 0x00, sizeof(buf));
1598 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAILPATH);
1599 mkdir(buf, DIRECTORY_PERMISSION);
1601 memset(buf, 0x00, sizeof(buf));
1602 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILHOME);
1603 mkdir(buf, DIRECTORY_PERMISSION);
1605 memset(buf, 0x00, sizeof(buf));
1606 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILTEMP);
1607 mkdir(buf, DIRECTORY_PERMISSION);
1609 _delete_temp_file(buf);
1610 EM_SAFE_FREE(prefix_path);
1612 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1613 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1614 mkdir(MAILHOME, DIRECTORY_PERMISSION);
1615 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1617 _delete_temp_file(MAILTEMP);
1620 if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1621 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1628 if (err_code != NULL)
1634 /* pData : a parameter which is registered when busy handler is registerd */
1635 /* count : retry count */
1636 #define EMAIL_STORAGE_MAX_RETRY_COUNT 20
1638 static int _callback_sqlite_busy_handler(void *pData, int retry)
1640 if (retry < EMAIL_STORAGE_MAX_RETRY_COUNT) {
1641 EM_DEBUG_LOG("Busy handler called!!: [%d]", retry);
1646 EM_DEBUG_EXCEPTION("Invalid Busy handler called:: [%d]", retry);
1650 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1651 int length_text_b, const void *text_b)
1653 EM_DEBUG_FUNC_BEGIN();
1655 char *converted_string_a = NULL;
1656 char *converted_string_b = NULL;
1658 EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1659 converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1660 EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1662 EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1663 converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1664 EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1666 if (converted_string_a && converted_string_b)
1667 result = strcmp(converted_string_a, converted_string_b);
1669 EM_SAFE_FREE(converted_string_a);
1670 EM_SAFE_FREE(converted_string_b);
1672 EM_DEBUG_FUNC_END();
1676 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1678 EM_DEBUG_FUNC_BEGIN();
1680 int error = EMAIL_ERROR_NONE;
1683 if (!emstorage_delete_file(db_file_path, &error)) {
1684 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1685 EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1690 if (!emstorage_delete_dir(MAILHOME, &error)) {
1691 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1700 EM_DEBUG_FUNC_END();
1704 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1706 EM_DEBUG_FUNC_BEGIN();
1708 int error = EMAIL_ERROR_NONE;
1711 /* Delete all files and directories */
1712 if (!_delete_all_files_and_directories(db_file_path, &error)) {
1713 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1717 /* Delete all accounts on MyAccount */
1719 /* Delete all managed connection to DB */
1720 emstorage_reset_db_handle_list();
1727 EM_DEBUG_FUNC_END();
1731 int _xsystem(const char *argv[])
1739 perror("fork failed");
1742 execvp(argv[0], (char *const *)argv);
1749 if (waitpid(pid, &status, 0) == -1) {
1750 perror("waitpid failed");
1754 if (WIFSIGNALED(status)) {
1759 if (!WIFEXITED(status)) {
1760 perror("should not happen");
1764 return WEXITSTATUS(status);
1767 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1769 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1771 EM_DEBUG_FUNC_BEGIN();
1773 int error = EMAIL_ERROR_NONE;
1776 EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1778 if (*sqlite_handle) { /*prevent 33351*/
1779 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1780 if (err_code != NULL)
1785 /* Generate db file */
1787 ret = stat(db_file_path, &sts);
1788 if (ret == -1 && errno == ENOENT) {
1789 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1790 ret = _xsystem(argv_script);
1793 EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1796 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1797 if (SQLITE_OK != rc) {
1798 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1799 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
1800 error = EMAIL_ERROR_PERMISSION_DENIED;
1802 error = EMAIL_ERROR_DB_FAILURE;
1804 sqlite3_close(*sqlite_handle);
1805 *sqlite_handle = NULL;
1807 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1808 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1809 if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1810 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1814 EM_DEBUG_LOG("Open DB again");
1815 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1816 if (SQLITE_OK != rc) {
1817 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1818 if (SQLITE_PERM == rc) {
1819 error = EMAIL_ERROR_PERMISSION_DENIED;
1821 error = EMAIL_ERROR_DB_FAILURE;
1823 sqlite3_close(*sqlite_handle);
1824 *sqlite_handle = NULL;
1825 goto FINISH_OFF; /*prevent 33351*/
1832 EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1833 /* register busy handler */
1834 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1835 if (SQLITE_OK != rc) {
1836 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1837 error = EMAIL_ERROR_DB_FAILURE;
1838 sqlite3_close(*sqlite_handle);
1839 *sqlite_handle = NULL;
1843 /* Register collation callback function */
1844 rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1845 if (SQLITE_OK != rc) {
1846 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1847 error = EMAIL_ERROR_DB_FAILURE;
1848 sqlite3_close(*sqlite_handle);
1849 *sqlite_handle = NULL;
1856 if (err_code != NULL)
1859 EM_DEBUG_FUNC_END("ret [%d]", ret);
1863 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1865 EM_DEBUG_FUNC_BEGIN();
1867 sqlite3 *_db_handle = NULL;
1869 int error = EMAIL_ERROR_NONE;
1870 char *prefix_path = NULL;
1872 _db_handle = emstorage_get_db_handle(multi_user_name);
1874 if( _db_handle == NULL) {
1875 char *output_file_path = NULL;
1876 char temp_file_path[MAX_PATH] = {0};
1878 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1879 error = emcore_get_container_path(multi_user_name, &prefix_path);
1880 if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1881 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1885 prefix_path = strdup("");
1888 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1889 if ((error = emcore_get_canonicalize_path(EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1890 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1894 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1895 EM_SAFE_FREE(output_file_path);
1897 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1900 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1901 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1905 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1907 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1908 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1909 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1911 emstorage_set_db_handle(multi_user_name, _db_handle);
1913 emstorage_initialize_field_count();
1918 EM_SAFE_FREE(prefix_path);
1920 if (err_code != NULL)
1923 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1927 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1929 EM_DEBUG_FUNC_BEGIN();
1930 #ifdef _MULTIPLE_DB_HANDLE
1931 sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1934 int error = EMAIL_ERROR_NONE;
1938 ret = sqlite3_close(_db_handle);
1939 if (ret != SQLITE_OK) {
1940 EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1941 error = EMAIL_ERROR_DB_FAILURE;
1945 #ifdef _MULTIPLE_DB_HANDLE
1946 emstorage_remove_db_handle();
1954 if (err_code != NULL)
1957 EM_DEBUG_FUNC_END("ret [%d]", ret);
1961 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
1963 EM_DEBUG_FUNC_BEGIN();
1966 int error = EMAIL_ERROR_NONE;
1968 char *prefix_path = NULL;
1969 char buf[MAX_PATH] = {0};
1971 if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
1972 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
1974 error = emcore_get_container_path(multi_user_name, &prefix_path);
1975 if (error != EMAIL_ERROR_NONE) {
1976 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1979 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
1982 retValue = mkdir(buf, DIRECTORY_PERMISSION);
1984 EM_DEBUG_LOG("mkdir return- %d", retValue);
1985 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
1987 if (emstorage_db_open(multi_user_name, &error) == NULL) {
1988 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
1992 if (_open_counter++ == 0) {
1993 _emstorage_open_once(multi_user_name, &error);
2000 EM_SAFE_FREE(prefix_path);
2002 if (err_code != NULL)
2005 EM_DEBUG_FUNC_END("ret [%d]", ret);
2009 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2011 EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2012 int err = EMAIL_ERROR_NONE;
2013 int field_count = 0;
2016 if(input_create_table_query == NULL || output_field_count == NULL) {
2017 err = EMAIL_ERROR_INVALID_PARAM;
2021 pos = input_create_table_query;
2025 if(pos == NULL || *pos == (char)0)
2028 pos = strchr(pos, ',');
2031 *output_field_count = field_count;
2033 EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2037 EM_DEBUG_FUNC_END("err [%d]", err);
2041 INTERNAL_FUNC int emstorage_initialize_field_count()
2043 EM_DEBUG_FUNC_BEGIN();
2044 int err = EMAIL_ERROR_NONE;
2046 char **create_table_query = NULL;
2048 if(_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2049 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2053 err = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2054 if (err != EMAIL_ERROR_NONE) {
2055 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2059 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL]));
2060 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_BOX_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_BOX_TBL]));
2061 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL]));
2062 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_RULE_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_RULE_TBL]));
2063 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2064 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_ATTACHMENT_TBL]));
2065 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL]));
2066 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL]));
2067 #ifdef __FEATURE_LOCAL_ACTIVITY__
2068 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]));
2069 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2070 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_CERTIFICATE_TBL]));
2071 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TASK_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TASK_TBL]));
2072 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TEXT_TBL]));
2075 if (create_table_query) {
2077 for (i = 0; i < query_len; i++) {
2078 if (create_table_query[i]) {
2079 EM_SAFE_FREE(create_table_query[i]);
2082 EM_SAFE_FREE(create_table_query);
2085 EM_DEBUG_FUNC_END("err [%d]", err);
2089 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2091 EM_DEBUG_FUNC_BEGIN();
2093 int error = EMAIL_ERROR_NONE;
2094 int rc = -1, ret = false;
2096 char sql_query_string[QUERY_SIZE] = {0, };
2097 char **create_table_query = NULL;
2099 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2101 error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2102 if (error != EMAIL_ERROR_NONE) {
2103 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2107 if (query_len < CREATE_TABLE_MAX) {
2108 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2109 error = EMAIL_ERROR_SYSTEM_FAILURE;
2113 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2116 char **result = NULL;
2118 /* 1. create mail_account_tbl */
2119 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2120 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2121 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2122 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2124 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2126 if (atoi(result[1]) < 1) {
2127 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2128 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2129 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2131 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2132 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2133 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2134 if (error != EMAIL_ERROR_NONE) {
2135 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2139 /* create mail_account_tbl unique index */
2140 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2141 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2142 if (error != EMAIL_ERROR_NONE) {
2143 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2147 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2148 } /* mail_account_tbl */
2149 else if (type == EMAIL_CREATE_DB_CHECK) {
2150 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2151 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)));
2154 sqlite3_free_table(result);
2157 /* 2. create mail_box_tbl */
2158 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2160 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2161 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2162 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2164 if (atoi(result[1]) < 1) {
2165 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2166 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2167 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2169 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2171 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2172 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2173 if (error != EMAIL_ERROR_NONE) {
2174 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2178 /* create mail_local_mailbox_tbl unique index */
2179 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2180 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2181 if (error != EMAIL_ERROR_NONE) {
2182 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2186 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2187 } /* mail_box_tbl */
2188 else if (type == EMAIL_CREATE_DB_CHECK) {
2189 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2190 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2191 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2193 sqlite3_free_table(result);
2196 /* 3. create mail_read_mail_uid_tbl */
2197 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2198 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2199 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2200 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2203 if (atoi(result[1]) < 1) {
2204 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2205 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2206 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2208 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2210 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2211 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2212 if (error != EMAIL_ERROR_NONE) {
2213 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2217 /* create mail_read_mail_uid_tbl unique index */
2218 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2219 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2220 if (error != EMAIL_ERROR_NONE) {
2221 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2225 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2226 } /* mail_read_mail_uid_tbl */
2227 else if (type == EMAIL_CREATE_DB_CHECK) {
2228 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2229 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2230 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2232 sqlite3_free_table(result);
2235 /* 4. create mail_rule_tbl */
2236 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2238 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2239 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2240 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2242 if (atoi(result[1]) < 1) {
2243 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2244 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2245 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2247 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2249 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2250 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2251 if (error != EMAIL_ERROR_NONE) {
2252 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2256 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2257 } /* mail_rule_tbl */
2258 else if (type == EMAIL_CREATE_DB_CHECK) {
2259 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2260 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2261 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2263 sqlite3_free_table(result);
2266 /* 5. create mail_tbl */
2267 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2268 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2269 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2270 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2272 if (atoi(result[1]) < 1) {
2273 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2274 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2275 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2276 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2278 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2279 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2280 if (error != EMAIL_ERROR_NONE) {
2281 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2285 /* create mail_tbl unique index */
2286 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2287 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2288 if (error != EMAIL_ERROR_NONE) {
2289 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2293 /* create mail_tbl index for date_time */
2294 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2295 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2296 if (error != EMAIL_ERROR_NONE) {
2297 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2301 /* create mail_tbl index for thread_item_count */
2302 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2303 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2304 if (error != EMAIL_ERROR_NONE) {
2305 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2309 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2310 /* just one time call */
2311 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2313 else if (type == EMAIL_CREATE_DB_CHECK) {
2314 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2315 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2316 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2318 sqlite3_free_table(result);
2321 /* 6. create mail_attachment_tbl */
2322 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2323 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2324 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2325 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2327 if (atoi(result[1]) < 1) {
2328 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2329 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2330 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2332 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2334 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2335 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2336 if (error != EMAIL_ERROR_NONE) {
2337 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2341 /* create mail_attachment_tbl unique index */
2342 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2343 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2344 if (error != EMAIL_ERROR_NONE) {
2345 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2349 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2350 } /* mail_attachment_tbl */
2351 else if (type == EMAIL_CREATE_DB_CHECK) {
2352 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2353 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2354 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2356 sqlite3_free_table(result);
2359 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2361 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2362 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2363 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2364 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2366 if (atoi(result[1]) < 1) {
2368 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2369 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2370 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2372 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2374 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2375 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2376 if (error != EMAIL_ERROR_NONE) {
2377 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2381 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2382 } /* mail_rule_tbl */
2383 else if (type == EMAIL_CREATE_DB_CHECK) {
2384 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2385 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2386 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2388 sqlite3_free_table(result);
2391 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2393 /* create mail_meeting_tbl */
2394 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2395 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2396 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2397 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2399 if (atoi(result[1]) < 1) {
2400 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2401 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2402 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2404 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2406 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2407 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2408 if (error != EMAIL_ERROR_NONE) {
2409 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2413 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2414 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2415 if (error != EMAIL_ERROR_NONE) {
2416 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2420 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2421 } /* mail_contact_sync_tbl */
2422 else if (type == EMAIL_CREATE_DB_CHECK) {
2423 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2424 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2425 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2427 sqlite3_free_table(result);
2430 #ifdef __FEATURE_LOCAL_ACTIVITY__
2432 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2433 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2434 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2435 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2437 if (atoi(result[1]) < 1) {
2439 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2440 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2441 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2443 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2445 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2446 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2447 if (error != EMAIL_ERROR_NONE) {
2448 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2452 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2453 } /* mail_rule_tbl */
2454 else if (type == EMAIL_CREATE_DB_CHECK) {
2455 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2456 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2457 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2458 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2460 sqlite3_free_table(result);
2462 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2463 /* create mail_certificate_tbl */
2464 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2465 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2466 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2467 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)));
2469 if (atoi(result[1]) < 1) {
2470 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2471 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2473 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2475 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2476 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2477 if (error != EMAIL_ERROR_NONE) {
2478 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2482 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2483 } /* mail_contact_sync_tbl */
2484 else if (type == EMAIL_CREATE_DB_CHECK) {
2485 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2486 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)));
2489 sqlite3_free_table(result);
2492 /* create mail_task_tbl */
2493 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2494 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2495 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)));
2497 if (atoi(result[1]) < 1) {
2498 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2499 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2501 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2503 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2504 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2505 if (error != EMAIL_ERROR_NONE) {
2506 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2510 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2511 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2512 if (error != EMAIL_ERROR_NONE) {
2513 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2517 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2518 } /* mail_task_tbl */
2519 else if (type == EMAIL_CREATE_DB_CHECK) {
2520 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2521 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)));
2524 sqlite3_free_table(result);
2527 #ifdef __FEATURE_BODY_SEARCH__
2528 /* create mail_text_tbl */
2529 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2530 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2531 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)));
2533 if (atoi(result[1]) < 1) {
2534 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2535 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2537 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2539 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2540 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2541 if (error != EMAIL_ERROR_NONE) {
2542 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2546 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2547 } /* mail_text_tbl */
2548 else if (type == EMAIL_CREATE_DB_CHECK) {
2549 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2550 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)));
2553 sqlite3_free_table(result);
2558 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2559 /* create mail_auto_download_activity_tbl */
2560 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2561 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2562 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)));
2564 if (atoi(result[1]) < 1) {
2565 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2566 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2568 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2570 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2571 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2572 if (error != EMAIL_ERROR_NONE) {
2573 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2577 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2578 } /* mail_auto_download_activity_tbl */
2579 else if (type == EMAIL_CREATE_DB_CHECK) {
2580 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2581 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_AUTO_DOWNLOAD_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2584 sqlite3_free_table(result);
2593 if(result) sqlite3_free_table(result);
2596 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2599 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2603 if (create_table_query) {
2605 for (i = 0; i < query_len; i++) {
2606 if (create_table_query[i]) {
2607 EM_SAFE_FREE(create_table_query[i]);
2610 EM_SAFE_FREE(create_table_query);
2613 if (err_code != NULL)
2616 EM_DEBUG_FUNC_END("ret [%d]", ret);
2620 /* Query series --------------------------------------------------------------*/
2621 INTERNAL_FUNC int emstorage_query_mail_count(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
2623 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);
2626 int error = EMAIL_ERROR_NONE;
2627 DB_STMT hStmt = NULL;
2628 char *sql_query_string = NULL;
2630 sqlite3 *local_db_handle = NULL;
2632 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2633 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2634 return EMAIL_ERROR_INVALID_PARAM;
2637 query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2638 sql_query_string = em_malloc(query_size);
2639 if (sql_query_string == NULL) {
2640 EM_DEBUG_EXCEPTION("em_malloc failed");
2641 error = EMAIL_ERROR_OUT_OF_MEMORY;
2645 local_db_handle = emstorage_get_db_connection(multi_user_name);
2647 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2649 SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2650 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2652 if (output_total_mail_count) {
2653 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2654 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2655 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2657 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2658 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2659 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2660 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2663 if (output_unseen_mail_count) {
2664 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2666 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2667 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2668 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2670 *output_unseen_mail_count = atoi(result[1]);
2671 sqlite3_free_table(result);
2676 if (hStmt != NULL) {
2677 rc = sqlite3_finalize(hStmt);
2678 if (rc != SQLITE_OK) {
2679 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
2680 error = EMAIL_ERROR_DB_FAILURE;
2684 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2686 EM_SAFE_FREE(sql_query_string);
2688 EM_DEBUG_FUNC_END("error [%d]", error);
2692 INTERNAL_FUNC int emstorage_query_mail_id_list(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
2694 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);
2702 int error = EMAIL_ERROR_NONE;
2703 int *result_mail_id_list = NULL;
2704 char **result = NULL;
2705 char *sql_query_string = NULL;
2706 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2708 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2709 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2710 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2712 query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10; // 10 is extra space
2713 sql_query_string = em_malloc(query_size);
2714 if (sql_query_string == NULL) {
2715 EM_DEBUG_EXCEPTION("em_malloc failed");
2716 error = EMAIL_ERROR_OUT_OF_MEMORY;
2720 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2722 /* Composing query */
2723 SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2724 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2726 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2728 /* Performing query */
2729 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2730 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2731 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2735 /* to get mail list */
2737 EM_DEBUG_LOG("No mail found...");
2738 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2742 EM_DEBUG_LOG("There are [%d] mails.", count);
2744 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2745 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2746 error = EMAIL_ERROR_OUT_OF_MEMORY;
2750 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2752 for (i = 0; i < count; i++)
2753 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2755 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2757 *output_mail_id_list = result_mail_id_list;
2758 *output_mail_id_count = count;
2763 sqlite3_free_table(result);
2765 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2767 EM_SAFE_FREE(sql_query_string);
2769 if(error != EMAIL_ERROR_NONE)
2770 EM_SAFE_FREE(result_mail_id_list);
2772 EM_DEBUG_FUNC_END("error [%d]", error);
2776 INTERNAL_FUNC int emstorage_query_mail_list(char *multi_user_name, const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list, int *result_count, int *err_code)
2778 EM_DEBUG_FUNC_BEGIN();
2779 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2781 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list)?0:1;
2782 int sql_query_string_length = 0;
2783 int local_inline_content_count = 0, local_attachment_count = 0;
2784 int cur_query = 0, base_count = 0, col_index;
2785 int ret = false, error = EMAIL_ERROR_NONE;
2786 char *date_time_string = NULL;
2787 char **result = NULL;
2788 char *field_mail_id = "mail_id";
2789 char *field_all = "mail_id, account_id, mailbox_id, mailbox_type, full_address_from, email_address_sender, full_address_to, subject, body_download_status, mail_size, 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, remaining_resend_times, tag_id, eas_data_length, eas_data ";
2790 char *select_query_form = "SELECT %s FROM mail_tbl ";
2791 char *target_field = NULL;
2792 char *sql_query_string = NULL;
2793 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2794 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2796 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2797 EM_IF_NULL_RETURN_VALUE(result_count, false);
2799 EMSTORAGE_START_READ_TRANSACTION(transaction);
2802 if (to_get_count) /* count only */
2803 target_field = field_mail_id;
2804 else /* mail list in plain form */
2805 target_field = field_all;
2807 sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2809 if(sql_query_string_length)
2810 sql_query_string = em_malloc(sql_query_string_length);
2812 if(sql_query_string == NULL) {
2813 EM_DEBUG_EXCEPTION("em_malloc failed...");
2814 error = EMAIL_ERROR_OUT_OF_MEMORY;
2818 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2820 strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2822 EM_DEBUG_LOG_DEV ("query[%s]", sql_query_string);
2824 /* performing query */
2825 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2826 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2827 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2828 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2829 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2835 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2839 col_index = base_count;
2841 EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2846 EM_DEBUG_LOG_DEV ("No mail found...");
2848 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2851 EM_DEBUG_LOG_DEV ("There are [%d] mails.", count);
2854 /* to get mail list */
2856 EM_DEBUG_LOG_DEV ("No mail found...");
2858 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2862 EM_DEBUG_LOG_DEV ("There are [%d] mails.", count);
2863 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2864 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2865 error = EMAIL_ERROR_OUT_OF_MEMORY;
2869 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2870 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >> ");
2871 for (i = 0; i < count; i++) {
2872 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2873 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2874 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2875 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2876 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2877 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2878 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2879 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2880 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2881 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2882 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2883 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2884 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2885 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2886 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2887 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2888 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2889 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2890 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2891 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2892 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2893 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2894 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2895 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2896 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2897 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2898 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2899 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2900 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2901 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2902 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2903 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2904 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2905 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2906 _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++);
2908 mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1:0;
2910 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2911 EM_PROFILE_END(emstorage_query_mail_list_loop);
2917 EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2920 sqlite3_free_table(result);
2923 *result_count = count;
2926 if (result_mail_list)
2927 *result_mail_list = mail_list_item_from_tbl;
2928 *result_count = count;
2931 EM_SAFE_FREE(mail_list_item_from_tbl);
2934 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2936 // sqlite3_db_release_memory(local_db_handle);
2938 EM_SAFE_FREE(sql_query_string);
2939 EM_SAFE_FREE(date_time_string);
2941 if (err_code != NULL)
2944 EM_PROFILE_END(emstorage_query_mail_list_func);
2945 EM_DEBUG_FUNC_END("ret [%d]", ret);
2950 INTERNAL_FUNC int emstorage_query_mail_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
2952 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);
2954 if (!conditional_clause || !result_mail_tbl || !result_count) {
2955 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
2956 if (err_code != NULL)
2957 *err_code = EMAIL_ERROR_INVALID_PARAM;
2961 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2962 int error = EMAIL_ERROR_NONE;
2963 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2964 emstorage_mail_tbl_t* p_data_tbl = NULL;
2965 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2967 EMSTORAGE_START_READ_TRANSACTION(transaction);
2969 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2971 EM_DEBUG_LOG_DEV ("Query[%s]", sql_query_string);
2973 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2974 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2975 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2978 EM_DEBUG_LOG("No mail found...");
2980 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2984 EM_DEBUG_LOG("There are [%d] mails.", count);
2985 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2986 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2987 error = EMAIL_ERROR_OUT_OF_MEMORY;
2991 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >> ");
2992 for (i = 0; i < count; i++) {
2993 _get_table_field_data_int (result, &(p_data_tbl[i].mail_id), col_index++);
2994 _get_table_field_data_int (result, &(p_data_tbl[i].account_id), col_index++);
2995 _get_table_field_data_int (result, &(p_data_tbl[i].mailbox_id), col_index++);
2996 _get_table_field_data_int (result, &(p_data_tbl[i].mailbox_type), col_index++);
2997 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
2998 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
2999 _get_table_field_data_int (result, &(p_data_tbl[i].server_mail_status), col_index++);
3000 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3001 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3002 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3003 _get_table_field_data_int (result, &(p_data_tbl[i].reference_mail_id), col_index++);
3004 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3005 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3006 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3007 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3008 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3009 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3010 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3011 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3012 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3013 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3014 _get_table_field_data_int (result, &(p_data_tbl[i].body_download_status), col_index++);
3015 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3016 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3017 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3018 _get_table_field_data_int (result, &(p_data_tbl[i].mail_size), col_index++);
3019 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3020 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3021 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3022 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3023 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3024 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3025 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3026 _get_table_field_data_int (result, &(p_data_tbl[i].DRM_status), col_index++);
3027 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].priority), col_index++);
3028 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].save_status), col_index++);
3029 _get_table_field_data_int (result, &(p_data_tbl[i].lock_status), col_index++);
3030 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].report_status), col_index++);
3031 _get_table_field_data_int (result, &(p_data_tbl[i].attachment_count), col_index++);
3032 _get_table_field_data_int (result, &(p_data_tbl[i].inline_content_count), col_index++);
3033 _get_table_field_data_int (result, &(p_data_tbl[i].thread_id), col_index++);
3034 _get_table_field_data_int (result, &(p_data_tbl[i].thread_item_count), col_index++);
3035 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3036 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3037 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].message_class), col_index++);
3038 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3039 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3040 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3041 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3042 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3043 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3044 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3045 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3046 _get_table_field_data_int (result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3047 _get_table_field_data_blob (result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3048 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3055 sqlite3_free_table(result);
3058 *result_mail_tbl = p_data_tbl;
3059 *result_count = count;
3062 *result_mail_tbl = NULL;
3066 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3068 // sqlite3_db_release_memory(local_db_handle);
3070 if (err_code != NULL)
3073 EM_DEBUG_FUNC_END("ret [%d]", ret);
3077 #ifdef __FEATURE_BODY_SEARCH__
3078 INTERNAL_FUNC int emstorage_query_mail_text_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code)
3080 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);
3082 if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3083 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3084 if (err_code != NULL)
3085 *err_code = EMAIL_ERROR_INVALID_PARAM;
3093 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3094 int error = EMAIL_ERROR_NONE;
3095 char **result = NULL;
3096 char sql_query_string[QUERY_SIZE] = {0, };
3097 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3098 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3100 EMSTORAGE_START_READ_TRANSACTION(transaction);
3102 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3104 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3106 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3107 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3108 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3111 EM_DEBUG_LOG("No mail found...");
3113 error= EMAIL_ERROR_MAIL_NOT_FOUND;
3117 EM_DEBUG_LOG("There are [%d] mails.", count);
3118 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3119 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3120 error = EMAIL_ERROR_OUT_OF_MEMORY;
3124 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >>");
3125 for (i = 0; i < count; i++) {
3126 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3127 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3128 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3129 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3136 sqlite3_free_table(result);
3139 *result_mail_text_tbl = p_data_tbl;
3140 *result_count = count;
3143 EM_SAFE_FREE(p_data_tbl);
3145 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3147 // sqlite3_db_release_memory(local_db_handle);
3149 if (err_code != NULL)
3152 EM_DEBUG_FUNC_END("ret [%d]", ret);
3157 INTERNAL_FUNC int emstorage_query_mailbox_tbl(char *multi_user_name, 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)
3159 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);
3165 int error = EMAIL_ERROR_NONE;
3167 int total_count = 0;
3169 char sql_query_string[QUERY_SIZE] = {0, };
3170 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, MBT.eas_data_length, MBT.eas_data ";
3171 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3172 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3174 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3176 if (input_get_mail_count == 0) { /* without mail count */
3178 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);
3180 else { /* with read count and total count */
3182 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 WHERE flags_deleted_field = 0 GROUP BY mailbox_id) AS MT ON MBT.mailbox_id = MT.mailbox_id %s %s", fields, input_conditional_clause, input_ordering_clause);
3185 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3187 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3188 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3189 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3191 EM_DEBUG_LOG_DEV("result count [%d]", count);
3194 EM_DEBUG_LOG_SEC ("Can't find mailbox query[%s]", sql_query_string);
3195 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3199 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3200 EM_DEBUG_EXCEPTION("em_malloc failed...");
3201 error = EMAIL_ERROR_OUT_OF_MEMORY;
3205 for (i = 0; i < count; i++) {
3206 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3207 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3208 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3209 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3210 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3211 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3212 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3213 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3214 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3215 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3216 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3217 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3218 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3219 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3220 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3222 if (input_get_mail_count == 1) {
3223 _get_table_field_data_int(result, &(total_count), col_index++);
3224 p_data_tbl[i].total_mail_count_on_local = total_count;
3225 _get_table_field_data_int(result, &(read_count), col_index++);
3226 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
3233 sqlite3_free_table(result);
3235 if (error == EMAIL_ERROR_NONE) {
3236 *output_mailbox_list = p_data_tbl;
3237 *output_mailbox_count = count;
3238 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3241 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3243 // sqlite3_db_release_memory(local_db_handle);
3245 EM_DEBUG_FUNC_END("error [%d]", error);
3249 /* Query series --------------------------------------------------------------*/
3250 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3252 EM_DEBUG_FUNC_BEGIN();
3253 int rc = -1, ret = false;
3254 int error = EMAIL_ERROR_NONE;
3257 char sql_query_string[QUERY_SIZE] = {0, };
3258 sqlite3 *local_db_handle = NULL;
3260 if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL||
3261 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3262 if (err_code != NULL)
3263 *err_code = EMAIL_ERROR_INVALID_PARAM;
3267 local_db_handle = emstorage_get_db_connection(multi_user_name);
3269 EMSTORAGE_START_READ_TRANSACTION(transaction);
3271 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3272 "SELECT COUNT(*) FROM mail_account_tbl "
3274 " user_email_address = '%s' AND "
3275 " incoming_server_user_name = '%s' AND "
3276 " incoming_server_type = %d AND "
3277 " incoming_server_address = '%s' AND "
3278 " outgoing_server_user_name = '%s' AND "
3279 " outgoing_server_type = %d AND "
3280 " outgoing_server_address = '%s'; ",
3281 account->user_email_address,
3282 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3283 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3285 EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3286 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3287 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3288 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3290 count = atoi(result[1]);
3291 sqlite3_free_table(result);
3293 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3295 if (count == 0) { /* not duplicated account */
3297 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3299 else { /* duplicated account */
3301 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3302 error = EMAIL_ERROR_ALREADY_EXISTS;
3307 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3309 if (err_code != NULL)
3312 EM_DEBUG_FUNC_END("ret [%d]", ret);
3317 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3319 EM_DEBUG_FUNC_BEGIN();
3322 if (err_code != NULL)
3323 *err_code = EMAIL_ERROR_INVALID_PARAM;
3327 int rc = -1, ret = false;
3328 int error = EMAIL_ERROR_NONE;
3330 DB_STMT hStmt = NULL;
3331 char sql_query_string[QUERY_SIZE] = {0, };
3334 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3335 EMSTORAGE_START_READ_TRANSACTION(transaction);
3336 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3337 EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3338 EMSTORAGE_PROTECTED_FUNC_CALL (sqlite3_prepare_v2 (local_db_handle, sql_query_string,
3339 EM_SAFE_STRLEN (sql_query_string), &hStmt, NULL), rc);
3340 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3341 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3342 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3344 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3345 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3346 ("sqlite3_step fail:%d", rc));
3348 *count = sqlite3_column_int(hStmt, 0);
3354 if (hStmt != NULL) {
3355 rc = sqlite3_finalize(hStmt);
3357 if (rc != SQLITE_OK) {
3358 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
3359 error = EMAIL_ERROR_DB_FAILURE;
3363 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3365 if (err_code != NULL)
3368 EM_DEBUG_FUNC_END("ret [%d]", ret);
3372 INTERNAL_FUNC int emstorage_get_account_list(char *multi_user_name, int *select_num, emstorage_account_tbl_t **account_list, int transaction, int with_password, int *err_code)
3374 EM_DEBUG_FUNC_BEGIN();
3376 int i = 0, count = 0, rc = -1, ret = false;
3377 int field_index = 0;
3379 int error = EMAIL_ERROR_NONE;
3380 emstorage_account_tbl_t *p_data_tbl = NULL;
3381 char sql_query_string[QUERY_SIZE] = {0, };
3382 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3384 sqlite3 *local_db_handle = NULL;
3385 DB_STMT hStmt = NULL;
3387 if (!select_num || !account_list) {
3388 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3389 if (err_code != NULL)
3390 *err_code = EMAIL_ERROR_INVALID_PARAM;
3394 local_db_handle = emstorage_get_db_connection(multi_user_name);
3396 EMSTORAGE_START_READ_TRANSACTION(transaction);
3398 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3399 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3400 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3402 count = atoi(result[1]);
3403 sqlite3_free_table(result);
3405 EM_DEBUG_LOG_DEV("count = %d", rc);
3408 EM_DEBUG_LOG("no account found...");
3409 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3413 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3414 sql_len = EM_SAFE_STRLEN(sql_query_string);
3416 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3421 "user_data_length, "
3425 "default_mail_slot_size, "
3428 "user_display_name, "
3429 "user_email_address, "
3430 "reply_to_address, "
3432 "incoming_server_type, "
3433 "incoming_server_address, "
3434 "incoming_server_port_number, "
3435 "incoming_server_user_name, "
3436 "incoming_server_password, "
3437 "incoming_server_secure_connection, "
3438 "incoming_server_authentication_method, "
3440 "keep_mails_on_pop_server_after_download, "
3442 "auto_download_size, "
3447 "outgoing_server_type, "
3448 "outgoing_server_address, "
3449 "outgoing_server_port_number, "
3450 "outgoing_server_user_name, "
3451 "outgoing_server_password, "
3452 "outgoing_server_secure_connection, "
3453 "outgoing_server_need_authentication, "
3454 "outgoing_server_use_same_authenticator, "
3457 "req_delivery_receipt, "
3458 "req_read_receipt, "
3462 "display_name_from, "
3464 "forward_with_files, "
3468 "add_my_address_to_bcc, "
3469 "notification_status, "
3471 "display_content_status, "
3472 "default_ringtone_status, "
3473 "alert_ringtone_path, "
3474 "auto_resend_times, "
3475 "outgoing_server_size_limit, "
3476 "wifi_auto_download, "
3478 "incoming_server_requires_apop, "
3480 "certificate_path, "
3485 sql_len = EM_SAFE_STRLEN(sql_query_string);
3487 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3489 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3491 EM_DEBUG_LOG_DEV ("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3492 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3493 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3496 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3497 ("sqlite3_step fail:%d", rc));
3498 if (rc == SQLITE_DONE) {
3499 EM_DEBUG_EXCEPTION("no account found...");
3501 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3507 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3508 EM_DEBUG_EXCEPTION("malloc failed...");
3509 error = EMAIL_ERROR_OUT_OF_MEMORY;
3512 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3513 for (i = 0; i < count; i++) {
3516 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3517 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3518 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3519 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3520 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3521 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3522 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3523 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3524 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3525 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3526 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3527 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3528 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3529 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3530 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3531 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3532 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3533 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3534 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3535 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3536 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3537 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3538 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3539 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3540 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3541 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3542 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3543 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3544 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3545 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3546 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3547 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3548 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3549 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3550 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3551 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3552 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3553 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3554 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3555 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3556 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3557 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3558 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3559 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3560 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3561 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3562 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3563 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3564 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3565 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3566 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3567 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3568 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3569 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3570 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3571 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3572 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3573 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3574 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3575 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3576 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3577 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3578 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3579 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3580 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3581 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3583 /* EAS passwd is not accessible */
3584 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3585 /* get password from the secure storage */
3586 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3587 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3589 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3590 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3592 /* get password file name */
3593 error = _get_password_file_name (multi_user_name, p_data_tbl[i].account_id,
3594 recv_password_file_name,
3595 send_password_file_name);
3596 if (error != EMAIL_ERROR_NONE){
3597 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3598 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3602 /* read password from secure storage */
3603 error = _read_password_from_secure_storage (recv_password_file_name,
3604 &(p_data_tbl[i].incoming_server_password));
3606 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3607 recv_password_file_name, error);
3608 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3611 error = _read_password_from_secure_storage (send_password_file_name,
3612 &(p_data_tbl[i].outgoing_server_password));
3614 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3615 send_password_file_name, error);
3616 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3621 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3622 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3623 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3624 ("sqlite3_step fail:%d", rc));
3631 *account_list = p_data_tbl;
3632 *select_num = count;
3633 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3635 else if (p_data_tbl != NULL)
3636 emstorage_free_account(&p_data_tbl, count, NULL);
3638 if (hStmt != NULL) {
3639 rc = sqlite3_finalize(hStmt);
3641 if (rc != SQLITE_OK) {
3642 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3643 error = EMAIL_ERROR_DB_FAILURE;
3647 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3649 if (err_code != NULL)
3652 EM_DEBUG_FUNC_END("ret [%d]", ret);
3656 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name, int mailbox_id, char *server_mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
3658 EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", mailbox_id, server_mail_id, mail, transaction, err_code);
3660 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3661 char conditional_clause[QUERY_SIZE] = {0, };
3662 emstorage_mail_tbl_t* p_data_tbl = NULL;
3664 if (!server_mail_id || !mail) {
3665 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3666 error = EMAIL_ERROR_INVALID_PARAM;
3670 if (mailbox_id == 0)
3671 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
3673 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND mailbox_id = %d", server_mail_id, mailbox_id);
3675 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3677 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
3678 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3688 if (err_code != NULL)
3691 EM_DEBUG_FUNC_END("ret [%d]", ret);
3696 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)
3698 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3700 if (account_id < ALL_ACCOUNT) {
3701 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3702 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3706 if (account_id != ALL_ACCOUNT) {
3707 cur_clause += (conditional_clause_count++ == 0)?
3708 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
3709 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3712 if (mailbox_id > 0) {
3713 cur_clause += (conditional_clause_count++ == 0)?
3714 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
3715 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3718 if (thread_id > 0) {
3719 cur_clause += (conditional_clause_count++ == 0)?
3720 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id):
3721 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3723 else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3724 cur_clause += (conditional_clause_count++ == 0)?
3725 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0"):
3726 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3728 else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3729 cur_clause += (conditional_clause_count++ == 0)?
3730 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0"):
3731 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3733 else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3734 cur_clause += (conditional_clause_count++ == 0)?
3735 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0"):
3736 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3739 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3740 if (addr_list && addr_list->address_count > 0) {
3741 if (!addr_list->address_type) {
3742 cur_clause += (conditional_clause_count++ == 0)?
3743 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]):
3744 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3746 for (i = 1; i < addr_list->address_count; i++)
3747 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3749 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3751 cur_clause += (conditional_clause_count++ == 0)?
3752 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]):
3753 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]);
3755 for (i = 1; i < addr_list->address_count; i++)
3756 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3758 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3762 if (input_except_delete_flagged_mails) {
3763 cur_clause += (conditional_clause_count++ == 0)?
3764 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0"):
3765 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3769 switch (search_type) {
3770 case EMAIL_SEARCH_FILTER_SUBJECT:
3771 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3772 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3774 case EMAIL_SEARCH_FILTER_SENDER:
3775 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3776 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3777 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3779 case EMAIL_SEARCH_FILTER_RECIPIENT:
3780 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3781 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3782 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3783 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3784 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3786 case EMAIL_SEARCH_FILTER_ALL:
3787 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3788 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3789 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3790 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3791 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3792 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3795 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3800 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3801 static char sorting_str[][50] = {
3802 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3803 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3804 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3805 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3806 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3807 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3808 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3809 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3810 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3811 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3812 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3813 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3814 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3815 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3816 " ORDER BY mailbox_id DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3817 " ORDER BY mailbox_id ASC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3818 " ORDER BY flags_flagged_field DESC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3819 " ORDER BY flags_flagged_field ASC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3820 " ORDER BY flags_seen_field DESC, date_time DESC", /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3821 " ORDER BY flags_seen_field ASC, date_time DESC" /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3824 if (sorting < EMAIL_SORT_END && sorting >= 0)
3825 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3827 EM_DEBUG_LOG(" Invalid Sorting order ");
3830 if (start_index != -1 && limit_count != -1)
3831 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3838 * emstorage_get_mail_list - Get the mail list information.
3842 INTERNAL_FUNC int emstorage_get_mail_list(char *multi_user_name, 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)
3844 EM_DEBUG_FUNC_BEGIN();
3845 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3847 int ret = false, error = EMAIL_ERROR_NONE;
3848 char conditional_clause_string[QUERY_SIZE] = { 0, };
3850 if (account_id < ALL_ACCOUNT) {
3851 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3852 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3854 EM_IF_NULL_RETURN_VALUE(result_count, false);
3856 _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);
3858 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3860 if(!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3861 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3868 if (err_code != NULL)
3871 EM_PROFILE_END(emstorage_get_mail_list_func);
3872 EM_DEBUG_FUNC_END("ret [%d]", ret);
3878 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3882 INTERNAL_FUNC int emstorage_get_mails(char *multi_user_name, 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)
3884 EM_DEBUG_FUNC_BEGIN();
3885 EM_PROFILE_BEGIN(emStorageGetMails);
3887 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3888 emstorage_mail_tbl_t *p_data_tbl = NULL;
3889 char conditional_clause_string[QUERY_SIZE] = {0, };
3891 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3892 EM_IF_NULL_RETURN_VALUE(result_count, false);
3894 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3895 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3896 if (err_code != NULL)
3897 *err_code = EMAIL_ERROR_INVALID_PARAM;
3901 _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);
3903 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3905 if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
3906 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3914 *mail_list = p_data_tbl;
3915 *result_count = count;
3916 EM_DEBUG_LOG("COUNT : %d", count);
3918 else if (p_data_tbl != NULL)
3919 emstorage_free_mail(&p_data_tbl, count, NULL);
3921 if (err_code != NULL)
3924 EM_PROFILE_END(emStorageGetMails);
3925 EM_DEBUG_FUNC_END("ret [%d]", ret);
3932 * emstorage_get_searched_mail_list - Get the mail list information after filtering
3936 INTERNAL_FUNC int emstorage_get_searched_mail_list(char *multi_user_name, 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)
3938 EM_DEBUG_FUNC_BEGIN();
3940 int ret = false, error = EMAIL_ERROR_NONE;
3941 char conditional_clause[QUERY_SIZE] = {0, };
3942 char *temp_search_value = NULL;
3943 char *temp_search_value2 = NULL;
3945 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3946 EM_IF_NULL_RETURN_VALUE(result_count, false);
3948 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3949 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3950 error = EMAIL_ERROR_INVALID_PARAM;
3954 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
3955 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
3957 _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);
3959 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3961 if(!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
3962 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3969 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3971 if (err_code != NULL)
3974 EM_SAFE_FREE(temp_search_value);
3975 EM_SAFE_FREE(temp_search_value2);
3977 EM_DEBUG_FUNC_END("ret [%d]", ret);
3982 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
3984 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
3986 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
3987 EM_DEBUG_EXCEPTION("Invalid parameter");
3988 return EMAIL_ERROR_INVALID_PARAM;
3991 EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
3993 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
3994 sprintf(recv_password_file_name, ".email_account_%d_recv_%s", account_id, multi_user_name);
3995 sprintf(send_password_file_name, ".email_account_%d_send_%s", account_id, multi_user_name);
3997 sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
3998 sprintf(send_password_file_name, ".email_account_%d_send", account_id);
4001 EM_DEBUG_FUNC_END();
4002 return EMAIL_ERROR_NONE;
4005 static int _read_password_from_secure_storage(char *file_name, char **password)
4007 EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4009 if (!file_name || !password) {
4010 EM_DEBUG_EXCEPTION("Invalid Parameter");
4011 return EMAIL_ERROR_INVALID_PARAM;
4014 int ret = EMAIL_ERROR_NONE;
4016 ret = emcore_get_password_in_key_manager(file_name, password);
4017 if (ret != EMAIL_ERROR_NONE) {
4018 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4024 EM_DEBUG_FUNC_END("ret [%d]", ret);
4028 INTERNAL_FUNC int emstorage_get_account_by_id(char *multi_user_name, int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
4030 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4033 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4034 if (err_code != NULL)
4035 *err_code = EMAIL_ERROR_INVALID_PARAM;
4040 int error = EMAIL_ERROR_NONE;
4041 emstorage_account_tbl_t* p_data_tbl = NULL;
4042 DB_STMT hStmt = NULL;
4043 char sql_query_string[QUERY_SIZE] = {0, };
4046 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4047 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4049 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4051 EMSTORAGE_START_READ_TRANSACTION(transaction);
4053 /* Make query string */
4054 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4055 sql_len = EM_SAFE_STRLEN(sql_query_string);
4057 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4058 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4059 "incoming_server_type,"
4060 "incoming_server_address,"
4061 "user_email_address,"
4062 "incoming_server_user_name,"
4064 "incoming_server_port_number,"
4065 "incoming_server_secure_connection,"
4066 "incoming_server_authentication_method,"
4067 "outgoing_server_type,"
4068 "outgoing_server_address,"
4069 "outgoing_server_port_number,"
4070 "outgoing_server_need_authentication,"
4071 "outgoing_server_secure_connection,"
4072 "outgoing_server_user_name,"
4073 "user_display_name,"
4077 "keep_mails_on_pop_server_after_download,"
4078 "auto_download_size,"
4083 "outgoing_server_use_same_authenticator,"
4084 "auto_resend_times,"
4085 "outgoing_server_size_limit,"
4086 "wifi_auto_download,"
4088 "incoming_server_requires_apop,"
4095 sql_len = EM_SAFE_STRLEN(sql_query_string);
4098 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4099 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4100 sql_len = EM_SAFE_STRLEN(sql_query_string);
4103 /* get from secure storage, not from db */
4104 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4105 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4108 "req_delivery_receipt,"
4113 "display_name_from,"
4115 "forward_with_files,"
4119 "add_my_address_to_bcc,"
4120 "notification_status,"
4122 "display_content_status,"
4123 "default_ringtone_status,"
4124 "alert_ringtone_path,"
4127 "default_mail_slot_size,"
4134 sql_len = EM_SAFE_STRLEN(sql_query_string);
4137 /* dummy value, FROM WHERE clause */
4138 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4141 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4143 /* execute a sql and count rows */
4144 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4145 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4146 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4148 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4149 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4150 ("sqlite3_step fail:%d", rc));
4152 if (rc == SQLITE_DONE) {
4153 EM_DEBUG_EXCEPTION("no matched account found...");
4154 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4158 /* Assign query result to structure */
4159 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4160 EM_DEBUG_EXCEPTION("malloc failed...");
4161 error = EMAIL_ERROR_OUT_OF_MEMORY;
4165 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4168 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4169 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4170 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
4171 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4172 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4173 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4174 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4175 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4176 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4177 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4178 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4179 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4180 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4181 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4182 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4183 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4184 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4185 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4186 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4187 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4188 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4189 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4190 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4191 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4192 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4193 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4194 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4195 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4196 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4197 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4198 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4199 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4200 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4201 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4202 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4203 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4204 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4207 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4208 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4210 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4211 /* get password file name */
4212 if ((error = _get_password_file_name(multi_user_name, p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4213 EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4217 /* read password from secure storage */
4218 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4219 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4223 EM_DEBUG_LOG_DEV ("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
4225 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4226 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4227 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4230 EM_DEBUG_LOG_DEV ("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
4234 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4235 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4236 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4237 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4238 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4239 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4240 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4241 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4242 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4243 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4244 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4245 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4246 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4247 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4248 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4249 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4250 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4251 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4252 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4253 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4254 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4255 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4256 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4257 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4258 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4259 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4260 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4261 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4268 *account = p_data_tbl;
4271 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4273 if (hStmt != NULL) {
4274 rc = sqlite3_finalize(hStmt);
4275 if (rc != SQLITE_OK) {
4276 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4277 error = EMAIL_ERROR_DB_FAILURE;
4281 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4283 if (err_code != NULL)
4286 EM_DEBUG_FUNC_END("ret [%d]", ret);
4290 INTERNAL_FUNC int emstorage_get_password_length_of_account(char *multi_user_name, int account_id, int password_type, int *password_length, int *err_code)
4292 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4294 if (account_id <= 0 || password_length == NULL) {
4295 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4296 if (err_code != NULL)
4297 *err_code = EMAIL_ERROR_INVALID_PARAM;
4302 int error = EMAIL_ERROR_NONE;
4303 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4304 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4305 char *temp_password = NULL;
4308 /* get password file name */
4309 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4310 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4314 /* read password from secure storage */
4315 if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4316 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4317 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4320 } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4321 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4322 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4326 EM_DEBUG_LOG("Invalid password type");
4327 if (err_code != NULL)
4328 *err_code = EMAIL_ERROR_INVALID_PARAM;
4332 *password_length = EM_SAFE_STRLEN(temp_password);
4334 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
4339 EM_SAFE_FREE(temp_password);
4341 if (err_code != NULL)
4344 EM_DEBUG_FUNC_END("ret [%d]", ret);
4348 INTERNAL_FUNC int emstorage_update_account_password(char *multi_user_name, int input_account_id, char *input_incoming_server_password, char *input_outgoing_server_password)
4350 EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_incoming_server_password[%p], input_outgoing_server_password[%p]", input_account_id, input_incoming_server_password, input_outgoing_server_password);
4352 int err = EMAIL_ERROR_NONE;
4353 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4354 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4356 if(input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4357 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4358 err = EMAIL_ERROR_INVALID_PARAM;
4362 /* get password file name */
4363 if ((err = _get_password_file_name(multi_user_name,
4365 recv_password_file_name,
4366 send_password_file_name)) != EMAIL_ERROR_NONE) {
4367 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4371 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4372 recv_password_file_name, input_incoming_server_password);
4373 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4374 send_password_file_name, input_outgoing_server_password);
4376 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4377 if (err != EMAIL_ERROR_NONE) {
4378 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4382 /* save recv passwords to the secure storage */
4383 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4384 if (err != EMAIL_ERROR_NONE) {
4385 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4389 err = emcore_remove_password_in_key_manager(send_password_file_name);
4390 if (err != EMAIL_ERROR_NONE) {
4391 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4395 /* save send passwords to the secure storage */
4396 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4397 if (err != EMAIL_ERROR_NONE) {
4398 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4404 EM_DEBUG_FUNC_END("err [%d]", err);
4408 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4410 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4412 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4413 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4414 if (err_code != NULL)
4415 *err_code = EMAIL_ERROR_INVALID_PARAM;
4419 int error = EMAIL_ERROR_NONE;
4420 int rc, ret = false;
4422 DB_STMT hStmt = NULL;
4423 char sql_query_string[QUERY_SIZE] = {0, };
4425 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4427 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4429 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4430 "UPDATE mail_account_tbl SET"
4432 ", logo_icon_path = ?"
4434 ", user_data_length = ?"
4435 ", account_svc_id = ?"
4437 ", sync_disabled = ?"
4438 ", default_mail_slot_size = ?"
4439 ", roaming_option = ?"
4441 ", user_display_name = ?"
4442 ", user_email_address = ?"
4443 ", reply_to_address = ?"
4444 ", return_address = ?"
4445 ", incoming_server_type = ?"
4446 ", incoming_server_address = ?"
4447 ", incoming_server_port_number = ?"
4448 ", incoming_server_user_name = ?"
4449 ", incoming_server_secure_connection = ?"
4450 ", incoming_server_authentication_method = ?"
4451 ", retrieval_mode = ?"
4452 ", keep_mails_on_pop_server_after_download = ?"
4453 ", check_interval = ?"
4454 ", auto_download_size = ?"
4455 ", peak_interval = ?"
4457 ", peak_start_time = ?"
4458 ", peak_end_time = ?"
4459 ", outgoing_server_type = ?"
4460 ", outgoing_server_address = ?"
4461 ", outgoing_server_port_number = ?"
4462 ", outgoing_server_user_name = ?"
4463 ", outgoing_server_secure_connection = ?"
4464 ", outgoing_server_need_authentication = ?"
4465 ", outgoing_server_use_same_authenticator = ?"
4467 ", keep_local_copy = ?"
4468 ", req_delivery_receipt = ?"
4469 ", req_read_receipt = ?"
4470 ", download_limit = ?"
4471 ", block_address = ?"
4472 ", block_subject = ?"
4473 ", display_name_from = ?"
4474 ", reply_with_body = ?"
4475 ", forward_with_files = ?"
4476 ", add_myname_card = ?"
4477 ", add_signature = ?"
4479 ", add_my_address_to_bcc = ?"
4480 ", notification_status = ?"
4481 ", vibrate_status = ?"
4482 ", display_content_status = ?"
4483 ", default_ringtone_status = ?"
4484 ", alert_ringtone_path = ?"
4485 ", auto_resend_times = ?"
4486 ", outgoing_server_size_limit = ?"
4487 ", wifi_auto_download = ?"
4488 ", pop_before_smtp = ?"
4489 ", incoming_server_requires_apop = ?"
4491 ", certificate_path = ?"
4495 " WHERE account_id = ?");
4497 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4498 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4499 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4500 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4501 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4505 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4506 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4507 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4508 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4509 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4510 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4511 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4512 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4513 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4514 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4515 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4516 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4517 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4518 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4519 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4520 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4521 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4522 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4523 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4524 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4525 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4526 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4527 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4528 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4529 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4530 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4531 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4532 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4533 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4534 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4535 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4536 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4537 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4538 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4539 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4540 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4541 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4542 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4543 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4544 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4545 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4546 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4547 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4548 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4549 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4550 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4551 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4552 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4553 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4554 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4555 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4556 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4557 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4558 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4559 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4560 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4561 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4562 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4563 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4564 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4565 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4566 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4567 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4568 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4569 _bind_stmt_field_data_int(hStmt, i++, account_id);
4571 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4572 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4573 ("sqlite3_step fail:%d", rc));
4574 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4575 ("sqlite3_step fail:%d", rc));
4577 /* validate account existence */
4578 rc = sqlite3_changes(local_db_handle);
4580 EM_DEBUG_EXCEPTION ("no matched account found...");
4581 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4585 if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4586 if((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4587 EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4595 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4597 if (hStmt != NULL) {
4598 rc = sqlite3_finalize(hStmt);
4599 if (rc != SQLITE_OK) {
4600 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
4601 error = EMAIL_ERROR_DB_FAILURE;
4605 if (error == EMAIL_ERROR_NONE) {
4606 if (!emcore_notify_storage_event (NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4607 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4610 if (err_code != NULL)
4613 EM_DEBUG_FUNC_END("ret [%d]", ret);
4617 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(char *multi_user_name, int account_id, char *field_name, int value, int transaction)
4619 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4620 int error = EMAIL_ERROR_NONE;
4622 char sql_query_string[QUERY_SIZE] = {0, };
4623 sqlite3 *local_db_handle = NULL;
4625 if (!account_id || !field_name) {
4626 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4627 return EMAIL_ERROR_INVALID_PARAM;
4630 local_db_handle = emstorage_get_db_connection(multi_user_name);
4632 /* Write query string */
4633 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4635 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4638 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4640 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4641 if (error != EMAIL_ERROR_NONE) {
4642 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4646 if (sqlite3_changes(local_db_handle) == 0)
4647 EM_DEBUG_LOG("no mail matched...");
4651 result = (error == EMAIL_ERROR_NONE) ? true : false;
4652 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4654 if (error == EMAIL_ERROR_NONE) {
4655 if (!emcore_notify_storage_event (NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4656 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_event failed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4658 EM_DEBUG_FUNC_END("error [%d]", error);
4662 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status,int *err_code)
4664 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4666 if(!result_sync_status) {
4667 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4668 if (err_code != NULL)
4669 *err_code = EMAIL_ERROR_INVALID_PARAM;
4673 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4674 char sql_query_string[QUERY_SIZE] = {0, };
4675 char **result = NULL;
4676 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4679 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4681 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4683 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4684 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4685 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4688 EM_DEBUG_EXCEPTION("no matched account found...");
4689 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4694 *result_sync_status = 0;
4696 for(i = 0; i < count; i++) {
4697 _get_table_field_data_int(result, &sync_status, col_index++);
4698 *result_sync_status |= sync_status;
4701 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4703 sqlite3_free_table(result);
4710 if (err_code != NULL)
4713 EM_DEBUG_FUNC_END("ret [%d]", ret);
4717 INTERNAL_FUNC int emstorage_update_sync_status_of_account(char *multi_user_name, int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
4719 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);
4721 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4722 char sql_query_string[QUERY_SIZE] = {0, };
4723 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4725 if(set_operator != SET_TYPE_SET && account_id) {
4726 if(!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4727 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4728 if (err_code != NULL)
4732 switch(set_operator) {
4733 case SET_TYPE_UNION :
4734 set_value = result_sync_status | set_value;
4736 case SET_TYPE_MINUS :
4737 set_value = result_sync_status & (~set_value);
4740 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4741 error = EMAIL_ERROR_NOT_SUPPORTED;
4744 EM_DEBUG_LOG("set_value [%d]", set_value);
4747 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4750 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4752 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4754 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4756 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4757 if (error != EMAIL_ERROR_NONE) {
4758 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4762 rc = sqlite3_changes(local_db_handle);
4765 EM_DEBUG_EXCEPTION("no matched account found...");
4766 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4773 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4775 if (error == EMAIL_ERROR_NONE) {
4776 if (!emcore_notify_storage_event (NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4777 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4780 if (err_code != NULL)
4783 EM_DEBUG_FUNC_END("ret [%d]", ret);
4787 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4789 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4792 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4793 if (err_code != NULL)
4794 *err_code = EMAIL_ERROR_INVALID_PARAM;
4798 int rc = -1, ret = false;
4799 int error = EMAIL_ERROR_NONE;
4800 DB_STMT hStmt = NULL;
4801 char sql_query_string[QUERY_SIZE] = {0, };
4802 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4803 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4805 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4807 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4809 char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
4810 char **result = NULL;
4812 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
4813 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4814 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4816 if (NULL==result[1]) rc = 1;
4817 else rc = atoi(result[1])+1;
4818 sqlite3_free_table(result);
4821 account_tbl->account_id = rc;
4823 if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4824 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4828 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4829 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4830 "INSERT INTO mail_account_tbl VALUES "
4832 " ? " /* account_id */
4833 " , ? " /* account_name */
4834 " , ? " /* logo_icon_path */
4835 " , ? " /* user_data */
4836 " , ? " /* user_data_length */
4837 " , ? " /* account_svc_id */
4838 " , ? " /* sync_status */
4839 " , ? " /* sync_disabled */
4840 " , ? " /* default_mail_slot_size */
4841 " , ? " /* roaming_option */
4842 " , ? " /* color_label */
4843 " , ? " /* user_display_name */
4844 " , ? " /* user_email_address */
4845 " , ? " /* reply_to_address */
4846 " , ? " /* return_address */
4847 " , ? " /* incoming_server_type */
4848 " , ? " /* incoming_server_address */
4849 " , ? " /* incoming_server_port_number */
4850 " , ? " /* incoming_server_user_name */
4851 " , ? " /* incoming_server_password */
4852 " , ? " /* incoming_server_secure_connection */
4853 " , ? " /* incoming_server_authentication_method */
4854 " , ? " /* retrieval_mode */
4855 " , ? " /* keep_mails_on_pop_server_after_download */
4856 " , ? " /* check_interval */
4857 " , ? " /* auto_download_size */
4858 " , ? " /* peak_interval */
4859 " , ? " /* peak_days */
4860 " , ? " /* peak_start_time */
4861 " , ? " /* peak_end_time */
4862 " , ? " /* outgoing_server_type */
4863 " , ? " /* outgoing_server_address */
4864 " , ? " /* outgoing_server_port_number */
4865 " , ? " /* outgoing_server_user_name */
4866 " , ? " /* outgoing_server_password */
4867 " , ? " /* outgoing_server_secure_connection */
4868 " , ? " /* outgoing_server_need_authentication */
4869 " , ? " /* outgoing_server_use_same_authenticator */
4870 " , ? " /* priority */
4871 " , ? " /* keep_local_copy */
4872 " , ? " /* req_delivery_receipt */
4873 " , ? " /* req_read_receipt */
4874 " , ? " /* download_limit */
4875 " , ? " /* block_address */
4876 " , ? " /* block_subject */
4877 " , ? " /* display_name_from */
4878 " , ? " /* reply_with_body */
4879 " , ? " /* forward_with_files */
4880 " , ? " /* add_myname_card */
4881 " , ? " /* add_signature */
4882 " , ? " /* signature */
4883 " , ? " /* add_my_address_to_bcc */
4884 " , ? " /* auto_resend_times */
4885 " , ? " /* outgoing_server_size_limit */
4886 " , ? " /* wifi_auto_download */
4887 " , ? " /* pop_before_smtp */
4888 " , ? " /* incoming_server_requires_apop */
4889 " , ? " /* smime_type */
4890 " , ? " /* certificate_path */
4891 " , ? " /* cipher_type */
4892 " , ? " /* digest_type */
4893 " , ? " /* notification_status */
4894 " , ? " /* vibrate_status */
4895 " , ? " /* display_content_status */
4896 " , ? " /* default_ringtone_status */
4897 " , ? " /* alert_ringtone_path */
4898 " , ? " /* user_name */
4901 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4902 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4903 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4907 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4908 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4909 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4910 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4911 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4912 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4913 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4914 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4915 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4916 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4917 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4918 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4919 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4920 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4921 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4922 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4923 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4924 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4925 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4926 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4927 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4928 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4929 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4930 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4931 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4932 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4933 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4934 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4935 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4936 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4937 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4938 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4939 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4940 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4941 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4942 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4943 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4944 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4945 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4946 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4947 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4948 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4949 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4950 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4951 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4952 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4953 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4954 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4955 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4956 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4957 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4958 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4959 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4960 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4961 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4962 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4963 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4964 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4965 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
4966 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4967 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4968 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4969 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4970 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4971 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4972 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4973 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4975 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4977 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4978 ("sqlite3_step fail:%d", rc));
4979 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4980 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
4983 /* save passwords to the secure storage */
4984 EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
4985 error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
4986 if (error != EMAIL_ERROR_NONE) {
4987 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
4991 error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
4992 if (error != EMAIL_ERROR_NONE) {
4993 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5000 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5002 if (hStmt != NULL) {
5003 rc = sqlite3_finalize(hStmt);
5004 if (rc != SQLITE_OK) {
5005 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
5006 error = EMAIL_ERROR_DB_FAILURE;
5010 if (error == EMAIL_ERROR_NONE) {
5011 if (!emcore_notify_storage_event (NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5012 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5015 if (err_code != NULL)
5018 EM_DEBUG_FUNC_END("ret [%d]", ret);
5022 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5024 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5026 if (account_id < FIRST_ACCOUNT_ID) {
5027 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5029 if (err_code != NULL)
5030 *err_code = EMAIL_ERROR_INVALID_PARAM;
5034 int rc = -1, ret = false;
5035 int error = EMAIL_ERROR_NONE;
5037 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5039 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5041 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5042 char sql_query_string[QUERY_SIZE] = {0, };
5043 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5044 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5046 /* get password file name */
5047 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5048 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5052 /* delete from db */
5053 memset(sql_query_string, 0x00, sizeof(sql_query_string));
5054 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5055 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5056 if (error != EMAIL_ERROR_NONE) {
5057 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5061 /* validate account existence */
5062 rc = sqlite3_changes(local_db_handle);
5064 EM_DEBUG_EXCEPTION(" no matched account found...");
5065 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5069 /* delete from secure storage */
5070 error = emcore_remove_password_in_key_manager(recv_password_file_name);
5071 if (error != EMAIL_ERROR_NONE) {
5072 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5076 error = emcore_remove_password_in_key_manager(send_password_file_name);
5077 if (error != EMAIL_ERROR_NONE) {
5078 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5085 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5087 if (err_code != NULL)
5090 EM_DEBUG_FUNC_END("ret [%d]", ret);
5095 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5097 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5100 int error = EMAIL_ERROR_NONE;
5103 if (!account_list || !*account_list) {
5104 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5105 error = EMAIL_ERROR_INVALID_PARAM;
5109 emstorage_account_tbl_t* p = *account_list;
5112 for (; i < count; i++) {
5113 EM_SAFE_FREE(p[i].account_name);
5114 EM_SAFE_FREE(p[i].incoming_server_address);
5115 EM_SAFE_FREE(p[i].user_email_address);
5116 EM_SAFE_FREE(p[i].user_data);
5117 EM_SAFE_FREE(p[i].incoming_server_user_name);
5118 EM_SAFE_FREE(p[i].incoming_server_password);
5119 EM_SAFE_FREE(p[i].outgoing_server_address);
5120 EM_SAFE_FREE(p[i].outgoing_server_user_name);
5121 EM_SAFE_FREE(p[i].outgoing_server_password);
5122 EM_SAFE_FREE(p[i].user_display_name);
5123 EM_SAFE_FREE(p[i].reply_to_address);
5124 EM_SAFE_FREE(p[i].return_address);
5125 EM_SAFE_FREE(p[i].logo_icon_path);
5126 EM_SAFE_FREE(p[i].options.display_name_from);
5127 EM_SAFE_FREE(p[i].options.signature);
5128 EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5129 EM_SAFE_FREE(p[i].certificate_path);
5130 EM_SAFE_FREE(p[i].user_name);
5134 *account_list = NULL;
5140 if (err_code != NULL)
5143 EM_DEBUG_FUNC_END("ret [%d]", ret);
5147 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5149 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);
5151 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5152 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5154 if (err_code != NULL)
5155 *err_code = EMAIL_ERROR_INVALID_PARAM;
5159 int rc = -1, ret = false;
5160 int error = EMAIL_ERROR_NONE;
5161 char sql_query_string[QUERY_SIZE] = {0, };
5163 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5164 EMSTORAGE_START_READ_TRANSACTION(transaction);
5166 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);
5170 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5171 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5172 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5174 *count = atoi(result[1]);
5175 sqlite3_free_table(result);
5182 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5184 if (err_code != NULL)
5187 EM_DEBUG_FUNC_END("ret [%d]", ret);
5191 INTERNAL_FUNC int emstorage_get_mailbox_list(char *multi_user_name, 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)
5193 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);
5195 if (!select_num || !mailbox_list) {
5196 EM_DEBUG_EXCEPTION("Invalid parameters");
5198 if (err_code != NULL)
5199 *err_code = EMAIL_ERROR_INVALID_PARAM;
5205 int error = EMAIL_ERROR_NONE;
5206 char conditional_clause_string[QUERY_SIZE] = {0, };
5207 char ordering_clause_string[QUERY_SIZE] = {0, };
5209 if (account_id == ALL_ACCOUNT) {
5210 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5211 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);
5214 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
5215 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5216 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);
5219 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5221 switch (sort_type) {
5222 case EMAIL_MAILBOX_SORT_BY_NAME_ASC :
5223 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5226 case EMAIL_MAILBOX_SORT_BY_NAME_DSC :
5227 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5230 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC :
5231 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5234 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC :
5235 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5239 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5241 if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5242 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5250 if (err_code != NULL)
5253 EM_DEBUG_FUNC_END("ret [%d]", ret);
5257 INTERNAL_FUNC int emstorage_get_mailbox_list_ex(char *multi_user_name, int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
5259 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);
5262 int error = EMAIL_ERROR_NONE;
5263 int where_clause_count = 0;
5264 char conditional_clause_string[QUERY_SIZE] = {0, };
5265 char ordering_clause_string[QUERY_SIZE] = {0, };
5267 if (!select_num || !mailbox_list) {
5268 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5269 error = EMAIL_ERROR_INVALID_PARAM;
5273 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5274 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);
5275 where_clause_count++;
5278 if (account_id > 0) {
5279 if (where_clause_count == 0) {
5280 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5281 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);
5284 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);
5287 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5288 " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5289 " THEN 2 ELSE 1 END ASC,"
5290 " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5291 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5292 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5294 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5295 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5303 if (err_code != NULL)
5306 EM_DEBUG_FUNC_END("ret [%d]", ret);
5310 INTERNAL_FUNC int emstorage_get_child_mailbox_list(char *multi_user_name, int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
5312 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);
5315 int error = EMAIL_ERROR_NONE;
5316 char conditional_clause_string[QUERY_SIZE] = {0, };
5318 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5319 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);
5320 if (err_code != NULL)
5321 *err_code = EMAIL_ERROR_INVALID_PARAM;
5325 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND UPPER(mailbox_name) LIKE UPPER('%q%%')", account_id, parent_mailbox_name);
5326 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5328 if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5329 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5337 if (err_code != NULL)
5340 EM_DEBUG_FUNC_END("ret [%d]", ret);
5344 INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(char *multi_user_name, int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
5346 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);
5347 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5348 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5350 if (err_code != NULL)
5351 *err_code = EMAIL_ERROR_INVALID_PARAM;
5357 int error = EMAIL_ERROR_NONE;
5358 char conditional_clause_string[QUERY_SIZE] = {0, };
5360 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5361 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5363 if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5364 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5372 if (err_code != NULL)
5375 EM_DEBUG_FUNC_END("ret [%d]", ret);
5379 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5381 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5383 int result_code = false;
5384 int error = EMAIL_ERROR_NONE;
5385 time_t current_time = 0;
5386 char sql_query_string[QUERY_SIZE] = {0, };
5387 sqlite3 *local_db_handle = NULL;
5389 if (!input_mailbox_id ) {
5390 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5391 return EMAIL_ERROR_INVALID_PARAM;
5394 time(¤t_time);
5396 local_db_handle = emstorage_get_db_connection(multi_user_name);
5397 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5399 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5400 "UPDATE mail_box_tbl SET"
5401 " last_sync_time = %d"
5402 " WHERE mailbox_id = %d"
5404 , input_mailbox_id);
5406 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5407 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5408 if (error != EMAIL_ERROR_NONE) {
5409 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5415 if(error == EMAIL_ERROR_NONE)
5418 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5420 EM_DEBUG_FUNC_END("error [%d]", error);
5424 INTERNAL_FUNC int emstorage_get_mailbox_by_name(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t **result_mailbox, int transaction, int *err_code)
5426 EM_DEBUG_FUNC_BEGIN_SEC("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);
5427 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5429 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5430 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5432 if (err_code != NULL)
5433 *err_code = EMAIL_ERROR_INVALID_PARAM;
5438 int error = EMAIL_ERROR_NONE;
5439 int result_count = 0;
5440 char conditional_clause_string[QUERY_SIZE] = {0, };
5442 if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5443 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5444 EM_DEBUG_EXCEPTION("malloc failed...");
5445 error = EMAIL_ERROR_OUT_OF_MEMORY;
5449 (*result_mailbox)->mailbox_id = 0;
5450 (*result_mailbox)->account_id = account_id;
5451 (*result_mailbox)->local_yn = 1;
5452 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5453 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5454 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
5455 (*result_mailbox)->deleted_flag = 0;
5456 (*result_mailbox)->modifiable_yn = 1;
5457 (*result_mailbox)->total_mail_count_on_server = 1;
5458 (*result_mailbox)->has_archived_mails = 0;
5459 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
5460 (*result_mailbox)->no_select = 0;
5465 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5467 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%q'", account_id, local_yn, mailbox_name);
5469 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5471 if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5472 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5481 if (err_code != NULL)
5484 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5485 EM_DEBUG_FUNC_END("ret [%d]", ret);
5489 INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t **output_mailbox, int transaction, int *err_code)
5491 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], output_mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, output_mailbox, transaction, err_code);
5493 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5494 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5495 if (err_code != NULL)
5496 *err_code = EMAIL_ERROR_INVALID_PARAM;
5501 int error = EMAIL_ERROR_NONE;
5502 int result_count = 0;
5503 emstorage_mailbox_tbl_t *result_mailbox = NULL;
5504 char conditional_clause_string[QUERY_SIZE] = {0,};
5507 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5509 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5511 if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5512 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5520 *output_mailbox = result_mailbox;
5522 if (err_code != NULL)
5525 EM_DEBUG_FUNC_END("ret [%d]", ret);
5529 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5531 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5533 if (input_mailbox_id <= 0 || !output_mailbox) {
5534 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5535 return EMAIL_ERROR_INVALID_PARAM;
5538 int ret = EMAIL_ERROR_NONE;
5539 int result_count = 0;
5540 char conditional_clause_string[QUERY_SIZE] = {0, };
5542 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5544 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5546 if( (ret = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5547 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5553 EM_DEBUG_FUNC_END("ret [%d]", ret);
5557 INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(char *multi_user_name, int account_id, char *keyword, emstorage_mailbox_tbl_t** result_mailbox, int * result_count, int transaction, int *err_code)
5559 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], keyword[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, keyword, result_mailbox, transaction, err_code);
5561 if (account_id < 0 || !keyword || !result_mailbox) {
5562 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5564 if (err_code != NULL)
5565 *err_code = EMAIL_ERROR_INVALID_PARAM;
5570 int error = EMAIL_ERROR_NONE;
5571 char conditional_clause_string[QUERY_SIZE] = {0, };
5573 if (account_id == 0)
5574 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5575 "WHERE alias LIKE \'%%%q%%\'", keyword);
5576 else if (account_id > 0)
5577 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5578 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5580 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5582 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5583 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5591 if (err_code != NULL)
5594 EM_DEBUG_FUNC_END("ret [%d]", ret);
5598 INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code)
5600 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);
5601 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5602 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5603 if (err_code != NULL)
5604 *err_code = EMAIL_ERROR_INVALID_PARAM;
5608 int rc, ret = false;
5609 int error = EMAIL_ERROR_NONE;
5610 emstorage_account_tbl_t* account = NULL;
5611 DB_STMT hStmt = NULL;
5612 char sql_query_string[QUERY_SIZE] = {0, };
5614 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5615 EMSTORAGE_START_READ_TRANSACTION(transaction);
5617 /* Check whether the account exists. */
5618 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5619 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5624 emstorage_free_account(&account, 1, NULL);
5626 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);
5628 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5631 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5633 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5634 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5637 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5638 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5639 ("sqlite3_step fail:%d", rc));
5641 if (rc == SQLITE_DONE) {
5642 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5643 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5647 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5652 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5653 if (hStmt != NULL) {
5654 rc = sqlite3_finalize(hStmt);
5655 if (rc != SQLITE_OK) {
5656 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
5657 error = EMAIL_ERROR_DB_FAILURE;
5661 if (err_code != NULL)
5664 EM_DEBUG_FUNC_END("ret [%d]", ret);
5668 INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
5670 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);
5671 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5672 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5673 if (err_code != NULL)
5674 *err_code = EMAIL_ERROR_INVALID_PARAM;
5678 int rc, ret = false;
5679 int error = EMAIL_ERROR_NONE;
5680 emstorage_account_tbl_t* account = NULL;
5681 DB_STMT hStmt = NULL;
5682 char sql_query_string[QUERY_SIZE] = {0, };
5684 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5685 EMSTORAGE_START_READ_TRANSACTION(transaction);
5687 /* Check whether the account exists. */
5688 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5689 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5694 emstorage_free_account(&account, 1, NULL);
5696 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);
5698 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5701 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5703 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5704 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5708 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5709 ("sqlite3_step fail:%d", rc));
5711 if (rc == SQLITE_DONE) {
5712 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5713 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5717 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5722 if (hStmt != NULL) {
5723 rc = sqlite3_finalize(hStmt);
5724 if (rc != SQLITE_OK) {
5725 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
5726 error = EMAIL_ERROR_DB_FAILURE;
5730 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5732 if (err_code != NULL)
5735 EM_DEBUG_FUNC_END("ret [%d]", ret);
5739 INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
5741 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);
5743 int error = EMAIL_ERROR_NONE;
5744 char sql_query_string[QUERY_SIZE] = {0, };
5745 char *replaced_mailbox_name = NULL;
5748 if (strstr(mailbox_name, "'")) {
5749 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5751 replaced_mailbox_name = strdup(mailbox_name);
5755 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5757 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5759 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5760 "UPDATE mail_box_tbl SET"
5761 " modifiable_yn = %d"
5762 " WHERE account_id = %d"
5763 " AND local_yn = %d"
5764 " AND mailbox_name = '%s'"
5768 , replaced_mailbox_name);
5770 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5771 if (error != EMAIL_ERROR_NONE) {
5772 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5779 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5781 EM_SAFE_FREE(replaced_mailbox_name);
5783 if (err_code != NULL)
5786 EM_DEBUG_FUNC_END("ret [%d]", ret);
5791 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name, int account_id, int input_mailbox_id, int total_count_on_server, int transaction, int *err_code)
5793 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);
5795 int error = EMAIL_ERROR_NONE;
5796 char sql_query_string[QUERY_SIZE] = {0, };
5798 if (account_id <= 0 || input_mailbox_id <= 0) {
5799 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5801 if (err_code != NULL)
5802 *err_code = EMAIL_ERROR_INVALID_PARAM;
5803 EM_DEBUG_FUNC_END("ret [%d]", ret);
5806 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5808 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5810 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5811 "UPDATE mail_box_tbl SET"
5812 " total_mail_count_on_server = %d"
5813 " WHERE account_id = %d"
5814 " AND mailbox_id = %d"
5815 , total_count_on_server
5817 , input_mailbox_id);
5819 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5820 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5821 if (error != EMAIL_ERROR_NONE) {
5822 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5829 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5831 if (err_code != NULL)
5834 EM_DEBUG_FUNC_END("ret [%d]", ret);
5839 INTERNAL_FUNC int emstorage_update_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t *result_mailbox, int transaction, int *err_code)
5841 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);
5843 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5844 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);
5846 if (err_code != NULL)
5847 *err_code = EMAIL_ERROR_INVALID_PARAM;
5848 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5852 int rc, ret = false;
5853 int error = EMAIL_ERROR_NONE;
5854 char sql_query_string[QUERY_SIZE] = {0, };
5855 DB_STMT hStmt = NULL;
5858 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5859 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5861 if (local_yn != -1) {
5862 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5863 "UPDATE mail_box_tbl SET"
5865 ", mailbox_name = ?"
5866 ", mailbox_type = ?"
5868 ", deleted_flag = ?"
5869 ", modifiable_yn= ?"
5870 ", mail_slot_size= ?"
5871 ", total_mail_count_on_server = ?"
5872 " WHERE account_id = %d"
5873 " AND local_yn = %d"
5874 " AND mailbox_id = '%d'"
5877 , input_mailbox_id);
5880 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5881 "UPDATE mail_box_tbl SET"
5883 ", mailbox_name = ?"
5884 ", mailbox_type = ?"
5886 ", deleted_flag = ?"
5887 ", modifiable_yn= ?"
5888 ", mail_slot_size= ?"
5889 ", total_mail_count_on_server = ?"
5890 " WHERE account_id = %d"
5891 " AND mailbox_id = '%d'"
5893 , input_mailbox_id);
5898 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5899 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5900 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5902 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5903 _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);
5904 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5905 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5906 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5907 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5908 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5909 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5912 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5913 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5914 ("sqlite3_step fail:%d", rc));
5915 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5916 ("sqlite3_step fail:%d", rc));
5921 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5923 if (hStmt != NULL) {
5924 rc = sqlite3_finalize(hStmt);
5925 if (rc != SQLITE_OK) {
5926 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
5927 error = EMAIL_ERROR_DB_FAILURE;
5931 if (err_code != NULL)
5934 EM_DEBUG_FUNC_END("ret [%d]", ret);
5938 INTERNAL_FUNC int emstorage_update_mailbox_type(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
5940 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, new_mailbox_type, transaction, err_code);
5942 if (account_id < FIRST_ACCOUNT_ID) {
5943 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5945 if (err_code != NULL)
5946 *err_code = EMAIL_ERROR_INVALID_PARAM;
5950 int rc, ret = false;
5951 int error = EMAIL_ERROR_NONE;
5952 char sql_query_string[QUERY_SIZE] = {0, };
5954 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5955 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5957 EM_DEBUG_LOG("emstorage_update_mailbox_type");
5959 DB_STMT hStmt_box_tbl = NULL;
5960 DB_STMT hStmt_mail_tbl = NULL;
5963 /* Update mail_box_tbl */
5964 if (local_yn != -1) {
5965 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5966 "UPDATE mail_box_tbl SET"
5968 " WHERE account_id = %d"
5969 " AND local_yn = %d"
5970 " AND mailbox_id = '%d'"
5973 , input_mailbox_id);
5976 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5977 "UPDATE mail_box_tbl SET"
5979 " WHERE account_id = %d"
5980 " AND mailbox_id = '%d'"
5982 , input_mailbox_id);
5985 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
5987 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
5989 if(SQLITE_OK != rc) {
5990 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5991 error = EMAIL_ERROR_DB_FAILURE;
5995 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
5998 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6000 if(rc == SQLITE_FULL) {
6001 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6002 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6006 if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
6007 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6008 error = EMAIL_ERROR_DB_FAILURE;
6013 /* Update mail_tbl */
6015 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6016 "UPDATE mail_tbl SET"
6018 " WHERE account_id = %d"
6019 " AND mailbox_id = '%d'"
6021 , input_mailbox_id);
6023 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6025 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6026 if(SQLITE_OK != rc) {
6027 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6028 error = EMAIL_ERROR_DB_FAILURE;
6032 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6034 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6035 if(rc == SQLITE_FULL) {
6036 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6037 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6041 if(rc != SQLITE_ROW && rc != SQLITE_DONE){
6042 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6043 error = EMAIL_ERROR_DB_FAILURE;
6050 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6052 if (hStmt_box_tbl != NULL) {
6053 rc = sqlite3_finalize(hStmt_box_tbl);
6054 if (rc != SQLITE_OK) {
6055 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6056 error = EMAIL_ERROR_DB_FAILURE;
6060 if (hStmt_mail_tbl != NULL) {
6061 rc = sqlite3_finalize(hStmt_mail_tbl);
6062 if (rc != SQLITE_OK) {
6063 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6064 error = EMAIL_ERROR_DB_FAILURE;
6068 if (err_code != NULL)
6071 EM_DEBUG_FUNC_END("ret [%d]", ret);
6075 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6077 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);
6079 int rc, ret = false;
6080 int error = EMAIL_ERROR_NONE;
6081 char sql_query_string[QUERY_SIZE] = {0, };
6083 if (input_mailbox_id < 0) {
6084 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6085 return EMAIL_ERROR_INVALID_PARAM;
6088 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6090 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6092 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6094 DB_STMT hStmt = NULL;
6097 /* Update mail_box_tbl */
6098 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6099 "UPDATE mail_box_tbl SET"
6101 " WHERE mailbox_id = %d"
6102 , input_mailbox_id);
6104 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6106 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6107 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6108 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6110 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6112 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6113 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6114 ("sqlite3_step fail:%d", rc));
6115 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6116 ("sqlite3_step fail:%d", rc));
6118 if (hStmt != NULL) {
6119 rc = sqlite3_finalize(hStmt);
6120 if (rc != SQLITE_OK) {
6121 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6122 error = EMAIL_ERROR_DB_FAILURE;
6130 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6132 if (hStmt != NULL) {
6133 rc = sqlite3_finalize(hStmt);
6134 if (rc != SQLITE_OK) {
6135 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6136 error = EMAIL_ERROR_DB_FAILURE;
6140 EM_DEBUG_FUNC_END("error [%d]", error);
6144 INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(char *multi_user_name, int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction)
6146 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);
6148 int err = EMAIL_ERROR_NONE;
6150 int cur_mailbox_id_string = 0;
6151 int mailbox_id_string_buffer_length = 0;
6152 char sql_query_string[QUERY_SIZE] = {0, };
6153 char *mailbox_id_string_buffer = NULL;
6154 char *parameter_string = NULL;
6155 sqlite3 *local_db_handle = NULL;
6157 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6158 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6159 return EMAIL_ERROR_INVALID_PARAM;
6162 local_db_handle = emstorage_get_db_connection(multi_user_name);
6164 /* Generating mail id list string */
6165 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6167 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6169 if(!mailbox_id_string_buffer) {
6170 EM_DEBUG_EXCEPTION("em_malloc failed");
6171 err = EMAIL_ERROR_OUT_OF_MEMORY;
6175 for(i = 0; i < input_mailbox_id_count; i++)
6176 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]);
6178 if(EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6179 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6181 /* Generating notification parameter string */
6182 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6184 if(!parameter_string) {
6185 EM_DEBUG_EXCEPTION("em_malloc failed");
6186 err = EMAIL_ERROR_OUT_OF_MEMORY;
6190 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6192 /* Write query string */
6193 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);
6195 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6198 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6199 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6200 if (err != EMAIL_ERROR_NONE) {
6201 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6205 if (sqlite3_changes(local_db_handle) == 0)
6206 EM_DEBUG_LOG("no mail matched...");
6211 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6213 if (err == EMAIL_ERROR_NONE && parameter_string) {
6214 if (!emcore_notify_storage_event (NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6215 EM_DEBUG_EXCEPTION_SEC ("emcore_notify_storage_event failed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6216 input_field_name, input_value);
6219 EM_SAFE_FREE(mailbox_id_string_buffer);
6220 EM_SAFE_FREE(parameter_string);
6222 EM_DEBUG_FUNC_END("err [%d]", err);
6226 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6228 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6231 if (err_code != NULL)
6232 *err_code = EMAIL_ERROR_INVALID_PARAM;
6236 int rc, ret = false;
6237 int error = EMAIL_ERROR_NONE;
6238 DB_STMT hStmt = NULL;
6239 char sql_query_string[QUERY_SIZE] = {0,};
6240 char **result = NULL;
6241 time_t current_time;
6242 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6243 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6245 EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
6247 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6248 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6249 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6251 time(¤t_time);
6253 if (NULL == result[1])
6256 rc = atoi(result[1]) + 1;
6257 sqlite3_free_table(result);
6259 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6261 mailbox_tbl->mailbox_id = rc;
6263 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6264 "INSERT INTO mail_box_tbl VALUES "
6265 "( ?" /* mailbox_id */
6266 ", ?" /* account_id */
6267 ", ?" /* local_yn */
6268 ", ?" /* mailbox_name */
6269 ", ?" /* mailbox_type */
6271 ", ?" /* deleted_flag */
6272 ", ?" /* modifiable_yn */
6273 ", ?" /* total_mail_count_on_server */
6274 ", ?" /* has_archived_mails */
6275 ", ?" /* mail_slot_size */
6276 ", ?" /* no_select */
6277 ", ?" /* last_sync_time */
6278 ", ?" /* eas_data_length */
6279 ", ?" /* eas_data */
6283 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6284 EM_DEBUG_LOG_DEV ("After sqlite3_prepare hStmt = %p", hStmt);
6285 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6286 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6290 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6291 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6292 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6293 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6294 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6295 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6296 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6297 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6298 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6299 _bind_stmt_field_data_int(hStmt, col_index++, 0);
6300 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6301 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6302 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6303 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6304 _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6307 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6308 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6309 ("sqlite3_step fail:%dn", rc));
6314 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6315 if (hStmt != NULL) {
6316 rc = sqlite3_finalize(hStmt);
6317 if (rc != SQLITE_OK) {
6318 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6319 error = EMAIL_ERROR_DB_FAILURE;
6323 if (error == EMAIL_ERROR_NONE) {
6324 if (!emcore_notify_storage_event (NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6325 mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6326 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6329 if (err_code != NULL)
6332 EM_DEBUG_FUNC_END("ret [%d]", ret);
6336 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6338 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6340 if (account_id < FIRST_ACCOUNT_ID) {
6342 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6344 if (err_code != NULL)
6345 *err_code = EMAIL_ERROR_INVALID_PARAM;
6348 int rc, ret = false;
6349 int error = EMAIL_ERROR_NONE;
6350 char sql_query_string[QUERY_SIZE] = {0,};
6351 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6352 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6355 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6357 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6358 if (error != EMAIL_ERROR_NONE) {
6359 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6363 rc = sqlite3_changes(local_db_handle);
6365 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6371 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6373 if (err_code != NULL)
6375 EM_DEBUG_FUNC_END("ret [%d]", ret);
6381 INTERNAL_FUNC int emstorage_delete_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
6383 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);
6385 if (account_id < FIRST_ACCOUNT_ID) {
6386 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6388 if (err_code != NULL)
6389 *err_code = EMAIL_ERROR_INVALID_PARAM;
6393 int rc, ret = false;
6394 int error = EMAIL_ERROR_NONE;
6395 char sql_query_string[QUERY_SIZE] = {0, };
6396 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6397 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6400 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6402 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);
6404 if (input_mailbox_id > 0) { /* 0 means all mailbox */
6405 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);
6408 EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6409 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6410 if (error != EMAIL_ERROR_NONE) {
6411 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6415 rc = sqlite3_changes(local_db_handle);
6417 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6418 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6425 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6427 if(error == EMAIL_ERROR_NONE) {
6428 if (!emcore_notify_storage_event (NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6429 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6432 if (err_code != NULL)
6435 EM_DEBUG_FUNC_END("ret [%d]", ret);
6439 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6441 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6444 int error = EMAIL_ERROR_NONE;
6447 if (!mailbox_list || !*mailbox_list) {
6448 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6450 error = EMAIL_ERROR_INVALID_PARAM;
6454 emstorage_mailbox_tbl_t* p = *mailbox_list;
6457 for (; i < count; i++) {
6458 EM_SAFE_FREE(p[i].mailbox_name);
6459 EM_SAFE_FREE(p[i].alias);
6460 EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6463 EM_SAFE_FREE(p); *mailbox_list = NULL;
6469 if (err_code != NULL)
6472 EM_DEBUG_FUNC_END("ret [%d]", ret);
6476 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
6478 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);
6480 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6481 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6483 if (err_code != NULL)
6484 *err_code = EMAIL_ERROR_INVALID_PARAM;
6488 int rc = -1, ret = false;
6489 int error = EMAIL_ERROR_NONE;
6490 char sql_query_string[QUERY_SIZE] = {0, };
6491 char *replaced_mailbox_name = NULL;
6493 if (strstr(mailbox_name, "'")) {
6494 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6496 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6499 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6502 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6503 EMSTORAGE_START_READ_TRANSACTION(transaction);
6504 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);
6505 EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6509 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6510 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6511 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6513 *count = atoi(result[1]);
6514 sqlite3_free_table(result);
6519 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6521 EM_SAFE_FREE(replaced_mailbox_name);
6523 if (err_code != NULL)
6526 EM_DEBUG_FUNC_END("ret [%d]", ret);
6530 INTERNAL_FUNC int emstorage_check_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
6532 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);
6534 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6535 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6537 if (err_code != NULL)
6538 *err_code = EMAIL_ERROR_INVALID_PARAM;
6542 int rc = -1, ret = false;
6543 int error = EMAIL_ERROR_NONE;
6544 char sql_query_string[QUERY_SIZE] = {0, };
6545 char *replaced_mailbox_name = NULL;
6547 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6549 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6550 EMSTORAGE_START_READ_TRANSACTION(transaction);
6553 if (strstr(mailbox_name, "'")) {
6554 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6556 replaced_mailbox_name = strdup(mailbox_name);
6559 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 server_uid = '%s' ", account_id, replaced_mailbox_name, uid);
6562 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND server_uid = '%s' ", account_id, uid);
6565 char **result = NULL;
6567 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6568 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6569 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6571 *exist = atoi(result[1]);
6572 sqlite3_free_table(result);
6582 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6584 EM_SAFE_FREE(replaced_mailbox_name);
6586 if (err_code != NULL)
6589 EM_DEBUG_FUNC_END("ret [%d]", ret);
6593 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6595 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6597 if (!mail || mail_id <= 0) {
6598 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6599 if (err_code != NULL)
6600 *err_code = EMAIL_ERROR_INVALID_PARAM;
6604 int rc, ret = false;
6605 int error = EMAIL_ERROR_NONE;
6606 DB_STMT hStmt = NULL;
6607 char sql_query_string[QUERY_SIZE] = {0, };
6609 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6610 EMSTORAGE_START_READ_TRANSACTION(transaction);
6612 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6614 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6615 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6617 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6618 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6621 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6622 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6623 ("sqlite3_step fail:%d", rc));
6625 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6626 if (*mail == NULL) {
6627 error = EMAIL_ERROR_OUT_OF_MEMORY;
6628 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6632 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6634 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6635 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6636 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6637 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6638 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6639 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6640 _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6642 (*mail)->server_mail_status = 1;
6647 if (hStmt != NULL) {
6648 rc = sqlite3_finalize(hStmt);
6649 if (rc != SQLITE_OK) {
6650 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6651 error = EMAIL_ERROR_DB_FAILURE;
6655 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6657 if (err_code != NULL)
6660 EM_DEBUG_FUNC_END("ret [%d]", ret);
6664 INTERNAL_FUNC int emstorage_get_downloaded_list(char *multi_user_name, int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t **read_mail_uid, int *count, int transaction, int *err_code)
6666 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6667 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);
6668 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6669 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6671 if (err_code != NULL)
6672 *err_code = EMAIL_ERROR_INVALID_PARAM;
6676 int rc, ret = false;
6677 int error = EMAIL_ERROR_NONE;
6679 DB_STMT hStmt = NULL;
6680 char sql_query_string[QUERY_SIZE] = {0, };
6682 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6685 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6686 EMSTORAGE_START_READ_TRANSACTION(transaction);
6689 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);
6691 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6693 EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6697 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6698 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6699 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6700 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6703 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6704 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6705 ("sqlite3_step fail:%d", rc));
6708 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6709 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6710 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6711 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6713 sqlite3_free_table(result);
6715 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6721 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6722 EM_DEBUG_EXCEPTION(" malloc failed...");
6723 error = EMAIL_ERROR_OUT_OF_MEMORY;
6727 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
6729 for (i = 0; i < *count; ++i) {
6730 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6731 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6732 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6733 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6734 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6735 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6736 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6737 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6739 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6740 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6741 ("sqlite3_step fail:%d", rc));
6748 *read_mail_uid = p_data_tbl;
6749 else if (p_data_tbl)
6750 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6752 if (hStmt != NULL) {
6753 rc = sqlite3_finalize(hStmt);
6754 if (rc != SQLITE_OK) {
6755 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6756 error = EMAIL_ERROR_DB_FAILURE;
6760 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6762 if (err_code != NULL)
6765 EM_PROFILE_END(emStorageGetDownloadList);
6766 EM_DEBUG_FUNC_END("ret [%d]", ret);
6770 INTERNAL_FUNC int emstorage_get_downloaded_mail_size(char *multi_user_name, int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
6772 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);
6774 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6775 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);
6777 if (err_code != NULL)
6778 *err_code = EMAIL_ERROR_INVALID_PARAM;
6782 int rc, ret = false;
6783 int error = EMAIL_ERROR_NONE;
6784 DB_STMT hStmt = NULL;
6785 char sql_query_string[QUERY_SIZE] = {0, };
6786 char *replaced_mailbox_name = NULL;
6788 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6789 EMSTORAGE_START_READ_TRANSACTION(transaction);
6792 if (strstr(mailbox_name, "'")) {
6793 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6795 replaced_mailbox_name = strdup(mailbox_name);
6798 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6800 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6801 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6802 "WHERE account_id = %d "
6803 "AND mailbox_id = '%s' "
6804 "AND local_uid = %d "
6805 "AND mailbox_name = '%s' "
6806 "AND server_uid = '%s'",
6807 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6810 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6811 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6812 "WHERE account_id = %d "
6813 "AND mailbox_id = '%s' "
6814 "AND local_uid = %d "
6815 "AND server_uid = '%s'",
6816 account_id, mailbox_id, local_uid, uid);
6820 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6821 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6822 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6825 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6826 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6827 ("sqlite3_step fail:%d", rc));
6829 if (rc == SQLITE_DONE) {
6830 EM_DEBUG_LOG("no matched mail found....");
6831 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6835 _get_stmt_field_data_int(hStmt, mail_size, 0);
6840 EM_SAFE_FREE(replaced_mailbox_name);
6842 if (hStmt != NULL) {
6843 rc = sqlite3_finalize(hStmt);
6844 if (rc != SQLITE_OK) {
6845 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6846 error = EMAIL_ERROR_DB_FAILURE;
6850 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6852 if (err_code != NULL)
6855 EM_DEBUG_FUNC_END("ret [%d]", ret);
6859 INTERNAL_FUNC int emstorage_add_downloaded_mail(char *multi_user_name, emstorage_read_mail_uid_tbl_t *read_mail_uid, int transaction, int *err_code)
6861 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6863 if (!read_mail_uid) {
6864 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6865 if (err_code != NULL)
6866 *err_code = EMAIL_ERROR_INVALID_PARAM;
6870 int rc, rc2, ret = false;
6871 int error = EMAIL_ERROR_NONE;
6872 DB_STMT hStmt = NULL;
6873 char sql_query_string[QUERY_SIZE] = {0, };
6875 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6876 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6878 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6879 char **result = NULL;
6882 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6883 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6884 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6886 if (NULL==result[1]) rc = 1;
6887 else rc = atoi(result[1])+1;
6888 sqlite3_free_table(result);
6890 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6891 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6892 "( ?" /* account_id */
6893 ", ?" /* mailbox_id */
6894 ", ?" /* mailbox_name */
6895 ", ?" /* local_uid */
6896 ", ?" /* server_uid */
6897 ", ?" /* rfc822_size */
6898 ", ?" /* sync_status */
6899 ", ?" /* flags_seen_field */
6900 ", ?" /* flags_flagged_field */
6904 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6905 if (rc2 != SQLITE_OK) {
6906 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6907 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6909 error = EMAIL_ERROR_DB_FAILURE;
6913 EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6914 "server_uid[%s] rfc822_size[%d] rc[%d]",
6915 read_mail_uid->account_id, read_mail_uid->mailbox_id,read_mail_uid->local_uid,
6916 read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6918 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6919 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6920 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6921 _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);
6922 _bind_stmt_field_data_string(hStmt, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6923 _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
6924 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
6925 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
6926 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6929 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6930 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6931 ("sqlite3_step fail:%d", rc));
6932 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6933 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6939 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6940 if (hStmt != NULL) {
6941 rc = sqlite3_finalize(hStmt);
6942 if (rc != SQLITE_OK) {
6943 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6944 error = EMAIL_ERROR_DB_FAILURE;
6948 if (err_code != NULL)
6951 EM_DEBUG_FUNC_END("ret [%d]", ret);
6955 #ifdef __FEATURE_BODY_SEARCH__
6956 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
6958 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
6961 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
6962 if (err_code != NULL)
6963 *err_code = EMAIL_ERROR_INVALID_PARAM;
6967 int rc, rc2, ret = false;
6968 int error = EMAIL_ERROR_NONE;
6969 DB_STMT hStmt = NULL;
6970 char sql_query_string[QUERY_SIZE] = {0, };
6972 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6973 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6975 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
6976 char **result = NULL;
6978 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6979 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6980 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6981 sqlite3_free_table(result);
6983 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6984 "INSERT INTO mail_text_tbl VALUES "
6990 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6991 if (rc2 != SQLITE_OK) {
6992 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6993 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
6995 error = EMAIL_ERROR_DB_FAILURE;
6999 EM_DEBUG_LOG ("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7000 mail_text->account_id, mail_text->mailbox_id);
7001 EM_DEBUG_LOG_DEV ("body_text VALUE [%s] ", mail_text->body_text);
7003 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7004 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7005 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7006 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7008 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7009 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7010 ("sqlite3_step fail:%d", rc));
7011 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7012 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7017 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7018 if (hStmt != NULL) {
7019 rc = sqlite3_finalize(hStmt);
7020 if (rc != SQLITE_OK) {
7021 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7022 error = EMAIL_ERROR_DB_FAILURE;
7026 if (err_code != NULL)
7029 EM_DEBUG_FUNC_END("ret [%d]", ret);
7034 INTERNAL_FUNC int emstorage_change_read_mail_uid(char *multi_user_name, 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)
7036 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);
7038 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7039 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);
7041 if (err_code != NULL)
7042 *err_code = EMAIL_ERROR_INVALID_PARAM;
7046 int rc, ret = false;
7047 int error = EMAIL_ERROR_NONE;
7048 DB_STMT hStmt = NULL;
7049 char sql_query_string[QUERY_SIZE] = {0, };
7051 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7053 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7055 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7056 "UPDATE mail_read_mail_uid_tbl SET"
7059 ", mailbox_name = ?"
7063 ", flags_seen_field = ?"
7064 ", flags_flagged_field = ?"
7065 " WHERE account_id = ?"
7066 " AND mailbox_id = ?"
7067 " AND local_uid = ?"
7068 " AND mailbox_name= ?"
7069 " AND server_uid = ?");
7072 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7073 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7074 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7075 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7080 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7081 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7082 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7083 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7084 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7085 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7086 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7087 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7088 _bind_stmt_field_data_int(hStmt, i++, account_id);
7089 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7090 _bind_stmt_field_data_int(hStmt, i++, local_uid);
7091 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7092 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7095 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7096 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7097 ("sqlite3_step fail:%d", rc));
7102 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7103 if (hStmt != NULL) {
7104 rc = sqlite3_finalize(hStmt);
7105 if (rc != SQLITE_OK) {
7106 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7107 error = EMAIL_ERROR_DB_FAILURE;
7111 if (err_code != NULL)
7114 EM_DEBUG_FUNC_END("ret [%d]", ret);
7118 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
7120 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_name[%s], uid[%s], transaction[%d], err_code[%p]", account_id, mailbox_name, uid, transaction, err_code);
7122 if (account_id < FIRST_ACCOUNT_ID) {
7123 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7125 if (err_code != NULL)
7126 *err_code = EMAIL_ERROR_INVALID_PARAM;
7131 int error = EMAIL_ERROR_NONE;
7132 char sql_query_string[QUERY_SIZE] = {0, };
7133 char *replaced_mailbox_name = NULL;
7134 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7136 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7138 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7140 if (mailbox_name) { /* NULL means all mailbox_name */
7141 if (strstr(mailbox_name, "'")) {
7142 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7144 replaced_mailbox_name = strdup(mailbox_name);
7147 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);
7150 if (uid) { /* NULL means all mail */
7151 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND server_uid='%s' ", uid);
7154 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7155 if (error != EMAIL_ERROR_NONE) {
7156 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7163 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7165 EM_SAFE_FREE(replaced_mailbox_name);
7167 if (err_code != NULL)
7170 EM_DEBUG_FUNC_END("ret [%d]", ret);
7174 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7176 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7179 int error = EMAIL_ERROR_NONE;
7182 if (!read_mail_uid || !*read_mail_uid) {
7183 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7185 error = EMAIL_ERROR_INVALID_PARAM;
7189 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7192 for (i = 0; i < count; i++) {
7193 EM_SAFE_FREE(p[i].mailbox_name);
7194 EM_SAFE_FREE(p[i].server_uid);
7197 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7203 if (err_code != NULL)
7206 EM_DEBUG_FUNC_END("ret [%d]", ret);
7210 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7212 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7215 EM_DEBUG_EXCEPTION("count[%p]", count);
7217 if (err_code != NULL)
7218 *err_code = EMAIL_ERROR_INVALID_PARAM;
7222 int rc = -1, ret = false;
7223 int error = EMAIL_ERROR_NONE;
7224 char sql_query_string[QUERY_SIZE] = {0, };
7226 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7227 EMSTORAGE_START_READ_TRANSACTION(transaction);
7229 if (account_id != ALL_ACCOUNT)
7230 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7232 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7236 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7237 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7238 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7240 *count = atoi(result[1]);
7241 sqlite3_free_table(result);
7246 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7248 if (err_code != NULL)
7251 EM_DEBUG_FUNC_END("ret [%d]", ret);
7255 INTERNAL_FUNC int emstorage_get_rule(char *multi_user_name, 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)
7257 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);
7259 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
7260 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);
7262 if (err_code != NULL)
7263 *err_code = EMAIL_ERROR_INVALID_PARAM;
7268 int error = EMAIL_ERROR_NONE;
7270 emstorage_rule_tbl_t* p_data_tbl = NULL;
7271 int i = 0, count = 0;
7272 DB_STMT hStmt = NULL;
7273 char sql_query_string[QUERY_SIZE] = {0, };
7277 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7278 EMSTORAGE_START_READ_TRANSACTION(transaction);
7280 if (account_id != ALL_ACCOUNT) {
7282 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7284 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7287 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7289 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7292 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7293 /* EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7294 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7295 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7298 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7299 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7300 ("sqlite3_step fail:%d", rc));
7303 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7304 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7305 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7306 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7308 sqlite3_free_table(result);
7311 EM_DEBUG_LOG_DEV ("No matching rule found...");
7313 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7318 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7319 EM_DEBUG_EXCEPTION(" malloc failed...");
7321 error = EMAIL_ERROR_OUT_OF_MEMORY;
7325 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7327 for (i = 0; i < count; i++) {
7328 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7329 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7330 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7331 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7332 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7333 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7334 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7335 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7336 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7337 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7339 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7340 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7341 ("sqlite3_step fail:%d", rc));
7348 EM_DEBUG_LOG("[%d] rules found.", count);
7351 *rule_list = p_data_tbl;
7352 *select_num = count;
7354 else if (p_data_tbl != NULL)
7355 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7357 if (hStmt != NULL) {
7358 rc = sqlite3_finalize(hStmt);
7359 if (rc != SQLITE_OK) {
7360 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7361 error = EMAIL_ERROR_DB_FAILURE;
7365 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7367 if (err_code != NULL)
7370 EM_DEBUG_FUNC_END("ret [%d]", ret);
7374 INTERNAL_FUNC int emstorage_get_rule_by_id(char *multi_user_name, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
7376 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7377 int error = EMAIL_ERROR_NONE;
7379 DB_STMT hStmt = NULL;
7382 EM_DEBUG_EXCEPTION("Invalid parameter");
7383 error = EMAIL_ERROR_INVALID_PARAM;
7388 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7389 error = EMAIL_ERROR_INVALID_PARAM;
7393 emstorage_rule_tbl_t* p_data_tbl = NULL;
7396 char sql_query_string[QUERY_SIZE] = {0, };
7397 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7398 EMSTORAGE_START_READ_TRANSACTION(transaction);
7400 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7402 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7403 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7404 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7407 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7408 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7409 ("sqlite3_step fail:%d", rc));
7411 if (rc == SQLITE_DONE) {
7412 EM_DEBUG_EXCEPTION(" no matched rule found...");
7413 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7417 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7418 EM_DEBUG_EXCEPTION(" malloc failed...");
7419 error = EMAIL_ERROR_OUT_OF_MEMORY;
7423 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7424 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7425 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7426 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7427 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7428 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7429 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7430 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7431 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7432 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7433 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7442 if (hStmt != NULL) {
7443 rc = sqlite3_finalize(hStmt);
7444 if (rc != SQLITE_OK) {
7445 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7446 error = EMAIL_ERROR_DB_FAILURE;
7450 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7452 if (err_code != NULL)
7455 EM_DEBUG_FUNC_END("ret [%d]", ret);
7459 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7461 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7463 if (!new_rule) { /* only global rule supported. */
7464 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7466 if (err_code != NULL)
7467 *err_code = EMAIL_ERROR_INVALID_PARAM;
7471 int rc, ret = false;
7472 int error = EMAIL_ERROR_NONE;
7474 DB_STMT hStmt = NULL;
7475 char sql_query_string[QUERY_SIZE] = {0, };
7476 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7477 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7479 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7480 "UPDATE mail_rule_tbl SET"
7486 ", target_mailbox_id = ?"
7491 " WHERE rule_id = %d"
7495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7496 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7497 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7498 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7502 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7503 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7504 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7505 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7506 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7507 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7508 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7509 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7510 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7511 _bind_stmt_field_data_int(hStmt, i++, rule_id);
7514 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7515 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7516 ("sqlite3_step fail:%d", rc));
7521 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7522 if (hStmt != NULL) {
7523 rc = sqlite3_finalize(hStmt);
7524 if (rc != SQLITE_OK) {
7525 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7526 error = EMAIL_ERROR_DB_FAILURE;
7530 if (err_code != NULL)
7533 EM_DEBUG_FUNC_END("ret [%d]", ret);
7537 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7539 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7542 EM_DEBUG_LOG("rule is NULL");
7543 if (err_code != NULL)
7544 *err_code = EMAIL_ERROR_INVALID_PARAM;
7548 DB_STMT hStmt = NULL;
7549 char sql_query_string[QUERY_SIZE] = {0,};
7550 int error = EMAIL_ERROR_NONE;
7554 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7555 EMSTORAGE_START_READ_TRANSACTION(transaction);
7557 switch(rule->action_type) {
7558 case EMAIL_FILTER_MOVE:
7559 if (rule->type == EMAIL_PRIORITY_SENDER) {
7560 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7561 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7562 rule->action_type, rule->type, rule->value2);
7564 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7565 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(filter_name) = UPPER(\'%q\')", rule->action_type, rule->type, rule->filter_name);
7568 case EMAIL_FILTER_BLOCK:
7569 if (rule->type == EMAIL_FILTER_FROM)
7570 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7571 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7572 rule->action_type, rule->type, rule->value2);
7573 else if (rule->type == EMAIL_FILTER_SUBJECT)
7574 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7575 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7576 rule->action_type, rule->type, rule->value);
7577 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7578 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7579 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND (type = %d AND UPPER(value) = UPPER(\'%q\')) OR (type = %d AND UPPER(value2) = UPPER(\'%q\'))",
7580 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7584 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7585 error = EMAIL_ERROR_INVALID_PARAM;
7590 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7591 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;},
7592 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7594 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7595 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;},
7596 ("sqlite3_step fail:%d", rc));
7598 if (rc == SQLITE_DONE) {
7599 EM_DEBUG_EXCEPTION(" no matched rule found...");
7600 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7607 if (hStmt != NULL) {
7608 rc = sqlite3_finalize(hStmt);
7609 if (rc != SQLITE_OK) {
7610 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7611 error = EMAIL_ERROR_DB_FAILURE;
7615 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7620 EM_DEBUG_FUNC_END("ret [%d]", ret);
7624 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7626 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7628 if (!rule) { /* only global rule supported. */
7629 EM_DEBUG_LOG("rule is NULL");
7630 if (err_code != NULL)
7631 *err_code = EMAIL_ERROR_INVALID_PARAM;
7635 int rc, rc_2, ret = false;
7636 int error = EMAIL_ERROR_NONE;
7637 DB_STMT hStmt = NULL;
7638 char sql_query_string[QUERY_SIZE] = {0, };
7640 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7642 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7646 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7648 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7649 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7650 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7652 if (NULL==result[1])
7655 rc = atoi(result[1])+1;
7657 sqlite3_free_table(result);
7661 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7662 "INSERT INTO mail_rule_tbl VALUES "
7663 "( ?" /* account id */
7665 ", ?" /* filter_name */
7669 ", ?" /* action_type */
7670 ", ?" /* target_mailbox_id */
7672 ", ?)"); /* flag2 */
7674 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7675 if (rc_2 != SQLITE_OK) {
7676 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7677 error = EMAIL_ERROR_DB_FAILURE;
7681 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7682 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7683 _bind_stmt_field_data_string(hStmt, FILTER_NAME_IDX_IN_MAIL_RULE_TBL, (char*)rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7684 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7685 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7686 _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7687 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7688 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7689 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7690 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7692 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7693 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7694 ("sqlite3_step fail:%d", rc));
7699 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7700 if (hStmt != NULL) {
7701 rc = sqlite3_finalize(hStmt);
7702 if (rc != SQLITE_OK) {
7703 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7704 error = EMAIL_ERROR_DB_FAILURE;
7708 if (err_code != NULL)
7711 EM_DEBUG_FUNC_END("ret [%d]", ret);
7715 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7717 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7719 if (rule_id <= 0) { /* only global rule supported. */
7720 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7722 if (err_code != NULL)
7723 *err_code = EMAIL_ERROR_INVALID_PARAM;
7727 int rc, ret = false;
7728 int error = EMAIL_ERROR_NONE;
7729 char sql_query_string[QUERY_SIZE] = {0, };
7730 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7731 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7733 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7734 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7735 if (error != EMAIL_ERROR_NONE) {
7736 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7740 rc = sqlite3_changes(local_db_handle);
7742 EM_DEBUG_EXCEPTION(" no matched rule found...");
7744 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7751 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7753 if (err_code != NULL)
7756 EM_DEBUG_FUNC_END("ret [%d]", ret);
7760 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7762 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7765 int error = EMAIL_ERROR_NONE;
7768 if (!rule_list || !*rule_list) {
7769 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7771 error = EMAIL_ERROR_INVALID_PARAM;
7775 emstorage_rule_tbl_t* p = *rule_list;
7778 for (; i < count; i++) {
7779 EM_SAFE_FREE(p[i].value);
7782 EM_SAFE_FREE(p); *rule_list = NULL;
7786 if (err_code != NULL)
7789 EM_DEBUG_FUNC_END("ret [%d]", ret);
7793 INTERNAL_FUNC int emstorage_get_mail_count(char *multi_user_name, int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
7795 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);
7797 if (!total && !unseen) {
7798 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7799 if (err_code != NULL)
7800 *err_code = EMAIL_ERROR_INVALID_PARAM;
7804 int rc = -1, ret = false;
7805 int error = EMAIL_ERROR_NONE;
7806 DB_STMT hStmt = NULL;
7807 char sql_query_string[QUERY_SIZE] = {0, };
7808 char *replaced_mailbox_name = NULL;
7810 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7811 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7812 EMSTORAGE_START_READ_TRANSACTION(transaction);
7815 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7817 if (account_id != ALL_ACCOUNT) {
7818 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);
7820 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);
7822 else if (mailbox_id)
7823 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);
7825 #ifdef USE_GET_RECORD_COUNT_API
7828 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7829 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
7830 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7832 *total = atoi(result[1]);
7833 sqlite3_free_table(result);
7836 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7837 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7838 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7840 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7841 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7842 ("sqlite3_step fail:%d", rc));
7843 _get_stmt_field_data_int(hStmt, total, 0);
7844 #endif /* USE_GET_RECORD_COUNT_API */
7848 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7850 if (account_id != ALL_ACCOUNT)
7851 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);
7854 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);
7857 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)");
7860 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7861 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7862 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7864 *unseen = atoi(result[1]);
7865 sqlite3_free_table(result);
7873 #ifndef USE_PREPARED_QUERY_
7874 if (hStmt != NULL) {
7875 rc = sqlite3_finalize(hStmt);
7876 if (rc != SQLITE_OK) {
7877 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7878 error = EMAIL_ERROR_DB_FAILURE;
7883 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7885 EM_SAFE_FREE(replaced_mailbox_name);
7887 if (err_code != NULL)
7890 EM_DEBUG_FUNC_END("ret [%d]", ret);
7894 INTERNAL_FUNC int emstorage_get_mail_field_by_id(char *multi_user_name, int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7896 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7898 if (mail_id <= 0 || !mail) {
7899 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7900 if (err_code != NULL)
7901 *err_code = EMAIL_ERROR_INVALID_PARAM;
7906 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7908 if (p_data_tbl == NULL) {
7909 EM_DEBUG_EXCEPTION("malloc failed...");
7910 if (err_code != NULL)
7911 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7915 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7916 DB_STMT hStmt = NULL;
7917 char sql_query_string[QUERY_SIZE] = {0, };
7919 int rc, ret = false;
7920 int error = EMAIL_ERROR_NONE;
7922 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7923 EMSTORAGE_START_READ_TRANSACTION(transaction);
7926 case RETRIEVE_SUMMARY:
7927 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7928 "SELECT account_id, "
7931 "server_mail_status, "
7932 "server_mailbox_name, "
7936 "file_path_mime_entity, "
7937 "flags_seen_field, "
7941 "thread_item_count "
7942 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7945 case RETRIEVE_FIELDS_FOR_DELETE:
7946 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7947 "SELECT account_id, "
7949 "server_mail_status, "
7950 "server_mailbox_name, "
7952 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7955 case RETRIEVE_ACCOUNT:
7956 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7957 "SELECT account_id "
7958 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7962 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7963 "SELECT account_id, "
7964 "flags_seen_field, "
7967 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7971 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7972 error = EMAIL_ERROR_INVALID_PARAM;
7976 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
7978 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7979 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7980 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7983 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7984 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7985 ("sqlite3_step fail:%d", rc));
7987 if (rc == SQLITE_DONE) {
7988 EM_DEBUG_LOG("no matched mail found...");
7989 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7993 case RETRIEVE_SUMMARY:
7994 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7995 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7996 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7997 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7998 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7999 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8000 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8001 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8002 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8003 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8004 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8005 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8006 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8007 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8010 case RETRIEVE_FIELDS_FOR_DELETE:
8011 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8012 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8013 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8014 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8015 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8018 case RETRIEVE_ACCOUNT:
8019 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8023 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8024 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8025 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8026 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8035 else if (p_data_tbl != NULL)
8036 emstorage_free_mail(&p_data_tbl, 1, NULL);
8038 if (hStmt != NULL) {
8039 rc = sqlite3_finalize(hStmt);
8040 if (rc != SQLITE_OK) {
8041 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8042 error = EMAIL_ERROR_DB_FAILURE;
8047 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8049 if (err_code != NULL)
8052 EM_DEBUG_FUNC_END("ret [%d]", ret);
8056 INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(char *multi_user_name, int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
8058 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);
8061 int error = EMAIL_ERROR_NONE;
8062 int query_string_length = 0;
8063 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8064 char **result = NULL;
8065 char *sql_query_string = NULL;
8066 emstorage_mail_tbl_t* p_data_tbl = NULL;
8067 sqlite3 *local_db_handle = NULL;
8069 if (number_of_mails <= 0 || !mail_ids) {
8070 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8071 if (err_code != NULL)
8072 *err_code = EMAIL_ERROR_INVALID_PARAM;
8076 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8078 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8079 sql_query_string = (char*)em_malloc(query_string_length);
8081 if (p_data_tbl == NULL || sql_query_string == NULL) {
8082 EM_DEBUG_EXCEPTION("malloc failed...");
8084 EM_SAFE_FREE(p_data_tbl);
8085 EM_SAFE_FREE(sql_query_string);
8087 if (err_code != NULL)
8088 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8092 local_db_handle = emstorage_get_db_connection(multi_user_name);
8094 EMSTORAGE_START_READ_TRANSACTION(transaction);
8097 case RETRIEVE_SUMMARY:
8098 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8099 "SELECT account_id, "
8102 "server_mail_status, "
8103 "server_mailbox_name, "
8107 "file_path_mime_entity, "
8109 "flags_seen_field, "
8113 "thread_item_count "
8114 "FROM mail_tbl WHERE mail_id in (");
8118 case RETRIEVE_FIELDS_FOR_DELETE:
8119 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8120 "SELECT account_id, "
8122 "server_mail_status, "
8123 "server_mailbox_name, "
8125 "FROM mail_tbl WHERE mail_id in (");
8129 case RETRIEVE_ACCOUNT:
8130 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8131 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8136 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8137 "SELECT account_id, "
8140 "flags_seen_field, "
8142 "FROM mail_tbl WHERE mail_id in (");
8147 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8148 error = EMAIL_ERROR_INVALID_PARAM;
8152 for(i = 0; i < number_of_mails; i++)
8153 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8154 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8156 EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8158 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8159 if (SQLITE_OK != rc && -1 != rc) {
8160 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8161 error = EMAIL_ERROR_DB_FAILURE;
8165 if (rc == SQLITE_DONE) {
8166 EM_DEBUG_LOG("no matched mail found...");
8167 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8171 EM_DEBUG_LOG("item_count [%d]", item_count);
8173 if(number_of_mails != item_count) {
8174 EM_DEBUG_EXCEPTION("Can't find all emails");
8175 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8179 col_index = field_count;
8181 for(i = 0; i < item_count; i++) {
8183 case RETRIEVE_SUMMARY:
8184 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8185 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8186 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8187 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8188 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8189 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8190 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8191 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8192 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8193 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8194 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8195 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8196 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8197 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8198 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8201 case RETRIEVE_FIELDS_FOR_DELETE:
8202 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8203 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8204 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8205 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8206 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8209 case RETRIEVE_ACCOUNT:
8210 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8214 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8215 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8216 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8217 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8218 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8229 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8232 sqlite3_free_table(result);
8234 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8236 EM_SAFE_FREE(sql_query_string);
8238 if (err_code != NULL)
8241 EM_DEBUG_FUNC_END("ret [%d]", ret);
8245 INTERNAL_FUNC int emstorage_get_mail_by_id(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
8247 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8249 if (mail_id <= 0 || !mail) {
8250 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8251 if (err_code != NULL)
8252 *err_code = EMAIL_ERROR_INVALID_PARAM;
8256 int ret = false, error = EMAIL_ERROR_NONE, count;
8257 char conditional_clause[QUERY_SIZE] = {0, };
8258 emstorage_mail_tbl_t* p_data_tbl = NULL;
8260 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8261 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8263 if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8264 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8273 else if (p_data_tbl != NULL)
8274 emstorage_free_mail(&p_data_tbl, 1, &error);
8276 if (err_code != NULL)
8279 EM_DEBUG_FUNC_END("ret [%d]", ret);
8283 #ifdef __FEATURE_BODY_SEARCH__
8284 INTERNAL_FUNC int emstorage_get_mail_text_by_id(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
8286 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8288 if (mail_id <= 0 || !mail_text) {
8289 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8290 if (err_code != NULL)
8291 *err_code = EMAIL_ERROR_INVALID_PARAM;
8296 int error = EMAIL_ERROR_NONE;
8298 char conditional_clause[QUERY_SIZE] = {0, };
8299 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8301 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8302 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8304 if(!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8305 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8313 *mail_text = p_data_tbl;
8314 else if (p_data_tbl != NULL)
8315 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8317 if (err_code != NULL)
8320 EM_DEBUG_FUNC_END("ret [%d]", ret);
8325 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8326 emstorage_search_filter_t *search,
8330 DB_STMT *search_handle,
8335 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8336 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8337 search, account_id, mailbox_id, sorting, search_handle,
8338 searched, transaction, err_code);
8340 if (!search_handle || !searched) {
8341 if (err_code != NULL)
8342 *err_code = EMAIL_ERROR_INVALID_PARAM;
8343 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8344 EM_DEBUG_FUNC_END("false");
8348 emstorage_search_filter_t* p = search;
8349 int error = EMAIL_ERROR_NONE;
8350 DB_STMT hStmt = NULL;
8351 char sql_query_string[QUERY_SIZE] = {0, };
8352 int rc, ret = false;
8353 int and = false, mail_count = 0;
8355 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8356 EMSTORAGE_START_READ_TRANSACTION(transaction);
8358 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8360 if (account_id != ALL_ACCOUNT) {
8361 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);
8366 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", mailbox_id);
8373 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8374 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);
8377 else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8378 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);
8381 else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8382 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);
8385 else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8386 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);
8394 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");
8396 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8399 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8400 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8401 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8404 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8405 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8406 ("sqlite3_step fail:%d", rc));
8410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8411 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8412 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8414 sqlite3_free_table(result);
8420 *search_handle = hStmt;
8421 *searched = mail_count;
8422 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8425 if (hStmt != NULL) {
8426 rc = sqlite3_finalize(hStmt);
8427 if (rc != SQLITE_OK) {
8428 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8429 error = EMAIL_ERROR_DB_FAILURE;
8433 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8436 if (err_code != NULL)
8439 EM_DEBUG_FUNC_END("ret [%d]", ret);
8443 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8445 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8447 if (search_handle == 0 || !data) {
8448 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8450 if (err_code != NULL)
8451 *err_code = EMAIL_ERROR_INVALID_PARAM;
8455 emstorage_mail_tbl_t* p_data_tbl = NULL;
8456 DB_STMT hStmt = search_handle;
8457 int rc, ret = false;
8458 int error = EMAIL_ERROR_NONE;
8462 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8465 case RETRIEVE_ENVELOPE:
8467 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8468 EM_DEBUG_EXCEPTION(" em_malloc failed...");
8469 error = EMAIL_ERROR_OUT_OF_MEMORY;
8473 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8474 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8475 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8476 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8477 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8478 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8479 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8480 _get_stmt_field_data_int (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8481 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8482 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8483 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8484 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8485 _get_stmt_field_data_char (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8486 _get_stmt_field_data_int (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8487 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8488 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8489 _get_stmt_field_data_int (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8490 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8491 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8492 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8493 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8494 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8495 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8496 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8497 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8498 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8499 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8500 _get_stmt_field_data_blob (hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8502 if (type == RETRIEVE_ALL) {
8503 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8504 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8505 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8506 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8507 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8508 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8509 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8510 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8511 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8512 _get_stmt_field_data_int (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8513 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8514 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8515 _get_stmt_field_data_int (hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8516 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8519 if (p_data_tbl->body_download_status) {
8522 if (p_data_tbl->file_path_html) {
8523 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8524 p_data_tbl->body_download_status = 0;
8526 else if (p_data_tbl->file_path_plain) {
8527 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8528 p_data_tbl->body_download_status = 0;
8531 p_data_tbl->body_download_status = 0;
8534 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8537 case RETRIEVE_SUMMARY:
8538 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8539 EM_DEBUG_EXCEPTION(" malloc failed...");
8541 error = EMAIL_ERROR_OUT_OF_MEMORY;
8545 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8547 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8548 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8549 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8550 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8551 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8553 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8556 case RETRIEVE_ADDRESS:
8557 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8558 EM_DEBUG_EXCEPTION(" malloc failed...");
8559 error = EMAIL_ERROR_OUT_OF_MEMORY;
8563 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8564 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8565 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8566 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8567 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8574 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8575 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8576 ("sqlite3_step fail:%d", rc));
8582 if (err_code != NULL)
8585 if (ret == false && p_data_tbl)
8586 emstorage_free_mail(&p_data_tbl, 1, NULL);
8588 EM_DEBUG_FUNC_END("ret [%d]", ret);
8592 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8594 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8596 int error = EMAIL_ERROR_NONE;
8597 int rc, ret = false;
8599 if (search_handle == 0) {
8600 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8601 error = EMAIL_ERROR_INVALID_PARAM;
8605 DB_STMT hStmt = search_handle;
8607 rc = sqlite3_finalize(hStmt);
8608 if (rc != SQLITE_OK) {
8609 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8610 error = EMAIL_ERROR_DB_FAILURE;
8616 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8618 if (err_code != NULL)
8621 EM_DEBUG_FUNC_END("ret [%d]", ret);
8625 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8627 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8629 if (mail_id <= 0 || !mail) {
8630 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8632 if (err_code != NULL)
8633 *err_code = EMAIL_ERROR_INVALID_PARAM;
8637 DB_STMT hStmt = NULL;
8638 char sql_query_string[QUERY_SIZE] = {0, };
8641 int error = EMAIL_ERROR_NONE;
8643 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8644 char mailbox_id_param_string[10] = {0,};
8646 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8648 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8649 "UPDATE mail_tbl SET"
8654 ", server_mail_status = ?"
8655 ", server_mailbox_name = ?"
8656 ", server_mail_id = ?"
8657 ", reference_mail_id = ?"
8658 ", full_address_from = ?"
8659 ", full_address_reply = ?" /* 10 */
8660 ", full_address_to = ?"
8661 ", full_address_cc = ?"
8662 ", full_address_bcc = ?"
8663 ", full_address_return = ?"
8665 ", body_download_status = ?"
8666 ", file_path_plain = ?"
8667 ", file_path_html = ?"
8668 ", file_path_mime_entity = ?"
8670 ", flags_seen_field = ?"
8671 ", flags_deleted_field = ?"
8672 ", flags_flagged_field = ?"
8673 ", flags_answered_field = ?"
8674 ", flags_recent_field = ?"
8675 ", flags_draft_field = ?"
8676 ", flags_forwarded_field = ?"
8682 ", report_status = ?"
8683 ", preview_text = ?"
8685 ", scheduled_sending_time = ?"
8686 ", remaining_resend_times = ?"
8688 ", replied_time = ?"
8689 ", forwarded_time = ?"
8690 ", default_charset = ?"
8691 ", eas_data_length = ?"
8693 " WHERE mail_id = %d AND account_id != 0 "
8697 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8698 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8699 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8701 _bind_stmt_field_data_int (hStmt, i++, mail->mail_id);
8702 _bind_stmt_field_data_int (hStmt, i++, mail->account_id);
8703 _bind_stmt_field_data_int (hStmt, i++, mail->mailbox_id);
8704 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
8705 _bind_stmt_field_data_int (hStmt, i++, mail->server_mail_status);
8706 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8707 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8708 _bind_stmt_field_data_int (hStmt, i++, mail->reference_mail_id);
8709 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8710 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8711 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8712 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8713 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8714 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8715 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8716 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
8717 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8718 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8719 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8720 _bind_stmt_field_data_int (hStmt, i++, mail->date_time);
8721 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
8722 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
8723 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
8724 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
8725 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
8726 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
8727 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
8728 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
8729 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
8730 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
8731 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
8732 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8733 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
8734 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8735 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
8736 _bind_stmt_field_data_int (hStmt, i++, mail->scheduled_sending_time);
8737 _bind_stmt_field_data_int (hStmt, i++, mail->remaining_resend_times);
8738 _bind_stmt_field_data_int (hStmt, i++, mail->tag_id);
8739 _bind_stmt_field_data_int (hStmt, i++, mail->replied_time);
8740 _bind_stmt_field_data_int (hStmt, i++, mail->forwarded_time);
8741 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8742 _bind_stmt_field_data_int (hStmt, i++, mail->eas_data_length);
8743 _bind_stmt_field_data_blob (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8745 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8746 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8747 ("sqlite3_step fail:%d", rc));
8749 rc = sqlite3_changes(local_db_handle);
8751 EM_DEBUG_LOG(" no matched mail found...");
8752 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8759 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8760 if (hStmt != NULL) {
8761 rc = sqlite3_finalize(hStmt);
8762 if (rc != SQLITE_OK) {
8763 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8764 error = EMAIL_ERROR_DB_FAILURE;
8769 if(error == EMAIL_ERROR_NONE && mail) {
8770 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8771 if (!emcore_notify_storage_event (NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8772 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event failed [NOTI_MAIL_UPDATE]");
8775 if (err_code != NULL)
8778 EM_DEBUG_FUNC_END("ret [%d]", ret);
8783 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8787 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int *err_code)
8789 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8791 EM_IF_NULL_RETURN_VALUE(err_code, false);
8794 int error = EMAIL_ERROR_NONE;
8796 char sql_query_string[QUERY_SIZE] = {0, };
8797 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8799 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8800 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);
8802 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8803 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8804 if (error != EMAIL_ERROR_NONE) {
8805 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8809 rc = sqlite3_changes(local_db_handle);
8811 EM_DEBUG_LOG(" No Matched Mail Exists ");
8812 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8819 if (err_code != NULL)
8822 EM_DEBUG_FUNC_END("ret [%d]", ret);
8826 INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(char *multi_user_name, int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
8828 EM_DEBUG_FUNC_BEGIN_SEC("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);
8830 int error = EMAIL_ERROR_NONE;
8833 int cur_mail_id_string = 0;
8834 int mail_id_string_buffer_length = 0;
8835 int parameter_string_length = 0;
8836 char *sql_query_string = NULL;
8837 char *mail_id_string_buffer = NULL;
8838 char *parameter_string = NULL;
8839 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8840 email_mail_attribute_type target_mail_attribute_type = 0;
8842 if (!mail_ids || !field_name || account_id == 0) {
8843 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8844 if (err_code != NULL)
8845 *err_code = EMAIL_ERROR_INVALID_PARAM;
8849 if( (error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8850 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8851 if (err_code != NULL)
8856 /* Generating mail id list string */
8857 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8859 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8861 if(!mail_id_string_buffer) {
8862 EM_DEBUG_EXCEPTION("em_malloc failed");
8863 if (err_code != NULL)
8864 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8868 for(i = 0; i < mail_ids_count; i++)
8869 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8871 if(EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8872 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8874 /* Generating notification parameter string */
8875 parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8876 parameter_string = em_malloc(parameter_string_length);
8878 if(!parameter_string) {
8879 EM_DEBUG_EXCEPTION("em_malloc failed");
8880 if (err_code != NULL)
8881 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8882 EM_SAFE_FREE (mail_id_string_buffer);
8886 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8887 query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8889 sql_query_string = em_malloc(query_size);
8890 if (sql_query_string == NULL) {
8891 EM_DEBUG_EXCEPTION("em_malloc failed");
8892 error = EMAIL_ERROR_OUT_OF_MEMORY;
8895 /* Write query string */
8896 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);
8898 EM_DEBUG_LOG_DEV ("sql_query_string [%s]", sql_query_string);
8901 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8902 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8903 if (error != EMAIL_ERROR_NONE) {
8904 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8908 if (sqlite3_changes(local_db_handle) == 0)
8909 EM_DEBUG_LOG("no mail matched...");
8914 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8916 if (error == EMAIL_ERROR_NONE && parameter_string) {
8917 if (!emcore_notify_storage_event (NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8918 EM_DEBUG_EXCEPTION_SEC ("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8921 EM_SAFE_FREE(mail_id_string_buffer);
8922 EM_SAFE_FREE(parameter_string);
8923 EM_SAFE_FREE(sql_query_string);
8926 if (err_code != NULL)
8929 EM_DEBUG_FUNC_END("error [%d]", error);
8933 #ifdef __FEATURE_BODY_SEARCH__
8934 INTERNAL_FUNC int emstorage_change_mail_text_field(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
8936 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8938 if (mail_id <= 0 || !mail_text) {
8939 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
8940 if (err_code != NULL)
8941 *err_code = EMAIL_ERROR_INVALID_PARAM;
8946 int error = EMAIL_ERROR_NONE;
8947 DB_STMT hStmt = NULL;
8948 char sql_query_string[QUERY_SIZE] = {0, };
8953 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8954 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8956 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8957 "UPDATE mail_text_tbl SET"
8959 " WHERE mail_id = %d AND account_id != 0"
8961 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8963 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8964 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8965 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8968 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
8970 if (hStmt != NULL) {
8971 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8972 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8973 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8975 rc = sqlite3_changes(local_db_handle);
8977 EM_DEBUG_LOG(" no matched mail found...");
8978 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8986 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8988 if (hStmt != NULL) {
8989 rc = sqlite3_finalize(hStmt);
8990 if (rc != SQLITE_OK) {
8991 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8992 error = EMAIL_ERROR_DB_FAILURE;
8997 if (err_code != NULL)
9000 EM_DEBUG_FUNC_END("ret [%d]", ret);
9005 INTERNAL_FUNC int emstorage_change_mail_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
9007 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9010 int error = EMAIL_ERROR_NONE;
9015 DB_STMT hStmt = NULL;
9016 char sql_query_string[QUERY_SIZE] = {0, };
9017 char mailbox_id_param_string[10] = {0,};
9018 sqlite3 *local_db_handle = NULL;
9020 if (mail_id <= 0 || !mail) {
9021 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9022 if (err_code != NULL)
9023 *err_code = EMAIL_ERROR_INVALID_PARAM;
9027 local_db_handle = emstorage_get_db_connection(multi_user_name);
9029 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9033 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9034 "UPDATE mail_tbl SET"
9035 " body_download_status = ?"
9036 ", file_path_plain = ?"
9037 ", file_path_html = ?"
9038 ", file_path_mime_entity = ?"
9039 ", flags_seen_field = ?"
9040 ", flags_deleted_field = ?"
9041 ", flags_flagged_field = ?"
9042 ", flags_answered_field = ?"
9043 ", flags_recent_field = ?"
9044 ", flags_draft_field = ?"
9045 ", flags_forwarded_field = ?"
9047 ", attachment_count = ?"
9049 ", meeting_request_status = ? "
9050 ", message_class = ? "
9051 ", digest_type = ? "
9053 " WHERE mail_id = %d AND account_id != 0"
9057 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9058 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9059 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9062 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9063 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9064 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9065 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9066 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9067 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9068 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9069 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9070 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9071 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9072 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9073 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9074 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9075 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9076 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9077 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9078 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9079 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9082 case UPDATE_MAILBOX: {
9084 emstorage_mailbox_tbl_t *mailbox_tbl;
9086 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9087 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9091 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9092 "UPDATE mail_tbl SET"
9093 " mailbox_id = '%d'"
9094 ",mailbox_type = '%d'"
9095 " WHERE mail_id = %d AND account_id != 0"
9096 , mailbox_tbl->mailbox_id
9097 , mailbox_tbl->mailbox_type
9102 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9103 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9104 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9107 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9109 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9115 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9116 "UPDATE mail_tbl SET"
9117 " flags_seen_field = %d"
9118 ",flags_deleted_field = %d"
9119 ",flags_flagged_field = %d"
9120 ",flags_answered_field = %d"
9121 ",flags_recent_field = %d"
9122 ",flags_draft_field = %d"
9123 ",flags_forwarded_field = %d"
9124 " WHERE mail_id = %d AND account_id != 0"
9125 , mail->flags_seen_field
9126 , mail->flags_deleted_field
9127 , mail->flags_flagged_field
9128 , mail->flags_answered_field
9129 , mail->flags_recent_field
9130 , mail->flags_draft_field
9131 , mail->flags_forwarded_field
9133 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9136 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9137 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9138 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9142 case UPDATE_EXTRA_FLAG:
9143 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9144 "UPDATE mail_tbl SET"
9146 ", save_status = %d"
9147 ", lock_status = %d"
9148 ", report_status = %d"
9150 " WHERE mail_id = %d AND account_id != 0"
9154 , mail->report_status
9157 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9160 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9161 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9162 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9165 case UPDATE_STICKY_EXTRA_FLAG:
9166 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9167 "UPDATE mail_tbl SET"
9169 " WHERE mail_id = %d AND account_id != 0"
9172 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9175 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9176 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9177 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9181 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9182 "UPDATE mail_tbl SET"
9183 " full_address_from = ?"
9184 ", full_address_reply = ?"
9185 ", full_address_to = ?"
9186 ", full_address_cc = ?"
9187 ", full_address_bcc = ?"
9188 ", full_address_return = ?"
9190 ", file_path_plain = ?"
9192 ", flags_seen_field = ?"
9193 ", flags_deleted_field = ?"
9194 ", flags_flagged_field = ?"
9195 ", flags_answered_field = ?"
9196 ", flags_recent_field = ?"
9197 ", flags_draft_field = ?"
9198 ", flags_forwarded_field = ?"
9202 ", report_status = ?"
9204 ", file_path_html = ?"
9205 ", file_path_mime_entity = ?"
9207 ", preview_text = ?"
9208 ", body_download_status = ?"
9209 ", attachment_count = ?"
9210 ", inline_content_count = ?"
9211 ", meeting_request_status = ?"
9212 ", message_class = ?"
9215 ", scheduled_sending_time = ?"
9216 ", remaining_resend_times = ?"
9218 ", replied_time = ?"
9219 ", forwarded_time = ?"
9220 ", default_charset = ?"
9221 ", eas_data_length = ?"
9223 " WHERE mail_id = %d AND account_id != 0"
9227 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9228 EM_DEBUG_LOG_DEV (" before sqlite3_prepare hStmt = %p", hStmt);
9229 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9230 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9232 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9233 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9234 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9235 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9236 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9237 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9238 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9239 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9240 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9241 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
9242 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
9243 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
9244 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
9245 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
9246 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
9247 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
9248 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
9249 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
9250 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
9251 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
9252 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
9253 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9254 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9255 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
9256 _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9257 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
9258 _bind_stmt_field_data_int (hStmt, i++, mail->attachment_count);
9259 _bind_stmt_field_data_int (hStmt, i++, mail->inline_content_count);
9260 _bind_stmt_field_data_int (hStmt, i++, mail->meeting_request_status);
9261 _bind_stmt_field_data_int (hStmt, i++, mail->message_class);
9262 _bind_stmt_field_data_int (hStmt, i++, mail->digest_type);
9263 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
9264 _bind_stmt_field_data_int (hStmt, i++, mail->scheduled_sending_time);
9265 _bind_stmt_field_data_int (hStmt, i++, mail->remaining_resend_times);
9266 _bind_stmt_field_data_int (hStmt, i++, mail->tag_id);
9267 _bind_stmt_field_data_int (hStmt, i++, mail->replied_time);
9268 _bind_stmt_field_data_int (hStmt, i++, mail->forwarded_time);
9269 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9270 _bind_stmt_field_data_int (hStmt, i++, mail->eas_data_length);
9271 _bind_stmt_field_data_blob (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9274 case UPDATE_DATETIME: {
9275 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9276 "UPDATE mail_tbl SET"
9277 " date_time = '%ld'"
9278 " WHERE mail_id = %d AND account_id != 0"
9282 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9283 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9284 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9288 case UPDATE_FROM_CONTACT_INFO:
9289 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9290 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9291 "UPDATE mail_tbl SET"
9292 " email_address_sender = ?,"
9293 " WHERE mail_id = %d",
9298 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9299 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9300 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9302 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9305 case UPDATE_TO_CONTACT_INFO:
9306 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9307 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9308 "UPDATE mail_tbl SET"
9309 " email_address_recipient = ?,"
9310 " WHERE mail_id = %d",
9315 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9316 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9317 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9319 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9322 case UPDATE_ALL_CONTACT_INFO:
9323 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9324 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9325 "UPDATE mail_tbl SET"
9326 " email_address_sender = ?,"
9327 " email_address_recipient = ?,"
9328 " WHERE mail_id = %d",
9333 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9334 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9335 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9337 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9338 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9342 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9343 case UPDATE_PARTIAL_BODY_DOWNLOAD:
9345 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9346 "UPDATE mail_tbl SET"
9347 " body_download_status = ?"
9348 ", file_path_plain = ?"
9349 ", file_path_html = ?"
9350 ", file_path_mime_entity = ?"
9351 ", attachment_count = ?"
9352 ", inline_content_count = ?"
9353 ", preview_text = ?"
9356 " WHERE mail_id = %d"
9360 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9361 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9362 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9365 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9366 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9367 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9368 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9369 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9370 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9371 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9372 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9373 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9378 case UPDATE_FILE_PATH:
9379 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9380 "UPDATE mail_tbl SET"
9381 ", file_path_plain = ?"
9382 ", file_path_html = ?"
9383 ", file_path_mime_entity = ?"
9384 " WHERE mail_id = %d"
9388 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9389 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9390 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9391 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9393 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9394 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9395 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9399 EM_DEBUG_LOG(" type[%d]", type);
9401 error = EMAIL_ERROR_INVALID_PARAM;
9405 if (hStmt != NULL) {
9407 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9408 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9409 ("sqlite3_step fail:%d", rc));
9410 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9411 ("sqlite3_step fail:%d", rc));
9412 rc = sqlite3_changes(local_db_handle);
9414 EM_DEBUG_LOG(" no matched mail found...");
9415 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9420 if (mail->account_id == 0) {
9421 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9422 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9423 EM_DEBUG_EXCEPTION ("emstorage_get_mail_field_by_id error [%d]", error);
9426 mail->account_id = mail_for_account_tbl->account_id;
9427 if (mail_for_account_tbl)
9428 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9434 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9436 if (hStmt != NULL) {
9437 rc = sqlite3_finalize(hStmt);
9438 if (rc != SQLITE_OK) {
9439 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
9440 error = EMAIL_ERROR_DB_FAILURE;
9444 if (error == EMAIL_ERROR_NONE && move_flag != 1 && transaction) {
9445 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9446 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9448 if (mail->mailbox_id == mailbox_id) {
9449 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9450 if (!emcore_notify_storage_event (NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9451 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event error [ NOTI_MAIL_UPDATE ] >>>> ");
9454 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9455 if (!emcore_notify_storage_event (NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9456 EM_DEBUG_EXCEPTION (" emcore_notify_storage_event error [ NOTI_MAIL_UPDATE ]");
9460 if (err_code != NULL)
9463 EM_DEBUG_FUNC_END("ret [%d]", ret);
9467 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9469 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9471 int rc, ret = false;
9472 int error = EMAIL_ERROR_NONE;
9473 int latest_mail_id = 0;
9474 sqlite3 *local_db_handle = NULL;
9475 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9476 char **result = NULL;
9478 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9479 _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9480 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9482 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9483 if (ret < 0 || latest_mail_id == 0) {
9484 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9486 local_db_handle = emstorage_get_db_connection(multi_user_name);
9488 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9489 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9490 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9491 if (NULL == result[1])
9494 rc = atoi(result[1]) + 1;
9496 sqlite3_free_table(result);
9497 latest_mail_id = rc;
9502 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9505 *mail_id = latest_mail_id;
9507 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9508 _unlockshm_mutex(mapped_for_generating_mail_id);
9509 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9515 if (err_code != NULL)
9518 EM_DEBUG_FUNC_END("ret [%d]", ret);
9522 INTERNAL_FUNC int emstorage_add_mail(char *multi_user_name, emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
9524 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9525 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9527 if (!mail_tbl_data) {
9528 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9529 if (err_code != NULL)
9530 *err_code = EMAIL_ERROR_INVALID_PARAM;
9534 int rc, ret = false;
9535 int error = EMAIL_ERROR_NONE;
9536 char sql_query_string[QUERY_SIZE] = {0, };
9537 DB_STMT hStmt = NULL;
9539 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9541 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9544 /* increase unique id */
9545 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9548 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9549 /* EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9550 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9551 if (rc != SQLITE_OK) {
9552 EM_DEBUG_EXCEPTION ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9553 error = EMAIL_ERROR_DB_FAILURE;
9554 sqlite3_free_table(result);
9558 if (NULL == result[1])
9561 rc = atoi(result[1])+1;
9563 sqlite3_free_table(result);
9565 mail_tbl_data->mail_id = rc;
9566 mail_tbl_data->thread_id = rc;
9569 if (mail_tbl_data->date_time == 0)
9570 mail_tbl_data->date_time = time(NULL);
9572 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9573 "INSERT INTO mail_tbl VALUES "
9575 ", ?" /* account_id */
9576 ", ?" /* mailbox_id */
9577 ", ?" /* mailbox_type */
9580 ", ?" /* date_time */
9581 ", ?" /* server_mail_status */
9582 ", ?" /* server_mailbox_name */
9583 ", ?" /* server_mail_id */
9584 ", ?" /* message_id */
9586 ", ?" /* reference_mail_id */
9587 ", ?" /* full_address_from */
9588 ", ?" /* full_address_reply */
9589 ", ?" /* full_address_to */
9590 ", ?" /* full_address_cc */
9592 ", ?" /* full_address_bcc */
9593 ", ?" /* full_address_return */
9594 ", ?" /* email_address_sender */
9595 ", ?" /* email_address_recipient */
9596 ", ?" /* alias_sender */
9598 ", ?" /* alias_recipient */
9599 ", ?" /* body_download_status */
9600 ", ?" /* file_path_plain */
9601 ", ?" /* file_path_html */
9602 ", ?" /* file_path_mime_entity */
9604 ", ?" /* mail_size */
9605 ", ?" /* flags_seen_field */
9606 ", ?" /* flags_deleted_field */
9607 ", ?" /* flags_flagged_field */
9608 ", ?" /* flags_answered_field */
9610 ", ?" /* flags_recent_field */
9611 ", ?" /* flags_draft_field */
9612 ", ?" /* flags_forwarded_field */
9613 ", ?" /* DRM_status */
9614 ", ?" /* priority */
9616 ", ?" /* save_status */
9617 ", ?" /* lock_status */
9618 ", ?" /* report_status */
9619 ", ?" /* attachment_count */
9620 ", ?" /* inline_content_count */
9622 ", ?" /* thread_id */
9623 ", ?" /* thread_item_count */
9624 ", ?" /* preview_text */
9625 ", ?" /* meeting_request_status */
9626 ", ?" /* message_class */
9628 ", ?" /* digest_type */
9629 ", ?" /* smime_type */
9630 ", ?" /* scheduled_sending_time */
9631 ", ?" /* remaining_resend_times */
9634 ", ?" /* replied_time */
9635 ", ?" /* forwarded_time */
9636 ", ?" /* default charset */
9637 ", ?" /* eas_data_length */
9638 ", ?" /* eas_data */
9639 ", ?" /* user_name */
9642 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9644 EM_SAFE_STRLEN(sql_query_string),
9648 if (rc != SQLITE_OK) {
9649 EM_DEBUG_EXCEPTION ("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9650 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9651 error = EMAIL_ERROR_DB_FAILURE;
9655 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9656 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9657 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9658 _bind_stmt_field_data_int (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9659 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9661 _bind_stmt_field_data_int (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9662 _bind_stmt_field_data_int (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9663 _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);
9664 _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);
9665 _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);
9667 _bind_stmt_field_data_int (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9668 _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);
9669 _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);
9670 _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);
9671 _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);
9673 _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);
9674 _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);
9675 _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);
9676 _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);
9677 _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);
9679 _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);
9680 _bind_stmt_field_data_int (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9681 _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);
9682 _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);
9683 _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);
9685 _bind_stmt_field_data_int (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9686 _bind_stmt_field_data_int (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9687 _bind_stmt_field_data_int (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9688 _bind_stmt_field_data_int (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9689 _bind_stmt_field_data_int (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9691 _bind_stmt_field_data_int (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9692 _bind_stmt_field_data_int (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9693 _bind_stmt_field_data_int (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9694 _bind_stmt_field_data_int (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9695 _bind_stmt_field_data_int (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9697 _bind_stmt_field_data_int (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9698 _bind_stmt_field_data_int (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9699 _bind_stmt_field_data_int (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9700 _bind_stmt_field_data_int (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9701 _bind_stmt_field_data_int (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9703 _bind_stmt_field_data_int (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9704 _bind_stmt_field_data_int (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9705 _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9706 _bind_stmt_field_data_int (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9707 _bind_stmt_field_data_int (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9709 _bind_stmt_field_data_int (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9710 _bind_stmt_field_data_int (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9711 _bind_stmt_field_data_int (hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9712 _bind_stmt_field_data_int (hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9713 _bind_stmt_field_data_int (hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9715 _bind_stmt_field_data_int (hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9716 _bind_stmt_field_data_int (hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9717 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9718 _bind_stmt_field_data_int (hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9719 _bind_stmt_field_data_blob (hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9720 _bind_stmt_field_data_string(hStmt, USER_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->user_name, 0, TEXT_2_LEN_IN_MAIL_TBL);
9722 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9723 if (rc == SQLITE_FULL) {
9724 EM_DEBUG_EXCEPTION ("sqlite3_step error [%d]", rc);
9725 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9728 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9729 EM_DEBUG_EXCEPTION ("sqlite3_step error [%d]", rc);
9730 error = EMAIL_ERROR_DB_FAILURE;
9736 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9738 if (hStmt != NULL) {
9739 rc = sqlite3_finalize(hStmt);
9740 if (rc != SQLITE_OK) {
9741 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
9742 error = EMAIL_ERROR_DB_FAILURE;
9746 if (err_code != NULL)
9749 EM_PROFILE_END(profile_emstorage_add_mail);
9750 EM_DEBUG_FUNC_END("ret [%d]", ret);
9754 INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(char *multi_user_name, int input_source_account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
9756 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);
9758 int ret = false, i, cur_conditional_clause = 0;
9759 int error = EMAIL_ERROR_NONE;
9760 int target_account_id;
9761 int conditional_clause_len = 0;
9762 char *sql_query_string = NULL, *conditional_clause = NULL;
9763 emstorage_mailbox_tbl_t *result_mailbox = NULL;
9764 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9765 char* target_mailbox_name = NULL;
9767 if (!mail_ids || input_mailbox_id <= 0) {
9768 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9769 if (err_code != NULL)
9770 *err_code = EMAIL_ERROR_INVALID_PARAM;
9774 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9776 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9777 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9778 if (err_code != NULL)
9783 if (result_mailbox->mailbox_name) {
9784 if (strstr(result_mailbox->mailbox_name, "'")) {
9785 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9787 target_mailbox_name = strdup(result_mailbox->mailbox_name);
9791 target_mailbox_type = result_mailbox->mailbox_type;
9792 target_account_id = result_mailbox->account_id;
9793 emstorage_free_mailbox(&result_mailbox, 1, NULL);
9795 conditional_clause_len = (sizeof(char) * 8 * number_of_mails) + 512;
9796 conditional_clause = em_malloc(conditional_clause_len);
9797 if (conditional_clause == NULL) {
9798 EM_DEBUG_EXCEPTION("em_malloc failed");
9799 error = EMAIL_ERROR_OUT_OF_MEMORY;
9802 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9804 for(i = 0; i < number_of_mails; i++)
9805 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9808 char *last_comma = rindex(conditional_clause, ',');
9809 if(last_comma) *last_comma = ')'; /* replace , with ) */
9811 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9813 /* Updating a mail_tbl */
9815 sql_query_string = em_malloc(conditional_clause_len);
9816 if (sql_query_string == NULL) {
9817 EM_DEBUG_EXCEPTION("em_malloc failed");
9818 error = EMAIL_ERROR_OUT_OF_MEMORY;
9821 SNPRINTF(sql_query_string, conditional_clause_len, "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);
9822 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9824 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9825 if (error != EMAIL_ERROR_NONE) {
9826 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9830 /* Updating a mail_attachment_tbl */
9831 memset(sql_query_string, 0x00, conditional_clause_len);
9832 SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_attachment_tbl SET mailbox_id = '%d', account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9833 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9834 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9835 if (error != EMAIL_ERROR_NONE) {
9836 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9840 /* Updating a mail_meeting_tbl */
9841 memset(sql_query_string, 0x00, conditional_clause_len);
9842 SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_meeting_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9843 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9844 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9845 if (error != EMAIL_ERROR_NONE) {
9846 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9850 #ifdef __FEATURE_BODY_SEARCH__
9851 /* Updating mail_text_tbl */
9852 memset(sql_query_string, 0x00, conditional_clause_len);
9853 SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_text_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9854 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9855 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9856 if (error != EMAIL_ERROR_NONE) {
9857 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9862 /* Updating a mail_read_mail_uid_tbl */
9863 memset(conditional_clause, 0x00, conditional_clause_len);
9864 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9866 for(i = 0; i < number_of_mails; i++)
9867 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9870 last_comma = rindex(conditional_clause, ',');
9871 if(last_comma) *last_comma = ')'; /* replace , with ) */
9873 memset(sql_query_string, 0x00, conditional_clause_len);
9874 SNPRINTF(sql_query_string, conditional_clause_len, "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);
9875 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9876 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9877 if (error != EMAIL_ERROR_NONE) {
9878 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9885 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9887 EM_SAFE_FREE(target_mailbox_name);
9888 EM_SAFE_FREE(conditional_clause);
9889 EM_SAFE_FREE(sql_query_string);
9891 if (err_code != NULL)
9894 EM_DEBUG_FUNC_END("ret [%d]", ret);
9898 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9900 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9903 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9904 if (err_code != NULL)
9905 *err_code = EMAIL_ERROR_INVALID_PARAM;
9910 int error = EMAIL_ERROR_NONE;
9911 char sql_query_string[QUERY_SIZE] = {0, };
9912 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9913 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9915 memset(sql_query_string, 0x00, sizeof(sql_query_string));
9917 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9918 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9919 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9920 if (error != EMAIL_ERROR_NONE) {
9921 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9928 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9930 if (err_code != NULL)
9933 EM_DEBUG_FUNC_END("ret [%d]", ret);
9937 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
9939 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
9941 int ret = false, i, cur_sql_query_string = 0;
9942 int error = EMAIL_ERROR_NONE;
9944 char *sql_query_string = NULL;
9947 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9948 if (err_code != NULL)
9949 *err_code = EMAIL_ERROR_INVALID_PARAM;
9953 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9954 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9956 query_size = (sizeof(char) * 8 * number_of_mails) + 512;
9957 sql_query_string = em_malloc(query_size);
9958 if (sql_query_string == NULL) {
9959 EM_DEBUG_EXCEPTION("em_malloc failed");
9960 error = EMAIL_ERROR_OUT_OF_MEMORY;
9964 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
9966 for(i = 0; i < number_of_mails; i++)
9967 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
9970 char *last_comma = rindex(sql_query_string, ',');
9971 if (last_comma != NULL) *last_comma = ')'; /* replace , with ) */
9973 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9974 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9975 if (error != EMAIL_ERROR_NONE) {
9976 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9980 #ifdef __FEATURE_BODY_SEARCH__
9981 /* delete mail_text from mail_text_tbl */
9982 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
9984 for(i = 0; i < number_of_mails; i++)
9985 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
9987 last_comma = rindex(sql_query_string, ',');
9988 *last_comma = ')'; /* replace , with ) */
9990 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9991 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9992 if (error != EMAIL_ERROR_NONE) {
9993 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10001 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10003 EM_SAFE_FREE(sql_query_string);
10004 if (err_code != NULL)
10007 EM_DEBUG_FUNC_END("ret [%d]", ret);
10011 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10013 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10015 if (account_id < FIRST_ACCOUNT_ID) {
10016 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10018 if (err_code != NULL)
10019 *err_code = EMAIL_ERROR_INVALID_PARAM;
10023 int rc, ret = false;
10024 int error = EMAIL_ERROR_NONE;
10025 char sql_query_string[QUERY_SIZE] = {0, };
10027 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10028 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10030 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10031 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10032 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10033 if (error != EMAIL_ERROR_NONE) {
10034 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10038 rc = sqlite3_changes(local_db_handle);
10040 EM_DEBUG_LOG(" no mail found...");
10041 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10044 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
10045 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10046 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10047 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10048 if (error != EMAIL_ERROR_NONE) {
10049 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10053 rc = sqlite3_changes(local_db_handle);
10055 EM_DEBUG_LOG("No mail found...");
10056 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10059 #ifdef __FEATURE_BODY_SEARCH__
10060 /* Delete all mail_text in mail_text_tbl table based on account id */
10061 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10062 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10063 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10064 if (error != EMAIL_ERROR_NONE) {
10065 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10069 rc = sqlite3_changes(local_db_handle);
10071 EM_DEBUG_LOG("No mail found...");
10072 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10079 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10081 if (error == EMAIL_ERROR_NONE) {
10082 if (!emcore_notify_storage_event (NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10083 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
10086 if (err_code != NULL)
10089 EM_DEBUG_FUNC_END("ret [%d]", ret);
10093 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10095 EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10097 if (mailbox == NULL) {
10098 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10099 if (err_code != NULL)
10100 *err_code = EMAIL_ERROR_INVALID_PARAM;
10105 int error = EMAIL_ERROR_NONE;
10106 char sql_query_string[QUERY_SIZE] = {0, };
10107 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10108 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10110 if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10111 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_type = %d", mailbox->account_id, mailbox->mailbox_type);
10112 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10113 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10114 if (error != EMAIL_ERROR_NONE) {
10115 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10119 /* Delete Mails from mail_read_mail_uid_tbl */
10120 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'", mailbox->account_id, mailbox->mailbox_name);
10121 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10122 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10123 if (error != EMAIL_ERROR_NONE) {
10124 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10128 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10129 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10130 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10131 if (error != EMAIL_ERROR_NONE) {
10132 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10136 /* Delete Mails from mail_read_mail_uid_tbl */
10137 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10138 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10139 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10140 if (error != EMAIL_ERROR_NONE) {
10141 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10145 #ifdef __FEATURE_BODY_SEARCH__
10146 /* Delete Mails from mail_text_tbl */
10147 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10148 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10149 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10150 if (error != EMAIL_ERROR_NONE) {
10151 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10159 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10161 if (error == EMAIL_ERROR_NONE) {
10162 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10163 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10166 if (err_code != NULL)
10169 EM_DEBUG_FUNC_END("ret [%d]", ret);
10173 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10175 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10178 if ((mail_list == NULL) || (*mail_list == NULL)) {
10179 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10182 *err_code = EMAIL_ERROR_INVALID_PARAM;
10186 emstorage_mail_tbl_t* p = *mail_list;
10189 for (; i < count; i++, p++) {
10190 EM_SAFE_FREE(p->server_mailbox_name);
10191 EM_SAFE_FREE(p->server_mail_id);
10192 EM_SAFE_FREE(p->full_address_from);
10193 EM_SAFE_FREE(p->full_address_reply);
10194 EM_SAFE_FREE(p->full_address_to);
10195 EM_SAFE_FREE(p->full_address_cc);
10196 EM_SAFE_FREE(p->full_address_bcc);
10197 EM_SAFE_FREE(p->full_address_return);
10198 EM_SAFE_FREE(p->subject);
10199 EM_SAFE_FREE(p->file_path_plain);
10200 EM_SAFE_FREE(p->file_path_html);
10201 EM_SAFE_FREE(p->file_path_mime_entity);
10202 EM_SAFE_FREE(p->message_id);
10203 EM_SAFE_FREE(p->email_address_sender);
10204 EM_SAFE_FREE(p->email_address_recipient);
10205 EM_SAFE_FREE(p->preview_text);
10206 EM_SAFE_FREE(p->alias_sender);
10207 EM_SAFE_FREE(p->alias_recipient);
10208 EM_SAFE_FREE(p->default_charset);
10209 EM_SAFE_FREE(p->pgp_password);
10210 EM_SAFE_FREE(p->eas_data);
10211 EM_SAFE_FREE(p->user_name);
10213 EM_SAFE_FREE(*mail_list);
10216 if (err_code != NULL)
10217 *err_code = EMAIL_ERROR_NONE;
10219 EM_DEBUG_FUNC_END();
10223 #ifdef __FEATURE_BODY_SEARCH__
10224 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10226 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10229 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10230 EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10234 emstorage_mail_text_tbl_t *p = *mail_text_list;
10237 for (; i < count; i++, p++) {
10238 EM_SAFE_FREE(p->body_text);
10240 EM_SAFE_FREE(*mail_text_list);
10243 EM_DEBUG_FUNC_END();
10247 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10249 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10251 if (mail_id <= 0 || !count) {
10252 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10253 if (err_code != NULL)
10254 *err_code = EMAIL_ERROR_INVALID_PARAM;
10258 int rc = -1, ret = false;
10259 int error = EMAIL_ERROR_NONE;
10260 char sql_query_string[QUERY_SIZE] = {0, };
10262 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10263 EMSTORAGE_START_READ_TRANSACTION(transaction);
10265 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10269 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10270 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10271 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10273 *count = atoi(result[1]);
10274 sqlite3_free_table(result);
10279 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10281 if (err_code != NULL)
10284 EM_DEBUG_FUNC_END("ret [%d]", ret);
10288 INTERNAL_FUNC int emstorage_get_attachment_list(char *multi_user_name, int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
10290 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);
10292 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10293 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10294 return EMAIL_ERROR_INVALID_PARAM;
10297 int error = EMAIL_ERROR_NONE;
10300 char **result = NULL;
10301 char sql_query_string[QUERY_SIZE] = {0, };
10302 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10303 DB_STMT hStmt = NULL;
10304 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10306 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10307 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10308 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10309 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10310 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10312 *output_attachment_count = atoi(result[1]);
10313 sqlite3_free_table(result);
10315 if(*output_attachment_count == 0) {
10316 error = EMAIL_ERROR_NONE;
10320 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10323 EM_DEBUG_EXCEPTION("em_malloc failed...");
10324 error = EMAIL_ERROR_OUT_OF_MEMORY;
10328 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10329 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10331 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10332 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)));
10334 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10335 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10337 if (rc == SQLITE_DONE) {
10338 EM_DEBUG_EXCEPTION("no matched attachment found...");
10339 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10342 for (i = 0; i < *output_attachment_count; i++) {
10343 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10344 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10345 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10346 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10347 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10348 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10349 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10350 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10351 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10352 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10353 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10354 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10355 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10356 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10357 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10358 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10359 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10364 if (error == EMAIL_ERROR_NONE)
10365 *output_attachment_list = p_data_tbl;
10366 else if (p_data_tbl != NULL)
10367 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10370 rc = sqlite3_finalize(hStmt);
10371 if (rc != SQLITE_OK) {
10372 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
10373 error = EMAIL_ERROR_DB_FAILURE;
10374 if (*output_attachment_list)
10375 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10379 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10381 EM_DEBUG_FUNC_END("error [%d]", error);
10385 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10387 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10389 if (attachment_id <= 0 || !attachment) {
10390 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10391 if (err_code != NULL)
10392 *err_code = EMAIL_ERROR_INVALID_PARAM;
10396 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10397 int rc, ret = false;
10398 int error = EMAIL_ERROR_NONE;
10399 char sql_query_string[QUERY_SIZE] = {0, };
10401 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10402 EMSTORAGE_START_READ_TRANSACTION(transaction);
10404 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10406 sqlite3_stmt* hStmt = NULL;
10408 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10409 EM_DEBUG_LOG_DEV (" before sqlite3_prepare hStmt = %p", hStmt);
10411 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10412 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10415 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10416 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10417 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10419 if (rc == SQLITE_DONE) {
10420 EM_DEBUG_LOG ("no matched attachment found...");
10421 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10425 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10426 EM_DEBUG_EXCEPTION("malloc failed...");
10427 error = EMAIL_ERROR_OUT_OF_MEMORY;
10431 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10432 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10433 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10434 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10435 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10436 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10437 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10438 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10439 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10440 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10441 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10442 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10443 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10445 #ifdef __ATTACHMENT_OPTI__
10446 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10447 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10454 *attachment = p_data_tbl;
10456 if (hStmt != NULL) {
10457 rc = sqlite3_finalize(hStmt);
10458 if (rc != SQLITE_OK) {
10459 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10460 error = EMAIL_ERROR_DB_FAILURE;
10464 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10466 if (err_code != NULL)
10469 EM_DEBUG_FUNC_END("ret [%d]", ret);
10473 INTERNAL_FUNC int emstorage_get_attachment_nth(char *multi_user_name, int mail_id, int nth, emstorage_attachment_tbl_t **attachment_tbl, int transaction, int *err_code)
10475 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);
10477 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10478 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10480 if (err_code != NULL)
10481 *err_code = EMAIL_ERROR_INVALID_PARAM;
10485 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10487 int rc, ret = false;
10488 int error = EMAIL_ERROR_NONE;
10489 char sql_query_string[QUERY_SIZE] = {0, };
10491 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10492 EMSTORAGE_START_READ_TRANSACTION(transaction);
10494 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));
10495 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10497 DB_STMT hStmt = NULL;
10499 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10500 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10501 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10504 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10505 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10506 ("sqlite3_step fail:%d", rc));
10508 if (rc == SQLITE_DONE) {
10509 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10510 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10514 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10515 EM_DEBUG_EXCEPTION(" malloc failed...");
10516 error = EMAIL_ERROR_OUT_OF_MEMORY;
10520 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10521 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10522 p_data_tbl->attachment_name = cpy_str(p);
10523 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10524 p_data_tbl->attachment_path = cpy_str(p);
10525 if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10526 p_data_tbl->content_id = cpy_str(p);
10527 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10528 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10529 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10530 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10531 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10532 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10533 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10534 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10535 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10536 p_data_tbl->attachment_mime_type = cpy_str(p);
10537 #ifdef __ATTACHMENT_OPTI__
10538 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10539 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10540 p_data_tbl->section= cpy_str(p);
10546 *attachment_tbl = p_data_tbl;
10548 if (hStmt != NULL) {
10549 rc = sqlite3_finalize(hStmt);
10550 if (rc != SQLITE_OK) {
10551 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10552 error = EMAIL_ERROR_DB_FAILURE;
10556 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10558 if (err_code != NULL)
10561 EM_DEBUG_FUNC_END("ret [%d]", ret);
10565 INTERNAL_FUNC int emstorage_get_attachment_by_attachment_path(char *multi_user_name, char *attachment_path, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10567 EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10569 if (attachment_path == NULL || !attachment) {
10570 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10571 if (err_code != NULL)
10572 *err_code = EMAIL_ERROR_INVALID_PARAM;
10576 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10577 int rc, ret = false;
10578 int error = EMAIL_ERROR_NONE;
10579 char sql_query_string[QUERY_SIZE] = {0, };
10581 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10582 EMSTORAGE_START_READ_TRANSACTION(transaction);
10584 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10586 sqlite3_stmt* hStmt = NULL;
10588 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10589 EM_DEBUG_LOG_DEV ("before sqlite3_prepare hStmt = %p", hStmt);
10591 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10592 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10595 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10596 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10597 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10599 if (rc == SQLITE_DONE) {
10600 EM_DEBUG_LOG ("no matched attachment found...");
10601 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10605 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10606 EM_DEBUG_EXCEPTION("malloc failed...");
10607 error = EMAIL_ERROR_OUT_OF_MEMORY;
10611 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10612 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10613 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10614 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10615 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10616 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10617 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10618 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10619 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10620 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10621 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10622 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10623 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10625 #ifdef __ATTACHMENT_OPTI__
10626 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10627 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10634 *attachment = p_data_tbl;
10636 if (hStmt != NULL) {
10637 rc = sqlite3_finalize(hStmt);
10638 if (rc != SQLITE_OK) {
10639 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10640 error = EMAIL_ERROR_DB_FAILURE;
10644 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10646 if (err_code != NULL)
10649 EM_DEBUG_FUNC_END("ret [%d]", ret);
10653 INTERNAL_FUNC int emstorage_change_attachment_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t *attachment, int transaction, int *err_code)
10655 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10657 if (mail_id <= 0 || !attachment) {
10658 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10659 if (err_code != NULL)
10660 *err_code = EMAIL_ERROR_INVALID_PARAM;
10664 int rc, ret = false;
10665 int error = EMAIL_ERROR_NONE;
10666 DB_STMT hStmt = NULL;
10667 char sql_query_string[QUERY_SIZE] = {0, };
10671 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10673 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10676 case UPDATE_MAILBOX:
10677 EM_DEBUG_LOG("UPDATE_MAILBOX");
10678 if (!attachment->mailbox_id) {
10679 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10680 error = EMAIL_ERROR_INVALID_PARAM;
10683 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10684 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10686 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10687 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10688 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10689 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10691 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10694 case UPDATE_SAVENAME:
10695 EM_DEBUG_LOG("UPDATE_SAVENAME");
10696 if (!attachment->attachment_path) {
10697 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10698 error = EMAIL_ERROR_INVALID_PARAM;
10702 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10703 "UPDATE mail_attachment_tbl SET"
10704 " attachment_size = ?"
10705 ", attachment_save_status = ?"
10706 ", attachment_path = ?"
10707 " WHERE mail_id = %d"
10708 " AND attachment_id = %d"
10709 , attachment->mail_id
10710 , attachment->attachment_id);
10713 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10714 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10715 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10716 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10718 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10719 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10720 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10724 EM_DEBUG_LOG("type[%d]", type);
10725 error = EMAIL_ERROR_INVALID_PARAM;
10728 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10730 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10731 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10732 ("sqlite3_step fail:%d", rc));
10737 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10739 if (hStmt != NULL) {
10740 rc = sqlite3_finalize(hStmt);
10741 if (rc != SQLITE_OK) {
10742 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10743 error = EMAIL_ERROR_DB_FAILURE;
10747 if (err_code != NULL)
10749 EM_DEBUG_FUNC_END("ret [%d]", ret);
10753 INTERNAL_FUNC int emstorage_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_transaction)
10755 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_new_mailbox_name[%p] input_new_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_transaction[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_eas_data, input_eas_data_length, input_transaction);
10759 int error = EMAIL_ERROR_NONE;
10761 int account_id = 0;
10762 char sql_query_string[QUERY_SIZE] = {0, };
10763 char *replaced_mailbox_name = NULL;
10764 char *replaced_alias = NULL;
10765 sqlite3 *local_db_handle = NULL;
10766 DB_STMT hStmt = NULL;
10767 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10769 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10770 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10771 return EMAIL_ERROR_INVALID_PARAM;
10774 if (strstr(input_new_mailbox_name, "'")) {
10775 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10777 replaced_mailbox_name = strdup(input_new_mailbox_name);
10780 if (strstr(input_new_mailbox_alias, "'")) {
10781 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10783 replaced_alias = strdup(input_new_mailbox_alias);
10786 local_db_handle = emstorage_get_db_connection(multi_user_name);
10788 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10789 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10790 EM_SAFE_FREE (replaced_mailbox_name);
10791 EM_SAFE_FREE (replaced_alias);
10795 if (old_mailbox_data == NULL) {
10796 EM_DEBUG_LOG("old_mailbox_data is NULL");
10797 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10801 account_id = old_mailbox_data->account_id;
10803 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10805 if(input_eas_data && input_eas_data_length > 0) {
10806 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10807 "UPDATE mail_box_tbl SET"
10808 " mailbox_name = ?"
10811 ",eas_data_length = ?"
10812 " WHERE mailbox_id = ?");
10814 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10816 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10817 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10819 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10820 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10821 _bind_stmt_field_data_blob (hStmt, field_idx++ , input_eas_data, input_eas_data_length);
10822 _bind_stmt_field_data_int (hStmt, field_idx++ , input_eas_data_length);
10823 _bind_stmt_field_data_int (hStmt, field_idx++ , input_mailbox_id);
10826 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10827 "UPDATE mail_box_tbl SET"
10828 " mailbox_name = ?"
10830 " WHERE mailbox_id = ?");
10832 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10834 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10835 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10837 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10838 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10839 _bind_stmt_field_data_int (hStmt, field_idx++ , input_mailbox_id);
10843 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10844 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10845 ("sqlite3_step fail:%d", rc));
10846 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10847 ("sqlite3_step fail:%d", rc));
10849 if (sqlite3_changes(local_db_handle) == 0)
10850 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10855 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10856 if (error == EMAIL_ERROR_NONE) {
10857 if (!emcore_notify_storage_event (NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10858 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
10861 if (!emcore_notify_storage_event (NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10862 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10865 EM_SAFE_FREE(replaced_mailbox_name);
10866 EM_SAFE_FREE(replaced_alias);
10868 if (old_mailbox_data)
10869 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10871 if (hStmt != NULL) {
10872 rc = sqlite3_finalize(hStmt);
10873 if (rc != SQLITE_OK) {
10874 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10875 error = EMAIL_ERROR_DB_FAILURE;
10879 EM_DEBUG_FUNC_END("error [%d]", error);
10883 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10885 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10886 int rc, ret = false;
10887 int error = EMAIL_ERROR_NONE;
10888 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10891 if (!attachment_no) {
10892 EM_DEBUG_EXCEPTION("Invalid attachment");
10893 if (err_code != NULL)
10894 *err_code = EMAIL_ERROR_INVALID_PARAM;
10898 *attachment_no = -1;
10900 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10903 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10904 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10905 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10907 if (NULL == result[1])
10910 rc = atoi(result[1])+1;
10912 sqlite3_free_table(result);
10914 *attachment_no = rc;
10915 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
10920 if (err_code != NULL)
10923 EM_DEBUG_FUNC_END("ret [%d]", ret);
10927 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
10929 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
10933 int rc, ret = false;
10934 int error = EMAIL_ERROR_NONE;
10935 char sql_query_string[QUERY_SIZE] = {0, };
10937 DB_STMT hStmt = NULL;
10938 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10940 if (!attachment_tbl) {
10941 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
10942 if (err_code != NULL)
10943 *err_code = EMAIL_ERROR_INVALID_PARAM;
10947 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10949 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10951 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10952 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10953 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10955 if (NULL==result[1]) rc = 1;
10956 else rc = atoi(result[1]) + 1;
10957 sqlite3_free_table(result);
10959 attachment_tbl->attachment_id = rc;
10961 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10962 "INSERT INTO mail_attachment_tbl VALUES "
10963 "( ?" /* attachment_id */
10964 ", ?" /* attachment_name */
10965 ", ?" /* attachment_path */
10966 ", ?" /* content_id */
10967 ", ?" /* attachment_size */
10968 ", ?" /* mail_id */
10969 ", ?" /* account_id */
10970 ", ?" /* mailbox_id */
10971 ", ?" /* attachment_save_status */
10972 ", ?" /* attachment_drm_type */
10973 ", ?" /* attachment_drm_method */
10974 ", ?" /* attachment_inline_content_status */
10975 ", ?" /* attachment_mime_type */
10976 #ifdef __ATTACHMENT_OPTI__
10983 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10984 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10985 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10987 _bind_stmt_field_data_int (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
10988 _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);
10989 _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);
10990 _bind_stmt_field_data_string(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
10991 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
10992 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
10993 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
10994 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
10995 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
10996 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
10997 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
10998 _bind_stmt_field_data_int (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
10999 _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);
11000 #ifdef __ATTACHMENT_OPTI__
11001 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11002 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11006 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11007 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11008 ("sqlite3_step fail:%d", rc));
11009 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11010 ("sqlite3_step fail:%d", rc));
11012 rc = sqlite3_changes(local_db_handle);
11014 EM_DEBUG_LOG(" no matched mail found...");
11015 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11022 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11024 if (hStmt != NULL) {
11025 rc = sqlite3_finalize(hStmt);
11026 if (rc != SQLITE_OK) {
11027 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11028 error = EMAIL_ERROR_DB_FAILURE;
11032 if (err_code != NULL)
11035 EM_DEBUG_FUNC_END("ret [%d]", ret);
11039 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11041 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11043 int rc, ret = false, field_idx = 0;
11044 int error = EMAIL_ERROR_NONE;
11045 DB_STMT hStmt = NULL;
11046 char sql_query_string[QUERY_SIZE] = {0, };
11048 if (!attachment_tbl) {
11049 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11051 *err_code = EMAIL_ERROR_INVALID_PARAM;
11055 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11057 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11059 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11060 "UPDATE mail_attachment_tbl SET "
11061 " attachment_name = ?"
11062 ", attachment_path = ?"
11064 ", attachment_size = ?"
11068 ", attachment_save_status = ?"
11069 ", attachment_drm_type = ?"
11070 ", attachment_drm_method = ?"
11071 ", attachment_inline_content_status = ? "
11072 ", attachment_mime_type = ? "
11073 " WHERE attachment_id = ?;");
11076 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11078 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11079 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11081 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11082 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11083 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11084 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_size);
11085 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mail_id);
11086 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->account_id);
11087 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mailbox_id);
11088 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11089 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11090 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11091 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11092 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11093 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_id);
11096 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11097 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11098 ("sqlite3_step fail:%d", rc));
11099 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11100 ("sqlite3_step fail:%d", rc));
11102 rc = sqlite3_changes(local_db_handle);
11104 EM_DEBUG_LOG(" no matched mail found...");
11105 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11112 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11113 if (hStmt != NULL) {
11114 rc = sqlite3_finalize(hStmt);
11115 if (rc != SQLITE_OK) {
11116 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11117 error = EMAIL_ERROR_DB_FAILURE;
11121 if (err_code != NULL)
11124 EM_DEBUG_FUNC_END("ret [%d]", ret);
11128 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11130 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11132 if (attachment_id < 0) {
11133 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11134 if (err_code != NULL)
11135 *err_code = EMAIL_ERROR_INVALID_PARAM;
11140 int error = EMAIL_ERROR_NONE;
11141 char sql_query_string[QUERY_SIZE] = {0, };
11142 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11144 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11146 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11148 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11149 if (error != EMAIL_ERROR_NONE) {
11150 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11157 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11162 EM_DEBUG_FUNC_END("ret [%d]", ret);
11166 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11168 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11170 int error = EMAIL_ERROR_NONE;
11171 char sql_query_string[QUERY_SIZE] = {0, };
11172 sqlite3 *local_db_handle = NULL;
11174 if (mail_id <= 0) {
11175 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11176 if (err_code != NULL)
11177 *err_code = EMAIL_ERROR_INVALID_PARAM;
11181 local_db_handle = emstorage_get_db_connection(multi_user_name);
11183 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11185 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11186 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11187 if (error != EMAIL_ERROR_NONE) {
11188 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11195 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11200 EM_DEBUG_FUNC_END("ret [%d]", ret);
11204 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11206 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11208 int error = EMAIL_ERROR_NONE;
11210 char sql_query_string[QUERY_SIZE] = {0, };
11211 char *replaced_mailbox = NULL;
11212 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11214 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11216 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11218 if (account_id != ALL_ACCOUNT) /* '0' means all account */
11219 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);
11221 if (mailbox) /* NULL means all mailbox_name */ {
11222 if (strstr(mailbox, "'")) {
11223 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11225 replaced_mailbox = strdup(mailbox);
11228 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);
11229 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11232 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11233 if (error != EMAIL_ERROR_NONE) {
11234 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11241 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11243 if (err_code != NULL)
11246 EM_DEBUG_FUNC_END("ret [%d]", ret);
11250 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11252 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11255 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11256 EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11257 if (err_code != NULL)
11258 *err_code = EMAIL_ERROR_INVALID_PARAM;
11262 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11265 for (i = 0; i < count; i++) {
11266 EM_SAFE_FREE(p[i].attachment_name);
11267 EM_SAFE_FREE(p[i].attachment_path);
11268 EM_SAFE_FREE(p[i].content_id);
11269 EM_SAFE_FREE(p[i].attachment_mime_type);
11270 #ifdef __ATTACHMENT_OPTI__
11271 EM_SAFE_FREE(p[i].section);
11276 *attachment_tbl_list = NULL;
11279 if (err_code != NULL)
11280 *err_code = EMAIL_ERROR_NONE;
11281 EM_DEBUG_FUNC_END();
11285 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11287 EM_PROFILE_BEGIN(emStorageBeginTransaction);
11290 _timedlock_shm_mutex(mapped_for_db_lock, 2);
11292 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11295 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11296 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11297 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11299 if (ret == false) {
11300 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11303 EM_PROFILE_END(emStorageBeginTransaction);
11304 EM_DEBUG_FUNC_END("ret [%d]", ret);
11308 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11310 EM_DEBUG_FUNC_BEGIN();
11314 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11316 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11317 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11319 if (ret == false && err_code != NULL)
11320 *err_code = EMAIL_ERROR_DB_FAILURE;
11322 _unlockshm_mutex(mapped_for_db_lock);
11324 EM_DEBUG_FUNC_END("ret [%d]", ret);
11328 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11330 EM_DEBUG_FUNC_BEGIN();
11333 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11335 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11336 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11337 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11339 if (ret == false && err_code != NULL)
11340 *err_code = EMAIL_ERROR_DB_FAILURE;
11342 _unlockshm_mutex(mapped_for_db_lock);
11344 EM_DEBUG_FUNC_END("ret [%d]", ret);
11348 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11350 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11352 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11354 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11356 if (err_code != NULL)
11357 *err_code = EMAIL_ERROR_INVALID_PARAM;
11363 int error = EMAIL_ERROR_NONE;
11364 int mail_count = 0;
11366 if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11367 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11372 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11373 if (mail_count >= mailbox->mail_slot_size)
11384 if (err_code != NULL)
11387 EM_DEBUG_FUNC_END("ret [%d]", ret);
11391 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11393 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11396 int error = EMAIL_ERROR_NONE;
11397 char sql_query_string[QUERY_SIZE] = {0, };
11398 const email_db_object_t* tables = _g_db_tables;
11399 const email_db_object_t* indexes = _g_db_indexes;
11401 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11402 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11404 if (!emstorage_delete_dir(MAILHOME, &error)) {
11405 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11410 mkdir(MAILHOME, DIRECTORY_PERMISSION);
11411 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11412 chmod(MAILTEMP, 0777);
11414 /* first clear index. */
11415 while (indexes->object_name) {
11416 if (indexes->data_flag) {
11417 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11418 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11419 if (error != EMAIL_ERROR_NONE) {
11420 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11427 while (tables->object_name) {
11428 if (tables->data_flag) {
11429 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11430 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11431 if (error != EMAIL_ERROR_NONE) {
11432 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11442 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11444 if (err_code != NULL)
11447 EM_DEBUG_FUNC_END("ret [%d]", ret);
11450 /*======================= DB File Utils =============================================*/
11451 #include <dirent.h>
11452 #include <sys/types.h>
11453 #define DIR_SEPERATOR "/"
11455 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11457 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11458 char delims[] = "/";
11459 char *result = NULL;
11460 gchar **token = NULL;
11462 token = g_strsplit_set(file_name, delims, 1);
11464 if (token && token[0]) {
11465 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11466 result = EM_SAFE_STRDUP(token[0]);
11468 EM_DEBUG_LOG(">>>> No Need to create Directory");
11475 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11476 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11478 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], move_buf[%p], path_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf, err_code);
11479 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11482 int error = EMAIL_ERROR_NONE;
11483 char *dir_name = NULL;
11484 char create_dir[1024]={0};
11485 char *temp_file = NULL;
11486 char *prefix_path = NULL;
11488 if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11489 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], move_buf[%p], path_buf[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf);
11490 error = EMAIL_ERROR_INVALID_PARAM;
11494 sprintf(path_buf, "%s", MAILHOME);
11495 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11498 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11501 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11504 temp_file = EM_SAFE_STRDUP(fname);
11505 if (temp_file && strstr(temp_file, "/")) {
11506 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11511 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11512 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11513 mkdir(create_dir, DIRECTORY_PERMISSION);
11514 EM_SAFE_FREE(dir_name);
11518 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11519 if (EM_SAFE_STRLEN(fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11520 char *modified_name = NULL;
11521 int remain_len = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11523 if (remain_len <= 0) {
11524 error = EMAIL_ERROR_MAX_EXCEEDED;
11528 if (remain_len > MAX_FILENAME) {
11529 remain_len = MAX_FILENAME;
11532 modified_name = em_shrink_filename(fname, remain_len);
11534 if (!modified_name) {
11535 error = EMAIL_ERROR_MAX_EXCEEDED;
11539 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
11540 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11541 EM_SAFE_FREE(modified_name);
11543 if (EM_SAFE_STRLEN(fname) > MAX_FILENAME - 1) {
11544 char *modified_name = NULL;
11546 modified_name = em_shrink_filename(fname, MAX_FILENAME);
11547 if (!modified_name) {
11548 error = EMAIL_ERROR_MAX_EXCEEDED;
11552 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
11553 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11554 EM_SAFE_FREE(modified_name);
11556 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, fname);
11561 EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11563 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11564 error = emcore_get_container_path(multi_user_name, &prefix_path);
11565 if (error != EMAIL_ERROR_NONE) {
11566 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11569 sprintf(move_buf, "%s/%s", prefix_path, path_buf);
11570 EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11572 sprintf(move_buf, "%s", path_buf);
11573 EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11580 EM_SAFE_FREE(temp_file);
11581 EM_SAFE_FREE(prefix_path);
11583 if (err_code != NULL)
11586 EM_PROFILE_END(profile_emstorage_get_save_name);
11587 EM_DEBUG_FUNC_END("ret [%d]", ret);
11591 INTERNAL_FUNC int emstorage_get_dele_name(char *multi_user_name, int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
11593 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);
11595 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11596 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);
11597 if (err_code != NULL)
11598 *err_code = EMAIL_ERROR_INVALID_PARAM;
11602 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11605 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11610 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11615 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11617 EM_DEBUG_FUNC_END();
11621 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11623 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11624 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11626 int error = EMAIL_ERROR_NONE;
11629 char *prefix_path = NULL;
11630 char errno_buf[ERRNO_BUF_SIZE] = {0};
11632 memset(buf, 0x00, sizeof(buf));
11634 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11635 error = emcore_get_container_path(multi_user_name, &prefix_path);
11636 if (error != EMAIL_ERROR_NONE) {
11637 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11641 prefix_path = strdup("");
11644 if (account_id >= FIRST_ACCOUNT_ID) {
11645 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11651 if (stat(buf, &sbuf) == 0) {
11652 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11653 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11654 error = EMAIL_ERROR_SYSTEM_FAILURE;
11659 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11660 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11661 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11662 error = EMAIL_ERROR_SYSTEM_FAILURE;
11664 error = EMAIL_ERROR_OUT_OF_MEMORY;
11668 if (account_id == EML_FOLDER) {
11675 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11677 if (account_id < FIRST_ACCOUNT_ID) {
11678 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11679 error = EMAIL_ERROR_INVALID_PARAM;
11683 if (space_left_in_buffer + 10 > sizeof(buf)) {
11684 EM_DEBUG_EXCEPTION("Buffer overflowed");
11685 error = EMAIL_ERROR_OUT_OF_MEMORY;
11689 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11691 if (stat(buf, &sbuf) == 0) {
11692 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11693 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11694 error = EMAIL_ERROR_SYSTEM_FAILURE;
11699 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11700 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11701 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11702 error = EMAIL_ERROR_SYSTEM_FAILURE;
11704 error = EMAIL_ERROR_OUT_OF_MEMORY;
11708 if (account_id == EML_FOLDER) {
11715 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11716 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11718 error = EMAIL_ERROR_INVALID_PARAM;
11722 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11724 if (stat(buf, &sbuf) == 0) {
11725 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11726 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11728 error = EMAIL_ERROR_SYSTEM_FAILURE;
11733 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11734 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11735 error = EMAIL_ERROR_SYSTEM_FAILURE;
11737 error = EMAIL_ERROR_OUT_OF_MEMORY;
11741 if (account_id == EML_FOLDER) {
11751 EM_SAFE_FREE(prefix_path);
11753 if (err_code != NULL)
11756 EM_PROFILE_END(profile_emcore_save_create_dir);
11757 EM_DEBUG_FUNC_END("ret [%d]", ret);
11761 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11763 EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11764 EM_DEBUG_LOG("Using the fsync function");
11766 int error = EMAIL_ERROR_NONE;
11767 struct stat st_buf;
11772 char buf[FILE_MAX_BUFFER_SIZE] = {0};
11773 char errno_buf[ERRNO_BUF_SIZE] = {0};
11775 if (!src_file || !dst_file) {
11776 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11778 error = EMAIL_ERROR_INVALID_PARAM;
11782 if (stat(src_file, &st_buf) < 0) {
11783 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11785 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
11789 error = em_open(src_file, O_RDONLY, 0, &fp_src);
11790 if (error != EMAIL_ERROR_NONE) {
11791 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11795 error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11796 if (error != EMAIL_ERROR_NONE) {
11797 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s: %d", dst_file, error);
11801 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11803 while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11804 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11805 EM_DEBUG_LOG("Nread Value [%d]", nread);
11807 ssize_t byte_written = 0;
11808 size_t remain_byte = nread;
11812 while(remain_byte > 0 && buf_ptr && errno == 0) {
11813 byte_written = write(fp_dst, buf_ptr, remain_byte);
11815 if (byte_written < 0) {
11816 /* interrupted by a signal */
11817 if (errno == EINTR) {
11822 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11823 error = EMAIL_ERROR_UNKNOWN;
11826 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11827 remain_byte -= byte_written;
11828 buf_ptr += byte_written;
11832 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11838 EM_SAFE_CLOSE (fp_src);
11840 if (fp_dst >=0) { /*prevent 24474*/
11842 EM_DEBUG_LOG("Before fsync");
11848 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11851 if (err_code != NULL)
11853 EM_DEBUG_FUNC_END("ret [%d]", ret);
11856 /* create Directory if user has deleted [deepam.p@samsung.com] */
11857 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11859 EM_DEBUG_FUNC_BEGIN();
11861 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11862 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
11863 mkdir(MAILHOME, DIRECTORY_PERMISSION);
11864 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11865 chmod(MAILTEMP, 0777);
11867 EM_DEBUG_FUNC_END();
11870 static int _get_temp_file_name(char **filename, int *err_code)
11872 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11875 int error = EMAIL_ERROR_NONE;
11877 if (filename == NULL) {
11878 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11879 error = EMAIL_ERROR_INVALID_PARAM;
11883 char tempname[512] = {0x00, };
11886 gettimeofday(&tv, NULL);
11889 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
11891 char *p = EM_SAFE_STRDUP(tempname);
11893 EM_DEBUG_EXCEPTION(" strdup failed...");
11894 error = EMAIL_ERROR_OUT_OF_MEMORY;
11903 if (err_code != NULL)
11906 EM_DEBUG_FUNC_END("ret [%d]", ret);
11910 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
11912 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
11914 EM_IF_NULL_RETURN_VALUE(file_path, false);
11915 EM_IF_NULL_RETURN_VALUE(char_set, false);
11916 EM_IF_NULL_RETURN_VALUE(err_code, false);
11920 struct stat st_buf;
11922 char *low_char_set = NULL;
11923 char *match_str = NULL;
11926 int error = EMAIL_ERROR_NONE;
11927 int data_count_to_written = 0;
11928 char *temp_file_name = NULL;
11930 FILE* fp_src = NULL;
11931 FILE* fp_dest = NULL;
11935 if (stat(file_path, &st_buf) < 0) {
11936 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
11937 error = EMAIL_ERROR_SYSTEM_FAILURE;
11941 buf_size = st_buf.st_size;
11942 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
11943 buf = (char *)calloc(1, buf_size+1);
11946 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
11950 error = em_fopen(file_path, "rb", &fp_src);
11951 if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
11952 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
11956 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
11957 if (nread > 0 && nread <= buf_size) {
11958 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
11961 * 1.Add check for whether content type is there.
11962 * 2. If not based on the character set, Append it in File
11965 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
11966 if (low_char_set == NULL) {
11967 EM_DEBUG_EXCEPTION("calloc failed");
11968 error = EMAIL_ERROR_OUT_OF_MEMORY;
11972 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
11973 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
11974 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
11975 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
11976 match_str = strstr(buf, CONTENT_TYPE_DATA);
11978 if (match_str == NULL) {
11979 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
11980 if (fp_src !=NULL) {
11981 fclose(fp_src);fp_src = NULL;
11983 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
11984 buf1 = (char *)calloc(1, data_count_to_written);
11987 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
11988 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
11989 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
11990 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
11992 /* 1. Create a temporary file name */
11993 if (!_get_temp_file_name(&temp_file_name, &err)) {
11994 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
11995 if (err_code != NULL) *err_code = err;
11998 EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12000 /* Open the Temp file in Append mode */
12001 error = em_fopen(temp_file_name, "ab", &fp_dest);
12002 if (error != EMAIL_ERROR_NONE) {
12003 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12007 /* 2. write the Latest data */
12008 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12010 if (nwritten > 0) {
12011 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
12013 /* 3. Append old data */
12014 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12016 if (nwritten <= 0) {
12017 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12018 error = EMAIL_ERROR_SYSTEM_FAILURE;
12022 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12024 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12025 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12032 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12033 error = EMAIL_ERROR_SYSTEM_FAILURE;
12045 EM_SAFE_FREE(buf1);
12046 EM_SAFE_FREE(low_char_set);
12047 EM_SAFE_FREE(temp_file_name);
12049 if (fp_src != NULL) {
12054 if (fp_dest != NULL) {
12060 EM_DEBUG_FUNC_END("ret [%d]", ret);
12065 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12067 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12070 int error = EMAIL_ERROR_NONE;
12071 char errno_buf[ERRNO_BUF_SIZE] = {0};
12073 if (src_file == NULL || dst_file == NULL) {
12074 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12075 error = EMAIL_ERROR_INVALID_PARAM;
12079 EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12081 if (strcmp(src_file, dst_file) != 0) {
12082 if (rename(src_file, dst_file) != 0) {
12083 /* EM_DEBUG_EXCEPTION("%s", strerror(errno) ); */
12084 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() */
12085 /* does not work across different mount points, even if the same file system is mounted on both.) */
12086 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12087 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12088 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12092 EM_DEBUG_LOG("src[%s] removed", src_file);
12096 if (errno == ENOENT) {
12097 struct stat temp_file_stat;
12098 if (stat(src_file, &temp_file_stat) < 0) {
12099 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12101 if (stat(dst_file, &temp_file_stat) < 0)
12102 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12104 error = EMAIL_ERROR_FILE_NOT_FOUND;
12109 EM_DEBUG_EXCEPTION("rename failed: %s", EM_STRERROR(errno_buf));
12110 error = EMAIL_ERROR_SYSTEM_FAILURE;
12117 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12123 if (err_code != NULL)
12126 EM_DEBUG_FUNC_END("ret [%d]", ret);
12130 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12132 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12135 int error = EMAIL_ERROR_NONE;
12137 if (src_file == NULL) {
12138 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12140 error = EMAIL_ERROR_INVALID_PARAM;
12144 if (remove(src_file) != 0) {
12145 if (errno != ENOENT) {
12146 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12148 error = EMAIL_ERROR_SYSTEM_FAILURE;
12152 EM_DEBUG_EXCEPTION(" no file found...");
12154 error = EMAIL_ERROR_FILE_NOT_FOUND;
12161 if (err_code != NULL)
12164 EM_DEBUG_FUNC_END("ret [%d]", ret);
12169 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12171 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12173 if (src_dir == NULL) {
12174 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12176 if (err_code != NULL)
12177 *err_code = EMAIL_ERROR_INVALID_PARAM;
12181 int error = EMAIL_ERROR_NONE;
12188 dirp = opendir(src_dir);
12190 if (dirp == NULL) {
12191 if (errno == ENOENT) {
12192 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12193 if (err_code != NULL)
12194 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12198 EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12199 if (err_code != NULL)
12200 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12205 while ((dp=readdir(dirp))) {
12206 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12209 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12211 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12212 /* check directory */
12213 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
12214 /* recursive call */
12215 if (!emstorage_delete_dir(buf, &error)) {
12217 if (err_code != NULL)
12223 if (remove(buf) < 0) {
12224 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12226 if (err_code != NULL)
12227 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12233 EM_DEBUG_EXCEPTION("content does not exist...");
12238 EM_DEBUG_LOG_DEV ("remove direcotory [%s]", src_dir);
12240 /* EM_DEBUG_FUNC_BEGIN(); */
12242 if (remove(src_dir) < 0) {
12243 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12244 if (err_code != NULL)
12245 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12249 if (err_code != NULL)
12255 /* faizan.h@samsung.com */
12256 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, int *err_code)
12258 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12260 int error = EMAIL_ERROR_NONE;
12261 char sql_query_string[QUERY_SIZE] = {0, };
12262 int transaction = true;
12264 if (!old_server_uid || !new_server_uid) {
12265 EM_DEBUG_EXCEPTION("Invalid parameters");
12267 *err_code = EMAIL_ERROR_INVALID_PARAM;
12271 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12273 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12275 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12276 "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
12278 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12279 if (error != EMAIL_ERROR_NONE) {
12280 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12287 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12289 if (err_code != NULL)
12292 EM_DEBUG_FUNC_END("ret [%d]", ret);
12297 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
12299 EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12302 int error = EMAIL_ERROR_NONE;
12303 char sql_query_string[QUERY_SIZE] = {0, };
12304 int transaction = true;
12306 if (!mail_id || !new_server_uid || !mbox_name) {
12307 EM_DEBUG_EXCEPTION("Invalid parameters");
12308 if (err_code != NULL)
12309 *err_code = EMAIL_ERROR_INVALID_PARAM;
12313 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12314 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12317 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12318 "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
12320 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12321 if (error != EMAIL_ERROR_NONE) {
12322 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12330 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12332 if (err_code != NULL)
12335 EM_DEBUG_FUNC_END("ret [%d]", ret);
12340 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12342 EM_DEBUG_FUNC_BEGIN();
12345 int transaction = true;
12346 int error = EMAIL_ERROR_NONE;
12347 char sql_query_string[QUERY_SIZE] = {0,};
12349 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12351 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12353 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12355 if (account_id <= ALL_ACCOUNT)
12356 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12358 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d) and account_id = %d ", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED, account_id);
12360 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12361 if (error != EMAIL_ERROR_NONE) {
12362 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12369 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12371 if (err_code != NULL)
12374 EM_DEBUG_FUNC_END("ret [%d]", ret);
12379 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12381 EM_DEBUG_FUNC_BEGIN();
12383 if ((!mail_ids) ||(account_id <= 0 && account_id != -1)) {
12384 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12385 if (err_code != NULL)
12386 *err_code = EMAIL_ERROR_INVALID_PARAM;
12392 int error = EMAIL_ERROR_NONE;
12396 int *p_mail_ids = NULL;
12397 int transaction = false;
12398 char **result = NULL;
12399 char sql_query_string[QUERY_SIZE] = {0, };
12400 char temp_query_string[QUERY_SIZE] = {0,};
12401 char sql_select_query_string[QUERY_SIZE] = {0, };
12403 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12404 EMSTORAGE_START_READ_TRANSACTION(transaction);
12406 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12408 if (account_id == -1)
12409 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12411 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)", account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12414 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
12416 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12419 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12421 EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12423 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12424 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12425 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12427 EM_DEBUG_LOG("Count : %d", count);
12430 EM_DEBUG_EXCEPTION("no Mails found...");
12432 error= EMAIL_ERROR_MAIL_NOT_FOUND;
12436 p_mail_ids = em_malloc(count * sizeof(int));
12437 if (p_mail_ids == NULL) {
12438 EM_DEBUG_EXCEPTION("em_malloc failed...");
12439 error = EMAIL_ERROR_OUT_OF_MEMORY;
12445 for (i = 0; i < count; i++) {
12446 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12454 sqlite3_free_table(result);
12456 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12458 // sqlite3_db_release_memory(local_db_handle);
12461 if (mail_ids != NULL)
12462 *mail_ids = p_mail_ids;
12464 if (mail_number != NULL)
12465 *mail_number = count;
12467 EM_SAFE_FREE(p_mail_ids);
12470 if (err_code != NULL)
12473 EM_DEBUG_FUNC_END("ret [%d]", ret);
12477 int setting_system_command(const char *command)
12480 int pid = 0, status = 0;
12481 char *const environ[] = { NULL };
12496 argv[2] = (char *)command;
12499 execve("/bin/sh", argv, environ);
12503 if (waitpid(pid, &status, 0) == -1) {
12504 if (errno != EINTR)
12516 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
12518 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12520 if (!total_usage) {
12521 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12523 if (err_code != NULL)
12524 *err_code = EMAIL_ERROR_INVALID_PARAM;
12529 int error = EMAIL_ERROR_NONE;
12530 char syscmd[256] = {0, };
12531 char line[256] = {0, };
12532 char *line_from_file = NULL;
12534 unsigned long total_diskusage = 0;
12536 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12537 if (setting_system_command(syscmd) == -1) {
12538 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12540 error = EMAIL_ERROR_SYSTEM_FAILURE;
12544 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
12545 EM_DEBUG_LOG(" cmd : %s", syscmd);
12546 if (setting_system_command(syscmd) == -1) {
12547 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12549 error = EMAIL_ERROR_SYSTEM_FAILURE;
12553 error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12554 if (error != EMAIL_ERROR_NONE) {
12555 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12559 line_from_file = fgets(line, sizeof(line), fp);
12561 if(line_from_file == NULL) {
12562 EM_DEBUG_EXCEPTION("fgets failed");
12563 error = EMAIL_ERROR_SYSTEM_FAILURE;
12566 total_diskusage = strtoul(line, NULL, 10);
12568 memset(syscmd, 0, sizeof(syscmd));
12569 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12570 if (setting_system_command(syscmd) == -1) {
12571 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12572 error = EMAIL_ERROR_SYSTEM_FAILURE;
12576 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
12581 if (err_code != NULL)
12585 *total_usage = total_diskusage;
12589 if(fp) fclose(fp); /* prevent 32730 */
12591 EM_DEBUG_FUNC_END("ret [%d]", ret);
12594 #define MAILHOME_UTF8 tzplatform_mkpath(TZ_USER_DATA,"email/.email_data/7/348/UTF-8")
12596 INTERNAL_FUNC int emstorage_test(char *multi_user_name, int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
12598 DB_STMT hStmt = NULL;
12600 int error = EMAIL_ERROR_NONE;
12602 char sql_query_string[QUERY_SIZE] = {0, };
12604 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12605 "INSERT INTO mail_tbl VALUES "
12606 "( ?" /* mail_id */
12607 ", ?" /* account_id */
12608 ", ?" /* mail_size */
12609 ", ?" /* server_mail_status */
12610 ", ?" /* server_mailbox_name */
12611 ", ?" /* server_mail_id */
12612 ", ?" /* reference_mail_id */
12613 ", ?" /* full_address_from */
12614 ", ?" /* full_address_reply */
12615 ", ?" /* full_address_to */
12616 ", ?" /* full_address_cc */
12617 ", ?" /* full_address_bcc */
12618 ", ?" /* full_address_return */
12619 ", ?" /* subject */
12620 ", ?" /* body_download_status */
12621 ", ?" /* file_path_plain */
12622 ", ?" /* file_path_html */
12623 ", ?" /* date_time */
12624 ", ?" /* flags_seen_field */
12625 ", ?" /* flags_deleted_field */
12626 ", ?" /* flags_flagged_field */
12627 ", ?" /* flags_answered_field */
12628 ", ?" /* flags_recent_field */
12629 ", ?" /* flags_draft_field */
12630 ", ?" /* flags_forwarded_field */
12631 ", ?" /* DRM_status */
12632 ", ?" /* priority */
12633 ", ?" /* save_status */
12634 ", ?" /* lock_status */
12635 ", ?" /* message_id */
12636 ", ?" /* report_status */
12637 ", ?" /* email_address_sender */
12638 ", ?" /* email_address_recipient */
12639 ", ?" /* attachment_count */
12640 ", ?" /* inline_content_count */
12641 ", ?" /* preview_text */
12642 ", ?" /* thread_id */
12643 ", ?" /* mailbox_type */
12644 ", ?" /* alias_sender */
12645 ", ?" /* alias_recipient */
12646 ", ?" /* thread_item_count */
12647 ", ?" /* meeting_request_status */
12648 ", ?" /* message_class */
12649 ", ?" /* digest_type */
12650 ", ?" /* smime_type */
12651 ", ?" /* scheduled_sending_time */
12652 ", ?" /* remaining_resend_times */
12654 ", ?" /* replied_time */
12655 ", ?" /* forwarded_time */
12656 ", ?" /* default_charset */
12657 ", ?" /* eas_data_length */
12658 ", ?" /* eas_data */
12661 int transaction = true;
12662 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12664 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12667 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12668 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12669 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12671 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12672 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12673 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12674 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12675 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12676 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12677 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12678 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12679 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12680 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12681 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12682 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12683 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12684 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12685 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12686 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12687 _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);
12688 _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);
12689 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12690 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12691 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12692 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12693 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12694 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12695 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12696 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12697 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12698 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12699 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12700 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12701 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12702 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12703 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12704 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12705 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12706 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12707 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12708 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12709 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12710 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12711 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12712 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12713 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12714 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12715 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12716 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12717 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12718 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12719 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12720 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12721 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12722 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12723 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12724 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12726 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12727 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12728 ("sqlite3_step fail:%d", rc));
12729 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12730 ("sqlite3_step fail:%d", rc));
12734 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12735 if (hStmt != NULL) {
12736 rc = sqlite3_finalize(hStmt);
12737 if (rc != SQLITE_OK) {
12738 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12739 error = EMAIL_ERROR_DB_FAILURE;
12743 if (err_code != NULL)
12746 EM_DEBUG_FUNC_END("ret [%d]", ret);
12751 INTERNAL_FUNC int emstorage_get_max_mail_count()
12753 return EMAIL_MAIL_MAX_COUNT;
12756 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12758 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name, emstorage_mail_tbl_t *mail_tbl, int *thread_id, int *result_latest_mail_id_in_thread, int *thread_item_count)
12760 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);
12761 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12762 int rc = 0, query_size = 0, query_size_account = 0;
12763 int account_id = 0;
12764 int err_code = EMAIL_ERROR_NONE;
12765 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12766 time_t latest_date_time = 0;
12767 char *subject = NULL;
12768 char *sql_query_string = NULL, *sql_account = NULL;
12769 char *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12770 char *sql_format_account = " AND account_id = %d ";
12771 char *sql_format_order_by = " ORDER BY date_time DESC ";
12772 char **result = NULL;
12773 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12775 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12777 EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12779 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12780 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12781 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12782 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12784 account_id = mail_tbl->account_id;
12785 subject = mail_tbl->subject;
12787 EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12789 if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12790 result_thread_id = mail_tbl->mail_id;
12795 if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12796 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12797 err_code = EMAIL_ERROR_UNKNOWN;
12798 result_thread_id = -1;
12802 EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12804 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12805 result_thread_id = -1;
12809 EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12811 if (account_id > 0) {
12812 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12813 sql_account = malloc(query_size_account);
12814 if (sql_account == NULL) {
12815 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
12816 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12819 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12822 /* prevent 34362 */
12823 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
12824 sql_query_string = malloc(query_size);
12826 if (sql_query_string == NULL) {
12827 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
12828 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12832 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12834 if (account_id > 0)
12835 strcat(sql_query_string, sql_account);
12837 strcat(sql_query_string, sql_format_order_by);
12838 strcat(sql_query_string, ";");
12840 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12842 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12844 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12845 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12847 EM_DEBUG_LOG("Result rows count : %d", count);
12850 result_thread_id = -1;
12852 _get_table_field_data_int (result, &result_thread_id, 3);
12853 _get_table_field_data_time_t(result, &latest_date_time, 4);
12854 _get_table_field_data_int (result, &latest_mail_id_in_thread, 5);
12856 if (latest_date_time < mail_tbl->date_time)
12857 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12859 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
12860 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
12864 *thread_id = result_thread_id;
12865 *thread_item_count = count;
12867 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
12868 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
12869 EM_DEBUG_LOG("err_code : %d", err_code);
12871 EM_SAFE_FREE(sql_account);
12872 EM_SAFE_FREE(sql_query_string);
12874 sqlite3_free_table(result);
12876 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
12881 INTERNAL_FUNC int emstorage_get_thread_id_from_mailbox(char *multi_user_name, int account_id, int mailbox_id, char *mail_subject, int *thread_id, int *thread_item_count)
12883 EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], subject [%p], thread_id [%p], thread_item_count [%p]", mailbox_id, mail_subject, thread_id, thread_item_count);
12884 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12887 int query_size = 0;
12888 int query_size_account = 0;
12889 int err_code = EMAIL_ERROR_NONE;
12891 int result_thread_id = -1;
12892 char *sql_query_string = NULL;
12893 char *sql_account = NULL;
12894 char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12895 char *sql_format_account = " AND account_id = %d ";
12896 char *sql_format_order_by = " ORDER BY date_time DESC ";
12897 char **result = NULL;
12898 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12899 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12901 EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
12902 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12903 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12905 EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
12907 if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12908 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12909 err_code = EMAIL_ERROR_UNKNOWN;
12910 result_thread_id = -1;
12914 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12915 result_thread_id = -1;
12919 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12921 if (account_id > 0) {
12922 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12923 sql_account = malloc(query_size_account);
12924 if (sql_account == NULL) {
12925 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
12926 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12929 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12932 query_size = EM_SAFE_STRLEN(sql_format) + EM_SAFE_STRLEN(stripped_subject)*2 + 50 + query_size_account + EM_SAFE_STRLEN(sql_format_order_by); /* + query_size_mailbox; */
12933 sql_query_string = malloc(query_size);
12935 if (sql_query_string == NULL) {
12936 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
12937 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12941 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
12943 if (account_id > 0)
12944 strcat(sql_query_string, sql_account);
12946 strcat(sql_query_string, sql_format_order_by);
12947 strcat(sql_query_string, ";");
12949 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12951 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12953 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12954 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12956 EM_DEBUG_LOG("Result rows count : %d", count);
12959 result_thread_id = -1;
12961 _get_table_field_data_int (result, &result_thread_id, 1);
12965 *thread_id = result_thread_id;
12966 *thread_item_count = count;
12968 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
12969 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
12970 EM_DEBUG_LOG("err_code : %d", err_code);
12972 EM_SAFE_FREE(sql_account);
12973 EM_SAFE_FREE(sql_query_string);
12975 sqlite3_free_table(result);
12977 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
12982 INTERNAL_FUNC int emstorage_get_thread_information(char *multi_user_name, int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
12984 EM_DEBUG_FUNC_BEGIN();
12986 int count = 0, ret = false;
12987 int error = EMAIL_ERROR_NONE;
12988 emstorage_mail_tbl_t *p_data_tbl = NULL;
12989 char conditional_clause[QUERY_SIZE] = {0, };
12991 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
12993 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
12994 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
12996 if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
12997 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13002 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13008 *mail_tbl = p_data_tbl;
13009 else if (p_data_tbl != NULL)
13010 emstorage_free_mail(&p_data_tbl, 1, NULL);
13012 if (err_code != NULL)
13015 EM_DEBUG_FUNC_END("ret [%d]", ret);
13019 INTERNAL_FUNC int emstorage_get_sender_list(char *multi_user_name, 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)
13021 EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
13022 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13024 if ((!sender_list) ||(!sender_count)) {
13025 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13026 if (err_code != NULL)
13027 *err_code = EMAIL_ERROR_INVALID_PARAM;
13031 int rc = -1, ret = false;
13032 int error = EMAIL_ERROR_NONE;
13034 int i, col_index = 0;
13035 int read_count = 0;
13036 email_sender_list_t *p_sender_list = NULL;
13037 char sql_query_string[QUERY_SIZE] = {0, };
13038 char **result = NULL;
13039 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13041 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13042 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13047 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);
13048 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13049 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) ");
13052 /* '0' (ALL_ACCOUNT) means all account */
13053 if (account_id > ALL_ACCOUNT)
13054 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);
13056 if (search_value) {
13057 switch (search_type) {
13058 case EMAIL_SEARCH_FILTER_SUBJECT:
13059 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13060 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13062 case EMAIL_SEARCH_FILTER_SENDER:
13063 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13064 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13065 ") ", search_value);
13067 case EMAIL_SEARCH_FILTER_RECIPIENT:
13068 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13069 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13070 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13071 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13072 ") ", search_value, search_value, search_value);
13074 case EMAIL_SEARCH_FILTER_ALL:
13075 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13076 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13077 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13078 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13079 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13080 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13083 ")", search_value, search_value, search_value, search_value, search_value);
13089 /* sorting option is not available now. The order of sender list is ascending order by display name */
13090 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13091 "GROUP BY email_address_sender "
13092 "ORDER BY UPPER(alias_sender) ");
13094 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13096 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13097 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13098 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13100 EM_DEBUG_LOG("Count of Sender [%d]", count);
13102 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13103 EM_DEBUG_EXCEPTION("em_malloc failed...");
13104 error = EMAIL_ERROR_OUT_OF_MEMORY;
13110 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13111 for (i = 0; i < count; i++) {
13112 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13113 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13114 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13115 _get_table_field_data_int(result, &(read_count), col_index++);
13116 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
13118 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13120 sqlite3_free_table(result);
13127 *sender_list = p_sender_list;
13128 *sender_count = count;
13129 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13133 if (err_code != NULL)
13136 EM_DEBUG_FUNC_END("ret [%d]", ret);
13140 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13142 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13144 int err = EMAIL_ERROR_NONE;
13147 if (!sender_list || !*sender_list) {
13148 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13149 err = EMAIL_ERROR_INVALID_PARAM;
13153 email_sender_list_t* p = *sender_list;
13156 for (; i < count; i++) {
13157 EM_SAFE_FREE(p[i].address);
13158 EM_SAFE_FREE(p[i].display_name);
13162 *sender_list = NULL;
13169 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13171 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13173 int err = EMAIL_ERROR_NONE;
13174 email_address_info_t *p_address_info = NULL;
13175 GList *list = NULL;
13176 GList *node = NULL;
13179 if (!address_info_list || !*address_info_list) {
13180 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13181 err = EMAIL_ERROR_INVALID_PARAM;
13185 /* delete GLists */
13186 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13188 case EMAIL_ADDRESS_TYPE_FROM:
13189 list = (*address_info_list)->from;
13191 case EMAIL_ADDRESS_TYPE_TO:
13192 list = (*address_info_list)->to;
13194 case EMAIL_ADDRESS_TYPE_CC:
13195 list = (*address_info_list)->cc;
13197 case EMAIL_ADDRESS_TYPE_BCC:
13198 list = (*address_info_list)->bcc;
13202 /* delete dynamic-allocated memory for each item */
13203 node = g_list_first(list);
13204 while (node != NULL) {
13205 p_address_info = (email_address_info_t*)node->data;
13206 EM_SAFE_FREE(p_address_info->address);
13207 EM_SAFE_FREE(p_address_info->display_name);
13208 EM_SAFE_FREE(node->data);
13210 node = g_list_next(node);
13215 EM_SAFE_FREE(*address_info_list);
13216 *address_info_list = NULL;
13218 EM_DEBUG_FUNC_END("err [%d]", err);
13221 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13223 INTERNAL_FUNC int emstorage_add_pbd_activity(char *multi_user_name, email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
13225 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13227 if (!local_activity || !activity_id) {
13228 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13229 if (err_code != NULL)
13230 *err_code = EMAIL_ERROR_INVALID_PARAM;
13236 int error = EMAIL_ERROR_NONE;
13239 char sql_query_string[QUERY_SIZE] = {0, };
13240 DB_STMT hStmt = NULL;
13241 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13243 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13244 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13245 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13246 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13248 "? " /* Account ID */
13249 ",?" /* Local Mail ID */
13250 ",?" /* Server mail ID */
13251 ",?" /* Activity ID */
13252 ",?" /* Activity type*/
13253 ",?" /* Mailbox ID*/
13254 ",?" /* Mailbox name*/
13255 ",?" /* Multi User Name */
13258 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13259 char **result = NULL;
13262 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13264 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13265 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13267 if (NULL==result[1]) rc = 1;
13268 else rc = atoi(result[1])+1;
13269 sqlite3_free_table(result);
13272 *activity_id = local_activity->activity_id = rc;
13274 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13275 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13277 if (local_activity->mailbox_id)
13278 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13281 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13282 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13283 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13286 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13287 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13288 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13289 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13290 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13291 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13292 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13293 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13296 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13298 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13299 ("sqlite3_step fail:%d", rc));
13300 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13301 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13306 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13307 if (hStmt != NULL) {
13308 rc = sqlite3_finalize(hStmt);
13310 if (rc != SQLITE_OK) {
13311 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13312 error = EMAIL_ERROR_DB_FAILURE;
13316 if (err_code != NULL)
13319 EM_DEBUG_FUNC_END("ret [%d]", ret);
13323 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
13325 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13327 if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL|| NULL == count) {
13328 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13329 if (err_code != NULL)
13330 *err_code = EMAIL_ERROR_INVALID_PARAM;
13335 int error = EMAIL_ERROR_NONE;
13337 int i = 0, rc = -1;
13338 int *mbox_list = NULL;
13339 DB_STMT hStmt = NULL;
13340 char sql_query_string[QUERY_SIZE] = {0, };
13342 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13343 EMSTORAGE_START_READ_TRANSACTION(transaction);
13345 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13346 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);
13349 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13350 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13351 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13353 *count = atoi(result[1]);
13354 sqlite3_free_table(result);
13357 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13358 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13362 EM_DEBUG_LOG("Mailbox count = %d", *count);
13364 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13366 /* 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); */
13367 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);
13369 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13372 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13375 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13376 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13379 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13380 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13381 ("sqlite3_step fail:%d", rc));
13383 mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13384 if (NULL == mbox_list) {
13385 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13386 error = EMAIL_ERROR_OUT_OF_MEMORY;
13390 memset(mbox_list, 0x00, sizeof(int) * (*count));
13392 for (i = 0; i < (*count); i++) {
13393 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13394 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13395 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13396 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13397 ("sqlite3_step fail:%d", rc));
13398 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13405 *mailbox_list = mbox_list;
13407 EM_SAFE_FREE(mbox_list);
13409 if (hStmt != NULL) {
13410 rc = sqlite3_finalize(hStmt);
13412 if (rc != SQLITE_OK) {
13413 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13414 error = EMAIL_ERROR_DB_FAILURE;
13418 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13419 if (err_code != NULL)
13421 EM_DEBUG_FUNC_END("ret [%d]", ret);
13425 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13427 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13429 if (NULL == account_list || NULL == count) {
13430 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13431 if (err_code != NULL)
13432 *err_code = EMAIL_ERROR_INVALID_PARAM;
13437 int error = EMAIL_ERROR_NONE;
13438 char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13440 int i = 0, rc = -1;
13441 int *result_account_list = NULL;
13442 DB_STMT hStmt = NULL;
13443 char sql_query_string[QUERY_SIZE] = {0, };
13445 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13447 EMSTORAGE_START_READ_TRANSACTION(transaction);
13449 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13450 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13451 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13453 *count = atoi(result[1]);
13454 sqlite3_free_table(result);
13457 EM_DEBUG_EXCEPTION("no account found...");
13458 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13463 EM_DEBUG_LOG("Account count [%d]", *count);
13465 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13467 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13469 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13472 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13474 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13475 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13476 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13479 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13480 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13481 ("sqlite3_step fail:%d", rc));
13483 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13484 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13485 error = EMAIL_ERROR_OUT_OF_MEMORY;
13489 memset(result_account_list, 0x00, sizeof(int) * (*count));
13491 for (i = 0; i < (*count); i++) {
13492 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13494 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13495 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13496 ("sqlite3_step fail:%d", rc));
13497 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13504 *account_list = result_account_list;
13506 EM_SAFE_FREE(result_account_list);
13508 if (hStmt != NULL) {
13509 rc = sqlite3_finalize(hStmt);
13511 if (rc != SQLITE_OK) {
13512 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13513 error = EMAIL_ERROR_DB_FAILURE;
13517 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13518 if (err_code != NULL)
13520 EM_DEBUG_FUNC_END("ret [%d]", ret);
13524 INTERNAL_FUNC int emstorage_get_pbd_activity_data(char *multi_user_name, int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
13526 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13528 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13529 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);
13531 if (err_code != NULL)
13532 *err_code = EMAIL_ERROR_INVALID_PARAM;
13539 int error = EMAIL_ERROR_NONE;
13541 DB_STMT hStmt = NULL;
13542 email_event_partial_body_thd* event_list = NULL;
13543 char sql_query_string[QUERY_SIZE] = {0, };
13545 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13547 EMSTORAGE_START_READ_TRANSACTION(transaction);
13549 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13550 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);
13553 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13554 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13555 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13557 *count = atoi(result[1]);
13558 sqlite3_free_table(result);
13560 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13563 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13564 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13568 EM_DEBUG_LOG("Activity Count = %d", *count);
13570 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13571 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);
13573 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13576 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13578 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13579 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13580 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13583 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13584 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13585 ("sqlite3_step fail:%d", rc));
13587 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13588 EM_DEBUG_EXCEPTION("Malloc failed");
13590 error = EMAIL_ERROR_OUT_OF_MEMORY;
13593 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13595 for (i=0; i < (*count); i++) {
13596 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13597 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13598 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13599 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13600 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13601 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13602 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13603 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13605 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13606 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13607 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13608 ("sqlite3_step fail:%d", rc));
13610 event_list[i].event_type = 0;
13617 *event_start = event_list;
13619 for (i=0; i < (*count); i++)
13620 emcore_free_partial_body_thd_event(event_list, NULL);
13621 EM_SAFE_FREE(event_list); /*prevent 54559*/
13622 *event_start = NULL;
13626 if (hStmt != NULL) {
13627 rc = sqlite3_finalize(hStmt);
13629 if (rc != SQLITE_OK) {
13630 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13631 error = EMAIL_ERROR_DB_FAILURE;
13635 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13636 if (err_code != NULL)
13639 EM_DEBUG_FUNC_END("ret [%d]", ret);
13643 INTERNAL_FUNC int emstorage_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
13645 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);
13648 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13649 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);
13651 if (err_code != NULL)
13652 *err_code = EMAIL_ERROR_INVALID_PARAM;
13658 int error = EMAIL_ERROR_NONE;
13659 char sql_query_string[QUERY_SIZE] = {0, };
13660 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13662 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13663 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13665 if (activity_id == 0)
13666 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);
13668 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);
13670 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13671 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13672 if (error != EMAIL_ERROR_NONE) {
13673 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13677 /* validate activity existence */
13678 rc = sqlite3_changes(local_db_handle);
13680 EM_DEBUG_EXCEPTION("No matching activity found");
13681 error = EMAIL_ERROR_DATA_NOT_FOUND;
13689 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13691 if (err_code != NULL)
13694 EM_DEBUG_FUNC_END("ret [%d]", ret);
13698 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
13700 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13702 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13703 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13704 if (err_code != NULL)
13705 *err_code = EMAIL_ERROR_INVALID_PARAM;
13710 int error = EMAIL_ERROR_NONE;
13711 char sql_query_string[QUERY_SIZE] = {0, };
13713 DB_STMT hStmt = NULL;
13715 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13717 EMSTORAGE_START_READ_TRANSACTION(transaction);
13718 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13720 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);
13722 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13725 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13726 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13727 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13728 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13731 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13732 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13733 ("sqlite3_step fail:%d", rc));
13735 _get_stmt_field_data_int(hStmt, activity_count, 0);
13737 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13743 if (hStmt != NULL) {
13744 rc = sqlite3_finalize(hStmt);
13746 if (rc != SQLITE_OK) {
13747 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13748 error = EMAIL_ERROR_DB_FAILURE;
13752 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13754 if (err_code != NULL)
13757 EM_DEBUG_FUNC_END("ret [%d]", ret);
13761 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13763 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13765 if (NULL == activity_count || NULL == err_code) {
13766 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13767 if (err_code != NULL)
13768 *err_code = EMAIL_ERROR_INVALID_PARAM;
13773 int error = EMAIL_ERROR_NONE;
13774 DB_STMT hStmt = NULL;
13775 char sql_query_string[QUERY_SIZE] = {0, };
13777 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13779 EMSTORAGE_START_READ_TRANSACTION(transaction);
13780 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13782 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13784 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13786 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13787 EM_DEBUG_LOG_DEV (" before sqlite3_prepare hStmt = %p", hStmt);
13788 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13789 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13791 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13792 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13793 ("sqlite3_step fail:%d", rc));
13795 _get_stmt_field_data_int(hStmt, activity_count, 0);
13797 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13804 if (hStmt != NULL) {
13805 rc = sqlite3_finalize(hStmt);
13807 if (rc != SQLITE_OK) {
13808 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13809 error = EMAIL_ERROR_DB_FAILURE;
13813 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13814 if (err_code != NULL)
13817 EM_DEBUG_FUNC_END("ret [%d]", ret);
13821 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13823 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13824 if (account_id < FIRST_ACCOUNT_ID) {
13825 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13826 if (err_code != NULL)
13827 *err_code = EMAIL_ERROR_INVALID_PARAM;
13833 int error = EMAIL_ERROR_NONE;
13834 char sql_query_string[QUERY_SIZE] = {0, };
13836 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13838 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13839 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13840 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13842 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13843 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13844 if (error != EMAIL_ERROR_NONE) {
13845 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13849 rc = sqlite3_changes(local_db_handle);
13851 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
13852 error = EMAIL_ERROR_DATA_NOT_FOUND;
13861 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13863 if (err_code != NULL)
13866 EM_DEBUG_FUNC_END("ret [%d]", ret);
13870 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
13871 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*/
13873 INTERNAL_FUNC int emstorage_update_pbd_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
13875 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
13877 int rc = -1, ret = false;
13878 int error = EMAIL_ERROR_NONE;
13879 char sql_query_string[QUERY_SIZE] = {0, };
13881 int transaction = true;
13883 if (!old_server_uid || !new_server_uid || !mbox_name) {
13884 EM_DEBUG_EXCEPTION("Invalid parameters");
13885 error = EMAIL_ERROR_INVALID_PARAM;
13889 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13891 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13892 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13893 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13894 "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);
13896 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13897 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13898 if (error != EMAIL_ERROR_NONE) {
13899 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13903 rc = sqlite3_changes(local_db_handle);
13905 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
13911 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13913 if (err_code != NULL)
13916 EM_DEBUG_FUNC_END("ret [%d]", ret);
13921 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
13923 EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
13926 int error = EMAIL_ERROR_NONE;
13927 FILE* fp_dst = NULL;
13928 char errno_buf[ERRNO_BUF_SIZE] = {0};
13930 if (!data_string || !dst_file_name) {
13931 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
13932 error = EMAIL_ERROR_INVALID_PARAM;
13936 error = em_fopen(dst_file_name, "w", &fp_dst);
13937 if (error != EMAIL_ERROR_NONE) {
13938 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
13942 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
13943 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
13944 error = EMAIL_ERROR_UNKNOWN;
13952 if (fp_dst != NULL)
13955 if (err_code != NULL)
13958 EM_DEBUG_FUNC_END("ret [%d]", ret);
13962 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
13966 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
13967 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
13969 EM_DEBUG_FUNC_BEGIN();
13972 int error = EMAIL_ERROR_NONE;
13973 char sql_query_string[QUERY_SIZE] = {0, };
13975 int transaction = true;
13977 if (!old_server_uid || !new_server_uid || !mbox_name) {
13978 EM_DEBUG_EXCEPTION("Invalid parameters");
13979 if (err_code != NULL)
13980 *err_code = EMAIL_ERROR_INVALID_PARAM;
13984 EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
13986 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13987 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13990 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13991 "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\' , mailbox_name=\'%s\' WHERE server_uid=%s ", new_server_uid, mbox_name, old_server_uid);
13993 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13994 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13995 if (error != EMAIL_ERROR_NONE) {
13996 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14000 rc = sqlite3_changes(local_db_handle);
14003 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14004 error = EMAIL_ERROR_DATA_NOT_FOUND;
14011 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14013 if (err_code != NULL)
14016 EM_DEBUG_FUNC_END("ret [%d]", ret);
14023 * @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);
14024 * Prepare an array of mail_id and corresponding server mail id.
14026 *@author h.gahlaut@samsung.com
14027 * @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)
14028 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14029 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14030 * @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
14031 * @param[out] err_code Returns the error code.
14032 * @remarks An Example of Query to be exexuted in this API:
14033 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14034 * @return This function returns true on success or false on failure.
14036 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name, char *mail_ids, email_id_set_t** idset, int *id_set_count, int *err_code)
14038 EM_DEBUG_FUNC_BEGIN();
14039 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14041 int error = EMAIL_ERROR_NONE;
14043 email_id_set_t* p_id_set = NULL;
14045 const int buf_size = QUERY_SIZE;
14046 char sql_query_string[QUERY_SIZE] = {0, };
14047 int space_left_in_query_buffer = buf_size;
14050 char *server_mail_id = NULL;
14051 char **result = NULL;
14055 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14056 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]", mail_ids, idset, id_set_count);
14057 if (err_code != NULL)
14058 *err_code = EMAIL_ERROR_INVALID_PARAM;
14062 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14064 SNPRINTF(sql_query_string, space_left_in_query_buffer, "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid", mail_ids);
14066 EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14068 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14069 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14070 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14071 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14073 EM_DEBUG_LOG(" Count of mails [%d ]", count);
14076 EM_DEBUG_EXCEPTION("Can't find proper mail");
14077 error = EMAIL_ERROR_DATA_NOT_FOUND;
14082 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14083 EM_DEBUG_EXCEPTION(" em_malloc failed...");
14084 error = EMAIL_ERROR_OUT_OF_MEMORY;
14090 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14091 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14092 for (i = 0; i < count; i++) {
14093 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14094 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14095 if (server_mail_id) {
14096 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14097 EM_SAFE_FREE(server_mail_id);
14100 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14101 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14103 sqlite3_free_table(result);
14112 *id_set_count = count;
14113 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14116 EM_SAFE_FREE(p_id_set);
14119 if (err_code != NULL)
14122 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14124 EM_DEBUG_FUNC_END("ret [%d]", ret);
14132 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14134 EM_DEBUG_FUNC_BEGIN();
14135 int ret = true, transaction = true;
14136 int error = EMAIL_ERROR_NONE;
14137 char sql_query_string[QUERY_SIZE] = {0, };
14138 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14139 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14141 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14142 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14143 if (error != EMAIL_ERROR_NONE) {
14144 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14148 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14149 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14150 if (error != EMAIL_ERROR_NONE) {
14151 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14155 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14156 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14157 if (error != EMAIL_ERROR_NONE) {
14158 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14165 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14167 EM_DEBUG_FUNC_END("ret [%d]", ret);
14171 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14173 EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14175 int error = EMAIL_ERROR_NONE;
14176 char sql_query_string[QUERY_SIZE] = {0, };
14177 int transaction = true;
14179 if (old_filter_id < 0 || new_filter_id < 0) {
14180 EM_DEBUG_EXCEPTION("Invalid parameters");
14181 if (err_code != NULL)
14182 *err_code = EMAIL_ERROR_INVALID_PARAM;
14186 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14188 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14190 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14191 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14193 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14194 if (error != EMAIL_ERROR_NONE) {
14195 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14202 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14204 if (err_code != NULL)
14207 EM_DEBUG_FUNC_END("ret [%d]", ret);
14211 INTERNAL_FUNC int emstorage_filter_mails_by_rule(char *multi_user_name, int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
14213 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);
14215 if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14216 EM_DEBUG_EXCEPTION("Invalid Parameter");
14218 if (err_code != NULL)
14219 *err_code = EMAIL_ERROR_INVALID_PARAM;
14223 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14224 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14225 int tag_id = rule->rule_id;
14226 char **result = NULL, *where_pararaph = NULL;
14227 char sql_query_string[QUERY_SIZE] = {0, };
14228 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14230 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14232 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14233 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14234 if (where_pararaph == NULL) {
14235 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14236 error = EMAIL_ERROR_OUT_OF_MEMORY;
14240 if (account_id != ALL_ACCOUNT)
14241 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14243 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14245 if (rule->type & EMAIL_FILTER_SUBJECT) {
14246 if (rule->flag2 == RULE_TYPE_INCLUDES)
14247 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14248 else /* RULE_TYPE_EXACTLY */
14249 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14252 if (rule->type & EMAIL_FILTER_FROM) {
14253 if (rule->flag2 == RULE_TYPE_INCLUDES)
14254 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14255 #ifdef __FEATURE_COMPARE_DOMAIN__
14256 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14257 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND (full_address_from like \'@%%%q\' OR full_address_from like \'@%%%q>%%\')", rule->value2, rule->value2);
14258 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14259 else /* RULE_TYPE_EXACTLY */
14260 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\'", rule->value2);
14263 if (rule->type == EMAIL_PRIORITY_SENDER) {
14264 if (rule->flag2 == RULE_TYPE_INCLUDES)
14265 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14266 else /* RULE_TYPE_EXACTLY */
14267 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\' OR email_address_sender = \'%q\'", rule->value2, rule->value2);
14269 tag_id = PRIORITY_SENDER_TAG_ID;
14272 /* prevent 34361 */
14273 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14274 strcat(sql_query_string, where_pararaph);
14276 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14278 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14279 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14280 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14281 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14283 EM_DEBUG_LOG("Count of mails [%d]", count);
14286 mail_list = malloc(sizeof(int) * count);
14287 if (mail_list == NULL) {
14288 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14289 error = EMAIL_ERROR_OUT_OF_MEMORY;
14295 for (i = 0; i < count; i++)
14296 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14298 memset(sql_query_string, 0x00, QUERY_SIZE);
14300 switch (rule->action_type) {
14301 case EMAIL_FILTER_MOVE :
14302 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14304 case EMAIL_FILTER_BLOCK:
14306 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14311 switch (rule->action_type) {
14312 case EMAIL_FILTER_MOVE :
14313 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14315 case EMAIL_FILTER_BLOCK :
14316 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14319 EM_DEBUG_LOG("Not support");
14324 /* prevent 34361 */
14325 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14326 strcat(sql_query_string, where_pararaph);
14328 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14329 if (error != EMAIL_ERROR_NONE) {
14330 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14334 #ifdef __FEATURE_BODY_SEARCH__
14335 /* Updating mail_text_tbl */
14336 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14337 memset(sql_query_string, 0x00, QUERY_SIZE);
14338 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14339 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14340 strcat(sql_query_string, where_pararaph);
14342 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14343 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14344 if (error != EMAIL_ERROR_NONE) {
14345 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14357 if (filtered_mail_id_list)
14358 *filtered_mail_id_list = mail_list;
14360 if (count_of_mails)
14361 *count_of_mails = count;
14363 EM_SAFE_FREE(mail_list);
14365 sqlite3_free_table(result);
14369 EM_SAFE_FREE(where_pararaph);
14371 if (err_code != NULL)
14374 EM_DEBUG_FUNC_END("ret [%d]", ret);
14378 #define EMAIL_SLOT_UNIT 25
14379 INTERNAL_FUNC int emstorage_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
14381 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);
14382 int ret = false, err = EMAIL_ERROR_NONE;
14383 int where_pararaph_length = 0;
14384 char *where_pararaph = NULL;
14385 char sql_query_string[QUERY_SIZE] = {0, };
14387 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14389 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14391 if (new_slot_size > 0)
14392 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14393 else if (new_slot_size == 0)
14394 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14396 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14400 where_pararaph_length = 80;
14402 where_pararaph_length = 50;
14404 if (new_slot_size == 0)
14405 where_pararaph_length += 70;
14407 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14408 if (where_pararaph == NULL) {
14409 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14410 err = EMAIL_ERROR_OUT_OF_MEMORY;
14413 memset(where_pararaph, 0x00, where_pararaph_length);
14415 if (account_id > ALL_ACCOUNT) {
14418 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14420 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14423 if (new_slot_size == 0)
14424 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"));
14426 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14427 strcat(sql_query_string, where_pararaph);
14429 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14430 err = EMAIL_ERROR_OUT_OF_MEMORY;
14434 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14435 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14436 if (err != EMAIL_ERROR_NONE) {
14437 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14444 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14446 EM_SAFE_FREE(where_pararaph);
14448 if (err_code != NULL)
14451 EM_DEBUG_FUNC_END("ret [%d]", ret);
14455 INTERNAL_FUNC int emstorage_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14457 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);
14459 if (!meeting_req || meeting_req->mail_id <= 0) {
14461 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14463 if (err_code != NULL)
14464 *err_code = EMAIL_ERROR_INVALID_PARAM;
14471 int error = EMAIL_ERROR_NONE;
14472 DB_STMT hStmt = NULL;
14473 char sql_query_string[QUERY_SIZE] = {0, };
14475 time_t temp_unix_time = 0;
14477 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14478 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14480 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14481 "INSERT INTO mail_meeting_tbl VALUES "
14482 "( ?" /* mail_id */
14483 ", ?" /* account_id */
14484 ", ?" /* mailbox_id */
14485 ", ?" /* meeting_response */
14486 ", ?" /* start_time */
14487 ", ?" /* end_time */
14488 ", ?" /* location */
14489 ", ?" /* global_object_id */
14491 ", ?" /* standard_name */
14492 ", ?" /* standard_time_start_date */
14493 ", ?" /* standard_biad */
14494 ", ?" /* daylight_name */
14495 ", ?" /* daylight_time_start_date */
14496 ", ?" /* daylight_bias */
14499 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14500 if (rc != SQLITE_OK) {
14501 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14502 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14504 error = EMAIL_ERROR_DB_FAILURE;
14510 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14511 EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14512 EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14513 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14514 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14515 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14516 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14517 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14518 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14519 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14520 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14521 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14522 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14523 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14524 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14526 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14527 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14528 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14529 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14531 temp_unix_time = timegm(&(meeting_req->start_time));
14532 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14533 temp_unix_time = timegm(&(meeting_req->end_time));
14534 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14536 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14537 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14539 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14540 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14541 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14542 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14543 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14545 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14546 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14547 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14548 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14551 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14552 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14553 ("sqlite3_step fail:%d", rc));
14554 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14555 ("sqlite3_step fail:%d", rc));
14560 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14561 if (hStmt != NULL) {
14562 rc = sqlite3_finalize(hStmt);
14563 if (rc != SQLITE_OK) {
14564 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14565 error = EMAIL_ERROR_DB_FAILURE;
14569 if (err_code != NULL)
14572 EM_DEBUG_FUNC_END("ret [%d]", ret);
14576 INTERNAL_FUNC int emstorage_query_meeting_request(char *multi_user_name, const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction)
14578 EM_DEBUG_FUNC_BEGIN("conditional_clause[%s] output_meeting_req[%p] output_result_count[%p] transaction[%d]", conditional_clause, output_meeting_req, output_result_count, transaction);
14585 int err = EMAIL_ERROR_NONE;
14586 char **result = NULL;
14587 char sql_query_string[QUERY_SIZE] = {0, };
14588 email_meeting_request_t* p_temp_meeting_req = NULL;
14589 sqlite3 *local_db_handle = NULL;
14590 time_t temp_unix_time;
14592 if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14593 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14594 err = EMAIL_ERROR_INVALID_PARAM;
14598 col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14599 EM_DEBUG_LOG("col_index [%d]", col_index);
14601 local_db_handle = emstorage_get_db_connection(multi_user_name);
14603 EMSTORAGE_START_READ_TRANSACTION(transaction);
14605 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14607 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14609 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14610 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14611 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14614 EM_DEBUG_EXCEPTION("No meeting_request found...");
14615 err = EMAIL_ERROR_DATA_NOT_FOUND;
14619 EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14620 if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14621 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14622 err = EMAIL_ERROR_OUT_OF_MEMORY;
14626 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14628 for (i = 0; i < count; i++) {
14629 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14630 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14631 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14632 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14633 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14634 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14635 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14636 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14637 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14638 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14639 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14640 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14641 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14642 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14643 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14644 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14645 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14646 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14647 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14653 sqlite3_free_table(result);
14655 if (err == EMAIL_ERROR_NONE) {
14656 if (p_temp_meeting_req)
14657 *output_meeting_req = p_temp_meeting_req;
14658 *output_result_count = count;
14661 EM_SAFE_FREE(p_temp_meeting_req);
14663 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14665 // sqlite3_db_release_memory(local_db_handle);
14667 EM_DEBUG_FUNC_END("err [%d]", err);
14671 INTERNAL_FUNC int emstorage_get_meeting_request(char *multi_user_name, int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
14673 EM_DEBUG_FUNC_BEGIN();
14677 int error = EMAIL_ERROR_NONE;
14678 char conditional_clause[QUERY_SIZE] = {0, };
14680 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14683 SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14684 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14686 if((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14687 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14691 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14692 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14693 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14694 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14695 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14696 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14697 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14698 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14699 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14700 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14701 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14702 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14703 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14708 if (err_code != NULL)
14711 EM_DEBUG_FUNC_END("ret [%d]", ret);
14715 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14717 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14720 int error = EMAIL_ERROR_NONE;
14722 DB_STMT hStmt = NULL;
14723 char sql_query_string[QUERY_SIZE] = {0, };
14724 time_t temp_unix_time = 0;
14726 if (!meeting_req) {
14727 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14728 if (err_code != NULL)
14729 *err_code = EMAIL_ERROR_INVALID_PARAM;
14733 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14734 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14736 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14737 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14738 "UPDATE mail_meeting_tbl "
14740 /* " account_id = ?, " // not update here, this can be changed when move or copy */
14741 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
14742 " meeting_response = ?, "
14743 " start_time = ?, "
14746 " global_object_id = ?, "
14748 " standard_name = ?, "
14749 " standard_time_start_date = ?, "
14750 " standard_bias = ?, "
14751 " daylight_name = ?, "
14752 " daylight_time_start_date = ?, "
14753 " daylight_bias = ? "
14754 "WHERE mail_id = %d",
14755 meeting_req->mail_id);
14757 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14759 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14760 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14761 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14763 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14764 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14765 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14766 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14767 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14768 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14769 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14770 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14771 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14772 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14773 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14774 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14775 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14779 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14780 temp_unix_time = timegm(&(meeting_req->start_time));
14781 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14782 temp_unix_time = timegm(&(meeting_req->end_time));
14783 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14784 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14785 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14786 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14787 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14788 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14789 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14790 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14791 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14792 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14793 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14794 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14797 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14798 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14799 ("sqlite3_step fail:%d", rc));
14804 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14806 if (hStmt != NULL) {
14807 rc = sqlite3_finalize(hStmt);
14808 if (rc != SQLITE_OK) {
14809 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14810 error = EMAIL_ERROR_DB_FAILURE;
14814 if (err_code != NULL)
14817 EM_DEBUG_FUNC_END("ret [%d]", ret);
14821 INTERNAL_FUNC int emstorage_delete_meeting_request(char *multi_user_name, int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
14823 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);
14825 if (account_id < ALL_ACCOUNT || mail_id < 0) {
14826 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14828 if (err_code != NULL)
14829 *err_code = EMAIL_ERROR_INVALID_PARAM;
14834 int error = EMAIL_ERROR_NONE;
14836 char sql_query_string[QUERY_SIZE] = {0, };
14838 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14839 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14841 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
14843 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
14844 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);
14848 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);
14851 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
14852 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);
14855 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14856 if (error != EMAIL_ERROR_NONE) {
14857 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14864 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14869 EM_DEBUG_FUNC_END("ret [%d]", ret);
14874 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
14876 EM_DEBUG_FUNC_BEGIN();
14878 if (!meeting_req ) return;
14880 EM_SAFE_FREE(meeting_req->location);
14881 EM_SAFE_FREE(meeting_req->global_object_id);
14883 EM_DEBUG_FUNC_END();
14886 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(char *multi_user_name, int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
14888 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);
14889 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
14890 char sql_query_string[QUERY_SIZE] = {0, };
14891 char **result = NULL;
14892 int rc = -1, ret = false;
14893 int error = EMAIL_ERROR_NONE;
14894 int counter = 0, col_index = 0;
14895 int result_mail_id_count = 0;
14896 int *result_mail_id_list = NULL;
14898 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
14899 EM_DEBUG_EXCEPTION("Invalid Parameter");
14901 *err_code = EMAIL_ERROR_INVALID_PARAM;
14905 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);
14907 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
14909 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14910 EMSTORAGE_START_READ_TRANSACTION(transaction);
14912 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
14913 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14914 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14916 if (!result_mail_id_count) {
14917 EM_DEBUG_LOG("No mail found...");
14919 error= EMAIL_ERROR_MAIL_NOT_FOUND;
14923 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
14925 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
14926 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
14927 error = EMAIL_ERROR_OUT_OF_MEMORY;
14928 sqlite3_free_table(result);
14932 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
14936 for (counter = 0; counter < result_mail_id_count; counter++)
14937 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
14942 EM_DEBUG_LOG("finish off [%d]", ret);
14945 sqlite3_free_table(result);
14948 *mail_id_list = result_mail_id_list;
14949 *mail_id_count = result_mail_id_count;
14952 EM_SAFE_FREE(result_mail_id_list);
14954 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14956 if (err_code != NULL)
14959 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
14960 EM_DEBUG_FUNC_END("ret [%d]", ret);
14964 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
14966 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
14968 int rc = -1, ret = false;
14969 int err = EMAIL_ERROR_NONE;
14970 char sql_query_string[QUERY_SIZE] = {0, };
14972 int result_count = 0;
14974 if (mail_id == 0 || thread_id == NULL) {
14975 EM_DEBUG_EXCEPTION("Invalid Parameter");
14977 *err_code = EMAIL_ERROR_INVALID_PARAM;
14981 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14983 memset(sql_query_string, 0, QUERY_SIZE);
14984 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
14986 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
14987 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
14988 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14989 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14991 if (!result_count) {
14992 EM_DEBUG_LOG("No mail found...");
14994 err= EMAIL_ERROR_MAIL_NOT_FOUND;
14995 /* sqlite3_free_table(result); */
14999 _get_table_field_data_int(result, thread_id, 1);
15001 sqlite3_free_table(result);
15007 if (err_code != NULL)
15010 EM_DEBUG_FUNC_END("ret [%d]", ret);
15014 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name, int account_id, int mailbox_id, int thread_id, int *updated_thread_id, int latest_mail_id, int thread_item_count, int noti_type, int transaction, int *err_code)
15016 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], latest_mail_id [%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, thread_id, updated_thread_id, latest_mail_id, thread_item_count, err_code);
15018 int rc = -1, ret = false;
15019 int err = EMAIL_ERROR_NONE;
15020 char sql_query_string[QUERY_SIZE] = {0, };
15021 char **result = NULL;
15022 int result_count = 0;
15024 if (thread_id == 0) {
15025 EM_DEBUG_EXCEPTION("Invalid Parameter");
15027 *err_code = EMAIL_ERROR_INVALID_PARAM;
15031 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15033 if (thread_item_count == 0 && latest_mail_id == 0) {
15034 memset(sql_query_string, 0, QUERY_SIZE);
15035 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_id FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_id = %d", account_id, thread_id, mailbox_id);
15037 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15038 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15039 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15040 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15041 EM_DEBUG_LOG("result_count[%d]", result_count);
15042 if (result_count == 0) {
15043 EM_DEBUG_LOG("No mail found...");
15046 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND;
15047 sqlite3_free_table(result);
15051 _get_table_field_data_int(result, &latest_mail_id, 2);
15052 _get_table_field_data_int(result, &thread_item_count, 3);
15054 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15055 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15057 sqlite3_free_table(result);
15060 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15062 if (thread_item_count < 0)
15064 memset(sql_query_string, 0, QUERY_SIZE);
15065 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);
15066 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15067 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15068 if (err != EMAIL_ERROR_NONE) {
15069 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15072 } else if (thread_id != latest_mail_id) {
15073 /* Initialize the thread id */
15074 memset(sql_query_string, 0, QUERY_SIZE);
15075 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0, thread_id = %d WHERE account_id = %d AND mailbox_id = %d AND thread_id = %d", latest_mail_id, account_id, mailbox_id, thread_id);
15076 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15077 if (err != EMAIL_ERROR_NONE) {
15078 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15082 /* update the thread item count */
15083 memset(sql_query_string, 0, QUERY_SIZE);
15084 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);
15085 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15086 if (err != EMAIL_ERROR_NONE) {
15087 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15093 memset(sql_query_string, 0, QUERY_SIZE);
15094 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);
15095 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15096 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15097 if (err != EMAIL_ERROR_NONE) {
15098 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15105 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15107 if (thread_id != latest_mail_id)
15109 if(err == EMAIL_ERROR_NONE)
15111 EM_DEBUG_LOG("noti_type[%d]", noti_type);
15113 if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15114 char mailbox_id_str[25] = {0,};
15115 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15116 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15117 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_THREAD_ID_CHANGED] >>>> ");
15119 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15122 }else if(thread_item_count >= 0)
15124 if(err == EMAIL_ERROR_NONE)
15126 char parameter_string[500] = {0,};
15127 SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15128 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15129 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15133 if (err_code != NULL)
15136 EM_DEBUG_FUNC_END("ret [%d]", ret);
15140 INTERNAL_FUNC int emstorage_update_thread_id_of_mail(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int thread_id, int thread_item_count, int transaction, int *err_code)
15142 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], mail_id[%d], thread_id[%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, mail_id, thread_id, thread_item_count, err_code);
15145 int err = EMAIL_ERROR_NONE;
15146 char sql_query_string[QUERY_SIZE] = {0, };
15148 if (thread_id == 0) {
15149 EM_DEBUG_EXCEPTION("Invalid Parameter");
15151 *err_code = EMAIL_ERROR_INVALID_PARAM;
15155 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15157 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15159 memset(sql_query_string, 0, QUERY_SIZE);
15160 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d, thread_id = %d WHERE account_id = %d AND mail_id = %d", thread_item_count, thread_id, account_id, mail_id);
15161 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15162 if (err != EMAIL_ERROR_NONE) {
15163 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15170 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15172 if (err_code != NULL)
15175 EM_DEBUG_FUNC_END("ret [%d]", ret);
15179 #ifdef __FEATURE_LOCAL_ACTIVITY__
15181 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15184 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15186 EM_DEBUG_FUNC_BEGIN();
15188 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15190 int rc = -1, ret = false;
15191 int error = EMAIL_ERROR_NONE;
15192 DB_STMT hStmt = NULL;
15193 char sql_query_string[8192] = { 0x00, };
15196 if (!local_activity) {
15197 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15198 if (err_code != NULL)
15199 *err_code = EMAIL_ERROR_INVALID_PARAM;
15203 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15205 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15207 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15208 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15210 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15211 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15212 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15213 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15214 EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15215 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15216 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
15218 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15221 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15222 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15223 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15225 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15227 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15228 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15229 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15230 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15231 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15232 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15233 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15236 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15238 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15239 ("sqlite3_step fail:%d", rc));
15240 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15241 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15246 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15248 if (hStmt != NULL) {
15249 rc = sqlite3_finalize(hStmt);
15250 if (rc != SQLITE_OK) {
15251 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15252 error = EMAIL_ERROR_DB_FAILURE;
15256 if (err_code != NULL)
15259 EM_DEBUG_FUNC_END("ret [%d]", ret);
15264 * emstorage_get_activity - Get the Local activity Information
15268 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)
15270 EM_DEBUG_FUNC_BEGIN();
15272 int i = 0, count = 0, rc = -1, ret = false;
15273 int error = EMAIL_ERROR_NONE;
15274 emstorage_activity_tbl_t *p_activity_tbl = NULL;
15275 char sql_query_string[1024] = {0x00, };
15276 char **result = NULL;
15279 EM_IF_NULL_RETURN_VALUE(activity_list, false);
15280 EM_IF_NULL_RETURN_VALUE(select_num, false);
15283 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15284 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15286 *err_code = EMAIL_ERROR_INVALID_PARAM;
15290 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15292 EMSTORAGE_START_READ_TRANSACTION(transaction);
15294 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15296 if (activityid == ALL_ACTIVITIES) {
15297 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15300 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);
15303 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15307 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15308 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15309 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15313 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15314 EM_DEBUG_EXCEPTION(" em_malloc failed...");
15315 error = EMAIL_ERROR_OUT_OF_MEMORY;
15320 for (i = 0; i < count; i++) {
15321 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15322 if (result[col_index])
15323 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15325 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15326 if (result[col_index])
15327 p_activity_tbl[i].account_id = atoi(result[col_index++]);
15329 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15330 if (result[col_index])
15331 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15333 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15334 if (result[col_index])
15335 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15338 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15339 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
15340 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15344 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15345 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
15346 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15350 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15351 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
15352 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15359 sqlite3_free_table(result);
15364 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15367 *activity_list = p_activity_tbl;
15368 *select_num = count;
15369 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15371 else if (p_activity_tbl != NULL) {
15372 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15376 if (err_code != NULL)
15379 EM_DEBUG_FUNC_END("ret [%d]", ret);
15384 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15387 EM_DEBUG_FUNC_BEGIN();
15390 int err = EMAIL_ERROR_NONE;
15393 char **result = NULL;
15395 if (NULL == activity_id) {
15396 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15398 *err_code = EMAIL_ERROR_INVALID_PARAM;
15402 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15404 /* increase unique id */
15406 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15408 /* 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); */
15409 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15410 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15412 if (NULL==result[1])
15415 rc = atoi(result[1])+1;
15420 sqlite3_free_table(result);
15429 EM_DEBUG_FUNC_END("ret [%d]", ret);
15434 INTERNAL_FUNC int emstorage_get_activity_id_list(char *multi_user_name, int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
15437 EM_DEBUG_FUNC_BEGIN();
15439 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);
15441 if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0) {
15442 if (err_code != NULL)
15443 *err_code = EMAIL_ERROR_INVALID_PARAM;
15448 int error = EMAIL_ERROR_NONE;
15449 int i = 0, rc = -1, count = 0;
15450 char sql_query_string[1024] = {0x00, };
15451 int *activity_ids = NULL;
15453 char **result = NULL;
15454 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15455 EMSTORAGE_START_READ_TRANSACTION(transaction);
15457 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15459 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);
15461 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15464 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15465 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15466 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15470 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15472 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15473 EM_DEBUG_EXCEPTION(" em_malloc failed...");
15474 error = EMAIL_ERROR_OUT_OF_MEMORY;
15478 for (i = 0; i < count; i++) {
15479 activity_ids[i] = atoi(result[col_index]);
15481 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15490 *activity_id_count = count;
15491 *activity_id_list = activity_ids;
15494 else if (activity_ids != NULL) /* Prevent defect - 216566 */
15495 EM_SAFE_FREE(activity_ids);
15498 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15499 if (err_code != NULL) {
15503 EM_DEBUG_FUNC_END("ret [%d]", ret);
15507 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15509 EM_DEBUG_FUNC_BEGIN();
15511 int error = EMAIL_ERROR_NONE;
15514 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15516 if (NULL == activity_id_list) {
15517 error = EMAIL_ERROR_INVALID_PARAM;
15521 EM_SAFE_FREE (activity_id_list);
15529 if (NULL != error_code) {
15530 *error_code = error;
15533 EM_DEBUG_FUNC_END("ret [%d]", ret);
15538 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15539 * or based on server mail id
15542 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15544 EM_DEBUG_FUNC_BEGIN();
15547 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15549 if (!local_activity) {
15550 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15551 if (err_code != NULL)
15552 *err_code = EMAIL_ERROR_INVALID_PARAM;
15556 int rc = -1, ret = false; /* Prevent_FIX */
15557 int err = EMAIL_ERROR_NONE;
15559 int query_where = 0;
15560 char sql_query_string[8192] = { 0x00, };
15561 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15562 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15564 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15566 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15568 EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15570 if (local_activity->account_id) {
15571 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15572 " WHERE account_id = %d ", local_activity->account_id);
15577 if (local_activity->server_mailid) {
15578 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15579 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
15585 if (local_activity->mail_id) {
15586 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15588 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15589 " %s %s mail_id = %d ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
15596 if (local_activity->activity_type > 0) {
15597 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15598 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
15601 if (local_activity->activity_id > 0) {
15602 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15603 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
15607 EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15608 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15609 if (error != EMAIL_ERROR_NONE) {
15610 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15614 rc = sqlite3_changes(local_db_handle);
15616 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15617 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15623 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15625 if (hStmt != NULL) {
15626 rc = sqlite3_finalize(hStmt);
15627 if (rc != SQLITE_OK) {
15628 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
15629 error = EMAIL_ERROR_DB_FAILURE;
15633 if (err_code != NULL)
15635 EM_DEBUG_FUNC_END("ret [%d]", ret);
15640 * emstorage_free_local_activity - Free the Local Activity data
15642 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15644 EM_DEBUG_FUNC_BEGIN();
15646 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15649 int error = EMAIL_ERROR_INVALID_PARAM;
15652 if (!local_activity_list || !*local_activity_list) {
15653 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15655 error = EMAIL_ERROR_INVALID_PARAM;
15659 emstorage_activity_tbl_t* p = *local_activity_list;
15662 for (; i < count; i++) {
15663 EM_SAFE_FREE (p[i].dest_mbox);
15664 EM_SAFE_FREE (p[i].src_mbox);
15665 EM_SAFE_FREE (p[i].server_mailid);
15669 *local_activity_list = NULL;
15676 if (err_code != NULL)
15679 EM_DEBUG_FUNC_END("ret [%d]", ret);
15683 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15686 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
15688 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15690 int ret = EMAIL_ERROR_NONE;
15691 char key_value_string[QUERY_SIZE] = { 0, };
15692 char *temp_key_value_1 = NULL;
15693 char *temp_key_value_2 = NULL;
15695 if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
15696 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15697 return EMAIL_ERROR_INVALID_PARAM;
15700 switch(input_list_filter_rule->target_attribute) {
15701 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID :
15702 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID :
15703 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID :
15704 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE :
15705 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS :
15706 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID :
15707 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS :
15708 case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE :
15709 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
15710 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
15711 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE :
15712 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD :
15713 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD :
15714 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD :
15715 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD :
15716 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD :
15717 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD :
15718 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD :
15719 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS :
15720 case EMAIL_MAIL_ATTRIBUTE_PRIORITY :
15721 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS :
15722 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS :
15723 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS :
15724 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT :
15725 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT :
15726 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID :
15727 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT :
15728 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS :
15729 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS :
15730 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE :
15731 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE :
15732 case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES :
15733 case EMAIL_MAIL_ATTRIBUTE_TAG_ID :
15734 case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE :
15735 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15738 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
15739 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
15740 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
15741 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
15742 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
15743 case EMAIL_MAIL_ATTRIBUTE_FROM :
15744 case EMAIL_MAIL_ATTRIBUTE_TO :
15745 case EMAIL_MAIL_ATTRIBUTE_CC :
15746 case EMAIL_MAIL_ATTRIBUTE_BCC :
15747 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
15748 if(input_list_filter_rule->key_value.string_type_value == NULL) {
15749 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15750 ret = EMAIL_ERROR_INVALID_PARAM;
15754 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15756 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15757 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15759 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15760 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15762 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15765 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME :
15766 case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME :
15767 case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME :
15768 case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME :
15769 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15773 ret = EMAIL_ERROR_INVALID_PARAM;
15774 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15778 if(ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
15779 *output_key_value_string = strdup(key_value_string);
15784 EM_SAFE_FREE(temp_key_value_1);
15785 EM_SAFE_FREE(temp_key_value_2);
15787 EM_DEBUG_FUNC_END("ret [%d]", ret);
15791 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15792 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15794 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15796 if (index <= 0 || !cert_password_file_name) {
15797 EM_DEBUG_EXCEPTION("Invalid parameter");
15798 return EMAIL_ERROR_INVALID_PARAM;
15801 sprintf(cert_password_file_name, ".email_cert_%d", index);
15803 EM_DEBUG_FUNC_END();
15804 return EMAIL_ERROR_NONE;
15808 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15810 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15812 int ret = EMAIL_ERROR_NONE;
15814 int length_field_name = 0;
15815 int length_value = 0;
15816 char result_rule_string[QUERY_SIZE] = { 0 , };
15817 char *mod_field_name_string = NULL;
15818 char *mod_value_string = NULL;
15819 char *temp_field_name_string = NULL;
15820 char *temp_key_value_string = NULL;
15822 if(input_list_filter_rule == NULL || output_string == NULL) {
15823 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15824 return EMAIL_ERROR_INVALID_PARAM;
15827 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15829 if(temp_field_name_string == NULL) {
15830 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15831 return EMAIL_ERROR_INVALID_PARAM;
15834 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) {
15835 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15836 return EMAIL_ERROR_INVALID_PARAM;
15839 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
15840 length_value = EM_SAFE_STRLEN(temp_key_value_string);
15842 switch(input_list_filter_rule->target_attribute) {
15843 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
15844 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
15845 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
15846 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
15847 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
15848 case EMAIL_MAIL_ATTRIBUTE_FROM :
15849 case EMAIL_MAIL_ATTRIBUTE_TO :
15850 case EMAIL_MAIL_ATTRIBUTE_CC :
15851 case EMAIL_MAIL_ATTRIBUTE_BCC :
15852 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
15860 if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
15861 length_field_name += strlen("UPPER() ");
15862 length_value += strlen("UPPER() ");
15863 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
15864 if (mod_field_name_string == NULL) {
15865 EM_DEBUG_EXCEPTION("em_malloc failed");
15866 EM_SAFE_FREE(temp_field_name_string);
15867 ret = EMAIL_ERROR_OUT_OF_MEMORY;
15871 mod_value_string = em_malloc(sizeof(char) * length_value);
15872 if (mod_value_string == NULL) {
15873 EM_DEBUG_EXCEPTION("em_malloc failed");
15874 EM_SAFE_FREE(temp_field_name_string);
15875 ret = EMAIL_ERROR_OUT_OF_MEMORY;
15879 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
15880 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
15881 EM_SAFE_FREE(temp_key_value_string);
15884 mod_field_name_string = strdup(temp_field_name_string);
15885 mod_value_string = temp_key_value_string;
15888 switch (input_list_filter_rule->rule_type) {
15889 case EMAIL_LIST_FILTER_RULE_EQUAL :
15890 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
15892 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
15893 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
15895 case EMAIL_LIST_FILTER_RULE_LESS_THAN :
15896 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
15898 case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
15899 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
15901 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
15902 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
15904 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
15905 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
15907 case EMAIL_LIST_FILTER_RULE_INCLUDE :
15908 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
15910 case EMAIL_LIST_FILTER_RULE_IN :
15911 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
15913 case EMAIL_LIST_FILTER_RULE_NOT_IN :
15914 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
15917 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
15918 ret = EMAIL_ERROR_INVALID_PARAM;
15922 *output_string = strdup(result_rule_string);
15925 EM_SAFE_FREE(mod_field_name_string);
15926 EM_SAFE_FREE(mod_value_string);
15928 EM_DEBUG_FUNC_END("ret [%d]", ret);
15932 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
15934 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15936 char *field_name_string = NULL;
15937 char key_value_string[QUERY_SIZE] = {0,};
15938 char result_rule_string[QUERY_SIZE] = {0,};
15941 int query_size = 0;
15944 int error = EMAIL_ERROR_NONE;
15945 char **result = NULL;
15946 char sql_query_string[QUERY_SIZE] = {0,};
15947 char *sql_query_string2 = NULL;
15948 sqlite3 *local_db_handle = NULL;
15949 int *mail_ids = NULL;
15951 if(input_list_filter_rule == NULL || output_string == NULL) {
15952 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15953 return EMAIL_ERROR_INVALID_PARAM;
15956 field_name_string = EM_SAFE_STRDUP("attachment_name");
15957 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
15959 switch (input_list_filter_rule->rule_type) {
15961 case EMAIL_LIST_FILTER_RULE_INCLUDE:
15962 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
15965 case EMAIL_LIST_FILTER_RULE_MATCH:
15966 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
15970 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
15971 error = EMAIL_ERROR_INVALID_PARAM;
15975 local_db_handle = emstorage_get_db_connection(multi_user_name);
15977 EMSTORAGE_START_READ_TRANSACTION(true);
15978 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
15980 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
15981 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF;},
15982 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15987 EM_DEBUG_LOG("No mail found...");
15988 error= EMAIL_ERROR_MAIL_NOT_FOUND;
15989 *output_string = strdup("mail_id IN ( ) ");
15993 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >>");
15995 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
15996 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
15997 error = EMAIL_ERROR_OUT_OF_MEMORY;
16001 for (i = 0; i < count; i++) {
16002 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16003 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16006 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN END [count : %d] >>", count);
16007 sqlite3_free_table(result);
16008 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16010 // sqlite3_db_release_memory(local_db_handle);
16013 query_size = (10 * count) + strlen("mail_id IN ( ) ");
16015 sql_query_string2 = em_malloc(query_size);
16016 if (sql_query_string2 == NULL) {
16017 EM_DEBUG_EXCEPTION("em_malloc failed");
16018 error = EMAIL_ERROR_OUT_OF_MEMORY;
16022 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN ( ");
16023 for (i = 0; i < count-1; i++) {
16024 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16026 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d ) ", mail_ids[count-1]);
16028 *output_string = strdup(sql_query_string2);
16031 EM_SAFE_FREE(mail_ids); /* prevent */
16032 EM_SAFE_FREE(sql_query_string2);
16033 EM_SAFE_FREE(field_name_string);
16034 EM_DEBUG_FUNC_END("error [%d]", error);
16038 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16040 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16041 char *field_name_string = NULL;
16042 char key_value_string[QUERY_SIZE] = {0,};
16043 char result_rule_string[QUERY_SIZE] = {0,};
16047 int query_size = 0;
16048 int error = EMAIL_ERROR_NONE;
16049 char **result = NULL;
16050 char sql_query_string[QUERY_SIZE] = {0,};
16051 char *sql_query_string2 = NULL;
16052 sqlite3 *local_db_handle = NULL;
16053 int *mail_ids = NULL;
16055 if(input_list_filter_rule == NULL || output_string == NULL) {
16056 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16057 return EMAIL_ERROR_INVALID_PARAM;
16060 field_name_string = EM_SAFE_STRDUP("body_text");
16061 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16063 switch (input_list_filter_rule->rule_type) {
16065 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16066 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16069 case EMAIL_LIST_FILTER_RULE_MATCH:
16070 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16074 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16075 error = EMAIL_ERROR_INVALID_PARAM;
16079 local_db_handle = emstorage_get_db_connection(multi_user_name);
16081 EMSTORAGE_START_READ_TRANSACTION(true);
16082 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16084 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16085 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF;},
16086 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16091 EM_DEBUG_LOG("No mail found...");
16092 error= EMAIL_ERROR_MAIL_NOT_FOUND;
16093 *output_string = strdup("mail_id IN ( ) ");
16097 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >>");
16100 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16101 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16102 error = EMAIL_ERROR_OUT_OF_MEMORY;
16106 for (i = 0; i < count; i++) {
16107 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16108 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16111 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN END [count : %d] >>", count);
16112 sqlite3_free_table(result);
16113 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16115 // sqlite3_db_release_memory(local_db_handle);
16118 query_size = (10 * count) + strlen("mail_id IN ( ) ");
16119 sql_query_string2 = em_malloc(query_size);
16120 if (sql_query_string2 == NULL) {
16121 EM_DEBUG_EXCEPTION("em_malloc failed");
16122 error = EMAIL_ERROR_OUT_OF_MEMORY;
16126 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN ( ");
16127 for (i = 0; i < count-1; i++) {
16128 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16130 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d ) ", mail_ids[count-1]);
16132 *output_string = strdup(sql_query_string2);
16135 EM_SAFE_FREE(mail_ids); /* prevent */
16136 EM_SAFE_FREE(sql_query_string2);
16137 EM_SAFE_FREE(field_name_string);
16138 EM_DEBUG_FUNC_END("error [%d]", error);
16142 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
16143 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16145 char result_rule_string[QUERY_SIZE] = { 0 , };
16146 int ret = EMAIL_ERROR_NONE;
16148 emstorage_account_tbl_t *account_tbl_array = NULL;
16151 char *result_str = NULL;
16152 char *tmp_str1 = NULL;
16153 char *tmp_str2 = NULL;
16154 char query_per_account[QUERY_SIZE] = { 0 , };
16156 if(input_sorting_rule->force_boolean_check) {
16157 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16160 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16162 switch (input_sorting_rule->sort_order) {
16163 case EMAIL_SORT_ORDER_ASCEND :
16164 EM_SAFE_STRCAT(result_rule_string, " ASC ");
16167 case EMAIL_SORT_ORDER_DESCEND :
16168 EM_SAFE_STRCAT(result_rule_string, " DESC ");
16171 case EMAIL_SORT_ORDER_NOCASE_ASCEND :
16172 EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
16175 case EMAIL_SORT_ORDER_NOCASE_DESCEND :
16176 EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
16179 case EMAIL_SORT_ORDER_TO_CCBCC :
16180 memset(result_rule_string, 0, QUERY_SIZE);
16181 if (input_sorting_rule->key_value.string_type_value)
16182 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16183 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16184 input_sorting_rule->key_value.string_type_value);
16187 case EMAIL_SORT_ORDER_TO_CC_BCC :
16188 memset(result_rule_string, 0, QUERY_SIZE);
16189 if (input_sorting_rule->key_value.string_type_value)
16190 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16191 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16192 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16195 case EMAIL_SORT_ORDER_TO_CCBCC_ALL :
16196 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16197 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16202 EM_DEBUG_LOG("No account exist");
16203 ret = EMAIL_ERROR_INVALID_PARAM;
16207 for (i = 0; i < count; i++) {
16208 if (i > 0 && result_str) {
16209 tmp_str2 = result_str;
16210 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16211 EM_SAFE_FREE(tmp_str2);
16214 memset(query_per_account, 0, QUERY_SIZE);
16215 snprintf(query_per_account, QUERY_SIZE,
16216 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16217 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16219 tmp_str1 = result_str;
16221 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16223 result_str = g_strdup(query_per_account);
16224 EM_SAFE_FREE(tmp_str1);
16227 snprintf(result_rule_string, QUERY_SIZE,
16228 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16230 EM_SAFE_FREE(result_str);
16231 if (account_tbl_array)
16232 emstorage_free_account(&account_tbl_array, count, NULL);
16235 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND :
16236 memset(result_rule_string, 0, QUERY_SIZE);
16237 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16238 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16239 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16240 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16243 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND :
16244 memset(result_rule_string, 0, QUERY_SIZE);
16245 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16246 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16247 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16248 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16252 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16253 ret = EMAIL_ERROR_INVALID_PARAM;
16257 *output_string = strdup(result_rule_string);
16260 EM_DEBUG_FUNC_END("ret [%d]", ret);
16264 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(char *multi_user_name, 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)
16266 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);
16267 int ret = EMAIL_ERROR_NONE;
16269 int string_offset = 0;
16270 int query_size = 0;
16271 int new_query_size = 0;
16272 char *conditional_clause_string = NULL;
16273 char *result_string_for_a_item = NULL;
16275 if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16276 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16277 return EMAIL_ERROR_INVALID_PARAM;
16280 conditional_clause_string = em_malloc(QUERY_SIZE);
16281 if (conditional_clause_string == NULL) {
16282 EM_DEBUG_EXCEPTION("Memory is full");
16283 return EMAIL_ERROR_OUT_OF_MEMORY;
16286 if(input_filter_count > 0) {
16287 query_size = QUERY_SIZE;
16288 strcpy(conditional_clause_string, " WHERE ");
16290 for ( i = 0; i < input_filter_count; i++) {
16291 switch (input_filter_list[i].list_filter_item_type) {
16292 case EMAIL_LIST_FILTER_ITEM_RULE :
16293 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16294 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16297 case EMAIL_LIST_FILTER_ITEM_RULE_FTS :
16298 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16299 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16302 case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH :
16303 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16304 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16307 case EMAIL_LIST_FILTER_ITEM_OPERATOR :
16308 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16309 switch(input_filter_list[i].list_filter_item.operator_type) {
16310 case EMAIL_LIST_FILTER_OPERATOR_AND :
16311 result_string_for_a_item = strdup("AND ");
16313 case EMAIL_LIST_FILTER_OPERATOR_OR :
16314 result_string_for_a_item = strdup("OR ");
16316 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
16317 result_string_for_a_item = strdup(" (");
16319 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
16320 result_string_for_a_item = strdup(") ");
16326 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16327 ret = EMAIL_ERROR_INVALID_PARAM;
16331 if(result_string_for_a_item == NULL) {
16332 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16333 ret = EMAIL_ERROR_INVALID_PARAM;
16337 if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16338 EM_DEBUG_LOG("QUERY is too long");
16339 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16340 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16341 if (conditional_clause_string == NULL) {
16342 EM_DEBUG_EXCEPTION("realloc failed");
16343 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16347 query_size = new_query_size;
16350 strcat(conditional_clause_string, result_string_for_a_item);
16351 EM_SAFE_FREE(result_string_for_a_item);
16355 if(input_sorting_rule_count > 0) {
16356 strcat(conditional_clause_string, "ORDER BY ");
16358 for ( i = 0; i < input_sorting_rule_count; i++) {
16359 if( (ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16360 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16364 strcat(conditional_clause_string, ", ");
16365 strcat(conditional_clause_string, result_string_for_a_item);
16366 EM_SAFE_FREE(result_string_for_a_item);
16370 if (input_start_index != -1 && input_limit_count != -1) {
16371 string_offset = strlen(conditional_clause_string);
16372 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16375 *output_conditional_clause = strdup(conditional_clause_string);
16378 EM_SAFE_FREE(result_string_for_a_item);
16379 EM_SAFE_FREE(conditional_clause_string);
16381 EM_DEBUG_FUNC_END("ret [%d]", ret);
16385 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16387 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16388 int err = EMAIL_ERROR_NONE;
16390 email_list_filter_t *temp_filter_list = NULL;
16392 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16394 for ( i = 0; i < input_filter_count; i++) {
16395 temp_filter_list = (*input_filter_list) + i;
16396 if (!temp_filter_list) {
16400 if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16401 switch(temp_filter_list->list_filter_item.rule.target_attribute) {
16402 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
16403 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
16404 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
16405 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
16406 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
16407 case EMAIL_MAIL_ATTRIBUTE_FROM :
16408 case EMAIL_MAIL_ATTRIBUTE_TO :
16409 case EMAIL_MAIL_ATTRIBUTE_CC :
16410 case EMAIL_MAIL_ATTRIBUTE_BCC :
16411 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
16412 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
16413 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
16414 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16420 else if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_FTS && temp_filter_list->list_filter_item.rule_fts.target_attribute == EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT) {
16421 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16423 else if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_ATTACH && temp_filter_list->list_filter_item.rule_attach.target_attribute == EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME) {
16424 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16428 EM_SAFE_FREE (*input_filter_list);
16430 EM_DEBUG_FUNC_END("err [%d]", err);
16434 INTERNAL_FUNC int emstorage_add_certificate(char *multi_user_name, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
16436 EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
16438 if (!certificate) {
16439 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
16441 *err_code = EMAIL_ERROR_INVALID_PARAM;
16445 int rc = -1, ret = false;
16446 int error = EMAIL_ERROR_NONE;
16447 DB_STMT hStmt = NULL;
16448 char sql_query_string[QUERY_SIZE] = {0, };
16449 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16450 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16453 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16455 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16457 char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
16458 char **result = NULL;
16460 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
16461 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16462 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
16463 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16465 if (NULL==result[1]) rc = 1;
16466 else rc = atoi(result[1])+1;
16467 sqlite3_free_table(result);
16469 certificate->certificate_id = rc;
16470 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16471 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16472 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16476 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16477 "INSERT INTO mail_certificate_tbl VALUES "
16479 " ? " /* Index of certificate */
16480 " , ? " /* Select the account */
16481 " , ? " /* Year of issue */
16482 " , ? " /* Month of issue */
16483 " , ? " /* Day of issue */
16484 " , ? " /* Year of expiration */
16485 " , ? " /* Month of expiration */
16486 " , ? " /* Day of expiration */
16487 " , ? " /* Organization of issue */
16488 " , ? " /* Email address */
16489 " , ? " /* Subject of certificate */
16490 " , ? " /* Name of saved certificate */
16494 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
16495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16496 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16497 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16499 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
16500 _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
16501 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
16502 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
16503 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
16504 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
16505 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
16506 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
16507 _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);
16508 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
16509 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
16510 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
16511 /* rc = sqlite3_step(hStmt); */
16512 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16514 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
16515 ("sqlite3_step fail:%d", rc));
16516 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16517 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16522 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
16523 if (hStmt != NULL) {
16524 rc = sqlite3_finalize(hStmt);
16525 if (rc != SQLITE_OK) {
16526 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16527 error = EMAIL_ERROR_DB_FAILURE;
16531 if (error == EMAIL_ERROR_NONE) {
16532 if (!emcore_notify_storage_event (NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
16533 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
16536 if (err_code != NULL)
16539 EM_DEBUG_FUNC_END("ret [%d]", ret);
16543 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
16545 EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
16548 int error = EMAIL_ERROR_NONE;
16551 if (!certificate_list || !*certificate_list) {
16552 EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
16553 error = EMAIL_ERROR_INVALID_PARAM;
16557 emstorage_certificate_tbl_t *p = *certificate_list;
16560 for (; i < count; i++) {
16561 EM_SAFE_FREE(p[i].issue_organization_name);
16562 EM_SAFE_FREE(p[i].email_address);
16563 EM_SAFE_FREE(p[i].subject_str);
16564 EM_SAFE_FREE(p[i].filepath);
16565 EM_SAFE_FREE(p[i].password);
16569 *certificate_list = NULL;
16575 if (err_code != NULL)
16578 EM_DEBUG_FUNC_END("ret [%d]", ret);
16582 INTERNAL_FUNC int emstorage_get_certificate_list(char *multi_user_name, int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
16584 EM_DEBUG_FUNC_BEGIN();
16586 int i = 0, count = 0, rc = -1, ret = false;
16587 int error = EMAIL_ERROR_NONE;
16588 emstorage_certificate_tbl_t *p_data_tbl = NULL;
16590 DB_STMT hStmt = NULL;
16592 if (!select_num || !certificate_list) {
16593 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
16594 if (err_code != NULL)
16595 *err_code = EMAIL_ERROR_INVALID_PARAM;
16599 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16600 EMSTORAGE_START_READ_TRANSACTION(transaction);
16602 char sql_query_string[QUERY_SIZE] = {0, };
16603 char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
16606 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
16607 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16608 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
16609 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16611 count = atoi(result[1]);
16612 sqlite3_free_table(result);
16615 EM_DEBUG_EXCEPTION("no account found...");
16616 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16620 EM_DEBUG_LOG("count = %d", rc);
16621 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
16623 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
16624 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16626 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16627 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16628 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16630 /* rc = sqlite3_step(hStmt); */
16631 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16632 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16633 ("sqlite3_step fail:%d", rc));
16634 if (rc == SQLITE_DONE) {
16635 EM_DEBUG_EXCEPTION("no account found...");
16637 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16643 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count))) {
16644 EM_DEBUG_EXCEPTION("malloc failed...");
16645 error = EMAIL_ERROR_OUT_OF_MEMORY;
16648 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
16649 for (i = 0; i < count; i++) {
16650 /* get recordset */
16651 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
16652 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16653 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16654 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16655 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16656 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16657 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16658 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
16659 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
16660 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
16661 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
16662 if (with_password == true) {
16663 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16664 /* get password from the secure storage */
16665 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16667 EM_SAFE_FREE(p_data_tbl[i].password);
16669 /* get password file name */
16670 if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16671 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16675 /* read password from secure storage */
16676 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
16677 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
16680 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl[i].password);
16684 /* rc = sqlite3_step(hStmt); */
16685 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16686 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
16687 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16688 ("sqlite3_step fail:%d", rc));
16695 *certificate_list = p_data_tbl;
16696 *select_num = count;
16697 EM_DEBUG_LOG("COUNT : %d", count);
16699 else if (p_data_tbl != NULL)
16700 emstorage_free_certificate(&p_data_tbl, count, NULL);
16701 if (hStmt != NULL) {
16702 rc = sqlite3_finalize(hStmt);
16704 if (rc != SQLITE_OK) {
16705 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16706 error = EMAIL_ERROR_DB_FAILURE;
16710 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
16712 if (err_code != NULL)
16715 EM_DEBUG_FUNC_END("ret [%d]", ret);
16719 INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *multi_user_name, char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
16721 EM_DEBUG_FUNC_BEGIN_SEC("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
16723 if (!certificate) {
16724 EM_DEBUG_EXCEPTION_SEC("email_address[%s], certificate[%p]", email_address, certificate);
16725 if (err_code != NULL)
16726 *err_code = EMAIL_ERROR_INVALID_PARAM;
16731 int error = EMAIL_ERROR_NONE;
16732 emstorage_certificate_tbl_t *p_data_tbl = NULL;
16733 DB_STMT hStmt = NULL;
16734 char sql_query_string[QUERY_SIZE] = {0, };
16737 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16738 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16741 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16742 EMSTORAGE_START_READ_TRANSACTION(transaction);
16744 /* Make query string */
16745 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
16746 sql_len = EM_SAFE_STRLEN(sql_query_string);
16748 /* dummy value, FROM WHERE clause */
16749 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
16752 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
16754 /* execute a sql and count rows */
16755 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16756 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16757 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16759 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16760 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16761 ("sqlite3_step fail:%d", rc));
16763 if (rc == SQLITE_DONE) {
16764 EM_DEBUG_EXCEPTION("no matched certificate found...");
16765 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16769 /* Assign query result to structure */
16770 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
16771 EM_DEBUG_EXCEPTION("malloc failed...");
16772 error = EMAIL_ERROR_OUT_OF_MEMORY;
16776 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
16777 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
16778 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16779 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16780 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16781 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16782 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16783 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16784 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
16785 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
16786 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
16787 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
16789 if (with_password) {
16790 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16791 /* get password file name */
16792 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16793 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16797 /* read password from secure storage */
16798 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
16799 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
16802 EM_DEBUG_LOG_SEC("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
16809 *certificate = p_data_tbl;
16811 if (hStmt != NULL) {
16812 rc = sqlite3_finalize(hStmt);
16813 if (rc != SQLITE_OK) {
16814 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16815 error = EMAIL_ERROR_DB_FAILURE;
16819 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
16821 if (err_code != NULL)
16824 EM_DEBUG_FUNC_END("ret [%d]", ret);
16828 INTERNAL_FUNC int emstorage_get_certificate_by_index(char *multi_user_name, int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
16830 EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
16832 if (!certificate) {
16833 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
16834 if (err_code != NULL)
16835 *err_code = EMAIL_ERROR_INVALID_PARAM;
16840 int error = EMAIL_ERROR_NONE;
16841 emstorage_certificate_tbl_t *p_data_tbl = NULL;
16842 DB_STMT hStmt = NULL;
16843 char sql_query_string[QUERY_SIZE] = {0, };
16846 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16847 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16850 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16851 EMSTORAGE_START_READ_TRANSACTION(transaction);
16853 /* Make query string */
16854 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
16855 sql_len = EM_SAFE_STRLEN(sql_query_string);
16857 /* dummy value, FROM WHERE clause */
16858 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
16861 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
16863 /* execute a sql and count rows */
16864 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16865 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16866 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16868 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16869 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16870 ("sqlite3_step fail:%d", rc));
16872 if (rc == SQLITE_DONE) {
16873 EM_DEBUG_EXCEPTION("no matched certificate found...");
16874 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16878 /* Assign query result to structure */
16879 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
16880 EM_DEBUG_EXCEPTION("malloc failed...");
16881 error = EMAIL_ERROR_OUT_OF_MEMORY;
16885 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
16887 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
16888 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16889 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16890 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16891 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16892 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16893 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16894 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
16895 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
16896 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
16897 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
16899 if (with_password) {
16900 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16901 /* get password file name */
16902 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16903 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16907 /* read password from secure storage */
16908 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
16909 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
16912 EM_DEBUG_LOG_SEC("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
16919 *certificate = p_data_tbl;
16921 if (hStmt != NULL) {
16922 rc = sqlite3_finalize(hStmt);
16923 if (rc != SQLITE_OK) {
16924 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16925 error = EMAIL_ERROR_DB_FAILURE;
16929 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
16931 if (err_code != NULL)
16934 EM_DEBUG_FUNC_END("ret [%d]", ret);
16938 INTERNAL_FUNC int emstorage_delete_certificate(char *multi_user_name, int certificate_id, int transaction, int *err_code)
16940 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
16942 if (certificate_id < 1) {
16943 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
16945 *err_code = EMAIL_ERROR_INVALID_PARAM;
16949 int rc = -1, ret = false;
16950 int error = EMAIL_ERROR_NONE;
16951 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16952 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16954 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
16955 char sql_query_string[QUERY_SIZE] = {0, };
16956 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16957 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16958 /* get password file name */
16959 if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16960 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16964 /* delete from db */
16965 memset(sql_query_string, 0x00, sizeof(sql_query_string));
16966 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
16967 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16968 if (error != EMAIL_ERROR_NONE) {
16969 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
16973 /* validate account existence */
16974 rc = sqlite3_changes(local_db_handle);
16976 EM_DEBUG_EXCEPTION(" no matched certificate found...");
16977 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16984 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
16986 if (err_code != NULL)
16989 EM_DEBUG_FUNC_END("ret [%d]", ret);
16993 INTERNAL_FUNC int emstorage_update_certificate(char *multi_user_name, int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
16995 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
16997 if (certificate_id < 1) {
16998 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
17000 *err_code = EMAIL_ERROR_INVALID_PARAM;
17004 int error = EMAIL_ERROR_NONE;
17005 int rc, ret = false;
17007 DB_STMT hStmt = NULL;
17008 char sql_query_string[QUERY_SIZE] = {0, };
17009 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
17010 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
17013 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17014 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17016 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17017 "UPDATE mail_certificate_tbl SET"
17019 ", issue_month = ?" /* Index of certificate */
17020 ", issue_day = ?" /* Select the account */
17021 ", expiration_year = ?" /* Year of issue */
17022 ", expiration_month = ?" /* Month of issue */
17023 ", expiration_day = ?" /* Day of issue */
17024 ", issue_organization_name = ?" /* Year of expiration */
17025 ", email_address = ?" /* Month of expiration */
17026 ", subject_str = ?" /* Day of expiration */
17027 ", filepath = ?" /* Organization of issue */
17029 " WHERE certificate_id = ?");
17031 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17032 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
17033 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17034 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17036 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
17037 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
17038 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
17039 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
17040 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
17041 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
17042 _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);
17043 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
17044 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
17045 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
17047 /* rc = sqlite3_step(hStmt); */
17048 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17049 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
17050 ("sqlite3_step fail:%d", rc));
17051 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17052 ("sqlite3_step fail:%d", rc));
17054 /* validate account existence */
17055 rc = sqlite3_changes(local_db_handle);
17057 EM_DEBUG_EXCEPTION(" no matched account found...");
17059 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
17066 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17068 if (hStmt != NULL) {
17069 rc = sqlite3_finalize(hStmt);
17070 if (rc != SQLITE_OK) {
17071 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17072 error = EMAIL_ERROR_DB_FAILURE;
17076 if (error == EMAIL_ERROR_NONE) {
17077 if (!emcore_notify_storage_event (NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
17078 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
17081 if (err_code != NULL)
17084 EM_DEBUG_FUNC_END("ret [%d]", ret);
17088 /* Tasks --------------------------------------------------------------------------*/
17089 INTERNAL_FUNC int emstorage_add_task(char *multi_user_name, 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)
17091 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);
17095 int err = EMAIL_ERROR_NONE;
17097 DB_STMT hStmt = NULL;
17098 char sql_query_string[QUERY_SIZE] = {0, };
17099 sqlite3 *local_db_handle = NULL;
17100 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
17101 char **result = NULL;
17103 if (input_task_parameter == NULL || output_task_id == NULL) {
17104 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17105 return EMAIL_ERROR_INVALID_PARAM;
17108 local_db_handle = emstorage_get_db_connection(multi_user_name);
17109 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
17111 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17112 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17113 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17115 if (NULL == result[1])
17118 task_id = atoi(result[1])+1;
17120 *output_task_id = task_id;
17122 sqlite3_free_table(result);
17125 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17126 "INSERT INTO mail_task_tbl VALUES "
17128 " ? " /* task_id */
17129 " , ? " /* task_type */
17130 " , ? " /* task_status */
17131 " , ? " /* task_priority */
17132 " , ? " /* task_parameter_length */
17133 " , ? " /* task_parameter */
17134 " , ? " /* date_time */
17137 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17138 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17139 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17141 EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
17144 _bind_stmt_field_data_int(hStmt, i++, task_id);
17145 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
17146 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
17147 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
17148 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
17149 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
17150 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
17152 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17154 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
17155 ("sqlite3_step fail:%d", rc));
17156 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17157 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17159 ret = (err == EMAIL_ERROR_NONE);
17162 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
17164 if (hStmt != NULL) {
17165 rc = sqlite3_finalize(hStmt);
17166 if (rc != SQLITE_OK) {
17167 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17168 err = EMAIL_ERROR_DB_FAILURE;
17172 EM_DEBUG_FUNC_END("err [%d]", err);
17176 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
17178 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
17180 int err = EMAIL_ERROR_NONE;
17181 char sql_query_string[QUERY_SIZE] = {0, };
17183 sqlite3 *local_db_handle = NULL;
17186 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17187 return EMAIL_ERROR_INVALID_PARAM;
17190 local_db_handle = emstorage_get_db_connection(multi_user_name);
17192 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
17194 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
17195 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17196 if (err != EMAIL_ERROR_NONE) {
17197 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
17204 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
17206 EM_DEBUG_FUNC_END("err [%d]", err);
17210 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
17212 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
17214 int err = EMAIL_ERROR_NONE;
17215 char sql_query_string[QUERY_SIZE] = {0, };
17217 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17218 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
17220 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17221 "UPDATE mail_task_tbl SET"
17222 " task_status = %d"
17223 " WHERE task_id = %d"
17226 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17227 if (err != EMAIL_ERROR_NONE) {
17228 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
17235 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
17237 EM_DEBUG_FUNC_END("err [%d]", err);
17241 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
17242 const char *input_conditional_clause,
17243 const char *input_ordering_clause,
17244 email_task_t **output_task_list,
17245 int *output_task_count)
17247 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
17248 "output_task_list[%p], output_task_count[%d]",
17249 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
17250 int i = 0, count = 0, rc = -1;
17252 int field_index = 0;
17253 int err = EMAIL_ERROR_NONE;
17254 email_task_t *task_item_from_tbl = NULL;
17255 char sql_query_string[QUERY_SIZE] = {0, };
17256 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
17258 sqlite3 *local_db_handle = NULL;
17259 DB_STMT hStmt = NULL;
17261 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
17262 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
17264 local_db_handle = emstorage_get_db_connection(multi_user_name);
17266 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
17267 "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
17268 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
17270 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17271 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17272 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17274 count = atoi(result[1]);
17275 sqlite3_free_table(result);
17277 EM_DEBUG_LOG("count = %d", rc);
17280 EM_DEBUG_EXCEPTION("no task found...");
17281 err = EMAIL_ERROR_TASK_NOT_FOUND;
17285 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
17286 "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
17287 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
17289 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17291 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
17292 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17293 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17295 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17296 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17297 ("sqlite3_step fail:%d", rc));
17299 if (rc == SQLITE_DONE) {
17300 EM_DEBUG_EXCEPTION("no task found...");
17301 err = EMAIL_ERROR_TASK_NOT_FOUND;
17306 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
17307 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
17308 err = EMAIL_ERROR_OUT_OF_MEMORY;
17312 for (i = 0; i < count; i++) {
17313 /* get recordset */
17316 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
17317 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
17318 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
17319 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
17320 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
17321 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
17323 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17324 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
17325 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17326 ("sqlite3_step fail:%d", rc));
17331 if (err == EMAIL_ERROR_NONE) {
17332 if (output_task_list)
17333 *output_task_list = task_item_from_tbl;
17334 *output_task_count = count;
17336 for (i = 0; i < count; i++) {
17337 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
17339 EM_SAFE_FREE(task_item_from_tbl);
17342 if (hStmt != NULL) {
17343 rc = sqlite3_finalize(hStmt);
17345 if (rc != SQLITE_OK) {
17346 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17347 err = EMAIL_ERROR_DB_FAILURE;
17351 EM_DEBUG_FUNC_END("err [%d]", err);
17355 INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(char *multi_user_name, int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *mail_id)
17357 EM_DEBUG_FUNC_BEGIN("account_id:[%d], mailbox_type:[%d], message_id:[%s], server_uid:[%s]", account_id, input_mailbox_type, message_id, server_uid);
17358 int err = EMAIL_ERROR_NONE;
17360 if (message_id == NULL) {
17361 EM_DEBUG_EXCEPTION("Invalid parameter");
17362 err = EMAIL_ERROR_INVALID_PARAM;
17368 int temp_mail_id = 0;
17369 int where_pararaph_length = 0;
17370 char *where_pararaph = NULL;
17371 char sql_query_string[QUERY_SIZE] = {0, };
17372 char **result = NULL;
17373 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17375 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
17377 where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
17378 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
17379 if (where_pararaph == NULL) {
17380 EM_DEBUG_EXCEPTION("em_malloc failed");
17381 err = EMAIL_ERROR_OUT_OF_MEMORY;
17385 if (account_id != ALL_ACCOUNT)
17386 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE account_id = %d AND mailbox_type = %d AND message_id like '%q'", account_id, input_mailbox_type, message_id);
17388 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
17390 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
17391 strcat(sql_query_string, where_pararaph);
17393 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
17395 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
17396 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17397 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17400 EM_DEBUG_LOG("Count of mails [%d]", count);
17403 _get_table_field_data_int(result, &temp_mail_id, 1);
17404 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
17406 memset(sql_query_string, 0x00, QUERY_SIZE);
17407 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
17409 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
17410 strcat(sql_query_string, where_pararaph);
17412 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17413 if (err != EMAIL_ERROR_NONE) {
17414 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
17419 err = EMAIL_ERROR_MAIL_NOT_FOUND;
17424 sqlite3_free_table(result);
17428 EM_SAFE_FREE(where_pararaph);
17430 if (mail_id != NULL)
17431 *mail_id = temp_mail_id;
17433 EM_DEBUG_FUNC_END("err : [%d]", err);
17436 /* Tasks --------------------------------------------------------------------------*/
17438 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17439 INTERNAL_FUNC int emstorage_add_auto_download_activity(char *multi_user_name, email_event_auto_download *local_activity, int *activity_id, int transaction, int *err_code)
17441 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
17443 if (!local_activity || !activity_id) {
17444 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
17445 if (err_code != NULL)
17446 *err_code = EMAIL_ERROR_INVALID_PARAM;
17452 int error = EMAIL_ERROR_NONE;
17455 char sql_query_string[QUERY_SIZE] = {0, };
17456 DB_STMT hStmt = NULL;
17458 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17459 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17461 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17462 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17463 "INSERT INTO mail_auto_download_activity_tbl VALUES "
17465 "? " /* Activity ID */
17467 ",?" /* Account ID */
17468 ",?" /* Local Mail ID */
17469 ",?" /* Server mail ID */
17470 ",?" /* Mailbox ID*/
17471 ",?" /* Multi USER NAME */
17474 char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
17475 char **result = NULL;
17478 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17480 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17481 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17483 if (NULL==result[1]) rc = 1;
17484 else rc = atoi(result[1])+1;
17485 sqlite3_free_table(result);
17488 *activity_id = local_activity->activity_id = rc;
17490 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
17491 local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
17493 if (local_activity->mailbox_id)
17494 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17497 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17498 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17499 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17502 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17503 _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17504 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17505 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17506 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17507 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17508 _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17511 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17513 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
17514 ("sqlite3_step fail:%d", rc));
17515 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17516 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17521 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17522 if (hStmt != NULL) {
17523 rc = sqlite3_finalize(hStmt);
17525 if (rc != SQLITE_OK) {
17526 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17527 error = EMAIL_ERROR_DB_FAILURE;
17531 if (err_code != NULL)
17534 EM_DEBUG_FUNC_END("ret [%d]", ret);
17539 INTERNAL_FUNC int emstorage_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
17541 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);
17543 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17544 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);
17546 if (err_code != NULL)
17547 *err_code = EMAIL_ERROR_INVALID_PARAM;
17553 int error = EMAIL_ERROR_NONE;
17554 char sql_query_string[QUERY_SIZE] = {0, };
17555 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17557 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17558 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17560 if (activity_id == 0)
17561 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
17563 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
17565 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17566 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17567 if (error != EMAIL_ERROR_NONE) {
17568 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17572 /* validate activity existence */
17573 rc = sqlite3_changes(local_db_handle);
17575 EM_DEBUG_EXCEPTION("No matching activity found");
17576 error = EMAIL_ERROR_DATA_NOT_FOUND;
17584 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17586 if (err_code != NULL)
17589 EM_DEBUG_FUNC_END("ret [%d]", ret);
17593 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17595 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17597 if (account_id < FIRST_ACCOUNT_ID) {
17598 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17599 if (err_code != NULL)
17600 *err_code = EMAIL_ERROR_INVALID_PARAM;
17606 int error = EMAIL_ERROR_NONE;
17607 char sql_query_string[QUERY_SIZE] = {0, };
17609 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17611 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17612 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17613 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17615 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17616 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17617 if (error != EMAIL_ERROR_NONE) {
17618 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17622 rc = sqlite3_changes(local_db_handle);
17624 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17625 error = EMAIL_ERROR_DATA_NOT_FOUND;
17633 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17635 if (err_code != NULL)
17638 EM_DEBUG_FUNC_END("ret [%d]", ret);
17643 INTERNAL_FUNC int emstorage_delete_auto_download_activity_by_mailbox(char *multi_user_name, int account_id, int mailbox_id, int transaction, int *err_code)
17645 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17647 if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17648 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17649 if (err_code != NULL)
17650 *err_code = EMAIL_ERROR_INVALID_PARAM;
17656 int error = EMAIL_ERROR_NONE;
17657 char sql_query_string[QUERY_SIZE] = {0, };
17659 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17661 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17662 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17663 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
17665 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17666 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17667 if (error != EMAIL_ERROR_NONE) {
17668 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17672 rc = sqlite3_changes(local_db_handle);
17674 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17675 error = EMAIL_ERROR_DATA_NOT_FOUND;
17683 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17685 if (err_code != NULL)
17688 EM_DEBUG_FUNC_END("ret [%d]", ret);
17693 INTERNAL_FUNC int emstorage_get_auto_download_activity(char *multi_user_name, int account_id, int input_mailbox_id, email_event_auto_download **event_start, int *count, int transaction, int *err_code)
17695 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17697 if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17698 EM_DEBUG_EXCEPTION("account_id[%d], event_start[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
17700 if (err_code != NULL)
17701 *err_code = EMAIL_ERROR_INVALID_PARAM;
17708 int error = EMAIL_ERROR_NONE;
17710 DB_STMT hStmt = NULL;
17711 email_event_auto_download *event_list = NULL;
17712 char sql_query_string[QUERY_SIZE] = {0, };
17714 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17716 EMSTORAGE_START_READ_TRANSACTION(transaction);
17718 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17719 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17722 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17723 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17724 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17726 *count = atoi(result[1]);
17727 sqlite3_free_table(result);
17729 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17732 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17733 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17737 EM_DEBUG_LOG("Activity Count = %d", *count);
17739 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17740 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17742 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17745 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17747 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17748 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17749 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17752 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17753 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17754 ("sqlite3_step fail:%d", rc));
17756 if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17757 EM_DEBUG_EXCEPTION("Malloc failed");
17759 error = EMAIL_ERROR_OUT_OF_MEMORY;
17763 for (i=0; i < (*count); i++) {
17764 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17765 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17766 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17767 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17768 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17769 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17771 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17772 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
17773 ("sqlite3_step fail:%d", rc));
17781 *event_start = event_list;
17783 EM_SAFE_FREE(event_list);
17784 *event_start = NULL;
17788 if (hStmt != NULL) {
17789 rc = sqlite3_finalize(hStmt);
17791 if (rc != SQLITE_OK) {
17792 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17793 error = EMAIL_ERROR_DB_FAILURE;
17797 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17798 if (err_code != NULL)
17801 EM_DEBUG_FUNC_END("ret [%d]", ret);
17806 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17808 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17810 if (!activity_count || !err_code) {
17811 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17812 if (err_code != NULL)
17813 *err_code = EMAIL_ERROR_INVALID_PARAM;
17819 int error = EMAIL_ERROR_NONE;
17820 DB_STMT hStmt = NULL;
17821 char sql_query_string[QUERY_SIZE] = {0, };
17823 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17825 EMSTORAGE_START_READ_TRANSACTION(transaction);
17826 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17828 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17830 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17833 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17834 EM_DEBUG_LOG_DEV ("before sqlite3_prepare hStmt = %p", hStmt);
17835 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
17836 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17839 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17840 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
17841 ("sqlite3_step fail:%d", rc));
17843 _get_stmt_field_data_int(hStmt, activity_count, 0);
17845 EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17851 if (hStmt != NULL) {
17852 rc = sqlite3_finalize(hStmt);
17854 if (rc != SQLITE_OK) {
17855 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17856 error = EMAIL_ERROR_DB_FAILURE;
17860 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17861 if (err_code != NULL)
17864 EM_DEBUG_FUNC_END("ret [%d]", ret);
17869 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17871 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17873 if (!account_list || !count) {
17874 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17875 if (err_code != NULL)
17876 *err_code = EMAIL_ERROR_INVALID_PARAM;
17881 int error = EMAIL_ERROR_NONE;
17882 char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17884 int i = 0, rc = -1;
17885 int *result_account_list = NULL;
17886 DB_STMT hStmt = NULL;
17887 char sql_query_string[QUERY_SIZE] = {0, };
17888 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17890 EMSTORAGE_START_READ_TRANSACTION(transaction);
17892 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17893 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17894 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17896 *count = atoi(result[1]);
17897 sqlite3_free_table(result);
17900 EM_DEBUG_EXCEPTION("no account found...");
17901 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17906 EM_DEBUG_LOG("Account count [%d]", *count);
17908 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17910 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17912 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17915 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17917 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17918 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
17919 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17922 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17923 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17924 ("sqlite3_step fail:%d", rc));
17926 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17927 EM_DEBUG_EXCEPTION(" em_malloc failed...");
17928 error = EMAIL_ERROR_OUT_OF_MEMORY;
17932 for (i = 0; i < (*count); i++) {
17933 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17935 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17936 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17937 ("sqlite3_step fail:%d", rc));
17938 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17946 *account_list = result_account_list;
17948 EM_SAFE_FREE(result_account_list);
17950 if (hStmt != NULL) {
17951 rc = sqlite3_finalize(hStmt);
17953 if (rc != SQLITE_OK) {
17954 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17955 error = EMAIL_ERROR_DB_FAILURE;
17959 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17960 if (err_code != NULL)
17962 EM_DEBUG_FUNC_END("ret [%d]", ret);
17967 INTERNAL_FUNC int emstorage_get_auto_download_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
17969 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17971 if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17972 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17973 if (err_code != NULL)
17974 *err_code = EMAIL_ERROR_INVALID_PARAM;
17979 int error = EMAIL_ERROR_NONE;
17981 int i = 0, rc = -1;
17982 int *mbox_list = NULL;
17983 DB_STMT hStmt = NULL;
17984 char sql_query_string[QUERY_SIZE] = {0, };
17986 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17988 EMSTORAGE_START_READ_TRANSACTION(transaction);
17990 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17991 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17994 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17995 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17996 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17998 *count = atoi(result[1]);
17999 sqlite3_free_table(result);
18002 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
18003 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
18007 EM_DEBUG_LOG("Mailbox count = %d", *count);
18009 memset(sql_query_string, 0x00, sizeof(sql_query_string));
18011 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
18013 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
18016 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
18019 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18020 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
18023 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
18024 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18025 ("sqlite3_step fail:%d", rc));
18027 mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
18028 if (mbox_list == NULL) {
18029 EM_DEBUG_EXCEPTION(" em_malloc failed...");
18030 error = EMAIL_ERROR_OUT_OF_MEMORY;
18034 for (i = 0; i < (*count); i++) {
18035 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
18036 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
18038 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18039 ("sqlite3_step fail:%d", rc));
18040 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
18048 *mailbox_list = mbox_list;
18050 EM_SAFE_FREE(mbox_list);
18052 if (hStmt != NULL) {
18053 rc = sqlite3_finalize(hStmt);
18055 if (rc != SQLITE_OK) {
18056 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
18057 error = EMAIL_ERROR_DB_FAILURE;
18061 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
18062 if (err_code != NULL)
18064 EM_DEBUG_FUNC_END("ret [%d]", ret);
18069 INTERNAL_FUNC int emstorage_get_auto_download_activity_count_by_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
18071 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
18073 if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
18074 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
18075 if (err_code != NULL)
18076 *err_code = EMAIL_ERROR_INVALID_PARAM;
18082 int error = EMAIL_ERROR_NONE;
18083 char sql_query_string[QUERY_SIZE] = {0, };
18084 DB_STMT hStmt = NULL;
18086 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
18088 EMSTORAGE_START_READ_TRANSACTION(transaction);
18089 memset(sql_query_string, 0x00, sizeof(sql_query_string));
18090 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
18092 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
18094 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
18095 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
18096 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18097 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
18099 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
18100 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18101 ("sqlite3_step fail:%d", rc));
18103 _get_stmt_field_data_int(hStmt, activity_count, 0);
18105 EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
18111 if (hStmt != NULL) {
18112 rc = sqlite3_finalize(hStmt);
18114 if (rc != SQLITE_OK) {
18115 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
18116 error = EMAIL_ERROR_DB_FAILURE;
18120 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
18122 if (err_code != NULL)
18125 EM_DEBUG_FUNC_END("ret [%d]", ret);
18130 INTERNAL_FUNC int emstorage_update_auto_download_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mailbox_name, int mailbox_id, int *err_code)
18132 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
18134 int rc = -1, ret = false;
18135 int error = EMAIL_ERROR_NONE;
18136 char sql_query_string[QUERY_SIZE] = {0, };
18137 int transaction = true;
18139 if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
18140 EM_DEBUG_EXCEPTION("Invalid parameters");
18141 error = EMAIL_ERROR_INVALID_PARAM;
18145 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
18147 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
18148 memset(sql_query_string, 0x00, sizeof(sql_query_string));
18150 if (mailbox_id > 0)
18151 SNPRINTF(sql_query_string, sizeof(sql_query_string),
18152 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s , mailbox_id ='%d' WHERE server_mail_id = %s ", new_server_uid, mailbox_id, old_server_uid);
18153 else if (mailbox_name)
18154 SNPRINTF(sql_query_string, sizeof(sql_query_string),
18155 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
18157 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
18158 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18159 if (error != EMAIL_ERROR_NONE) {
18160 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
18164 rc = sqlite3_changes(local_db_handle);
18166 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
18173 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
18175 if (err_code != NULL)
18178 EM_DEBUG_FUNC_END("ret [%d]", ret);
18184 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
18189 } email_column_info_t;
18191 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
18193 EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
18197 char *column_name = NULL;
18198 char *column_type = NULL;
18199 GList *new_list = *((GList**)arg1);
18200 email_column_info_t *column_info_item = NULL;
18202 for (i = 0; i < argc; ++i) {
18203 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
18204 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
18206 EM_SAFE_FREE(column_name);
18208 column_name = EM_SAFE_STRDUP(argv[i]);
18211 else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
18213 EM_SAFE_FREE(column_type);
18215 column_type = EM_SAFE_STRDUP(argv[i]);
18220 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
18221 column_info_item = em_malloc(sizeof(email_column_info_t));
18222 if (column_info_item == NULL) {
18223 EM_DEBUG_EXCEPTION("em_malloc failed");
18227 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
18228 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
18229 new_list = g_list_append(new_list, (gpointer)column_info_item);
18230 *((GList**)arg1) = new_list;
18235 EM_SAFE_FREE(column_name);
18236 EM_SAFE_FREE(column_type);
18238 EM_DEBUG_FUNC_END();
18242 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
18244 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
18245 int err = EMAIL_ERROR_NONE;
18246 int result_from_sqlite = 0;
18247 char *error_message_from_sqlite = NULL;
18248 char sql_query_string[QUERY_SIZE] = {0, };
18249 GList *new_list = NULL;
18250 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
18252 SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
18254 result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
18256 if (result_from_sqlite != SQLITE_OK)
18257 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
18259 EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
18261 if (new_list && output_column_info) {
18262 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
18263 *output_column_info = new_list;
18266 EM_DEBUG_FUNC_END("err [%d]", err);
18270 static int emstorage_create_renamed_table(char *multi_user_name, char **input_full_query, int input_query_index, char *input_source_table_name, char *input_new_table_name)
18272 EM_DEBUG_FUNC_BEGIN("input_full_query [%p] input_query_index[%d] input_source_table_name[%p] input_new_table_name[%p]", input_full_query, input_query_index, input_source_table_name, input_new_table_name);
18273 int error = EMAIL_ERROR_NONE;
18275 sqlite3 *local_db_handle = NULL;
18276 char sql_query_string[QUERY_SIZE] = {0, };
18278 if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
18279 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18280 error = EMAIL_ERROR_INVALID_PARAM;
18284 local_db_handle = emstorage_get_db_connection(multi_user_name);
18286 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18287 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18289 EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
18291 EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
18292 reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
18294 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18297 if (error == EMAIL_ERROR_NONE) {
18298 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18301 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18304 EM_DEBUG_FUNC_END("error [%d]", error);
18308 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
18310 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
18311 int error = EMAIL_ERROR_NONE;
18313 sqlite3 *local_db_handle = NULL;
18314 char sql_query_string[QUERY_SIZE] = {0, };
18316 if (input_table_name == NULL || input_new_column == NULL) {
18317 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18318 error = EMAIL_ERROR_INVALID_PARAM;
18322 local_db_handle = emstorage_get_db_connection(multi_user_name);
18324 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18325 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18326 SNPRINTF(sql_query_string, QUERY_SIZE, "ALTER TABLE %s ADD COLUMN %s %s;", input_table_name, input_new_column->column_name, input_new_column->column_type);
18327 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18330 if (error == EMAIL_ERROR_NONE) {
18331 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18334 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18337 EM_DEBUG_FUNC_END("error [%d]", error);
18341 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
18343 EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
18344 int error = EMAIL_ERROR_NONE;
18346 sqlite3 *local_db_handle = NULL;
18347 char sql_query_string[QUERY_SIZE] = {0, };
18349 if (input_table_name == NULL) {
18350 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18351 error = EMAIL_ERROR_INVALID_PARAM;
18355 local_db_handle = emstorage_get_db_connection(multi_user_name);
18357 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18358 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18359 SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
18360 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18363 if (error == EMAIL_ERROR_NONE) {
18364 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18367 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18370 EM_DEBUG_FUNC_END("error [%d]", error);
18374 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
18376 EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
18377 email_column_info_t *left_one = (email_column_info_t*)old_column_info;
18378 email_column_info_t *right_one = (email_column_info_t*)new_column_info;
18380 if (old_column_info == NULL || new_column_info == NULL)
18383 return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
18386 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
18388 EM_DEBUG_FUNC_BEGIN();
18391 int error = EMAIL_ERROR_NONE;
18393 email_column_info_t *new_column_info = NULL;
18394 email_column_info_t *p_column_info = NULL;
18395 char **create_table_query = NULL;
18396 GList *found_data = NULL;
18397 GList *column_list_of_old_table = NULL;
18398 GList *column_list_of_new_table = NULL;
18399 char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
18400 { "mail_box_tbl", "mail_box_tbl_new" },
18401 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
18402 { "mail_rule_tbl", "mail_rule_tbl_new" },
18403 { "mail_tbl", "mail_tbl_new" },
18404 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
18405 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
18406 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
18410 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
18411 #ifdef __FEATURE_LOCAL_ACTIVITY__
18412 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
18416 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
18417 { "mail_task_tbl", "mail_task_tbl_new" },
18418 #ifdef __FEATURE_BODY_SEARCH__
18419 { "mail_text_tbl", "mail_text_tbl_new" },
18424 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
18425 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
18432 error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
18434 if (error != EMAIL_ERROR_NONE) {
18435 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
18439 if (query_len < CREATE_TABLE_MAX) {
18440 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
18441 error = EMAIL_ERROR_SYSTEM_FAILURE;
18445 for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
18446 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
18447 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
18448 /* Check existing of _new table */
18449 emstorage_drop_table(multi_user_name, table_names[i][1]);
18450 error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
18451 if (error != EMAIL_ERROR_NONE) {
18452 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
18456 emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
18457 emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
18459 /* Compare fields and add new field */
18460 for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
18461 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
18462 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
18463 if (found_data == NULL) {
18465 emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
18469 emstorage_drop_table(multi_user_name, table_names[i][1]);
18472 EM_DEBUG_LOG("Skipped");
18476 if (create_table_query) {
18478 for (i = 0; i < query_len; i++) {
18479 if (create_table_query[i]) {
18480 EM_SAFE_FREE(create_table_query[i]);
18483 EM_SAFE_FREE(create_table_query);
18486 found_data = g_list_first(column_list_of_old_table);
18487 while(found_data != NULL) {
18488 p_column_info = (email_column_info_t *)found_data->data;
18489 EM_SAFE_FREE(p_column_info->column_name);
18490 EM_SAFE_FREE(p_column_info->column_type);
18491 EM_SAFE_FREE(p_column_info);
18493 found_data = g_list_next(found_data);
18495 g_list_free(column_list_of_old_table);
18497 found_data = g_list_first(column_list_of_new_table);
18498 while(found_data != NULL) {
18499 p_column_info = (email_column_info_t *)found_data->data;
18500 EM_SAFE_FREE(p_column_info->column_name);
18501 EM_SAFE_FREE(p_column_info->column_type);
18502 EM_SAFE_FREE(p_column_info);
18504 found_data = g_list_next(found_data);
18506 g_list_free(column_list_of_new_table);
18508 EM_DEBUG_FUNC_END("error [%d]", error);
18511 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */