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-gmime.h"
67 #include "email-core-key-manager.h"
69 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
70 #define CONTENT_DATA "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
71 #define CONTENT_TYPE_DATA "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define FLAG_SEEN 0x01
74 #define FLAG_DELETED 0x02
75 #define FLAG_FLAGGED 0x04
76 #define FLAG_ANSWERED 0x08
78 #define FLAG_DRAFT 0x20
82 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL 256
83 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL 128
84 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL 1027
85 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL 256
87 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
88 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
89 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
90 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
91 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
92 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
93 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL 50
94 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
95 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL 30
96 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
97 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
98 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
99 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL 256
100 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL 256
101 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL 128
102 #define ALIAS_LEN_IN_MAIL_BOX_TBL 128
103 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
104 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
105 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
106 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL 256
107 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL 256
108 #define VALUE_LEN_IN_MAIL_RULE_TBL 256
109 #define VALUE2_LEN_IN_MAIL_RULE_TBL 256
110 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL 128
111 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 128
112 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL 256
113 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 256
114 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL 256
115 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
116 #define MAILBOX_LEN_IN_MAIL_TBL 128
117 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL 128
118 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL 128
119 #define FROM_LEN_IN_MAIL_TBL 256
120 #define SENDER_LEN_IN_MAIL_TBL 256
121 #define REPLY_TO_LEN_IN_MAIL_TBL 256
122 #define TO_LEN_IN_MAIL_TBL 3999
123 #define CC_LEN_IN_MAIL_TBL 3999
124 #define BCC_LEN_IN_MAIL_TBL 3999
125 #define RETURN_PATH_LEN_IN_MAIL_TBL 3999
126 #define SUBJECT_LEN_IN_MAIL_TBL 1027
127 #define THREAD_TOPIC_LEN_IN_MAIL_TBL 256
128 #define TEXT_1_LEN_IN_MAIL_TBL 256
129 #define TEXT_2_LEN_IN_MAIL_TBL 256
130 #define MIME_ENTITY_LEN_IN_MAIL_TBL 256
131 #define DATETIME_LEN_IN_MAIL_TBL 128
132 #define MESSAGE_ID_LEN_IN_MAIL_TBL 256
133 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
134 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
135 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
136 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
137 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL 128
138 #define LOCATION_LEN_IN_MAIL_MEETING_TBL 1024
139 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL 512
140 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL 32
141 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL 32
142 #define PREVIEWBODY_LEN_IN_MAIL_TBL 512
143 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
145 /* this define is used for query to change data (delete, insert, update) */
146 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
148 if (transaction_flag) { \
149 if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
150 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
156 /* this define is used for query to change data (delete, insert, update) */
157 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
159 if (transaction_flag) { \
160 if (result_code == true) { \
161 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
162 EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
163 error_code = EMAIL_ERROR_DB_FAILURE; \
164 result_code = false; \
168 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169 EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
170 error_code = EMAIL_ERROR_DB_FAILURE; \
176 /* this define is used for query to read (select) */
177 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
178 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) { \
185 /*_unlockshm_mutex(mapped_for_db_lock);*/\
188 /* for safety DB operation */
189 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
192 #define _MULTIPLE_DB_HANDLE
194 #ifdef _MULTIPLE_DB_HANDLE
202 #define MAX_DB_CLIENT 100
204 /* static int _db_handle_count = 0; */
205 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
208 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
210 EM_DEBUG_FUNC_BEGIN();
212 pthread_t current_thread_id = THREAD_SELF();
213 sqlite3 *result_db_handle = NULL;
215 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
216 ENTER_CRITICAL_SECTION(_db_handle_lock);
217 for (i = 0; i < MAX_DB_CLIENT; i++) {
218 if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
221 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
222 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
223 result_db_handle = _db_handle_list[i].db_handle;
227 LEAVE_CRITICAL_SECTION(_db_handle_lock);
229 ENTER_CRITICAL_SECTION(_db_handle_lock);
230 for (i = 0; i < MAX_DB_CLIENT; i++) {
231 if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
234 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
235 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
236 result_db_handle = _db_handle_list[i].db_handle;
240 LEAVE_CRITICAL_SECTION(_db_handle_lock);
243 if (!result_db_handle)
244 EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
247 return result_db_handle;
250 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
252 EM_DEBUG_FUNC_BEGIN();
253 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
254 pthread_t current_thread_id = THREAD_SELF();
256 ENTER_CRITICAL_SECTION(_db_handle_lock);
257 for (i = 0; i < MAX_DB_CLIENT; i++) {
258 if (_db_handle_list[i].thread_id == 0) {
259 _db_handle_list[i].thread_id = current_thread_id;
260 _db_handle_list[i].db_handle = db_handle;
261 /* Only distinguished container and host */
262 _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
263 EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
264 error_code = EMAIL_ERROR_NONE;
268 LEAVE_CRITICAL_SECTION(_db_handle_lock);
270 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
271 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
273 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
277 int emstorage_remove_db_handle()
279 EM_DEBUG_FUNC_BEGIN();
280 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
281 ENTER_CRITICAL_SECTION(_db_handle_lock);
282 for (i = 0; i < MAX_DB_CLIENT; i++) {
283 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
284 _db_handle_list[i].thread_id = 0;
285 _db_handle_list[i].db_handle = NULL;
286 EM_SAFE_FREE(_db_handle_list[i].user_name);
288 EM_DEBUG_LOG("index [%d]", i);
289 error_code = EMAIL_ERROR_NONE;
293 LEAVE_CRITICAL_SECTION(_db_handle_lock);
295 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
296 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
298 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
302 int emstorage_reset_db_handle_list()
304 EM_DEBUG_FUNC_BEGIN();
307 ENTER_CRITICAL_SECTION(_db_handle_lock);
308 for (i = 0; i < MAX_DB_CLIENT; i++) {
309 _db_handle_list[i].thread_id = 0;
310 _db_handle_list[i].db_handle = NULL;
311 EM_SAFE_FREE(_db_handle_list[i].user_name);
313 LEAVE_CRITICAL_SECTION(_db_handle_lock)
316 return EMAIL_ERROR_NONE;
320 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
322 return emstorage_db_open(multi_user_name, NULL);
325 #else /* _MULTIPLE_DB_HANDLE */
327 sqlite3 *_db_handle = NULL;
329 sqlite3 *emstorage_get_db_connection()
331 if (NULL == _db_handle)
332 emstorage_db_open(NULL);
335 #endif /* _MULTIPLE_DB_HANDLE */
337 /* ------------------------------------------------------------------------------ */
338 /* Mutex using shared memory */
340 pthread_mutex_t mutex;
344 mmapped_t *mapped_for_db_lock = NULL;
345 int shm_fd_for_db_lock = 0;
347 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
348 mmapped_t *mapped_for_generating_mail_id = NULL;
349 int shm_fd_for_generating_mail_id = 0;
350 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
352 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
353 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
355 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
356 return_value = function_call;\
357 _unlockshm_mutex(mapped_for_db_lock);\
360 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
361 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
362 { return_value = function_call; }
363 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
365 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
367 EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
368 int error = EMAIL_ERROR_NONE;
370 DB_STMT db_statement = NULL;
372 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
373 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
374 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
376 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
377 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
378 ("sqlite3_step failed [%d] [%s]", rc, query_string));
382 if (db_statement != NULL) {
383 rc = sqlite3_finalize(db_statement);
384 if (rc != SQLITE_OK) {
385 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
386 error = EMAIL_ERROR_DB_FAILURE;
393 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
395 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
396 char errno_buf[ERRNO_BUF_SIZE] = {0};
398 if (!shm_file_name) {
399 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
400 return EMAIL_ERROR_INVALID_PARAM;
403 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
405 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
406 return EMAIL_ERROR_SYSTEM_FAILURE;
410 EM_DEBUG_LOG("** Create SHM FILE **");
411 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
412 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
413 return EMAIL_ERROR_SYSTEM_FAILURE;
416 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
417 if (m == MAP_FAILED) {
418 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
420 return EMAIL_ERROR_SYSTEM_FAILURE;
425 pthread_mutexattr_t mattr;
426 pthread_mutexattr_init(&mattr);
427 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
428 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
429 pthread_mutex_init(&(m->mutex), &mattr);
430 pthread_mutexattr_destroy(&mattr);
432 pthread_mutex_destroy(&(m->mutex));
433 munmap(m, sizeof(mmapped_t));
437 return EMAIL_ERROR_NONE;
440 int emstorage_shm_file_destroy(const char *shm_file_name)
442 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
443 char errno_buf[ERRNO_BUF_SIZE] = {0};
445 if (!shm_file_name) {
446 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
447 return EMAIL_ERROR_INVALID_PARAM;
450 if (shm_unlink(shm_file_name) != 0)
451 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
453 return EMAIL_ERROR_NONE;
456 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
458 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
459 char errno_buf[ERRNO_BUF_SIZE] = {0};
461 if (!shm_file_name || !param_shm_fd || !param_mapped) {
462 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
463 return EMAIL_ERROR_INVALID_PARAM;
466 if (!(*param_mapped)) {
467 EM_DEBUG_LOG("** mapping begin **");
468 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
469 *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
470 if ((*param_shm_fd) == -1) {
471 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
472 return EMAIL_ERROR_SYSTEM_FAILURE;
476 fchmod((*param_shm_fd), 0666);
477 EM_DEBUG_LOG("** Create SHM FILE **");
478 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
479 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
480 return EMAIL_ERROR_SYSTEM_FAILURE;
483 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
484 if (tmp == MAP_FAILED) {
485 EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
486 return EMAIL_ERROR_SYSTEM_FAILURE;
491 pthread_mutexattr_t mattr;
492 pthread_mutexattr_init(&mattr);
493 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
494 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
495 pthread_mutex_init(&(tmp->mutex), &mattr);
496 pthread_mutexattr_destroy(&mattr);
502 return EMAIL_ERROR_NONE;
505 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
507 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
510 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
511 return EMAIL_ERROR_INVALID_PARAM;
514 struct timespec abs_time;
515 clock_gettime(CLOCK_REALTIME, &abs_time);
516 abs_time.tv_sec += sec;
517 char errno_buf[ERRNO_BUF_SIZE] = {0};
519 int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
521 if (err == EOWNERDEAD) {
522 err = pthread_mutex_consistent(&(param_mapped->mutex));
523 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
524 } else if (err != 0) {
525 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
530 return EMAIL_ERROR_NONE;
533 void _unlockshm_mutex(mmapped_t *param_mapped)
535 EM_DEBUG_FUNC_BEGIN();
536 pthread_mutex_unlock(&(param_mapped->mutex));
539 /* ------------------------------------------------------------------------------ */
542 static int _open_counter = 0;
544 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
545 static int _read_password_from_secure_storage(char *file_name, char **password);
547 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
548 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
552 const char *object_name;
553 unsigned int data_flag;
556 static const email_db_object_t _g_db_tables[] = {
557 { "mail_read_mail_uid_tbl", 1},
559 { "mail_attachment_tbl", 1},
563 static const email_db_object_t _g_db_indexes[] = {
564 { "mail_read_mail_uid_idx1", 1},
566 { "mail_attachment_idx1", 1},
571 CREATE_TABLE_MAIL_ACCOUNT_TBL,
572 CREATE_TABLE_MAIL_BOX_TBL,
573 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
574 CREATE_TABLE_MAIL_RULE_TBL,
575 CREATE_TABLE_MAIL_TBL,
576 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
577 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
578 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
580 CREATE_TABLE_DUMMY_INDEX1,
582 CREATE_TABLE_MAIL_MEETING_TBL,
583 #ifdef __FEATURE_LOCAL_ACTIVITY__
584 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
586 CREATE_TABLE_DUMMY_INDEX2,
588 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
589 CREATE_TABLE_MAIL_TASK_TBL,
590 #ifdef __FEATURE_BODY_SEARCH__
591 CREATE_TABLE_MAIL_TEXT_TBL,
593 CREATE_TABLE_DUMMY_INDEX3,
596 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
597 CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
599 CREATE_TABLE_DUMMY_INDEX4,
603 CREATE_TABLE_MAIL_ACCOUNT_IDX,
604 CREATE_TABLE_MAIL_BOX_IDX,
605 CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
606 CREATE_TABLE_MAIL_IDX,
607 CREATE_TABLE_MAIL_ATTACHMENT_IDX,
608 CREATE_TABLE_MAIL_MEETING_IDX,
609 CREATE_TABLE_MAIL_TASK_IDX,
610 CREATE_TABLE_MAIL_DATETIME_IDX,
611 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,
622 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
623 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
624 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
625 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
626 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
627 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
628 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
629 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
630 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
631 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
632 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
641 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
642 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
643 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
644 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
645 ALIAS_IDX_IN_MAIL_BOX_TBL,
646 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
647 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
648 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
649 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
650 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
654 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
655 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
656 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
657 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
658 SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
659 RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
660 SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
661 FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
662 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
666 #ifdef __FEATURE_BODY_SEARCH__
668 MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
669 ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
670 MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
671 BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
672 FIELD_COUNT_OF_MAIL_TEXT_TBL,
677 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
678 RULE_ID_IDX_IN_MAIL_RULE_TBL,
679 FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
680 TYPE_IDX_IN_MAIL_RULE_TBL,
681 VALUE_IDX_IN_MAIL_RULE_TBL,
682 VALUE2_IDX_IN_MAIL_RULE_TBL,
683 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
684 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
685 FLAG1_IDX_IN_MAIL_RULE_TBL,
686 FLAG2_IDX_IN_MAIL_RULE_TBL,
690 MAIL_ID_IDX_IN_MAIL_TBL = 0,
691 ACCOUNT_ID_IDX_IN_MAIL_TBL,
692 MAILBOX_ID_IDX_IN_MAIL_TBL,
693 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
694 SUBJECT_IDX_IN_MAIL_TBL,
695 DATETIME_IDX_IN_MAIL_TBL,
696 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
697 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
698 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
699 MESSAGE_ID_IDX_IN_MAIL_TBL,
700 REFERENCE_ID_IDX_IN_MAIL_TBL,
701 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
702 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
703 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
704 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
705 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
706 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
707 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
708 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
709 ALIAS_SENDER_IDX_IN_MAIL_TBL,
710 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
711 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
712 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
713 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
714 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
715 MAIL_SIZE_IDX_IN_MAIL_TBL,
716 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
717 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
718 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
719 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
720 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
721 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
722 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
723 DRM_STATUS_IDX_IN_MAIL_TBL,
724 PRIORITY_IDX_IN_MAIL_TBL,
725 SAVE_STATUS_IDX_IN_MAIL_TBL,
726 LOCK_STATUS_IDX_IN_MAIL_TBL,
727 REPORT_STATUS_IDX_IN_MAIL_TBL,
728 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
729 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
730 THREAD_ID_IDX_IN_MAIL_TBL,
731 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
732 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
733 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
734 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
735 DIGEST_TYPE_IDX_IN_MAIL_TBL,
736 SMIME_TYPE_IDX_IN_MAIL_TBL,
737 SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
738 REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
739 TAG_ID_IDX_IN_MAIL_TBL,
740 REPLIED_TIME_IDX_IN_MAIL_TBL,
741 FORWARDED_TIME_IDX_IN_MAIL_TBL,
742 DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
743 EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
744 EAS_DATA_IDX_IN_MAIL_TBL,
745 USER_NAME_IDX_IN_MAIL_TBL,
746 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
750 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
751 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
752 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
753 CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
754 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
755 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
756 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
757 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
758 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
759 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
760 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
761 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
762 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
763 #ifdef __ATTACHMENT_OPTI__
764 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
765 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
770 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
771 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
772 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
773 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
774 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
775 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
777 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
778 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
779 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
780 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
782 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
789 ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
790 STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
791 ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
792 MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
793 SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
794 MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
795 MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
799 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
800 typedef struct _em_mail_id_list {
802 struct _em_mail_id_list *next;
805 static char *g_test_query[] = {
806 /* 1. select mail_account_tbl */
809 " incoming_server_type, "
810 " incoming_server_address, "
811 " user_email_address, "
812 " incoming_server_user_name, "
813 " incoming_server_password, "
815 " incoming_server_port_number, "
816 " incoming_server_secure_connection, "
817 " incoming_server_authentication_method,"
818 " outgoing_server_type, "
819 " outgoing_server_address, "
820 " outgoing_server_port_number, "
821 " outgoing_server_need_authentication, "
822 " outgoing_server_secure_connection, "
823 " outgoing_server_user_name, "
824 " outgoing_server_password, "
829 " keep_mails_on_pop_server_after_download, "
830 " auto_resend_times, "
831 " outgoing_server_size_limit, "
832 " wifi_auto_download, "
834 " incoming_server_requires_apop,"
836 " is_preset_account, "
840 " req_delivery_receipt, "
841 " req_read_receipt, "
845 " display_name_from, "
847 " forward_with_files, "
851 ", add_my_address_to_bcc"
852 ", notification_status "
854 ", display_content_status "
855 ", default_ringtone_status "
856 ", alert_ringtone_path "
865 " FROM mail_account_tbl",
866 /* 2. select mail_box_tbl */
876 " total_mail_count_on_server, "
877 " has_archived_mails, "
881 " FROM mail_box_tbl ",
882 /* 3. select mail_read_mail_uid_tbl */
891 " flags_seen_field, "
893 " FROM mail_read_mail_uid_tbl ",
894 /* 4. select mail_rule_tbl */
903 " target_mailbox_id, "
906 " FROM mail_rule_tbl ",
907 /* 5. select mail_tbl */
916 " server_mail_status, "
917 " server_mailbox_name, "
920 " reference_mail_id, "
921 " full_address_from, "
922 " full_address_reply, "
925 " full_address_bcc, "
926 " full_address_return, "
927 " email_address_sender, "
928 " email_address_recipient, "
931 " body_download_status, "
934 " file_path_mime_entity, "
936 " flags_seen_field ,"
937 " flags_deleted_field ,"
938 " flags_flagged_field ,"
939 " flags_answered_field ,"
940 " flags_recent_field ,"
941 " flags_draft_field ,"
942 " flags_forwarded_field,"
948 " attachment_count, "
949 " inline_content_count, "
951 " thread_item_count, "
953 " meeting_request_status, "
958 /* 6. select mail_attachment_tbl */
967 " attachment_save_status, "
968 " attachment_drm_type, "
969 " attachment_drm_method, "
970 " attachment_inline_content_status, "
971 " attachment_mime_type "
972 " FROM mail_attachment_tbl ",
974 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
984 " FROM mail_partial_body_activity_tbl ",
991 " meeting_response, "
995 " global_object_id, "
998 " standard_time_start_date, "
1001 " daylight_time_start_date, "
1003 " FROM mail_meeting_tbl ",
1005 #ifdef __FEATURE_LOCAL_ACTIVITY__
1014 " FROM mail_local_activity_tbl ",
1021 " expiration_year, "
1022 " expiration_month, "
1024 " issue_organization_name, "
1029 " FROM mail_certificate_tbl ",
1035 " task_parameter_length, "
1036 " task_parameter , "
1038 " FROM mail_task_tbl ",
1039 #ifdef __FEATURE_BODY_SEARCH__
1045 " FROM mail_text_tbl ",
1047 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1055 " multi_user_name, "
1056 " FROM mail_auto_download_activity_tbl ",
1061 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1063 static int _get_table_field_data_char(char **table, char *buf, int index)
1065 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1066 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1070 if (table[index] != NULL) {
1071 *buf = (char)atoi(table[index]);
1075 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1081 static int _get_table_field_data_int(char **table, int *buf, int index)
1083 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1084 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1088 if (table[index] != NULL) {
1089 *buf = atoi(table[index]);
1093 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1099 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
1101 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1102 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1106 if (table[index] != NULL) {
1107 *buf = (time_t)atol(table[index]);
1111 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1117 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1121 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1122 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1126 char *pTemp = table[index];
1131 sLen = EM_SAFE_STRLEN(pTemp);
1133 *buf = (char *) em_malloc(sLen + 1);
1135 EM_DEBUG_EXCEPTION("malloc is failed");
1138 strncpy(*buf, pTemp, sLen);
1142 #ifdef _PRINT_STORAGE_LOG_
1144 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1146 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1154 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1156 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1157 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1161 char *pTemp = table[index];
1166 memset(buf, 0, buffer_size);
1167 strncpy(buf, pTemp, buffer_size - 1);
1169 #ifdef _PRINT_STORAGE_LOG_
1171 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1173 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1179 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1181 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1182 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1186 char *temp_buffer = table[index];
1188 if (temp_buffer == NULL)
1191 *buffer = malloc(buffer_size);
1192 if (*buffer == NULL) {
1193 EM_DEBUG_EXCEPTION("allocation failed.");
1196 memset(*buffer, 0, buffer_size);
1197 memcpy(*buffer, temp_buffer, buffer_size);
1199 #ifdef _PRINT_STORAGE_LOG_
1201 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1203 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1209 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1211 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1212 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1216 if (sqlite3_column_text(hStmt, index) != NULL) {
1217 *buf = (char)sqlite3_column_int(hStmt, index);
1218 #ifdef _PRINT_STORAGE_LOG_
1219 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1224 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1229 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1231 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1232 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1236 if (sqlite3_column_text(hStmt, index) != NULL) {
1237 *buf = sqlite3_column_int(hStmt, index);
1238 #ifdef _PRINT_STORAGE_LOG_
1239 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1244 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1249 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1251 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1252 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1256 if (sqlite3_column_text(hStmt, index) != NULL) {
1257 *buf = (time_t)sqlite3_column_int(hStmt, index);
1258 #ifdef _PRINT_STORAGE_LOG_
1259 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1264 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1268 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1270 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1271 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1276 sLen = sqlite3_column_bytes(hStmt, index);
1278 #ifdef _PRINT_STORAGE_LOG_
1279 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1283 *buf = (char *) em_malloc(sLen + 1);
1285 EM_DEBUG_EXCEPTION("em_mallocfailed");
1289 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1293 #ifdef _PRINT_STORAGE_LOG_
1295 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1297 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1303 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1305 if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1306 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1311 sLen = sqlite3_column_bytes(hStmt, index);
1313 #ifdef _PRINT_STORAGE_LOG_
1314 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1318 *buf = (char *) em_malloc(sLen);
1320 EM_DEBUG_EXCEPTION("em_mallocfailed");
1324 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1330 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1332 if ((hStmt == NULL) || (index < 0)) {
1333 EM_DEBUG_EXCEPTION("index[%d]", index);
1337 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1339 if (ret != SQLITE_OK) {
1340 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1347 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1349 if ((hStmt == NULL) || (index < 0)) {
1350 EM_DEBUG_EXCEPTION("index[%d]", index);
1354 int ret = sqlite3_bind_int(hStmt, index+1, value);
1356 if (ret != SQLITE_OK) {
1357 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1364 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1366 if ((hStmt == NULL) || (index < 0)) {
1367 EM_DEBUG_EXCEPTION("index[%d]", index);
1371 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1373 if (ret != SQLITE_OK) {
1374 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1381 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1383 if ((hStmt == NULL) || (index < 0)) {
1384 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1388 #ifdef _PRINT_STORAGE_LOG_
1389 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1394 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1396 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1398 if (ret != SQLITE_OK) {
1399 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1405 /* destroy function for sqlite3_bind_text */
1406 void _bind_stmt_free_string(void* arg)
1408 EM_DEBUG_FUNC_BEGIN();
1409 char* p = (char*) arg;
1411 EM_DEBUG_FUNC_END();
1414 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1416 if ((hStmt == NULL) || (index < 0)) {
1417 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1421 #ifdef _PRINT_STORAGE_LOG_
1422 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1426 if (value != NULL) {
1427 if (strlen(value) <= max_len)
1428 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1430 char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1432 EM_DEBUG_EXCEPTION("em_mallocfailed");
1435 snprintf(buf, max_len-1, "%s", value);
1436 ret = sqlite3_bind_text(hStmt, index+1, buf, -1, _bind_stmt_free_string);
1439 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1441 if (ret != SQLITE_OK) {
1442 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1449 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1451 if ((hStmt == NULL) || (index < 0)) {
1452 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1456 #ifdef _PRINT_STORAGE_LOG_
1457 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1462 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1464 ret = sqlite3_bind_null(hStmt, index+1);
1466 if (ret != SQLITE_OK) {
1467 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1474 static int _delete_temp_file(const char *path)
1476 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1479 struct dirent *entry = NULL;
1481 char buf[1024] = {0x00, };
1483 if ((dp = opendir(path)) == NULL) {
1484 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1488 while ((entry = readdir(dp)) != NULL) {
1489 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1494 EM_DEBUG_FUNC_END();
1498 char *cpy_str(char *src)
1503 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1504 EM_DEBUG_EXCEPTION("mailoc failed...");
1507 strncpy(p, src, EM_SAFE_STRLEN(src));
1513 static void _emstorage_close_once(void)
1515 EM_DEBUG_FUNC_BEGIN();
1517 EM_DEBUG_FUNC_END();
1520 INTERNAL_FUNC int emstorage_close(int *err_code)
1522 EM_DEBUG_FUNC_BEGIN();
1525 int error = EMAIL_ERROR_NONE;
1527 if (!emstorage_db_close(NULL, &error))
1529 if (--_open_counter == 0)
1530 _emstorage_close_once();
1534 if (err_code != NULL)
1537 EM_DEBUG_FUNC_END("ret [%d]", ret);
1541 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1543 EM_DEBUG_FUNC_BEGIN();
1545 int error = EMAIL_ERROR_NONE;
1547 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1548 char buf[MAX_PATH] = {0};
1549 char *prefix_path = NULL;
1551 error = emcore_get_container_path(multi_user_name, &prefix_path);
1552 if (error != EMAIL_ERROR_NONE) {
1553 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1557 memset(buf, 0x00, sizeof(buf));
1558 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAILPATH);
1559 mkdir(buf, DIRECTORY_PERMISSION);
1561 memset(buf, 0x00, sizeof(buf));
1562 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILHOME);
1563 mkdir(buf, DIRECTORY_PERMISSION);
1565 memset(buf, 0x00, sizeof(buf));
1566 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILTEMP);
1567 mkdir(buf, DIRECTORY_PERMISSION);
1569 _delete_temp_file(buf);
1570 EM_SAFE_FREE(prefix_path);
1572 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1573 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1574 mkdir(MAILHOME, DIRECTORY_PERMISSION);
1575 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1577 _delete_temp_file(MAILTEMP);
1580 if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1581 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1588 if (err_code != NULL)
1594 /* pData : a parameter which is registered when busy handler is registerd */
1595 /* count : retry count */
1596 static int _callback_sqlite_busy_handler(void *pData, int count)
1598 if (10 - count > 0) {
1599 struct timespec time = {
1601 .tv_nsec = (count + 1) * 100 * 1000 * 1000
1603 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1604 nanosleep(&time, NULL);
1607 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1612 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1613 int length_text_b, const void *text_b)
1615 EM_DEBUG_FUNC_BEGIN();
1617 char *converted_string_a = NULL;
1618 char *converted_string_b = NULL;
1620 EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1621 converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1622 EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1624 EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1625 converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1626 EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1628 if (converted_string_a && converted_string_b)
1629 result = strcmp(converted_string_a, converted_string_b);
1631 EM_SAFE_FREE(converted_string_a);
1632 EM_SAFE_FREE(converted_string_b);
1634 EM_DEBUG_FUNC_END();
1638 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1640 EM_DEBUG_FUNC_BEGIN();
1642 int error = EMAIL_ERROR_NONE;
1645 if (!emstorage_delete_file(db_file_path, &error)) {
1646 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1647 EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1652 if (!emstorage_delete_dir((char *)MAILHOME, &error)) {
1653 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1662 EM_DEBUG_FUNC_END();
1666 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1668 EM_DEBUG_FUNC_BEGIN();
1670 int error = EMAIL_ERROR_NONE;
1673 /* Delete all files and directories */
1674 if (!_delete_all_files_and_directories(db_file_path, &error)) {
1675 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1679 /* Delete all accounts on MyAccount */
1681 /* Delete all managed connection to DB */
1682 emstorage_reset_db_handle_list();
1689 EM_DEBUG_FUNC_END();
1693 int _xsystem(const char *argv[])
1701 perror("fork failed");
1704 if (execvp(argv[0], (char *const *)argv) == -1) {
1705 perror("execute init db script");
1715 if (waitpid(pid, &status, 0) == -1) {
1716 perror("waitpid failed");
1720 if (WIFSIGNALED(status)) {
1725 if (!WIFEXITED(status)) {
1726 perror("should not happen");
1730 return WEXITSTATUS(status);
1733 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1735 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1737 EM_DEBUG_FUNC_BEGIN();
1738 int err = EMAIL_ERROR_NONE;
1739 char *prefix_path = NULL;
1740 char *output_file_path = NULL;
1741 char temp_file_path[MAX_PATH] = {0};
1743 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1744 err = emcore_get_container_path(multi_user_name, &prefix_path);
1745 if (err != EMAIL_ERROR_NONE) {
1746 if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1747 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1752 prefix_path = strdup("");
1755 if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1756 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1757 if (err != EMAIL_ERROR_NONE) {
1758 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1762 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1764 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1767 EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1769 if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1771 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1772 ret = _xsystem(argv_script);
1775 EM_DEBUG_EXCEPTION("_xsystem failed");
1776 err = EMAIL_ERROR_SYSTEM_FAILURE;
1782 EM_SAFE_FREE(prefix_path);
1783 EM_SAFE_FREE(output_file_path);
1785 EM_DEBUG_FUNC_END();
1789 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1791 EM_DEBUG_FUNC_BEGIN();
1793 int error = EMAIL_ERROR_NONE;
1796 EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1798 if (*sqlite_handle) { /*prevent 33351*/
1799 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1800 if (err_code != NULL)
1805 EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1808 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1809 if (SQLITE_OK != rc) {
1810 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1811 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
1812 error = EMAIL_ERROR_PERMISSION_DENIED;
1814 error = EMAIL_ERROR_DB_FAILURE;
1816 sqlite3_close(*sqlite_handle);
1817 *sqlite_handle = NULL;
1819 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1820 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1821 if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1822 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1826 EM_DEBUG_LOG("Open DB again");
1827 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1828 if (SQLITE_OK != rc) {
1829 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1830 if (SQLITE_PERM == rc) {
1831 error = EMAIL_ERROR_PERMISSION_DENIED;
1833 error = EMAIL_ERROR_DB_FAILURE;
1835 sqlite3_close(*sqlite_handle);
1836 *sqlite_handle = NULL;
1837 goto FINISH_OFF; /*prevent 33351*/
1843 /* register busy handler */
1844 EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1845 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1846 if (SQLITE_OK != rc) {
1847 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1848 error = EMAIL_ERROR_DB_FAILURE;
1849 sqlite3_close(*sqlite_handle);
1850 *sqlite_handle = NULL;
1854 /* Register collation callback function */
1855 rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1856 if (SQLITE_OK != rc) {
1857 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1858 error = EMAIL_ERROR_DB_FAILURE;
1859 sqlite3_close(*sqlite_handle);
1860 *sqlite_handle = NULL;
1867 if (err_code != NULL)
1870 EM_DEBUG_FUNC_END("ret [%d]", ret);
1874 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1876 EM_DEBUG_FUNC_BEGIN();
1878 sqlite3 *_db_handle = NULL;
1880 int error = EMAIL_ERROR_NONE;
1881 char *prefix_path = NULL;
1883 _db_handle = emstorage_get_db_handle(multi_user_name);
1885 if (_db_handle == NULL) {
1886 char *output_file_path = NULL;
1887 char temp_file_path[MAX_PATH] = {0};
1889 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1890 error = emcore_get_container_path(multi_user_name, &prefix_path);
1891 if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1892 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1896 prefix_path = strdup("");
1899 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1900 if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1901 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1905 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1906 EM_SAFE_FREE(output_file_path);
1908 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1911 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1912 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1916 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1918 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1919 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1920 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1922 emstorage_set_db_handle(multi_user_name, _db_handle);
1924 emstorage_initialize_field_count();
1929 EM_SAFE_FREE(prefix_path);
1931 if (err_code != NULL)
1934 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1938 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1940 EM_DEBUG_FUNC_BEGIN();
1941 #ifdef _MULTIPLE_DB_HANDLE
1942 sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1945 int error = EMAIL_ERROR_NONE;
1949 ret = sqlite3_close(_db_handle);
1950 if (ret != SQLITE_OK) {
1951 EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1952 error = EMAIL_ERROR_DB_FAILURE;
1956 #ifdef _MULTIPLE_DB_HANDLE
1957 emstorage_remove_db_handle();
1965 if (err_code != NULL)
1968 EM_DEBUG_FUNC_END("ret [%d]", ret);
1972 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
1974 EM_DEBUG_FUNC_BEGIN();
1977 int error = EMAIL_ERROR_NONE;
1979 char *prefix_path = NULL;
1980 char buf[MAX_PATH] = {0};
1982 if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
1983 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
1985 error = emcore_get_container_path(multi_user_name, &prefix_path);
1986 if (error != EMAIL_ERROR_NONE) {
1987 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1990 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
1993 if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
1994 retValue = mkdir(buf, DIRECTORY_PERMISSION);
1996 EM_DEBUG_LOG("mkdir return- %d", retValue);
1997 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2000 if (emstorage_db_open(multi_user_name, &error) == NULL) {
2001 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2005 if (_open_counter++ == 0) {
2006 _emstorage_open_once(multi_user_name, &error);
2013 EM_SAFE_FREE(prefix_path);
2015 if (err_code != NULL)
2018 EM_DEBUG_FUNC_END("ret [%d]", ret);
2022 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2024 EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2025 int err = EMAIL_ERROR_NONE;
2026 int field_count = 0;
2029 if (input_create_table_query == NULL || output_field_count == NULL) {
2030 err = EMAIL_ERROR_INVALID_PARAM;
2034 pos = input_create_table_query;
2038 if (pos == NULL || *pos == (char)0)
2041 pos = strchr(pos, ',');
2044 *output_field_count = field_count;
2046 EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2050 EM_DEBUG_FUNC_END("err [%d]", err);
2054 INTERNAL_FUNC int emstorage_initialize_field_count()
2056 EM_DEBUG_FUNC_BEGIN();
2057 int err = EMAIL_ERROR_NONE;
2059 char **create_table_query = NULL;
2061 if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2062 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2066 err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2067 if (err != EMAIL_ERROR_NONE) {
2068 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2072 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]));
2073 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]));
2074 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]));
2075 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]));
2076 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2077 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]));
2078 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]));
2079 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]));
2080 #ifdef __FEATURE_LOCAL_ACTIVITY__
2081 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]));
2082 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2083 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]));
2084 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]));
2085 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]));
2088 if (create_table_query) {
2090 for (i = 0; i < query_len; i++) {
2091 if (create_table_query[i]) {
2092 EM_SAFE_FREE(create_table_query[i]);
2095 EM_SAFE_FREE(create_table_query);
2098 EM_DEBUG_FUNC_END("err [%d]", err);
2102 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2104 EM_DEBUG_FUNC_BEGIN();
2106 int error = EMAIL_ERROR_NONE;
2107 int rc = -1, ret = false;
2109 char sql_query_string[QUERY_SIZE] = {0, };
2110 char **create_table_query = NULL;
2112 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2114 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2115 if (error != EMAIL_ERROR_NONE) {
2116 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2120 if (query_len < CREATE_TABLE_MAX) {
2121 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2122 error = EMAIL_ERROR_SYSTEM_FAILURE;
2126 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2129 char **result = NULL;
2131 /* 1. create mail_account_tbl */
2132 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2133 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2134 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2135 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2137 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2139 if (atoi(result[1]) < 1) {
2140 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2141 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2142 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2144 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2145 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2146 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2147 if (error != EMAIL_ERROR_NONE) {
2148 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2152 /* create mail_account_tbl unique index */
2153 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2154 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2155 if (error != EMAIL_ERROR_NONE) {
2156 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2160 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2161 } /* mail_account_tbl */
2162 else if (type == EMAIL_CREATE_DB_CHECK) {
2163 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2164 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)));
2167 sqlite3_free_table(result);
2170 /* 2. create mail_box_tbl */
2171 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2173 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2174 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2175 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2177 if (atoi(result[1]) < 1) {
2178 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2179 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2180 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2182 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2184 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2185 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2186 if (error != EMAIL_ERROR_NONE) {
2187 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2191 /* create mail_local_mailbox_tbl unique index */
2192 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2193 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2194 if (error != EMAIL_ERROR_NONE) {
2195 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2199 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2200 } /* mail_box_tbl */
2201 else if (type == EMAIL_CREATE_DB_CHECK) {
2202 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2203 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2204 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2206 sqlite3_free_table(result);
2209 /* 3. create mail_read_mail_uid_tbl */
2210 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2211 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2212 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2213 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2216 if (atoi(result[1]) < 1) {
2217 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2218 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2219 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2221 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2223 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2224 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2225 if (error != EMAIL_ERROR_NONE) {
2226 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2230 /* create mail_read_mail_uid_tbl unique index */
2231 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2232 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2233 if (error != EMAIL_ERROR_NONE) {
2234 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2238 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2239 } /* mail_read_mail_uid_tbl */
2240 else if (type == EMAIL_CREATE_DB_CHECK) {
2241 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2242 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2243 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2245 sqlite3_free_table(result);
2248 /* 4. create mail_rule_tbl */
2249 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2251 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2252 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2253 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2255 if (atoi(result[1]) < 1) {
2256 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2257 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2258 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2260 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2262 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2263 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2264 if (error != EMAIL_ERROR_NONE) {
2265 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2269 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2270 } /* mail_rule_tbl */
2271 else if (type == EMAIL_CREATE_DB_CHECK) {
2272 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2273 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2274 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2276 sqlite3_free_table(result);
2279 /* 5. create mail_tbl */
2280 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2281 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2282 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2283 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2285 if (atoi(result[1]) < 1) {
2286 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2287 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2288 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2289 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2291 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2292 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2293 if (error != EMAIL_ERROR_NONE) {
2294 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2298 /* create mail_tbl unique index */
2299 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2300 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2301 if (error != EMAIL_ERROR_NONE) {
2302 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2306 /* create mail_tbl index for date_time */
2307 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2308 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2309 if (error != EMAIL_ERROR_NONE) {
2310 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2314 /* create mail_tbl index for thread_item_count */
2315 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2316 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2317 if (error != EMAIL_ERROR_NONE) {
2318 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2322 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2323 /* just one time call */
2324 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2326 else if (type == EMAIL_CREATE_DB_CHECK) {
2327 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2328 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2329 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2331 sqlite3_free_table(result);
2334 /* 6. create mail_attachment_tbl */
2335 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2336 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2337 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2338 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2340 if (atoi(result[1]) < 1) {
2341 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2342 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2343 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2345 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2347 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2348 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2349 if (error != EMAIL_ERROR_NONE) {
2350 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2354 /* create mail_attachment_tbl unique index */
2355 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2356 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2357 if (error != EMAIL_ERROR_NONE) {
2358 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2362 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2363 } /* mail_attachment_tbl */
2364 else if (type == EMAIL_CREATE_DB_CHECK) {
2365 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2366 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2367 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2369 sqlite3_free_table(result);
2372 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2374 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2375 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2376 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2377 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2379 if (atoi(result[1]) < 1) {
2381 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2382 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2383 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2385 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2387 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2388 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2389 if (error != EMAIL_ERROR_NONE) {
2390 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2394 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2395 } /* mail_rule_tbl */
2396 else if (type == EMAIL_CREATE_DB_CHECK) {
2397 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2398 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2399 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2401 sqlite3_free_table(result);
2404 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2406 /* create mail_meeting_tbl */
2407 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2408 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2409 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2410 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2412 if (atoi(result[1]) < 1) {
2413 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2414 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2415 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2417 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2419 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2420 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2421 if (error != EMAIL_ERROR_NONE) {
2422 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2426 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2427 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2428 if (error != EMAIL_ERROR_NONE) {
2429 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2433 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2434 } /* mail_contact_sync_tbl */
2435 else if (type == EMAIL_CREATE_DB_CHECK) {
2436 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2437 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2438 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2440 sqlite3_free_table(result);
2443 #ifdef __FEATURE_LOCAL_ACTIVITY__
2445 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2446 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2447 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2448 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2450 if (atoi(result[1]) < 1) {
2452 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2453 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2454 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2456 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2458 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2459 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2460 if (error != EMAIL_ERROR_NONE) {
2461 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2465 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2466 } /* mail_rule_tbl */
2467 else if (type == EMAIL_CREATE_DB_CHECK) {
2468 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2469 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2470 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2471 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2473 sqlite3_free_table(result);
2475 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2476 /* create mail_certificate_tbl */
2477 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2478 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2479 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2480 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)));
2482 if (atoi(result[1]) < 1) {
2483 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2484 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2486 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2488 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2489 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2490 if (error != EMAIL_ERROR_NONE) {
2491 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2496 } /* mail_contact_sync_tbl */
2497 else if (type == EMAIL_CREATE_DB_CHECK) {
2498 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2499 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)));
2502 sqlite3_free_table(result);
2505 /* create mail_task_tbl */
2506 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2507 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2508 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)));
2510 if (atoi(result[1]) < 1) {
2511 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2512 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2514 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2516 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2517 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2518 if (error != EMAIL_ERROR_NONE) {
2519 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2523 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2524 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2525 if (error != EMAIL_ERROR_NONE) {
2526 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2530 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2531 } /* mail_task_tbl */
2532 else if (type == EMAIL_CREATE_DB_CHECK) {
2533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2534 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)));
2537 sqlite3_free_table(result);
2540 #ifdef __FEATURE_BODY_SEARCH__
2541 /* create mail_text_tbl */
2542 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2543 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2544 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)));
2546 if (atoi(result[1]) < 1) {
2547 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2548 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2550 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2552 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2553 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2554 if (error != EMAIL_ERROR_NONE) {
2555 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2559 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2560 } /* mail_text_tbl */
2561 else if (type == EMAIL_CREATE_DB_CHECK) {
2562 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2563 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)));
2566 sqlite3_free_table(result);
2571 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2572 /* create mail_auto_download_activity_tbl */
2573 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2574 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2575 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)));
2577 if (atoi(result[1]) < 1) {
2578 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2579 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2581 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2583 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2584 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2585 if (error != EMAIL_ERROR_NONE) {
2586 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2590 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2591 } /* mail_auto_download_activity_tbl */
2592 else if (type == EMAIL_CREATE_DB_CHECK) {
2593 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2594 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)));
2597 sqlite3_free_table(result);
2606 if (result) sqlite3_free_table(result);
2609 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2611 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2615 if (create_table_query) {
2617 for (i = 0; i < query_len; i++) {
2618 if (create_table_query[i]) {
2619 EM_SAFE_FREE(create_table_query[i]);
2622 EM_SAFE_FREE(create_table_query);
2625 if (err_code != NULL)
2628 EM_DEBUG_FUNC_END("ret [%d]", ret);
2632 /* Query series --------------------------------------------------------------*/
2633 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)
2635 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);
2638 int error = EMAIL_ERROR_NONE;
2639 DB_STMT hStmt = NULL;
2640 char *sql_query_string = NULL;
2642 sqlite3 *local_db_handle = NULL;
2644 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2645 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2646 return EMAIL_ERROR_INVALID_PARAM;
2649 query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2650 sql_query_string = em_malloc(query_size);
2651 if (sql_query_string == NULL) {
2652 EM_DEBUG_EXCEPTION("em_mallocfailed");
2653 error = EMAIL_ERROR_OUT_OF_MEMORY;
2657 local_db_handle = emstorage_get_db_connection(multi_user_name);
2659 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2661 SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2662 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2664 if (output_total_mail_count) {
2665 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2666 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2667 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2669 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2670 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2671 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2672 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2675 if (output_unseen_mail_count) {
2676 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2678 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2679 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2680 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2682 *output_unseen_mail_count = atoi(result[1]);
2683 sqlite3_free_table(result);
2688 if (hStmt != NULL) {
2689 rc = sqlite3_finalize(hStmt);
2690 if (rc != SQLITE_OK) {
2691 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2692 error = EMAIL_ERROR_DB_FAILURE;
2696 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2698 EM_SAFE_FREE(sql_query_string);
2700 EM_DEBUG_FUNC_END("error [%d]", error);
2704 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)
2706 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);
2714 int error = EMAIL_ERROR_NONE;
2715 int *result_mail_id_list = NULL;
2716 char **result = NULL;
2717 char *sql_query_string = NULL;
2718 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2720 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2721 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2722 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2724 query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10; // 10 is extra space
2725 sql_query_string = em_malloc(query_size);
2726 if (sql_query_string == NULL) {
2727 EM_DEBUG_EXCEPTION("em_mallocfailed");
2728 error = EMAIL_ERROR_OUT_OF_MEMORY;
2732 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2734 /* Composing query */
2735 SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2736 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2738 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2740 /* Performing query */
2741 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2742 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2743 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2747 /* to get mail list */
2749 EM_DEBUG_LOG("No mail found...");
2750 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2754 EM_DEBUG_LOG("There are [%d] mails.", count);
2756 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2757 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2758 error = EMAIL_ERROR_OUT_OF_MEMORY;
2762 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2764 for (i = 0; i < count; i++)
2765 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2767 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2769 *output_mail_id_list = result_mail_id_list;
2770 *output_mail_id_count = count;
2775 sqlite3_free_table(result);
2777 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2779 EM_SAFE_FREE(sql_query_string);
2781 if (error != EMAIL_ERROR_NONE)
2782 EM_SAFE_FREE(result_mail_id_list);
2784 EM_DEBUG_FUNC_END("error [%d]", error);
2788 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)
2790 EM_DEBUG_FUNC_BEGIN();
2791 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2793 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2794 int sql_query_string_length = 0;
2795 int local_inline_content_count = 0, local_attachment_count = 0;
2796 int cur_query = 0, base_count = 0, col_index;
2797 int ret = false, error = EMAIL_ERROR_NONE;
2798 char *date_time_string = NULL;
2799 char **result = NULL;
2800 char *field_mail_id = "mail_id";
2801 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 ";
2802 char *select_query_form = "SELECT %s FROM mail_tbl ";
2803 char *target_field = NULL;
2804 char *sql_query_string = NULL;
2805 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2806 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2808 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2809 EM_IF_NULL_RETURN_VALUE(result_count, false);
2811 EMSTORAGE_START_READ_TRANSACTION(transaction);
2814 if (to_get_count) /* count only */
2815 target_field = field_mail_id;
2816 else /* mail list in plain form */
2817 target_field = field_all;
2819 sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2821 if (sql_query_string_length)
2822 sql_query_string = em_malloc(sql_query_string_length);
2824 if (sql_query_string == NULL) {
2825 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2826 error = EMAIL_ERROR_OUT_OF_MEMORY;
2830 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2832 strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2834 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2836 /* performing query */
2837 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2838 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2839 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2840 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2841 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2847 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2851 col_index = base_count;
2853 EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2858 EM_DEBUG_LOG_DEV("No mail found...");
2860 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2863 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2865 /* to get mail list */
2867 EM_DEBUG_LOG_DEV("No mail found...");
2869 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2873 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2874 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2875 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2876 error = EMAIL_ERROR_OUT_OF_MEMORY;
2880 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2881 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2882 for (i = 0; i < count; i++) {
2883 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2884 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2885 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2886 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2887 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2888 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2889 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2890 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2891 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2892 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2893 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2894 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2895 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2896 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2897 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2898 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2899 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2900 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2901 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2902 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2903 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2904 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2905 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2906 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2907 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2908 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2909 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2910 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2911 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2912 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2913 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2914 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2915 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2916 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2917 _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++);
2919 mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2921 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2922 EM_PROFILE_END(emstorage_query_mail_list_loop);
2928 EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2931 sqlite3_free_table(result);
2934 *result_count = count;
2937 if (result_mail_list)
2938 *result_mail_list = mail_list_item_from_tbl;
2939 *result_count = count;
2941 EM_SAFE_FREE(mail_list_item_from_tbl);
2944 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2946 // sqlite3_db_release_memory(local_db_handle);
2948 EM_SAFE_FREE(sql_query_string);
2949 EM_SAFE_FREE(date_time_string);
2951 if (err_code != NULL)
2954 EM_PROFILE_END(emstorage_query_mail_list_func);
2955 EM_DEBUG_FUNC_END("ret [%d]", ret);
2960 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)
2962 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);
2964 if (!conditional_clause || !result_mail_tbl || !result_count) {
2965 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
2966 if (err_code != NULL)
2967 *err_code = EMAIL_ERROR_INVALID_PARAM;
2971 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2972 int error = EMAIL_ERROR_NONE;
2973 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2974 emstorage_mail_tbl_t* p_data_tbl = NULL;
2975 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2977 EMSTORAGE_START_READ_TRANSACTION(transaction);
2979 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2981 EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
2983 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2984 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2985 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2988 EM_DEBUG_LOG("No mail found...");
2990 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2994 EM_DEBUG_LOG("There are [%d] mails.", count);
2995 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2996 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2997 error = EMAIL_ERROR_OUT_OF_MEMORY;
3001 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3002 for (i = 0; i < count; i++) {
3003 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3004 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3005 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3006 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3007 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3008 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3009 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3010 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3011 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3012 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3013 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3014 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3015 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3016 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3017 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3018 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3019 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3020 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3021 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3022 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3023 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3024 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3025 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3026 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3027 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3028 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3029 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3030 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3031 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3032 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3033 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3034 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3035 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3036 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3037 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3038 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3039 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3040 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3041 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3042 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3043 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3044 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3045 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3046 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3047 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3048 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3049 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3050 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3051 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3052 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3053 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3054 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3055 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3056 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3057 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3058 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3065 sqlite3_free_table(result);
3068 *result_mail_tbl = p_data_tbl;
3069 *result_count = count;
3071 *result_mail_tbl = NULL;
3075 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3077 // sqlite3_db_release_memory(local_db_handle);
3079 if (err_code != NULL)
3082 EM_DEBUG_FUNC_END("ret [%d]", ret);
3086 #ifdef __FEATURE_BODY_SEARCH__
3087 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)
3089 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);
3091 if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3092 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3093 if (err_code != NULL)
3094 *err_code = EMAIL_ERROR_INVALID_PARAM;
3102 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3103 int error = EMAIL_ERROR_NONE;
3104 char **result = NULL;
3105 char sql_query_string[QUERY_SIZE] = {0, };
3106 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3107 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3109 EMSTORAGE_START_READ_TRANSACTION(transaction);
3111 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3113 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3115 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3116 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3117 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3120 EM_DEBUG_LOG("No mail found...");
3122 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3126 EM_DEBUG_LOG("There are [%d] mails.", count);
3127 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3128 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3129 error = EMAIL_ERROR_OUT_OF_MEMORY;
3133 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3134 for (i = 0; i < count; i++) {
3135 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3136 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3137 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3138 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3145 sqlite3_free_table(result);
3148 *result_mail_text_tbl = p_data_tbl;
3149 *result_count = count;
3151 EM_SAFE_FREE(p_data_tbl);
3153 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3155 // sqlite3_db_release_memory(local_db_handle);
3157 if (err_code != NULL)
3160 EM_DEBUG_FUNC_END("ret [%d]", ret);
3165 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)
3167 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);
3173 int error = EMAIL_ERROR_NONE;
3175 int total_count = 0;
3177 char sql_query_string[QUERY_SIZE] = {0, };
3178 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 ";
3179 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3180 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3182 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3184 if (input_get_mail_count == 0) { /* without mail count */
3186 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);
3187 } else { /* with read count and total count */
3189 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);
3192 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3194 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3195 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3196 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3198 EM_DEBUG_LOG_DEV("result count [%d]", count);
3201 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3202 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3206 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3207 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3208 error = EMAIL_ERROR_OUT_OF_MEMORY;
3212 for (i = 0; i < count; i++) {
3213 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3214 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3215 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3216 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3217 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3218 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3219 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3220 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3221 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3222 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3223 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3224 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3225 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3226 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3227 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3229 if (input_get_mail_count == 1) {
3230 _get_table_field_data_int(result, &(total_count), col_index++);
3231 p_data_tbl[i].total_mail_count_on_local = total_count;
3232 _get_table_field_data_int(result, &(read_count), col_index++);
3233 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
3240 sqlite3_free_table(result);
3242 if (error == EMAIL_ERROR_NONE) {
3243 *output_mailbox_list = p_data_tbl;
3244 *output_mailbox_count = count;
3245 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3248 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3250 // sqlite3_db_release_memory(local_db_handle);
3252 EM_DEBUG_FUNC_END("error [%d]", error);
3256 /* Query series --------------------------------------------------------------*/
3257 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3259 EM_DEBUG_FUNC_BEGIN();
3260 int rc = -1, ret = false;
3261 int error = EMAIL_ERROR_NONE;
3264 char sql_query_string[QUERY_SIZE] = {0, };
3265 sqlite3 *local_db_handle = NULL;
3267 if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3268 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3269 if (err_code != NULL)
3270 *err_code = EMAIL_ERROR_INVALID_PARAM;
3274 local_db_handle = emstorage_get_db_connection(multi_user_name);
3276 EMSTORAGE_START_READ_TRANSACTION(transaction);
3278 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3279 "SELECT COUNT(*) FROM mail_account_tbl "
3281 " user_email_address = '%s' AND "
3282 " incoming_server_user_name = '%s' AND "
3283 " incoming_server_type = %d AND "
3284 " incoming_server_address = '%s' AND "
3285 " outgoing_server_user_name = '%s' AND "
3286 " outgoing_server_type = %d AND "
3287 " outgoing_server_address = '%s'; ",
3288 account->user_email_address,
3289 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3290 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3292 EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3293 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3294 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3295 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3297 count = atoi(result[1]);
3298 sqlite3_free_table(result);
3300 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3302 if (count == 0) { /* not duplicated account */
3304 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3305 } else { /* duplicated account */
3307 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3308 error = EMAIL_ERROR_ALREADY_EXISTS;
3313 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3315 if (err_code != NULL)
3318 EM_DEBUG_FUNC_END("ret [%d]", ret);
3323 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3325 EM_DEBUG_FUNC_BEGIN();
3328 if (err_code != NULL)
3329 *err_code = EMAIL_ERROR_INVALID_PARAM;
3333 int rc = -1, ret = false;
3334 int error = EMAIL_ERROR_NONE;
3336 DB_STMT hStmt = NULL;
3337 char sql_query_string[QUERY_SIZE] = {0, };
3340 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3341 EMSTORAGE_START_READ_TRANSACTION(transaction);
3342 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3343 EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3344 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3345 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3346 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3347 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3348 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3350 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3351 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3352 ("sqlite3_step fail:%d", rc));
3354 *count = sqlite3_column_int(hStmt, 0);
3360 if (hStmt != NULL) {
3361 rc = sqlite3_finalize(hStmt);
3363 if (rc != SQLITE_OK) {
3364 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3365 error = EMAIL_ERROR_DB_FAILURE;
3369 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3371 if (err_code != NULL)
3374 EM_DEBUG_FUNC_END("ret [%d]", ret);
3378 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)
3380 EM_DEBUG_FUNC_BEGIN();
3382 int i = 0, count = 0, rc = -1, ret = false;
3383 int field_index = 0;
3385 int error = EMAIL_ERROR_NONE;
3386 emstorage_account_tbl_t *p_data_tbl = NULL;
3387 char sql_query_string[QUERY_SIZE] = {0, };
3388 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3390 sqlite3 *local_db_handle = NULL;
3391 DB_STMT hStmt = NULL;
3393 if (!select_num || !account_list) {
3394 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3395 if (err_code != NULL)
3396 *err_code = EMAIL_ERROR_INVALID_PARAM;
3400 local_db_handle = emstorage_get_db_connection(multi_user_name);
3402 EMSTORAGE_START_READ_TRANSACTION(transaction);
3404 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3405 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3406 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3408 count = atoi(result[1]);
3409 sqlite3_free_table(result);
3411 EM_DEBUG_LOG_DEV("count = %d", rc);
3414 EM_DEBUG_LOG("no account found...");
3415 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3419 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3420 sql_len = EM_SAFE_STRLEN(sql_query_string);
3422 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3427 "user_data_length, "
3431 "default_mail_slot_size, "
3434 "user_display_name, "
3435 "user_email_address, "
3436 "reply_to_address, "
3438 "incoming_server_type, "
3439 "incoming_server_address, "
3440 "incoming_server_port_number, "
3441 "incoming_server_user_name, "
3442 "incoming_server_password, "
3443 "incoming_server_secure_connection, "
3444 "incoming_server_authentication_method, "
3446 "keep_mails_on_pop_server_after_download, "
3448 "auto_download_size, "
3453 "outgoing_server_type, "
3454 "outgoing_server_address, "
3455 "outgoing_server_port_number, "
3456 "outgoing_server_user_name, "
3457 "outgoing_server_password, "
3458 "outgoing_server_secure_connection, "
3459 "outgoing_server_need_authentication, "
3460 "outgoing_server_use_same_authenticator, "
3463 "req_delivery_receipt, "
3464 "req_read_receipt, "
3468 "display_name_from, "
3470 "forward_with_files, "
3474 "add_my_address_to_bcc, "
3475 "notification_status, "
3477 "display_content_status, "
3478 "default_ringtone_status, "
3479 "alert_ringtone_path, "
3480 "auto_resend_times, "
3481 "outgoing_server_size_limit, "
3482 "wifi_auto_download, "
3484 "incoming_server_requires_apop, "
3486 "certificate_path, "
3491 sql_len = EM_SAFE_STRLEN(sql_query_string);
3493 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3497 EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3498 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3499 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3501 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3502 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3503 ("sqlite3_step fail:%d", rc));
3504 if (rc == SQLITE_DONE) {
3505 EM_DEBUG_EXCEPTION("no account found...");
3507 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3513 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3514 EM_DEBUG_EXCEPTION("malloc failed...");
3515 error = EMAIL_ERROR_OUT_OF_MEMORY;
3518 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3519 for (i = 0; i < count; i++) {
3522 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3523 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3524 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3525 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3526 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3527 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3528 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3529 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3530 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3531 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3532 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3533 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3534 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3535 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3536 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3537 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3538 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3539 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3540 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3541 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3542 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3543 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3544 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3545 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3546 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3547 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3548 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3549 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3550 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3551 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3552 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3553 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3554 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3555 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3556 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3557 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3558 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3559 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3560 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3561 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3562 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3563 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3564 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3565 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3566 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3567 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3568 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3569 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3570 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3571 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3572 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3573 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3574 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3575 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3576 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3577 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3578 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3579 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3580 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3581 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3582 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3583 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3584 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3585 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3586 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3587 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3589 /* EAS passwd is not accessible */
3590 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3591 /* get password from the secure storage */
3592 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3593 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3595 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3596 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3598 /* get password file name */
3599 error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3600 recv_password_file_name,
3601 send_password_file_name);
3602 if (error != EMAIL_ERROR_NONE) {
3603 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3604 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3608 /* read password from secure storage */
3609 error = _read_password_from_secure_storage(recv_password_file_name,
3610 &(p_data_tbl[i].incoming_server_password));
3612 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3613 recv_password_file_name, error);
3614 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3617 error = _read_password_from_secure_storage(send_password_file_name,
3618 &(p_data_tbl[i].outgoing_server_password));
3620 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3621 send_password_file_name, error);
3622 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3627 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3628 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3629 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3630 ("sqlite3_step fail:%d", rc));
3637 *account_list = p_data_tbl;
3638 *select_num = count;
3639 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3640 } else if (p_data_tbl != NULL)
3641 emstorage_free_account(&p_data_tbl, count, NULL);
3643 if (hStmt != NULL) {
3644 rc = sqlite3_finalize(hStmt);
3646 if (rc != SQLITE_OK) {
3647 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3648 error = EMAIL_ERROR_DB_FAILURE;
3652 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3654 if (err_code != NULL)
3657 EM_DEBUG_FUNC_END("ret [%d]", ret);
3661 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3662 char *server_mail_id,
3664 emstorage_mail_tbl_t **mail,
3668 EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3669 mailbox_id, server_mail_id, mail, transaction, err_code);
3671 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3672 char conditional_clause[QUERY_SIZE] = {0, };
3673 emstorage_mail_tbl_t* p_data_tbl = NULL;
3675 if (!server_mail_id || !mail) {
3676 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3677 error = EMAIL_ERROR_INVALID_PARAM;
3681 sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3683 if (mailbox_id > 0) {
3684 SNPRINTF(conditional_clause + strlen(conditional_clause),
3685 QUERY_SIZE - strlen(conditional_clause),
3686 " AND mailbox_id = %d",
3690 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3691 if (!emstorage_query_mail_tbl(multi_user_name,
3697 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3708 if (err_code != NULL)
3711 EM_DEBUG_FUNC_END("ret [%d]", ret);
3716 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)
3718 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3720 if (account_id < ALL_ACCOUNT) {
3721 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3722 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3726 if (account_id != ALL_ACCOUNT) {
3727 cur_clause += (conditional_clause_count++ == 0) ?
3728 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3729 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3732 if (mailbox_id > 0) {
3733 cur_clause += (conditional_clause_count++ == 0) ?
3734 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3735 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3738 if (thread_id > 0) {
3739 cur_clause += (conditional_clause_count++ == 0) ?
3740 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3741 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3742 } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3743 cur_clause += (conditional_clause_count++ == 0) ?
3744 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3745 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3746 } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3747 cur_clause += (conditional_clause_count++ == 0) ?
3748 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3749 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3750 } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3751 cur_clause += (conditional_clause_count++ == 0) ?
3752 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3753 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3756 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3757 if (addr_list && addr_list->address_count > 0) {
3758 if (!addr_list->address_type) {
3759 cur_clause += (conditional_clause_count++ == 0) ?
3760 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3761 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3763 for (i = 1; i < addr_list->address_count; i++)
3764 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3766 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3768 cur_clause += (conditional_clause_count++ == 0) ?
3769 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]) :
3770 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]);
3772 for (i = 1; i < addr_list->address_count; i++)
3773 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3775 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3779 if (input_except_delete_flagged_mails) {
3780 cur_clause += (conditional_clause_count++ == 0) ?
3781 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3782 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3786 switch (search_type) {
3787 case EMAIL_SEARCH_FILTER_SUBJECT:
3788 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3789 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3791 case EMAIL_SEARCH_FILTER_SENDER:
3792 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3793 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3794 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3796 case EMAIL_SEARCH_FILTER_RECIPIENT:
3797 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3798 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3799 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3800 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3801 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3803 case EMAIL_SEARCH_FILTER_ALL:
3804 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3805 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3806 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3807 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3808 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3809 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3812 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3817 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3818 static char sorting_str[][50] = {
3819 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3820 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3821 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3822 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3823 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3824 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3825 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3826 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3827 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3828 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3829 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3830 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3831 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3832 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3833 " ORDER BY mailbox_id DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3834 " ORDER BY mailbox_id ASC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3835 " ORDER BY flags_flagged_field DESC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3836 " ORDER BY flags_flagged_field ASC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3837 " ORDER BY flags_seen_field DESC, date_time DESC", /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3838 " ORDER BY flags_seen_field ASC, date_time DESC" /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3841 if (sorting < EMAIL_SORT_END && sorting >= 0)
3842 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3844 EM_DEBUG_LOG(" Invalid Sorting order ");
3847 if (start_index != -1 && limit_count != -1)
3848 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3855 * emstorage_get_mail_list - Get the mail list information.
3859 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)
3861 EM_DEBUG_FUNC_BEGIN();
3862 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3864 int ret = false, error = EMAIL_ERROR_NONE;
3865 char conditional_clause_string[QUERY_SIZE] = { 0, };
3867 if (account_id < ALL_ACCOUNT) {
3868 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3869 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3871 EM_IF_NULL_RETURN_VALUE(result_count, false);
3873 _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);
3875 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3877 if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3878 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3885 if (err_code != NULL)
3888 EM_PROFILE_END(emstorage_get_mail_list_func);
3889 EM_DEBUG_FUNC_END("ret [%d]", ret);
3895 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3899 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)
3901 EM_DEBUG_FUNC_BEGIN();
3902 EM_PROFILE_BEGIN(emStorageGetMails);
3904 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3905 emstorage_mail_tbl_t *p_data_tbl = NULL;
3906 char conditional_clause_string[QUERY_SIZE] = {0, };
3908 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3909 EM_IF_NULL_RETURN_VALUE(result_count, false);
3911 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3912 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3913 if (err_code != NULL)
3914 *err_code = EMAIL_ERROR_INVALID_PARAM;
3918 _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);
3920 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3922 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
3923 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3932 *mail_list = p_data_tbl;
3933 *result_count = count;
3934 EM_DEBUG_LOG("COUNT : %d", count);
3935 } else if (p_data_tbl != NULL)
3936 emstorage_free_mail(&p_data_tbl, count, NULL);
3938 if (err_code != NULL)
3941 EM_PROFILE_END(emStorageGetMails);
3942 EM_DEBUG_FUNC_END("ret [%d]", ret);
3949 * emstorage_get_searched_mail_list - Get the mail list information after filtering
3953 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)
3955 EM_DEBUG_FUNC_BEGIN();
3957 int ret = false, error = EMAIL_ERROR_NONE;
3958 char conditional_clause[QUERY_SIZE] = {0, };
3959 char *temp_search_value = NULL;
3960 char *temp_search_value2 = NULL;
3962 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3963 EM_IF_NULL_RETURN_VALUE(result_count, false);
3965 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3966 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3967 error = EMAIL_ERROR_INVALID_PARAM;
3971 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
3972 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
3974 _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);
3976 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3978 if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
3979 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3986 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3988 if (err_code != NULL)
3991 EM_SAFE_FREE(temp_search_value);
3992 EM_SAFE_FREE(temp_search_value2);
3994 EM_DEBUG_FUNC_END("ret [%d]", ret);
3999 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4001 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4003 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4004 EM_DEBUG_EXCEPTION("Invalid parameter");
4005 return EMAIL_ERROR_INVALID_PARAM;
4008 EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4010 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4011 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4012 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4014 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4015 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4018 EM_DEBUG_FUNC_END();
4019 return EMAIL_ERROR_NONE;
4022 static int _read_password_from_secure_storage(char *file_name, char **password)
4024 EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4026 if (!file_name || !password) {
4027 EM_DEBUG_EXCEPTION("Invalid Parameter");
4028 return EMAIL_ERROR_INVALID_PARAM;
4031 int ret = EMAIL_ERROR_NONE;
4033 ret = emcore_get_password_in_key_manager(file_name, password);
4034 if (ret != EMAIL_ERROR_NONE) {
4035 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4041 EM_DEBUG_FUNC_END("ret [%d]", ret);
4045 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)
4047 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4050 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4051 if (err_code != NULL)
4052 *err_code = EMAIL_ERROR_INVALID_PARAM;
4057 int error = EMAIL_ERROR_NONE;
4058 emstorage_account_tbl_t* p_data_tbl = NULL;
4059 DB_STMT hStmt = NULL;
4060 char sql_query_string[QUERY_SIZE] = {0, };
4063 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4064 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4066 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4068 EMSTORAGE_START_READ_TRANSACTION(transaction);
4070 /* Make query string */
4071 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4072 sql_len = EM_SAFE_STRLEN(sql_query_string);
4074 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4075 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4076 "incoming_server_type,"
4077 "incoming_server_address,"
4078 "user_email_address,"
4079 "incoming_server_user_name,"
4081 "incoming_server_port_number,"
4082 "incoming_server_secure_connection,"
4083 "incoming_server_authentication_method,"
4084 "outgoing_server_type,"
4085 "outgoing_server_address,"
4086 "outgoing_server_port_number,"
4087 "outgoing_server_need_authentication,"
4088 "outgoing_server_secure_connection,"
4089 "outgoing_server_user_name,"
4090 "user_display_name,"
4094 "keep_mails_on_pop_server_after_download,"
4095 "auto_download_size,"
4100 "outgoing_server_use_same_authenticator,"
4101 "auto_resend_times,"
4102 "outgoing_server_size_limit,"
4103 "wifi_auto_download,"
4105 "incoming_server_requires_apop,"
4112 sql_len = EM_SAFE_STRLEN(sql_query_string);
4115 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4116 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4117 sql_len = EM_SAFE_STRLEN(sql_query_string);
4120 /* get from secure storage, not from db */
4121 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4122 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4125 "req_delivery_receipt,"
4130 "display_name_from,"
4132 "forward_with_files,"
4136 "add_my_address_to_bcc,"
4137 "notification_status,"
4139 "display_content_status,"
4140 "default_ringtone_status,"
4141 "alert_ringtone_path,"
4144 "default_mail_slot_size,"
4151 sql_len = EM_SAFE_STRLEN(sql_query_string);
4154 /* dummy value, FROM WHERE clause */
4155 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4158 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4160 /* execute a sql and count rows */
4161 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4162 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4163 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4165 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4166 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4167 ("sqlite3_step fail:%d", rc));
4169 if (rc == SQLITE_DONE) {
4170 EM_DEBUG_EXCEPTION("no matched account found...");
4171 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4175 /* Assign query result to structure */
4176 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4177 EM_DEBUG_EXCEPTION("malloc failed...");
4178 error = EMAIL_ERROR_OUT_OF_MEMORY;
4182 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4185 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4186 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4187 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
4188 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4189 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4190 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4191 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4192 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4193 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4194 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4195 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4196 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4197 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4198 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4199 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4200 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4201 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4202 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4203 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4204 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4205 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4206 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4207 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4208 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4209 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4210 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4211 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4212 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4213 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4214 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4215 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4216 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4217 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4218 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4219 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4220 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4221 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4224 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4225 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4227 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4228 /* get password file name */
4229 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) {
4230 EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4234 /* read password from secure storage */
4235 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4236 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4240 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
4242 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4243 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4244 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4247 EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
4251 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4252 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4253 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4254 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4255 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4256 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4257 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4258 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4259 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4260 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4261 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4262 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4263 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4264 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4265 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4266 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4267 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4268 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4269 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4270 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4271 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4272 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4273 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4274 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4275 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4276 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4277 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4278 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4285 *account = p_data_tbl;
4288 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4290 if (hStmt != NULL) {
4291 rc = sqlite3_finalize(hStmt);
4292 if (rc != SQLITE_OK) {
4293 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4294 error = EMAIL_ERROR_DB_FAILURE;
4298 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4300 if (err_code != NULL)
4303 EM_DEBUG_FUNC_END("ret [%d]", ret);
4307 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)
4309 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4311 if (account_id <= 0 || password_length == NULL) {
4312 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4313 if (err_code != NULL)
4314 *err_code = EMAIL_ERROR_INVALID_PARAM;
4319 int error = EMAIL_ERROR_NONE;
4320 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4321 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4322 char *temp_password = NULL;
4325 /* get password file name */
4326 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4327 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4331 /* read password from secure storage */
4332 if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4333 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4334 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4337 } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4338 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4339 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4343 EM_DEBUG_LOG("Invalid password type");
4344 if (err_code != NULL)
4345 *err_code = EMAIL_ERROR_INVALID_PARAM;
4349 *password_length = EM_SAFE_STRLEN(temp_password);
4351 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
4356 EM_SAFE_FREE(temp_password);
4358 if (err_code != NULL)
4361 EM_DEBUG_FUNC_END("ret [%d]", ret);
4365 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)
4367 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);
4369 int err = EMAIL_ERROR_NONE;
4370 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4371 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4373 if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4374 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4375 err = EMAIL_ERROR_INVALID_PARAM;
4379 /* get password file name */
4380 if ((err = _get_password_file_name(multi_user_name,
4382 recv_password_file_name,
4383 send_password_file_name)) != EMAIL_ERROR_NONE) {
4384 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4388 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4389 recv_password_file_name, input_incoming_server_password);
4390 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4391 send_password_file_name, input_outgoing_server_password);
4393 if (input_incoming_server_password) {
4394 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4395 if (err != EMAIL_ERROR_NONE) {
4396 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4400 /* save recv passwords to the secure storage */
4401 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4402 if (err != EMAIL_ERROR_NONE) {
4403 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4408 if (input_outgoing_server_password) {
4409 err = emcore_remove_password_in_key_manager(send_password_file_name);
4410 if (err != EMAIL_ERROR_NONE) {
4411 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4415 /* save send passwords to the secure storage */
4416 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4417 if (err != EMAIL_ERROR_NONE) {
4418 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4424 EM_DEBUG_FUNC_END("err [%d]", err);
4428 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4430 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4432 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4433 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4434 if (err_code != NULL)
4435 *err_code = EMAIL_ERROR_INVALID_PARAM;
4439 int error = EMAIL_ERROR_NONE;
4440 int rc, ret = false;
4442 DB_STMT hStmt = NULL;
4443 char sql_query_string[QUERY_SIZE] = {0, };
4445 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4447 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4449 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4450 "UPDATE mail_account_tbl SET"
4452 ", logo_icon_path = ?"
4454 ", user_data_length = ?"
4455 ", account_svc_id = ?"
4457 ", sync_disabled = ?"
4458 ", default_mail_slot_size = ?"
4459 ", roaming_option = ?"
4461 ", user_display_name = ?"
4462 ", user_email_address = ?"
4463 ", reply_to_address = ?"
4464 ", return_address = ?"
4465 ", incoming_server_type = ?"
4466 ", incoming_server_address = ?"
4467 ", incoming_server_port_number = ?"
4468 ", incoming_server_user_name = ?"
4469 ", incoming_server_secure_connection = ?"
4470 ", incoming_server_authentication_method = ?"
4471 ", retrieval_mode = ?"
4472 ", keep_mails_on_pop_server_after_download = ?"
4473 ", check_interval = ?"
4474 ", auto_download_size = ?"
4475 ", peak_interval = ?"
4477 ", peak_start_time = ?"
4478 ", peak_end_time = ?"
4479 ", outgoing_server_type = ?"
4480 ", outgoing_server_address = ?"
4481 ", outgoing_server_port_number = ?"
4482 ", outgoing_server_user_name = ?"
4483 ", outgoing_server_secure_connection = ?"
4484 ", outgoing_server_need_authentication = ?"
4485 ", outgoing_server_use_same_authenticator = ?"
4487 ", keep_local_copy = ?"
4488 ", req_delivery_receipt = ?"
4489 ", req_read_receipt = ?"
4490 ", download_limit = ?"
4491 ", block_address = ?"
4492 ", block_subject = ?"
4493 ", display_name_from = ?"
4494 ", reply_with_body = ?"
4495 ", forward_with_files = ?"
4496 ", add_myname_card = ?"
4497 ", add_signature = ?"
4499 ", add_my_address_to_bcc = ?"
4500 ", notification_status = ?"
4501 ", vibrate_status = ?"
4502 ", display_content_status = ?"
4503 ", default_ringtone_status = ?"
4504 ", alert_ringtone_path = ?"
4505 ", auto_resend_times = ?"
4506 ", outgoing_server_size_limit = ?"
4507 ", wifi_auto_download = ?"
4508 ", pop_before_smtp = ?"
4509 ", incoming_server_requires_apop = ?"
4511 ", certificate_path = ?"
4515 " WHERE account_id = ?");
4517 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4518 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4519 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4520 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4521 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4525 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4526 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4527 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4528 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4529 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4530 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4531 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4532 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4533 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4534 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4535 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4536 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4537 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4538 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4539 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4540 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4541 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4542 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4543 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4544 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4545 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4546 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4547 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4548 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4549 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4550 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4551 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4552 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4553 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4554 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4555 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4556 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4557 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4558 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4559 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4560 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4561 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4562 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4563 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4564 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4565 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4566 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4567 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4568 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4569 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4570 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4571 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4572 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4573 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4574 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4575 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4576 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4577 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4578 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4579 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4580 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4581 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4582 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4583 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4584 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4585 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4586 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4587 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4588 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4589 _bind_stmt_field_data_int(hStmt, i++, account_id);
4591 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4592 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4593 ("sqlite3_step fail:%d", rc));
4594 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4595 ("sqlite3_step fail:%d", rc));
4597 /* validate account existence */
4598 rc = sqlite3_changes(local_db_handle);
4600 EM_DEBUG_EXCEPTION("no matched account found...");
4601 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4605 if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4606 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4607 EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4615 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4617 if (hStmt != NULL) {
4618 rc = sqlite3_finalize(hStmt);
4619 if (rc != SQLITE_OK) {
4620 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4621 error = EMAIL_ERROR_DB_FAILURE;
4625 if (error == EMAIL_ERROR_NONE) {
4626 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4627 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4630 if (err_code != NULL)
4633 EM_DEBUG_FUNC_END("ret [%d]", ret);
4637 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)
4639 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4640 int error = EMAIL_ERROR_NONE;
4642 char sql_query_string[QUERY_SIZE] = {0, };
4643 sqlite3 *local_db_handle = NULL;
4645 if (!account_id || !field_name) {
4646 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4647 return EMAIL_ERROR_INVALID_PARAM;
4650 local_db_handle = emstorage_get_db_connection(multi_user_name);
4652 /* Write query string */
4653 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4655 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4658 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4660 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4661 if (error != EMAIL_ERROR_NONE) {
4662 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4666 if (sqlite3_changes(local_db_handle) == 0)
4667 EM_DEBUG_LOG("no mail matched...");
4671 result = (error == EMAIL_ERROR_NONE) ? true : false;
4672 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4674 if (error == EMAIL_ERROR_NONE) {
4675 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4676 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4678 EM_DEBUG_FUNC_END("error [%d]", error);
4682 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4684 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4686 if (!result_sync_status) {
4687 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4688 if (err_code != NULL)
4689 *err_code = EMAIL_ERROR_INVALID_PARAM;
4693 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4694 char sql_query_string[QUERY_SIZE] = {0, };
4695 char **result = NULL;
4696 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4699 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4701 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4703 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4704 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4705 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4708 EM_DEBUG_EXCEPTION("no matched account found...");
4709 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4714 *result_sync_status = 0;
4716 for (i = 0; i < count; i++) {
4717 _get_table_field_data_int(result, &sync_status, col_index++);
4718 *result_sync_status |= sync_status;
4721 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4723 sqlite3_free_table(result);
4730 if (err_code != NULL)
4733 EM_DEBUG_FUNC_END("ret [%d]", ret);
4737 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)
4739 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);
4741 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4742 char sql_query_string[QUERY_SIZE] = {0, };
4743 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4745 if (set_operator != SET_TYPE_SET && account_id) {
4746 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4747 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4748 if (err_code != NULL)
4752 switch (set_operator) {
4753 case SET_TYPE_UNION:
4754 set_value = result_sync_status | set_value;
4756 case SET_TYPE_MINUS:
4757 set_value = result_sync_status & (~set_value);
4760 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4761 error = EMAIL_ERROR_NOT_SUPPORTED;
4764 EM_DEBUG_LOG("set_value [%d]", set_value);
4767 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4770 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4772 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4774 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4776 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4777 if (error != EMAIL_ERROR_NONE) {
4778 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4782 rc = sqlite3_changes(local_db_handle);
4785 EM_DEBUG_EXCEPTION("no matched account found...");
4786 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4793 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4795 if (error == EMAIL_ERROR_NONE) {
4796 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4797 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4800 if (err_code != NULL)
4803 EM_DEBUG_FUNC_END("ret [%d]", ret);
4807 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4809 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4812 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4813 if (err_code != NULL)
4814 *err_code = EMAIL_ERROR_INVALID_PARAM;
4820 int rc = -1, ret = false;
4821 int error = EMAIL_ERROR_NONE;
4822 DB_STMT hStmt = NULL;
4823 char sql_query_string[QUERY_SIZE] = {0, };
4824 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4825 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4827 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4829 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4831 char *sql = "SELECT rowid FROM mail_account_tbl;";
4832 char **result = NULL;
4834 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4835 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4836 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4838 if (NULL == result[1]) rc = 1;
4840 for (i = 1; i <= row_count; i++) {
4841 if (i != atoi(result[i])) {
4848 sqlite3_free_table(result);
4851 if (rc < 0 && rc > EMAIL_ACCOUNT_MAX) {
4852 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4853 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4857 account_tbl->account_id = rc;
4859 if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4860 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4864 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4865 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4866 "INSERT INTO mail_account_tbl VALUES "
4868 " ? " /* account_id */
4869 " , ? " /* account_name */
4870 " , ? " /* logo_icon_path */
4871 " , ? " /* user_data */
4872 " , ? " /* user_data_length */
4873 " , ? " /* account_svc_id */
4874 " , ? " /* sync_status */
4875 " , ? " /* sync_disabled */
4876 " , ? " /* default_mail_slot_size */
4877 " , ? " /* roaming_option */
4878 " , ? " /* color_label */
4879 " , ? " /* user_display_name */
4880 " , ? " /* user_email_address */
4881 " , ? " /* reply_to_address */
4882 " , ? " /* return_address */
4883 " , ? " /* incoming_server_type */
4884 " , ? " /* incoming_server_address */
4885 " , ? " /* incoming_server_port_number */
4886 " , ? " /* incoming_server_user_name */
4887 " , ? " /* incoming_server_password */
4888 " , ? " /* incoming_server_secure_connection */
4889 " , ? " /* incoming_server_authentication_method */
4890 " , ? " /* retrieval_mode */
4891 " , ? " /* keep_mails_on_pop_server_after_download */
4892 " , ? " /* check_interval */
4893 " , ? " /* auto_download_size */
4894 " , ? " /* peak_interval */
4895 " , ? " /* peak_days */
4896 " , ? " /* peak_start_time */
4897 " , ? " /* peak_end_time */
4898 " , ? " /* outgoing_server_type */
4899 " , ? " /* outgoing_server_address */
4900 " , ? " /* outgoing_server_port_number */
4901 " , ? " /* outgoing_server_user_name */
4902 " , ? " /* outgoing_server_password */
4903 " , ? " /* outgoing_server_secure_connection */
4904 " , ? " /* outgoing_server_need_authentication */
4905 " , ? " /* outgoing_server_use_same_authenticator */
4906 " , ? " /* priority */
4907 " , ? " /* keep_local_copy */
4908 " , ? " /* req_delivery_receipt */
4909 " , ? " /* req_read_receipt */
4910 " , ? " /* download_limit */
4911 " , ? " /* block_address */
4912 " , ? " /* block_subject */
4913 " , ? " /* display_name_from */
4914 " , ? " /* reply_with_body */
4915 " , ? " /* forward_with_files */
4916 " , ? " /* add_myname_card */
4917 " , ? " /* add_signature */
4918 " , ? " /* signature */
4919 " , ? " /* add_my_address_to_bcc */
4920 " , ? " /* auto_resend_times */
4921 " , ? " /* outgoing_server_size_limit */
4922 " , ? " /* wifi_auto_download */
4923 " , ? " /* pop_before_smtp */
4924 " , ? " /* incoming_server_requires_apop */
4925 " , ? " /* smime_type */
4926 " , ? " /* certificate_path */
4927 " , ? " /* cipher_type */
4928 " , ? " /* digest_type */
4929 " , ? " /* notification_status */
4930 " , ? " /* vibrate_status */
4931 " , ? " /* display_content_status */
4932 " , ? " /* default_ringtone_status */
4933 " , ? " /* alert_ringtone_path */
4934 " , ? " /* user_name */
4937 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4938 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4939 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4943 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4944 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4945 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4946 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4947 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4948 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4949 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4950 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4951 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4952 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4953 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4954 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4955 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4956 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4957 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4958 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4959 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4960 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4961 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4962 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4963 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4964 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4965 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4966 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4967 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4968 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4969 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4970 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4971 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4972 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4973 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4974 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4975 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4976 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4977 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4978 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4979 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4980 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4981 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4982 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4983 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4984 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4985 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4986 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4987 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4988 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4989 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4990 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4991 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4992 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4993 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4994 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4995 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4996 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4997 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4998 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4999 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5000 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5001 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5002 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5003 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5004 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5005 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5006 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5007 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5008 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5009 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5011 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5013 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5014 ("sqlite3_step fail:%d", rc));
5015 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5016 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5019 /* save passwords to the secure storage */
5020 EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5021 error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5022 if (error != EMAIL_ERROR_NONE) {
5023 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5027 error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5028 if (error != EMAIL_ERROR_NONE) {
5029 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5036 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5038 if (hStmt != NULL) {
5039 rc = sqlite3_finalize(hStmt);
5040 if (rc != SQLITE_OK) {
5041 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5042 error = EMAIL_ERROR_DB_FAILURE;
5046 if (error == EMAIL_ERROR_NONE) {
5047 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5048 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5051 if (err_code != NULL)
5054 EM_DEBUG_FUNC_END("ret [%d]", ret);
5058 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5060 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5062 if (account_id < FIRST_ACCOUNT_ID) {
5063 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5065 if (err_code != NULL)
5066 *err_code = EMAIL_ERROR_INVALID_PARAM;
5070 int rc = -1, ret = false;
5071 int error = EMAIL_ERROR_NONE;
5073 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5075 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5077 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5078 char sql_query_string[QUERY_SIZE] = {0, };
5079 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5080 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5082 /* get password file name */
5083 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5084 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5088 /* delete from db */
5089 memset(sql_query_string, 0x00, sizeof(sql_query_string));
5090 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5091 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5092 if (error != EMAIL_ERROR_NONE) {
5093 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5097 /* validate account existence */
5098 rc = sqlite3_changes(local_db_handle);
5100 EM_DEBUG_EXCEPTION(" no matched account found...");
5101 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5105 /* delete from secure storage */
5106 error = emcore_remove_password_in_key_manager(recv_password_file_name);
5107 if (error != EMAIL_ERROR_NONE) {
5108 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5112 error = emcore_remove_password_in_key_manager(send_password_file_name);
5113 if (error != EMAIL_ERROR_NONE) {
5114 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5121 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5123 if (err_code != NULL)
5126 EM_DEBUG_FUNC_END("ret [%d]", ret);
5131 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5133 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5136 int error = EMAIL_ERROR_NONE;
5139 if (!account_list || !*account_list) {
5140 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5141 error = EMAIL_ERROR_INVALID_PARAM;
5145 emstorage_account_tbl_t* p = *account_list;
5148 for (; i < count; i++) {
5149 EM_SAFE_FREE(p[i].account_name);
5150 EM_SAFE_FREE(p[i].incoming_server_address);
5151 EM_SAFE_FREE(p[i].user_email_address);
5152 EM_SAFE_FREE(p[i].user_data);
5153 EM_SAFE_FREE(p[i].incoming_server_user_name);
5154 EM_SAFE_FREE(p[i].incoming_server_password);
5155 EM_SAFE_FREE(p[i].outgoing_server_address);
5156 EM_SAFE_FREE(p[i].outgoing_server_user_name);
5157 EM_SAFE_FREE(p[i].outgoing_server_password);
5158 EM_SAFE_FREE(p[i].user_display_name);
5159 EM_SAFE_FREE(p[i].reply_to_address);
5160 EM_SAFE_FREE(p[i].return_address);
5161 EM_SAFE_FREE(p[i].logo_icon_path);
5162 EM_SAFE_FREE(p[i].options.display_name_from);
5163 EM_SAFE_FREE(p[i].options.signature);
5164 EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5165 EM_SAFE_FREE(p[i].certificate_path);
5166 EM_SAFE_FREE(p[i].user_name);
5170 *account_list = NULL;
5176 if (err_code != NULL)
5179 EM_DEBUG_FUNC_END("ret [%d]", ret);
5183 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5185 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);
5187 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5188 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5190 if (err_code != NULL)
5191 *err_code = EMAIL_ERROR_INVALID_PARAM;
5195 int rc = -1, ret = false;
5196 int error = EMAIL_ERROR_NONE;
5197 char sql_query_string[QUERY_SIZE] = {0, };
5199 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5200 EMSTORAGE_START_READ_TRANSACTION(transaction);
5202 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);
5206 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5207 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5208 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5210 *count = atoi(result[1]);
5211 sqlite3_free_table(result);
5218 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5220 if (err_code != NULL)
5223 EM_DEBUG_FUNC_END("ret [%d]", ret);
5227 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)
5229 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);
5231 if (!select_num || !mailbox_list) {
5232 EM_DEBUG_EXCEPTION("Invalid parameters");
5234 if (err_code != NULL)
5235 *err_code = EMAIL_ERROR_INVALID_PARAM;
5241 int error = EMAIL_ERROR_NONE;
5242 char conditional_clause_string[QUERY_SIZE] = {0, };
5243 char ordering_clause_string[QUERY_SIZE] = {0, };
5245 if (account_id == ALL_ACCOUNT) {
5246 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5247 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);
5249 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
5250 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5251 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);
5254 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5256 switch (sort_type) {
5257 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5258 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5261 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5262 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5265 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5266 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5269 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5270 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5274 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5276 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5277 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5285 if (err_code != NULL)
5288 EM_DEBUG_FUNC_END("ret [%d]", ret);
5292 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)
5294 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);
5297 int error = EMAIL_ERROR_NONE;
5298 int where_clause_count = 0;
5299 char conditional_clause_string[QUERY_SIZE] = {0, };
5300 char ordering_clause_string[QUERY_SIZE] = {0, };
5302 if (!select_num || !mailbox_list) {
5303 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5304 error = EMAIL_ERROR_INVALID_PARAM;
5308 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5309 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);
5310 where_clause_count++;
5313 if (account_id > 0) {
5314 if (where_clause_count == 0) {
5315 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5316 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);
5318 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);
5321 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5322 " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5323 " THEN 2 ELSE 1 END ASC,"
5324 " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5325 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5326 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5328 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, 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_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)
5346 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);
5349 int error = EMAIL_ERROR_NONE;
5350 char conditional_clause_string[QUERY_SIZE] = {0, };
5352 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5353 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);
5354 if (err_code != NULL)
5355 *err_code = EMAIL_ERROR_INVALID_PARAM;
5359 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);
5360 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5362 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) {
5363 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5371 if (err_code != NULL)
5374 EM_DEBUG_FUNC_END("ret [%d]", ret);
5378 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)
5380 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);
5381 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5382 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5384 if (err_code != NULL)
5385 *err_code = EMAIL_ERROR_INVALID_PARAM;
5391 int error = EMAIL_ERROR_NONE;
5392 char conditional_clause_string[QUERY_SIZE] = {0, };
5394 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5395 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5397 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) {
5398 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5406 if (err_code != NULL)
5409 EM_DEBUG_FUNC_END("ret [%d]", ret);
5413 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5415 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5417 int result_code = false;
5418 int error = EMAIL_ERROR_NONE;
5419 time_t current_time = 0;
5420 char sql_query_string[QUERY_SIZE] = {0, };
5421 sqlite3 *local_db_handle = NULL;
5423 if (!input_mailbox_id) {
5424 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5425 return EMAIL_ERROR_INVALID_PARAM;
5428 time(¤t_time);
5430 local_db_handle = emstorage_get_db_connection(multi_user_name);
5431 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5433 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5434 "UPDATE mail_box_tbl SET"
5435 " last_sync_time = %d"
5436 " WHERE mailbox_id = %d"
5438 , input_mailbox_id);
5440 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5441 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5442 if (error != EMAIL_ERROR_NONE) {
5443 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5449 if (error == EMAIL_ERROR_NONE)
5452 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5454 EM_DEBUG_FUNC_END("error [%d]", error);
5458 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)
5460 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);
5461 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5463 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5464 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5466 if (err_code != NULL)
5467 *err_code = EMAIL_ERROR_INVALID_PARAM;
5472 int error = EMAIL_ERROR_NONE;
5473 int result_count = 0;
5474 char conditional_clause_string[QUERY_SIZE] = {0, };
5476 if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5477 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5478 EM_DEBUG_EXCEPTION("malloc failed...");
5479 error = EMAIL_ERROR_OUT_OF_MEMORY;
5483 (*result_mailbox)->mailbox_id = 0;
5484 (*result_mailbox)->account_id = account_id;
5485 (*result_mailbox)->local_yn = 1;
5486 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5487 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5488 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
5489 (*result_mailbox)->deleted_flag = 0;
5490 (*result_mailbox)->modifiable_yn = 1;
5491 (*result_mailbox)->total_mail_count_on_server = 1;
5492 (*result_mailbox)->has_archived_mails = 0;
5493 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
5494 (*result_mailbox)->no_select = 0;
5497 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5499 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);
5501 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5503 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5504 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5513 if (err_code != NULL)
5516 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5517 EM_DEBUG_FUNC_END("ret [%d]", ret);
5521 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)
5523 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);
5525 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5526 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5527 if (err_code != NULL)
5528 *err_code = EMAIL_ERROR_INVALID_PARAM;
5533 int error = EMAIL_ERROR_NONE;
5534 int result_count = 0;
5535 emstorage_mailbox_tbl_t *result_mailbox = NULL;
5536 char conditional_clause_string[QUERY_SIZE] = {0,};
5539 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5541 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5543 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5544 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5552 *output_mailbox = result_mailbox;
5554 if (err_code != NULL)
5557 EM_DEBUG_FUNC_END("ret [%d]", ret);
5561 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5563 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5565 if (input_mailbox_id <= 0 || !output_mailbox) {
5566 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5567 return EMAIL_ERROR_INVALID_PARAM;
5570 int ret = EMAIL_ERROR_NONE;
5571 int result_count = 0;
5572 char conditional_clause_string[QUERY_SIZE] = {0, };
5574 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5576 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5578 if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5579 conditional_clause_string,
5580 "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5581 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5587 EM_DEBUG_FUNC_END("ret [%d]", ret);
5591 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)
5593 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);
5595 if (account_id < 0 || !keyword || !result_mailbox) {
5596 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5598 if (err_code != NULL)
5599 *err_code = EMAIL_ERROR_INVALID_PARAM;
5604 int error = EMAIL_ERROR_NONE;
5605 char conditional_clause_string[QUERY_SIZE] = {0, };
5607 if (account_id == 0)
5608 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5609 "WHERE alias LIKE \'%%%q%%\'", keyword);
5610 else if (account_id > 0)
5611 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5612 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5614 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5616 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5617 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5625 if (err_code != NULL)
5628 EM_DEBUG_FUNC_END("ret [%d]", ret);
5632 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)
5634 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);
5635 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5636 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5637 if (err_code != NULL)
5638 *err_code = EMAIL_ERROR_INVALID_PARAM;
5642 int rc, ret = false;
5643 int error = EMAIL_ERROR_NONE;
5644 emstorage_account_tbl_t* account = NULL;
5645 DB_STMT hStmt = NULL;
5646 char sql_query_string[QUERY_SIZE] = {0, };
5648 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5649 EMSTORAGE_START_READ_TRANSACTION(transaction);
5651 /* Check whether the account exists. */
5652 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5653 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5658 emstorage_free_account(&account, 1, NULL);
5660 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);
5662 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5665 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5667 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5668 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5671 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5672 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5673 ("sqlite3_step fail:%d", rc));
5675 if (rc == SQLITE_DONE) {
5676 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5677 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5681 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5686 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5687 if (hStmt != NULL) {
5688 rc = sqlite3_finalize(hStmt);
5689 if (rc != SQLITE_OK) {
5690 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5691 error = EMAIL_ERROR_DB_FAILURE;
5695 if (err_code != NULL)
5698 EM_DEBUG_FUNC_END("ret [%d]", ret);
5702 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)
5704 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);
5705 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5706 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5707 if (err_code != NULL)
5708 *err_code = EMAIL_ERROR_INVALID_PARAM;
5712 int rc, ret = false;
5713 int error = EMAIL_ERROR_NONE;
5714 emstorage_account_tbl_t* account = NULL;
5715 DB_STMT hStmt = NULL;
5716 char sql_query_string[QUERY_SIZE] = {0, };
5718 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5719 EMSTORAGE_START_READ_TRANSACTION(transaction);
5721 /* Check whether the account exists. */
5722 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5723 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5728 emstorage_free_account(&account, 1, NULL);
5730 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);
5732 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5735 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5737 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5738 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5741 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5742 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5743 ("sqlite3_step fail:%d", rc));
5745 if (rc == SQLITE_DONE) {
5746 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5747 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5751 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5756 if (hStmt != NULL) {
5757 rc = sqlite3_finalize(hStmt);
5758 if (rc != SQLITE_OK) {
5759 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5760 error = EMAIL_ERROR_DB_FAILURE;
5764 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5766 if (err_code != NULL)
5769 EM_DEBUG_FUNC_END("ret [%d]", ret);
5773 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)
5775 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);
5777 int error = EMAIL_ERROR_NONE;
5778 char sql_query_string[QUERY_SIZE] = {0, };
5779 char *replaced_mailbox_name = NULL;
5782 if (strstr(mailbox_name, "'")) {
5783 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5785 replaced_mailbox_name = strdup(mailbox_name);
5789 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5791 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5793 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5794 "UPDATE mail_box_tbl SET"
5795 " modifiable_yn = %d"
5796 " WHERE account_id = %d"
5797 " AND local_yn = %d"
5798 " AND mailbox_name = '%s'"
5802 , replaced_mailbox_name);
5804 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5805 if (error != EMAIL_ERROR_NONE) {
5806 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5813 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5815 EM_SAFE_FREE(replaced_mailbox_name);
5817 if (err_code != NULL)
5820 EM_DEBUG_FUNC_END("ret [%d]", ret);
5825 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5827 int input_mailbox_id,
5828 int total_count_on_server,
5832 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5833 "transaction[%d], err_code[%p]",
5834 account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
5837 int error = EMAIL_ERROR_NONE;
5838 char sql_query_string[QUERY_SIZE] = {0, };
5840 if (account_id <= 0 || input_mailbox_id <= 0) {
5841 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5842 if (err_code != NULL)
5843 *err_code = EMAIL_ERROR_INVALID_PARAM;
5844 EM_DEBUG_FUNC_END("ret [%d]", ret);
5848 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5850 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5852 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5853 "UPDATE mail_box_tbl SET"
5854 " total_mail_count_on_server = %d"
5855 " WHERE account_id = %d"
5856 " AND mailbox_id = %d"
5857 , total_count_on_server
5859 , input_mailbox_id);
5861 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5862 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5863 if (error != EMAIL_ERROR_NONE) {
5864 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5872 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5874 if (err_code != NULL)
5877 EM_DEBUG_FUNC_END("ret [%d]", ret);
5882 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)
5884 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);
5886 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5887 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);
5889 if (err_code != NULL)
5890 *err_code = EMAIL_ERROR_INVALID_PARAM;
5891 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5895 int rc, ret = false;
5896 int error = EMAIL_ERROR_NONE;
5897 char sql_query_string[QUERY_SIZE] = {0, };
5898 DB_STMT hStmt = NULL;
5901 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5902 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5904 if (local_yn != -1) {
5905 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5906 "UPDATE mail_box_tbl SET"
5908 ", mailbox_name = ?"
5909 ", mailbox_type = ?"
5911 ", deleted_flag = ?"
5912 ", modifiable_yn= ?"
5913 ", mail_slot_size= ?"
5914 ", total_mail_count_on_server = ?"
5915 " WHERE account_id = %d"
5916 " AND local_yn = %d"
5917 " AND mailbox_id = '%d'"
5920 , input_mailbox_id);
5922 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5923 "UPDATE mail_box_tbl SET"
5925 ", mailbox_name = ?"
5926 ", mailbox_type = ?"
5928 ", deleted_flag = ?"
5929 ", modifiable_yn= ?"
5930 ", mail_slot_size= ?"
5931 ", total_mail_count_on_server = ?"
5932 " WHERE account_id = %d"
5933 " AND mailbox_id = '%d'"
5935 , input_mailbox_id);
5940 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5941 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5942 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5944 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5945 _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);
5946 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5947 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5948 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5949 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5950 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5951 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5954 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5955 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5956 ("sqlite3_step fail:%d", rc));
5957 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5958 ("sqlite3_step fail:%d", rc));
5963 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5965 if (hStmt != NULL) {
5966 rc = sqlite3_finalize(hStmt);
5967 if (rc != SQLITE_OK) {
5968 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5969 error = EMAIL_ERROR_DB_FAILURE;
5973 if (err_code != NULL)
5976 EM_DEBUG_FUNC_END("ret [%d]", ret);
5980 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)
5982 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);
5984 if (account_id < FIRST_ACCOUNT_ID) {
5985 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5987 if (err_code != NULL)
5988 *err_code = EMAIL_ERROR_INVALID_PARAM;
5992 int rc, ret = false;
5993 int error = EMAIL_ERROR_NONE;
5994 char sql_query_string[QUERY_SIZE] = {0, };
5996 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5997 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5999 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6001 DB_STMT hStmt_box_tbl = NULL;
6002 DB_STMT hStmt_mail_tbl = NULL;
6005 /* Update mail_box_tbl */
6006 if (local_yn != -1) {
6007 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6008 "UPDATE mail_box_tbl SET"
6010 " WHERE account_id = %d"
6011 " AND local_yn = %d"
6012 " AND mailbox_id = '%d'"
6015 , input_mailbox_id);
6017 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6018 "UPDATE mail_box_tbl SET"
6020 " WHERE account_id = %d"
6021 " AND mailbox_id = '%d'"
6023 , input_mailbox_id);
6026 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6028 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6030 if (SQLITE_OK != rc) {
6031 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6032 error = EMAIL_ERROR_DB_FAILURE;
6036 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6039 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6041 if (rc == SQLITE_FULL) {
6042 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6043 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6047 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6048 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6049 error = EMAIL_ERROR_DB_FAILURE;
6054 /* Update mail_tbl */
6056 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6057 "UPDATE mail_tbl SET"
6059 " WHERE account_id = %d"
6060 " AND mailbox_id = '%d'"
6062 , input_mailbox_id);
6064 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6066 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6067 if (SQLITE_OK != rc) {
6068 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6069 error = EMAIL_ERROR_DB_FAILURE;
6073 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6075 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6076 if (rc == SQLITE_FULL) {
6077 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6078 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6082 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6083 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6084 error = EMAIL_ERROR_DB_FAILURE;
6091 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6093 if (hStmt_box_tbl != NULL) {
6094 rc = sqlite3_finalize(hStmt_box_tbl);
6095 if (rc != SQLITE_OK) {
6096 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6097 error = EMAIL_ERROR_DB_FAILURE;
6101 if (hStmt_mail_tbl != NULL) {
6102 rc = sqlite3_finalize(hStmt_mail_tbl);
6103 if (rc != SQLITE_OK) {
6104 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6105 error = EMAIL_ERROR_DB_FAILURE;
6109 if (err_code != NULL)
6112 EM_DEBUG_FUNC_END("ret [%d]", ret);
6116 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6118 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);
6120 int rc, ret = false;
6121 int error = EMAIL_ERROR_NONE;
6122 char sql_query_string[QUERY_SIZE] = {0, };
6124 if (input_mailbox_id < 0) {
6125 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6126 return EMAIL_ERROR_INVALID_PARAM;
6129 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6131 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6133 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6135 DB_STMT hStmt = NULL;
6138 /* Update mail_box_tbl */
6139 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6140 "UPDATE mail_box_tbl SET"
6142 " WHERE mailbox_id = %d"
6143 , input_mailbox_id);
6145 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6147 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6148 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6149 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6151 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6153 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6154 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6155 ("sqlite3_step fail:%d", rc));
6156 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6157 ("sqlite3_step fail:%d", rc));
6159 if (hStmt != NULL) {
6160 rc = sqlite3_finalize(hStmt);
6161 if (rc != SQLITE_OK) {
6162 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6163 error = EMAIL_ERROR_DB_FAILURE;
6171 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6173 if (hStmt != NULL) {
6174 rc = sqlite3_finalize(hStmt);
6175 if (rc != SQLITE_OK) {
6176 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6177 error = EMAIL_ERROR_DB_FAILURE;
6181 EM_DEBUG_FUNC_END("error [%d]", error);
6185 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)
6187 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);
6189 int err = EMAIL_ERROR_NONE;
6191 int cur_mailbox_id_string = 0;
6192 int mailbox_id_string_buffer_length = 0;
6193 char sql_query_string[QUERY_SIZE] = {0, };
6194 char *mailbox_id_string_buffer = NULL;
6195 char *parameter_string = NULL;
6196 sqlite3 *local_db_handle = NULL;
6198 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6199 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6200 return EMAIL_ERROR_INVALID_PARAM;
6203 local_db_handle = emstorage_get_db_connection(multi_user_name);
6205 /* Generating mail id list string */
6206 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6208 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6210 if (!mailbox_id_string_buffer) {
6211 EM_DEBUG_EXCEPTION("em_mallocfailed");
6212 err = EMAIL_ERROR_OUT_OF_MEMORY;
6216 for (i = 0; i < input_mailbox_id_count; i++)
6217 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]);
6219 if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6220 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6222 /* Generating notification parameter string */
6223 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6225 if (!parameter_string) {
6226 EM_DEBUG_EXCEPTION("em_mallocfailed");
6227 err = EMAIL_ERROR_OUT_OF_MEMORY;
6231 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6233 /* Write query string */
6234 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);
6236 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6239 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6240 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6241 if (err != EMAIL_ERROR_NONE) {
6242 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6246 if (sqlite3_changes(local_db_handle) == 0)
6247 EM_DEBUG_LOG("no mail matched...");
6252 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6254 if (err == EMAIL_ERROR_NONE && parameter_string) {
6255 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6256 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6257 input_field_name, input_value);
6260 EM_SAFE_FREE(mailbox_id_string_buffer);
6261 EM_SAFE_FREE(parameter_string);
6263 EM_DEBUG_FUNC_END("err [%d]", err);
6267 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6269 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6272 if (err_code != NULL)
6273 *err_code = EMAIL_ERROR_INVALID_PARAM;
6277 int rc, ret = false;
6278 int error = EMAIL_ERROR_NONE;
6279 DB_STMT hStmt = NULL;
6280 char sql_query_string[QUERY_SIZE] = {0,};
6281 char **result = NULL;
6282 time_t current_time;
6283 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6284 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6286 EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
6288 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6289 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6290 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6292 time(¤t_time);
6294 if (NULL == result[1])
6297 rc = atoi(result[1]) + 1;
6298 sqlite3_free_table(result);
6300 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6302 mailbox_tbl->mailbox_id = rc;
6304 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6305 "INSERT INTO mail_box_tbl VALUES "
6306 "(?" /* mailbox_id */
6307 ", ?" /* account_id */
6308 ", ?" /* local_yn */
6309 ", ?" /* mailbox_name */
6310 ", ?" /* mailbox_type */
6312 ", ?" /* deleted_flag */
6313 ", ?" /* modifiable_yn */
6314 ", ?" /* total_mail_count_on_server */
6315 ", ?" /* has_archived_mails */
6316 ", ?" /* mail_slot_size */
6317 ", ?" /* no_select */
6318 ", ?" /* last_sync_time */
6319 ", ?" /* eas_data_length */
6320 ", ?" /* eas_data */
6324 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6325 EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6326 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6327 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6331 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6332 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6333 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6334 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6335 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6336 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6337 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6338 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6339 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6340 _bind_stmt_field_data_int(hStmt, col_index++, 0);
6341 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6342 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6343 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6344 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6345 _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6348 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6349 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6350 ("sqlite3_step fail:%dn", rc));
6355 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6356 if (hStmt != NULL) {
6357 rc = sqlite3_finalize(hStmt);
6358 if (rc != SQLITE_OK) {
6359 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6360 error = EMAIL_ERROR_DB_FAILURE;
6364 if (error == EMAIL_ERROR_NONE) {
6365 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6366 mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6367 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6370 if (err_code != NULL)
6373 EM_DEBUG_FUNC_END("ret [%d]", ret);
6377 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6379 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6381 if (account_id < FIRST_ACCOUNT_ID) {
6383 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6385 if (err_code != NULL)
6386 *err_code = EMAIL_ERROR_INVALID_PARAM;
6389 int rc, ret = false;
6390 int error = EMAIL_ERROR_NONE;
6391 char sql_query_string[QUERY_SIZE] = {0,};
6392 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6393 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6396 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6398 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6399 if (error != EMAIL_ERROR_NONE) {
6400 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6404 rc = sqlite3_changes(local_db_handle);
6406 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6412 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6414 if (err_code != NULL)
6416 EM_DEBUG_FUNC_END("ret [%d]", ret);
6422 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)
6424 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);
6426 if (account_id < FIRST_ACCOUNT_ID) {
6427 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6429 if (err_code != NULL)
6430 *err_code = EMAIL_ERROR_INVALID_PARAM;
6434 int rc, ret = false;
6435 int error = EMAIL_ERROR_NONE;
6436 char sql_query_string[QUERY_SIZE] = {0, };
6437 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6438 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6441 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6443 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);
6445 if (input_mailbox_id > 0) { /* 0 means all mailbox */
6446 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);
6449 EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6450 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6451 if (error != EMAIL_ERROR_NONE) {
6452 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6456 rc = sqlite3_changes(local_db_handle);
6458 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6459 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6466 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6468 if (error == EMAIL_ERROR_NONE) {
6469 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6470 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6473 if (err_code != NULL)
6476 EM_DEBUG_FUNC_END("ret [%d]", ret);
6480 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6482 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6485 int error = EMAIL_ERROR_NONE;
6488 if (!mailbox_list || !*mailbox_list) {
6489 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6491 error = EMAIL_ERROR_INVALID_PARAM;
6495 emstorage_mailbox_tbl_t* p = *mailbox_list;
6498 for (; i < count; i++) {
6499 EM_SAFE_FREE(p[i].mailbox_name);
6500 EM_SAFE_FREE(p[i].alias);
6501 EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6504 EM_SAFE_FREE(p); *mailbox_list = NULL;
6510 if (err_code != NULL)
6513 EM_DEBUG_FUNC_END("ret [%d]", ret);
6517 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)
6519 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);
6521 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6522 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6524 if (err_code != NULL)
6525 *err_code = EMAIL_ERROR_INVALID_PARAM;
6529 int rc = -1, ret = false;
6530 int error = EMAIL_ERROR_NONE;
6531 char sql_query_string[QUERY_SIZE] = {0, };
6532 char *replaced_mailbox_name = NULL;
6534 if (strstr(mailbox_name, "'")) {
6535 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6537 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6540 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6543 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6544 EMSTORAGE_START_READ_TRANSACTION(transaction);
6545 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);
6546 EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6550 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6551 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6552 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6554 *count = atoi(result[1]);
6555 sqlite3_free_table(result);
6560 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6562 EM_SAFE_FREE(replaced_mailbox_name);
6564 if (err_code != NULL)
6567 EM_DEBUG_FUNC_END("ret [%d]", ret);
6571 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)
6573 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);
6575 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6576 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6578 if (err_code != NULL)
6579 *err_code = EMAIL_ERROR_INVALID_PARAM;
6583 int rc = -1, ret = false;
6584 int error = EMAIL_ERROR_NONE;
6585 char sql_query_string[QUERY_SIZE] = {0, };
6586 char *replaced_mailbox_name = NULL;
6588 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6590 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6591 EMSTORAGE_START_READ_TRANSACTION(transaction);
6594 if (strstr(mailbox_name, "'")) {
6595 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6597 replaced_mailbox_name = strdup(mailbox_name);
6600 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);
6602 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);
6605 char **result = NULL;
6607 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6608 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6609 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6611 *exist = atoi(result[1]);
6612 sqlite3_free_table(result);
6622 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6624 EM_SAFE_FREE(replaced_mailbox_name);
6626 if (err_code != NULL)
6629 EM_DEBUG_FUNC_END("ret [%d]", ret);
6633 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6635 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6637 if (!mail || mail_id <= 0) {
6638 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6639 if (err_code != NULL)
6640 *err_code = EMAIL_ERROR_INVALID_PARAM;
6644 int rc, ret = false;
6645 int error = EMAIL_ERROR_NONE;
6646 DB_STMT hStmt = NULL;
6647 char sql_query_string[QUERY_SIZE] = {0, };
6649 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6650 EMSTORAGE_START_READ_TRANSACTION(transaction);
6652 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6654 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6655 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6657 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6658 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6661 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6662 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6663 ("sqlite3_step fail:%d", rc));
6665 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6666 if (*mail == NULL) {
6667 error = EMAIL_ERROR_OUT_OF_MEMORY;
6668 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6672 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6674 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6675 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6676 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6677 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6678 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6679 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6680 _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6682 (*mail)->server_mail_status = 1;
6688 if (hStmt != NULL) {
6689 rc = sqlite3_finalize(hStmt);
6690 if (rc != SQLITE_OK) {
6691 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6692 error = EMAIL_ERROR_DB_FAILURE;
6696 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6698 if (err_code != NULL)
6701 EM_DEBUG_FUNC_END("ret [%d]", ret);
6705 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)
6707 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6708 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);
6709 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6710 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6712 if (err_code != NULL)
6713 *err_code = EMAIL_ERROR_INVALID_PARAM;
6717 int rc, ret = false;
6718 int error = EMAIL_ERROR_NONE;
6720 DB_STMT hStmt = NULL;
6721 char sql_query_string[QUERY_SIZE] = {0, };
6723 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6726 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6727 EMSTORAGE_START_READ_TRANSACTION(transaction);
6730 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);
6732 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6734 EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6738 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6739 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6740 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6741 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6744 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6745 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6746 ("sqlite3_step fail:%d", rc));
6749 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6750 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6751 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6752 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6754 sqlite3_free_table(result);
6756 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6762 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6763 EM_DEBUG_EXCEPTION(" malloc failed...");
6764 error = EMAIL_ERROR_OUT_OF_MEMORY;
6768 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6770 for (i = 0; i < *count; ++i) {
6771 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6772 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6773 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6774 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6775 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6776 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6777 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6778 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6780 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6781 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6782 ("sqlite3_step fail:%d", rc));
6789 *read_mail_uid = p_data_tbl;
6790 else if (p_data_tbl)
6791 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6793 if (hStmt != NULL) {
6794 rc = sqlite3_finalize(hStmt);
6795 if (rc != SQLITE_OK) {
6796 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6797 error = EMAIL_ERROR_DB_FAILURE;
6801 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6803 if (err_code != NULL)
6806 EM_PROFILE_END(emStorageGetDownloadList);
6807 EM_DEBUG_FUNC_END("ret [%d]", ret);
6811 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)
6813 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);
6815 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6816 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);
6818 if (err_code != NULL)
6819 *err_code = EMAIL_ERROR_INVALID_PARAM;
6823 int rc, ret = false;
6824 int error = EMAIL_ERROR_NONE;
6825 DB_STMT hStmt = NULL;
6826 char sql_query_string[QUERY_SIZE] = {0, };
6827 char *replaced_mailbox_name = NULL;
6829 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6830 EMSTORAGE_START_READ_TRANSACTION(transaction);
6833 if (strstr(mailbox_name, "'")) {
6834 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6836 replaced_mailbox_name = strdup(mailbox_name);
6839 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6841 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6842 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6843 "WHERE account_id = %d "
6844 "AND mailbox_id = '%s' "
6845 "AND local_uid = %d "
6846 "AND mailbox_name = '%s' "
6847 "AND server_uid = '%s'",
6848 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6850 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6851 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6852 "WHERE account_id = %d "
6853 "AND mailbox_id = '%s' "
6854 "AND local_uid = %d "
6855 "AND server_uid = '%s'",
6856 account_id, mailbox_id, local_uid, uid);
6860 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6861 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6862 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6865 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6866 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6867 ("sqlite3_step fail:%d", rc));
6869 if (rc == SQLITE_DONE) {
6870 EM_DEBUG_LOG("no matched mail found....");
6871 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6875 _get_stmt_field_data_int(hStmt, mail_size, 0);
6880 EM_SAFE_FREE(replaced_mailbox_name);
6882 if (hStmt != NULL) {
6883 rc = sqlite3_finalize(hStmt);
6884 if (rc != SQLITE_OK) {
6885 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6886 error = EMAIL_ERROR_DB_FAILURE;
6890 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6892 if (err_code != NULL)
6895 EM_DEBUG_FUNC_END("ret [%d]", ret);
6899 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)
6901 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6903 if (!read_mail_uid) {
6904 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6905 if (err_code != NULL)
6906 *err_code = EMAIL_ERROR_INVALID_PARAM;
6910 int rc, rc2, ret = false;
6911 int error = EMAIL_ERROR_NONE;
6912 DB_STMT hStmt = NULL;
6913 char sql_query_string[QUERY_SIZE] = {0, };
6915 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6916 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6918 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6919 char **result = NULL;
6922 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6923 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6924 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6926 if (NULL == result[1]) rc = 1;
6927 else rc = atoi(result[1])+1;
6928 sqlite3_free_table(result);
6930 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6931 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6932 "(?" /* account_id */
6933 ", ?" /* mailbox_id */
6934 ", ?" /* mailbox_name */
6935 ", ?" /* local_uid */
6936 ", ?" /* server_uid */
6937 ", ?" /* rfc822_size */
6938 ", ?" /* sync_status */
6939 ", ?" /* flags_seen_field */
6940 ", ?" /* flags_flagged_field */
6944 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6945 if (rc2 != SQLITE_OK) {
6946 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6947 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6949 error = EMAIL_ERROR_DB_FAILURE;
6953 EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6954 "server_uid[%s] rfc822_size[%d] rc[%d]",
6955 read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
6956 read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6958 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6959 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6960 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6961 _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);
6962 _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);
6963 _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
6964 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
6965 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
6966 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6969 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6970 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6971 ("sqlite3_step fail:%d", rc));
6972 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6973 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6979 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6980 if (hStmt != NULL) {
6981 rc = sqlite3_finalize(hStmt);
6982 if (rc != SQLITE_OK) {
6983 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6984 error = EMAIL_ERROR_DB_FAILURE;
6988 if (err_code != NULL)
6991 EM_DEBUG_FUNC_END("ret [%d]", ret);
6995 #ifdef __FEATURE_BODY_SEARCH__
6996 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
6998 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7001 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7002 if (err_code != NULL)
7003 *err_code = EMAIL_ERROR_INVALID_PARAM;
7007 int rc, rc2, ret = false;
7008 int error = EMAIL_ERROR_NONE;
7009 DB_STMT hStmt = NULL;
7010 char sql_query_string[QUERY_SIZE] = {0, };
7012 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7013 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7015 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7016 char **result = NULL;
7018 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7019 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7020 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7021 sqlite3_free_table(result);
7023 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7024 "INSERT INTO mail_text_tbl VALUES "
7030 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7031 if (rc2 != SQLITE_OK) {
7032 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7033 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7035 error = EMAIL_ERROR_DB_FAILURE;
7039 EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7040 mail_text->account_id, mail_text->mailbox_id);
7041 EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7043 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7044 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7045 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7046 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7048 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7049 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7050 ("sqlite3_step fail:%d", rc));
7051 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7052 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7057 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7058 if (hStmt != NULL) {
7059 rc = sqlite3_finalize(hStmt);
7060 if (rc != SQLITE_OK) {
7061 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7062 error = EMAIL_ERROR_DB_FAILURE;
7066 if (err_code != NULL)
7069 EM_DEBUG_FUNC_END("ret [%d]", ret);
7074 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)
7076 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);
7078 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7079 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);
7081 if (err_code != NULL)
7082 *err_code = EMAIL_ERROR_INVALID_PARAM;
7086 int rc, ret = false;
7087 int error = EMAIL_ERROR_NONE;
7088 DB_STMT hStmt = NULL;
7089 char sql_query_string[QUERY_SIZE] = {0, };
7091 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7093 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7095 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7096 "UPDATE mail_read_mail_uid_tbl SET"
7099 ", mailbox_name = ?"
7103 ", flags_seen_field = ?"
7104 ", flags_flagged_field = ?"
7105 " WHERE account_id = ?"
7106 " AND mailbox_id = ?"
7107 " AND local_uid = ?"
7108 " AND mailbox_name= ?"
7109 " AND server_uid = ?");
7112 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7113 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7114 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7115 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7120 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7121 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7122 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7123 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7124 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7125 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7126 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7127 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7128 _bind_stmt_field_data_int(hStmt, i++, account_id);
7129 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7130 _bind_stmt_field_data_int(hStmt, i++, local_uid);
7131 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7132 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7135 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7136 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7137 ("sqlite3_step fail:%d", rc));
7142 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7143 if (hStmt != NULL) {
7144 rc = sqlite3_finalize(hStmt);
7145 if (rc != SQLITE_OK) {
7146 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7147 error = EMAIL_ERROR_DB_FAILURE;
7151 if (err_code != NULL)
7154 EM_DEBUG_FUNC_END("ret [%d]", ret);
7158 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7166 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7167 "uid[%s], transaction[%d], err_code[%p]",
7168 account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7170 if (account_id < FIRST_ACCOUNT_ID) {
7171 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7173 if (err_code != NULL)
7174 *err_code = EMAIL_ERROR_INVALID_PARAM;
7179 int error = EMAIL_ERROR_NONE;
7180 char sql_query_string[QUERY_SIZE] = {0, };
7181 char *replaced_mailbox_name = NULL;
7182 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7184 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7186 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7187 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7189 if (mailbox_id > 0) {
7190 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7191 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7192 "AND mailbox_id = %d ", mailbox_id);
7195 if (mailbox_name) { /* NULL means all mailbox_name */
7196 if (strstr(mailbox_name, "'")) {
7197 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7199 replaced_mailbox_name = strdup(mailbox_name);
7202 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7203 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7204 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7207 if (uid) { /* NULL means all mail */
7208 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7209 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7213 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7214 if (error != EMAIL_ERROR_NONE) {
7215 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7222 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7224 EM_SAFE_FREE(replaced_mailbox_name);
7226 if (err_code != NULL)
7229 EM_DEBUG_FUNC_END("ret [%d]", ret);
7233 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7235 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7238 int error = EMAIL_ERROR_NONE;
7241 if (!read_mail_uid || !*read_mail_uid) {
7242 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7244 error = EMAIL_ERROR_INVALID_PARAM;
7248 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7251 for (i = 0; i < count; i++) {
7252 EM_SAFE_FREE(p[i].mailbox_name);
7253 EM_SAFE_FREE(p[i].server_uid);
7256 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7262 if (err_code != NULL)
7265 EM_DEBUG_FUNC_END("ret [%d]", ret);
7269 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7271 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7274 EM_DEBUG_EXCEPTION("count[%p]", count);
7276 if (err_code != NULL)
7277 *err_code = EMAIL_ERROR_INVALID_PARAM;
7281 int rc = -1, ret = false;
7282 int error = EMAIL_ERROR_NONE;
7283 char sql_query_string[QUERY_SIZE] = {0, };
7285 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7286 EMSTORAGE_START_READ_TRANSACTION(transaction);
7288 if (account_id != ALL_ACCOUNT)
7289 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7291 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7295 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7296 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7297 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7299 *count = atoi(result[1]);
7300 sqlite3_free_table(result);
7305 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7307 if (err_code != NULL)
7310 EM_DEBUG_FUNC_END("ret [%d]", ret);
7314 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)
7316 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);
7318 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
7319 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);
7321 if (err_code != NULL)
7322 *err_code = EMAIL_ERROR_INVALID_PARAM;
7327 int error = EMAIL_ERROR_NONE;
7329 emstorage_rule_tbl_t* p_data_tbl = NULL;
7330 int i = 0, count = 0;
7331 DB_STMT hStmt = NULL;
7332 char sql_query_string[QUERY_SIZE] = {0, };
7336 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7337 EMSTORAGE_START_READ_TRANSACTION(transaction);
7339 if (account_id != ALL_ACCOUNT) {
7341 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7343 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7346 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7348 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7351 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7352 /* EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7353 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7354 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7357 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7358 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7359 ("sqlite3_step fail:%d", rc));
7362 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7363 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7364 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7365 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7367 sqlite3_free_table(result);
7370 EM_DEBUG_LOG_DEV("No matching rule found...");
7372 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7377 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7378 EM_DEBUG_EXCEPTION(" malloc failed...");
7380 error = EMAIL_ERROR_OUT_OF_MEMORY;
7384 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7386 for (i = 0; i < count; i++) {
7387 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7388 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7389 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7390 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7391 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7392 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7393 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7394 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7395 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7396 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7398 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7399 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7400 ("sqlite3_step fail:%d", rc));
7407 EM_DEBUG_LOG("[%d] rules found.", count);
7410 *rule_list = p_data_tbl;
7411 *select_num = count;
7412 } else if (p_data_tbl != NULL)
7413 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7415 if (hStmt != NULL) {
7416 rc = sqlite3_finalize(hStmt);
7417 if (rc != SQLITE_OK) {
7418 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7419 error = EMAIL_ERROR_DB_FAILURE;
7423 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7425 if (err_code != NULL)
7428 EM_DEBUG_FUNC_END("ret [%d]", ret);
7432 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)
7434 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7435 int error = EMAIL_ERROR_NONE;
7437 DB_STMT hStmt = NULL;
7440 EM_DEBUG_EXCEPTION("Invalid parameter");
7441 error = EMAIL_ERROR_INVALID_PARAM;
7446 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7447 error = EMAIL_ERROR_INVALID_PARAM;
7451 emstorage_rule_tbl_t* p_data_tbl = NULL;
7454 char sql_query_string[QUERY_SIZE] = {0, };
7455 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7456 EMSTORAGE_START_READ_TRANSACTION(transaction);
7458 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7460 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7461 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7462 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7465 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7466 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7467 ("sqlite3_step fail:%d", rc));
7469 if (rc == SQLITE_DONE) {
7470 EM_DEBUG_EXCEPTION(" no matched rule found...");
7471 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7475 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7476 EM_DEBUG_EXCEPTION(" malloc failed...");
7477 error = EMAIL_ERROR_OUT_OF_MEMORY;
7481 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7482 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7483 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7484 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7485 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7486 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7487 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7488 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7489 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7490 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7491 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7500 if (hStmt != NULL) {
7501 rc = sqlite3_finalize(hStmt);
7502 if (rc != SQLITE_OK) {
7503 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7504 error = EMAIL_ERROR_DB_FAILURE;
7508 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7510 if (err_code != NULL)
7513 EM_DEBUG_FUNC_END("ret [%d]", ret);
7517 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7519 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7521 if (!new_rule) { /* only global rule supported. */
7522 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7524 if (err_code != NULL)
7525 *err_code = EMAIL_ERROR_INVALID_PARAM;
7529 int rc, ret = false;
7530 int error = EMAIL_ERROR_NONE;
7532 DB_STMT hStmt = NULL;
7533 char sql_query_string[QUERY_SIZE] = {0, };
7534 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7535 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7537 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7538 "UPDATE mail_rule_tbl SET"
7544 ", target_mailbox_id = ?"
7549 " WHERE rule_id = %d"
7553 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7554 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7555 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7556 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7560 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7561 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7562 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7563 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7564 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7565 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7566 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7567 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7568 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7569 _bind_stmt_field_data_int(hStmt, i++, rule_id);
7572 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7573 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7574 ("sqlite3_step fail:%d", rc));
7579 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7580 if (hStmt != NULL) {
7581 rc = sqlite3_finalize(hStmt);
7582 if (rc != SQLITE_OK) {
7583 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7584 error = EMAIL_ERROR_DB_FAILURE;
7588 if (err_code != NULL)
7591 EM_DEBUG_FUNC_END("ret [%d]", ret);
7595 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7597 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7600 EM_DEBUG_LOG("rule is NULL");
7601 if (err_code != NULL)
7602 *err_code = EMAIL_ERROR_INVALID_PARAM;
7606 DB_STMT hStmt = NULL;
7607 char sql_query_string[QUERY_SIZE] = {0,};
7608 int error = EMAIL_ERROR_NONE;
7612 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7613 EMSTORAGE_START_READ_TRANSACTION(transaction);
7615 switch (rule->action_type) {
7616 case EMAIL_FILTER_MOVE:
7617 if (rule->type == EMAIL_PRIORITY_SENDER) {
7618 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7619 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7620 rule->action_type, rule->type, rule->value2);
7622 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7623 "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);
7626 case EMAIL_FILTER_BLOCK:
7627 if (rule->type == EMAIL_FILTER_FROM)
7628 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7629 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7630 rule->action_type, rule->type, rule->value2);
7631 else if (rule->type == EMAIL_FILTER_SUBJECT)
7632 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7633 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7634 rule->action_type, rule->type, rule->value);
7635 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7636 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7637 "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\'))",
7638 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7642 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7643 error = EMAIL_ERROR_INVALID_PARAM;
7648 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7649 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7650 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7652 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7653 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7654 ("sqlite3_step fail:%d", rc));
7656 if (rc == SQLITE_DONE) {
7657 EM_DEBUG_EXCEPTION(" no matched rule found...");
7658 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7665 if (hStmt != NULL) {
7666 rc = sqlite3_finalize(hStmt);
7667 if (rc != SQLITE_OK) {
7668 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7669 error = EMAIL_ERROR_DB_FAILURE;
7673 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7678 EM_DEBUG_FUNC_END("ret [%d]", ret);
7682 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7684 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7686 if (!rule) { /* only global rule supported. */
7687 EM_DEBUG_LOG("rule is NULL");
7688 if (err_code != NULL)
7689 *err_code = EMAIL_ERROR_INVALID_PARAM;
7693 int rc, rc_2, ret = false;
7694 int error = EMAIL_ERROR_NONE;
7695 DB_STMT hStmt = NULL;
7696 char sql_query_string[QUERY_SIZE] = {0, };
7698 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7700 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7704 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7706 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7707 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7708 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7710 if (NULL == result[1])
7713 rc = atoi(result[1])+1;
7715 sqlite3_free_table(result);
7719 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7720 "INSERT INTO mail_rule_tbl VALUES "
7721 "(?" /* account id */
7723 ", ?" /* filter_name */
7727 ", ?" /* action_type */
7728 ", ?" /* target_mailbox_id */
7730 ", ?)"); /* flag2 */
7732 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7733 if (rc_2 != SQLITE_OK) {
7734 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7735 error = EMAIL_ERROR_DB_FAILURE;
7739 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7740 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7741 _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);
7742 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7743 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7744 _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7745 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7746 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7747 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7748 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7750 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7751 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7752 ("sqlite3_step fail:%d", rc));
7757 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7758 if (hStmt != NULL) {
7759 rc = sqlite3_finalize(hStmt);
7760 if (rc != SQLITE_OK) {
7761 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7762 error = EMAIL_ERROR_DB_FAILURE;
7766 if (err_code != NULL)
7769 EM_DEBUG_FUNC_END("ret [%d]", ret);
7773 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7775 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7777 if (rule_id <= 0) { /* only global rule supported. */
7778 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7780 if (err_code != NULL)
7781 *err_code = EMAIL_ERROR_INVALID_PARAM;
7785 int rc, ret = false;
7786 int error = EMAIL_ERROR_NONE;
7787 char sql_query_string[QUERY_SIZE] = {0, };
7788 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7789 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7791 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7792 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7793 if (error != EMAIL_ERROR_NONE) {
7794 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7798 rc = sqlite3_changes(local_db_handle);
7800 EM_DEBUG_EXCEPTION(" no matched rule found...");
7802 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7809 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7811 if (err_code != NULL)
7814 EM_DEBUG_FUNC_END("ret [%d]", ret);
7818 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7820 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7823 int error = EMAIL_ERROR_NONE;
7826 if (!rule_list || !*rule_list) {
7827 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7829 error = EMAIL_ERROR_INVALID_PARAM;
7833 emstorage_rule_tbl_t* p = *rule_list;
7836 for (; i < count; i++) {
7837 EM_SAFE_FREE(p[i].value);
7840 EM_SAFE_FREE(p); *rule_list = NULL;
7844 if (err_code != NULL)
7847 EM_DEBUG_FUNC_END("ret [%d]", ret);
7851 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)
7853 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);
7855 if (!total && !unseen) {
7856 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7857 if (err_code != NULL)
7858 *err_code = EMAIL_ERROR_INVALID_PARAM;
7862 int rc = -1, ret = false;
7863 int error = EMAIL_ERROR_NONE;
7864 DB_STMT hStmt = NULL;
7865 char sql_query_string[QUERY_SIZE] = {0, };
7866 char *replaced_mailbox_name = NULL;
7868 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7869 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7870 EMSTORAGE_START_READ_TRANSACTION(transaction);
7873 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7875 if (account_id != ALL_ACCOUNT) {
7876 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);
7878 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);
7879 } else if (mailbox_id)
7880 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);
7882 #ifdef USE_GET_RECORD_COUNT_API
7885 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7886 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7887 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7889 *total = atoi(result[1]);
7890 sqlite3_free_table(result);
7893 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7894 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7895 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7897 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7898 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7899 ("sqlite3_step fail:%d", rc));
7900 _get_stmt_field_data_int(hStmt, total, 0);
7901 #endif /* USE_GET_RECORD_COUNT_API */
7905 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7907 if (account_id != ALL_ACCOUNT)
7908 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);
7911 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);
7913 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)");
7916 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7917 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7918 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7920 *unseen = atoi(result[1]);
7921 sqlite3_free_table(result);
7929 #ifndef USE_PREPARED_QUERY_
7930 if (hStmt != NULL) {
7931 rc = sqlite3_finalize(hStmt);
7932 if (rc != SQLITE_OK) {
7933 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7934 error = EMAIL_ERROR_DB_FAILURE;
7939 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7941 EM_SAFE_FREE(replaced_mailbox_name);
7943 if (err_code != NULL)
7946 EM_DEBUG_FUNC_END("ret [%d]", ret);
7950 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)
7952 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7954 if (mail_id <= 0 || !mail) {
7955 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7956 if (err_code != NULL)
7957 *err_code = EMAIL_ERROR_INVALID_PARAM;
7962 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7964 if (p_data_tbl == NULL) {
7965 EM_DEBUG_EXCEPTION("malloc failed...");
7966 if (err_code != NULL)
7967 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7971 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7972 DB_STMT hStmt = NULL;
7973 char sql_query_string[QUERY_SIZE] = {0, };
7975 int rc, ret = false;
7976 int error = EMAIL_ERROR_NONE;
7978 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7979 EMSTORAGE_START_READ_TRANSACTION(transaction);
7982 case RETRIEVE_SUMMARY:
7983 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7984 "SELECT account_id, "
7987 "server_mail_status, "
7988 "server_mailbox_name, "
7992 "file_path_mime_entity, "
7993 "flags_seen_field, "
7997 "thread_item_count "
7998 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8001 case RETRIEVE_FIELDS_FOR_DELETE:
8002 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8003 "SELECT account_id, "
8005 "server_mail_status, "
8006 "server_mailbox_name, "
8008 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8011 case RETRIEVE_ACCOUNT:
8012 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8013 "SELECT account_id "
8014 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8018 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8019 "SELECT account_id, "
8020 "flags_seen_field, "
8023 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8027 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8028 error = EMAIL_ERROR_INVALID_PARAM;
8032 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8034 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8035 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8036 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8039 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8040 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8041 ("sqlite3_step fail:%d", rc));
8043 if (rc == SQLITE_DONE) {
8044 EM_DEBUG_LOG("no matched mail found...");
8045 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8049 case RETRIEVE_SUMMARY:
8050 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8051 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8052 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8053 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8054 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8055 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8056 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8057 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8058 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8059 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8060 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8061 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8062 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8063 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8066 case RETRIEVE_FIELDS_FOR_DELETE:
8067 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8068 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8069 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8070 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8071 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8074 case RETRIEVE_ACCOUNT:
8075 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8079 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8080 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8081 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8082 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8091 else if (p_data_tbl != NULL)
8092 emstorage_free_mail(&p_data_tbl, 1, NULL);
8094 if (hStmt != NULL) {
8095 rc = sqlite3_finalize(hStmt);
8096 if (rc != SQLITE_OK) {
8097 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8098 error = EMAIL_ERROR_DB_FAILURE;
8103 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8105 if (err_code != NULL)
8108 EM_DEBUG_FUNC_END("ret [%d]", ret);
8112 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)
8114 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);
8117 int error = EMAIL_ERROR_NONE;
8118 int query_string_length = 0;
8119 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8120 char **result = NULL;
8121 char *sql_query_string = NULL;
8122 emstorage_mail_tbl_t* p_data_tbl = NULL;
8123 sqlite3 *local_db_handle = NULL;
8125 if (number_of_mails <= 0 || !mail_ids) {
8126 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8127 if (err_code != NULL)
8128 *err_code = EMAIL_ERROR_INVALID_PARAM;
8132 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8134 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8135 sql_query_string = (char*)em_malloc(query_string_length);
8137 if (p_data_tbl == NULL || sql_query_string == NULL) {
8138 EM_DEBUG_EXCEPTION("malloc failed...");
8140 EM_SAFE_FREE(p_data_tbl);
8141 EM_SAFE_FREE(sql_query_string);
8143 if (err_code != NULL)
8144 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8148 local_db_handle = emstorage_get_db_connection(multi_user_name);
8150 EMSTORAGE_START_READ_TRANSACTION(transaction);
8153 case RETRIEVE_SUMMARY:
8154 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8155 "SELECT account_id, "
8158 "server_mail_status, "
8159 "server_mailbox_name, "
8163 "file_path_mime_entity, "
8165 "flags_seen_field, "
8169 "thread_item_count "
8170 "FROM mail_tbl WHERE mail_id in (");
8174 case RETRIEVE_FIELDS_FOR_DELETE:
8175 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8176 "SELECT account_id, "
8178 "server_mail_status, "
8179 "server_mailbox_name, "
8181 "FROM mail_tbl WHERE mail_id in (");
8185 case RETRIEVE_ACCOUNT:
8186 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8187 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8192 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8193 "SELECT account_id, "
8196 "flags_seen_field, "
8198 "FROM mail_tbl WHERE mail_id in (");
8203 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8204 error = EMAIL_ERROR_INVALID_PARAM;
8208 for (i = 0; i < number_of_mails; i++)
8209 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8210 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8212 EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8214 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8215 if (SQLITE_OK != rc && -1 != rc) {
8216 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8217 error = EMAIL_ERROR_DB_FAILURE;
8221 if (rc == SQLITE_DONE) {
8222 EM_DEBUG_LOG("no matched mail found...");
8223 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8227 EM_DEBUG_LOG("item_count [%d]", item_count);
8229 if (number_of_mails != item_count) {
8230 EM_DEBUG_EXCEPTION("Can't find all emails");
8231 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8235 col_index = field_count;
8237 for (i = 0; i < item_count; i++) {
8239 case RETRIEVE_SUMMARY:
8240 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8241 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8242 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8243 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8244 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8245 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8246 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8247 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8248 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8249 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8250 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8251 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8252 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8253 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8254 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8257 case RETRIEVE_FIELDS_FOR_DELETE:
8258 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8259 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8260 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8261 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8262 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8265 case RETRIEVE_ACCOUNT:
8266 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8270 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8271 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8272 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8273 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8274 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8285 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8288 sqlite3_free_table(result);
8290 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8292 EM_SAFE_FREE(sql_query_string);
8294 if (err_code != NULL)
8297 EM_DEBUG_FUNC_END("ret [%d]", ret);
8301 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)
8303 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8305 if (mail_id <= 0 || !mail) {
8306 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8307 if (err_code != NULL)
8308 *err_code = EMAIL_ERROR_INVALID_PARAM;
8312 int ret = false, error = EMAIL_ERROR_NONE, count;
8313 char conditional_clause[QUERY_SIZE] = {0, };
8314 emstorage_mail_tbl_t* p_data_tbl = NULL;
8316 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8317 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8319 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8320 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8329 else if (p_data_tbl != NULL)
8330 emstorage_free_mail(&p_data_tbl, 1, &error);
8332 if (err_code != NULL)
8335 EM_DEBUG_FUNC_END("ret [%d]", ret);
8339 #ifdef __FEATURE_BODY_SEARCH__
8340 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)
8342 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8344 if (mail_id <= 0 || !mail_text) {
8345 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8346 if (err_code != NULL)
8347 *err_code = EMAIL_ERROR_INVALID_PARAM;
8352 int error = EMAIL_ERROR_NONE;
8354 char conditional_clause[QUERY_SIZE] = {0, };
8355 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8357 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8358 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8360 if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8361 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8369 *mail_text = p_data_tbl;
8370 else if (p_data_tbl != NULL)
8371 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8373 if (err_code != NULL)
8376 EM_DEBUG_FUNC_END("ret [%d]", ret);
8381 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8382 emstorage_search_filter_t *search,
8386 DB_STMT *search_handle,
8391 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8392 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8393 search, account_id, mailbox_id, sorting, search_handle,
8394 searched, transaction, err_code);
8396 if (!search_handle || !searched) {
8397 if (err_code != NULL)
8398 *err_code = EMAIL_ERROR_INVALID_PARAM;
8399 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8400 EM_DEBUG_FUNC_END("false");
8404 emstorage_search_filter_t* p = search;
8405 int error = EMAIL_ERROR_NONE;
8406 DB_STMT hStmt = NULL;
8407 char sql_query_string[QUERY_SIZE] = {0, };
8408 int rc, ret = false;
8409 int and = false, mail_count = 0;
8411 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8412 EMSTORAGE_START_READ_TRANSACTION(transaction);
8414 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8416 if (account_id != ALL_ACCOUNT) {
8417 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);
8422 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);
8428 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8429 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);
8431 } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8432 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);
8434 } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8435 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);
8437 } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8438 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);
8446 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");
8448 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8451 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8452 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8453 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8456 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8457 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8458 ("sqlite3_step fail:%d", rc));
8462 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8463 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8464 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8466 sqlite3_free_table(result);
8472 *search_handle = hStmt;
8473 *searched = mail_count;
8474 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8476 if (hStmt != NULL) {
8477 rc = sqlite3_finalize(hStmt);
8478 if (rc != SQLITE_OK) {
8479 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8480 error = EMAIL_ERROR_DB_FAILURE;
8484 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8487 if (err_code != NULL)
8490 EM_DEBUG_FUNC_END("ret [%d]", ret);
8494 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8496 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8498 if (search_handle == 0 || !data) {
8499 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8501 if (err_code != NULL)
8502 *err_code = EMAIL_ERROR_INVALID_PARAM;
8506 emstorage_mail_tbl_t* p_data_tbl = NULL;
8507 DB_STMT hStmt = search_handle;
8508 int rc, ret = false;
8509 int error = EMAIL_ERROR_NONE;
8513 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8516 case RETRIEVE_ENVELOPE:
8518 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8519 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8520 error = EMAIL_ERROR_OUT_OF_MEMORY;
8524 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8525 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8526 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8527 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8528 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8529 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8530 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8531 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8532 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8533 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8534 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8535 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8536 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8537 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8538 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8539 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8540 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8541 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8542 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8543 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8544 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8545 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8546 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8547 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8548 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8549 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8550 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8551 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8553 if (type == RETRIEVE_ALL) {
8554 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8555 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8556 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8557 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8558 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8559 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8560 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8561 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8562 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8563 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8564 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8565 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8566 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8567 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8570 if (p_data_tbl->body_download_status) {
8573 if (p_data_tbl->file_path_html) {
8574 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8575 p_data_tbl->body_download_status = 0;
8576 } else if (p_data_tbl->file_path_plain) {
8577 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8578 p_data_tbl->body_download_status = 0;
8580 p_data_tbl->body_download_status = 0;
8583 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8586 case RETRIEVE_SUMMARY:
8587 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8588 EM_DEBUG_EXCEPTION(" malloc failed...");
8590 error = EMAIL_ERROR_OUT_OF_MEMORY;
8594 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8596 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8597 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8598 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8599 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8600 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8602 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8605 case RETRIEVE_ADDRESS:
8606 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8607 EM_DEBUG_EXCEPTION(" malloc failed...");
8608 error = EMAIL_ERROR_OUT_OF_MEMORY;
8612 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8613 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8614 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8615 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8616 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8623 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8624 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8625 ("sqlite3_step fail:%d", rc));
8631 if (err_code != NULL)
8634 if (ret == false && p_data_tbl)
8635 emstorage_free_mail(&p_data_tbl, 1, NULL);
8637 EM_DEBUG_FUNC_END("ret [%d]", ret);
8641 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8643 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8645 int error = EMAIL_ERROR_NONE;
8646 int rc, ret = false;
8648 if (search_handle == 0) {
8649 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8650 error = EMAIL_ERROR_INVALID_PARAM;
8654 DB_STMT hStmt = search_handle;
8656 rc = sqlite3_finalize(hStmt);
8657 if (rc != SQLITE_OK) {
8658 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8659 error = EMAIL_ERROR_DB_FAILURE;
8665 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8667 if (err_code != NULL)
8670 EM_DEBUG_FUNC_END("ret [%d]", ret);
8674 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8676 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8678 if (mail_id <= 0 || !mail) {
8679 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8681 if (err_code != NULL)
8682 *err_code = EMAIL_ERROR_INVALID_PARAM;
8686 DB_STMT hStmt = NULL;
8687 char sql_query_string[QUERY_SIZE] = {0, };
8690 int error = EMAIL_ERROR_NONE;
8692 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8693 char mailbox_id_param_string[10] = {0,};
8695 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8697 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8698 "UPDATE mail_tbl SET"
8703 ", server_mail_status = ?"
8704 ", server_mailbox_name = ?"
8705 ", server_mail_id = ?"
8706 ", reference_mail_id = ?"
8707 ", full_address_from = ?"
8708 ", full_address_reply = ?" /* 10 */
8709 ", full_address_to = ?"
8710 ", full_address_cc = ?"
8711 ", full_address_bcc = ?"
8712 ", full_address_return = ?"
8714 ", body_download_status = ?"
8715 ", file_path_plain = ?"
8716 ", file_path_html = ?"
8717 ", file_path_mime_entity = ?"
8719 ", flags_seen_field = ?"
8720 ", flags_deleted_field = ?"
8721 ", flags_flagged_field = ?"
8722 ", flags_answered_field = ?"
8723 ", flags_recent_field = ?"
8724 ", flags_draft_field = ?"
8725 ", flags_forwarded_field = ?"
8731 ", report_status = ?"
8732 ", preview_text = ?"
8734 ", scheduled_sending_time = ?"
8735 ", remaining_resend_times = ?"
8737 ", replied_time = ?"
8738 ", forwarded_time = ?"
8739 ", default_charset = ?"
8740 ", eas_data_length = ?"
8742 " WHERE mail_id = %d AND account_id != 0 "
8746 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8747 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8748 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8750 _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8751 _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8752 _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8753 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8754 _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8755 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8756 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8757 _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8758 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8759 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8760 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8761 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8762 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8763 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8764 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8765 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8766 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8767 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8768 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8769 _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8770 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8771 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8772 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8773 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8774 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8775 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8776 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8777 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8778 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8779 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8780 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8781 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8782 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8783 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8784 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8785 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8786 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8787 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8788 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8789 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8790 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8791 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8792 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8794 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8795 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8796 ("sqlite3_step fail:%d", rc));
8798 rc = sqlite3_changes(local_db_handle);
8800 EM_DEBUG_LOG(" no matched mail found...");
8801 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8808 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8809 if (hStmt != NULL) {
8810 rc = sqlite3_finalize(hStmt);
8811 if (rc != SQLITE_OK) {
8812 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8813 error = EMAIL_ERROR_DB_FAILURE;
8818 if (error == EMAIL_ERROR_NONE && mail) {
8819 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8820 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8821 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8824 if (err_code != NULL)
8827 EM_DEBUG_FUNC_END("ret [%d]", ret);
8832 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8836 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int *err_code)
8838 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8840 EM_IF_NULL_RETURN_VALUE(err_code, false);
8843 int error = EMAIL_ERROR_NONE;
8845 char sql_query_string[QUERY_SIZE] = {0, };
8846 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8848 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8849 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);
8851 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8852 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8853 if (error != EMAIL_ERROR_NONE) {
8854 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8858 rc = sqlite3_changes(local_db_handle);
8860 EM_DEBUG_LOG(" No Matched Mail Exists ");
8861 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8868 if (err_code != NULL)
8871 EM_DEBUG_FUNC_END("ret [%d]", ret);
8875 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)
8877 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);
8879 int error = EMAIL_ERROR_NONE;
8882 int cur_mail_id_string = 0;
8883 int mail_id_string_buffer_length = 0;
8884 int parameter_string_length = 0;
8885 char *sql_query_string = NULL;
8886 char *mail_id_string_buffer = NULL;
8887 char *parameter_string = NULL;
8888 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8889 email_mail_attribute_type target_mail_attribute_type = 0;
8891 if (!mail_ids || !field_name || account_id == 0) {
8892 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8893 if (err_code != NULL)
8894 *err_code = EMAIL_ERROR_INVALID_PARAM;
8898 if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8899 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8900 if (err_code != NULL)
8905 /* Generating mail id list string */
8906 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8908 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8910 if (!mail_id_string_buffer) {
8911 EM_DEBUG_EXCEPTION("em_mallocfailed");
8912 if (err_code != NULL)
8913 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8917 for (i = 0; i < mail_ids_count; i++)
8918 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8920 if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8921 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8923 /* Generating notification parameter string */
8924 parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8925 parameter_string = em_malloc(parameter_string_length);
8927 if (!parameter_string) {
8928 EM_DEBUG_EXCEPTION("em_mallocfailed");
8929 if (err_code != NULL)
8930 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8931 EM_SAFE_FREE(mail_id_string_buffer);
8935 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8936 query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8938 sql_query_string = em_malloc(query_size);
8939 if (sql_query_string == NULL) {
8940 EM_DEBUG_EXCEPTION("em_mallocfailed");
8941 error = EMAIL_ERROR_OUT_OF_MEMORY;
8944 /* Write query string */
8945 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);
8947 EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
8950 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8951 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8952 if (error != EMAIL_ERROR_NONE) {
8953 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8957 if (sqlite3_changes(local_db_handle) == 0)
8958 EM_DEBUG_LOG("no mail matched...");
8963 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8965 if (error == EMAIL_ERROR_NONE && parameter_string) {
8966 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8967 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8970 EM_SAFE_FREE(mail_id_string_buffer);
8971 EM_SAFE_FREE(parameter_string);
8972 EM_SAFE_FREE(sql_query_string);
8975 if (err_code != NULL)
8978 EM_DEBUG_FUNC_END("error [%d]", error);
8982 #ifdef __FEATURE_BODY_SEARCH__
8983 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)
8985 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8987 if (mail_id <= 0 || !mail_text) {
8988 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
8989 if (err_code != NULL)
8990 *err_code = EMAIL_ERROR_INVALID_PARAM;
8995 int error = EMAIL_ERROR_NONE;
8996 DB_STMT hStmt = NULL;
8997 char sql_query_string[QUERY_SIZE] = {0, };
9002 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9003 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9005 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9006 "UPDATE mail_text_tbl SET"
9008 " WHERE mail_id = %d AND account_id != 0"
9010 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9012 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9013 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9014 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9017 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9019 if (hStmt != NULL) {
9020 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9021 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9022 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9024 rc = sqlite3_changes(local_db_handle);
9026 EM_DEBUG_LOG(" no matched mail found...");
9027 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9035 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9037 if (hStmt != NULL) {
9038 rc = sqlite3_finalize(hStmt);
9039 if (rc != SQLITE_OK) {
9040 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9041 error = EMAIL_ERROR_DB_FAILURE;
9046 if (err_code != NULL)
9049 EM_DEBUG_FUNC_END("ret [%d]", ret);
9054 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)
9056 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9059 int error = EMAIL_ERROR_NONE;
9064 DB_STMT hStmt = NULL;
9065 char sql_query_string[QUERY_SIZE] = {0, };
9066 char mailbox_id_param_string[10] = {0,};
9067 sqlite3 *local_db_handle = NULL;
9069 if (mail_id <= 0 || !mail) {
9070 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9071 if (err_code != NULL)
9072 *err_code = EMAIL_ERROR_INVALID_PARAM;
9076 local_db_handle = emstorage_get_db_connection(multi_user_name);
9078 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9082 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9083 "UPDATE mail_tbl SET"
9084 " body_download_status = ?"
9085 ", file_path_plain = ?"
9086 ", file_path_html = ?"
9087 ", file_path_mime_entity = ?"
9088 ", flags_seen_field = ?"
9089 ", flags_deleted_field = ?"
9090 ", flags_flagged_field = ?"
9091 ", flags_answered_field = ?"
9092 ", flags_recent_field = ?"
9093 ", flags_draft_field = ?"
9094 ", flags_forwarded_field = ?"
9096 ", attachment_count = ?"
9098 ", meeting_request_status = ? "
9099 ", message_class = ? "
9100 ", digest_type = ? "
9102 " WHERE mail_id = %d AND account_id != 0"
9106 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9107 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9108 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9111 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9112 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9113 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9114 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9115 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9116 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9117 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9118 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9119 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9120 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9121 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9122 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9123 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9124 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9125 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9126 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9127 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9128 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9131 case UPDATE_MAILBOX: {
9133 emstorage_mailbox_tbl_t *mailbox_tbl;
9135 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9136 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9140 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9141 "UPDATE mail_tbl SET"
9142 " mailbox_id = '%d'"
9143 ",mailbox_type = '%d'"
9144 " WHERE mail_id = %d AND account_id != 0"
9145 , mailbox_tbl->mailbox_id
9146 , mailbox_tbl->mailbox_type
9151 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9152 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9153 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9156 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9158 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9164 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9165 "UPDATE mail_tbl SET"
9166 " flags_seen_field = %d"
9167 ",flags_deleted_field = %d"
9168 ",flags_flagged_field = %d"
9169 ",flags_answered_field = %d"
9170 ",flags_recent_field = %d"
9171 ",flags_draft_field = %d"
9172 ",flags_forwarded_field = %d"
9173 " WHERE mail_id = %d AND account_id != 0"
9174 , mail->flags_seen_field
9175 , mail->flags_deleted_field
9176 , mail->flags_flagged_field
9177 , mail->flags_answered_field
9178 , mail->flags_recent_field
9179 , mail->flags_draft_field
9180 , mail->flags_forwarded_field
9182 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9185 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9186 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9187 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9191 case UPDATE_EXTRA_FLAG:
9192 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9193 "UPDATE mail_tbl SET"
9195 ", save_status = %d"
9196 ", lock_status = %d"
9197 ", report_status = %d"
9199 " WHERE mail_id = %d AND account_id != 0"
9203 , mail->report_status
9206 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9209 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9210 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9211 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9214 case UPDATE_STICKY_EXTRA_FLAG:
9215 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9216 "UPDATE mail_tbl SET"
9218 " WHERE mail_id = %d AND account_id != 0"
9221 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9224 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9225 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9226 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9230 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9231 "UPDATE mail_tbl SET"
9232 " full_address_from = ?"
9233 ", full_address_reply = ?"
9234 ", full_address_to = ?"
9235 ", full_address_cc = ?"
9236 ", full_address_bcc = ?"
9237 ", full_address_return = ?"
9239 ", file_path_plain = ?"
9241 ", flags_seen_field = ?"
9242 ", flags_deleted_field = ?"
9243 ", flags_flagged_field = ?"
9244 ", flags_answered_field = ?"
9245 ", flags_recent_field = ?"
9246 ", flags_draft_field = ?"
9247 ", flags_forwarded_field = ?"
9251 ", report_status = ?"
9253 ", file_path_html = ?"
9254 ", file_path_mime_entity = ?"
9256 ", preview_text = ?"
9257 ", body_download_status = ?"
9258 ", attachment_count = ?"
9259 ", inline_content_count = ?"
9260 ", meeting_request_status = ?"
9261 ", message_class = ?"
9264 ", scheduled_sending_time = ?"
9265 ", remaining_resend_times = ?"
9267 ", replied_time = ?"
9268 ", forwarded_time = ?"
9269 ", default_charset = ?"
9270 ", eas_data_length = ?"
9272 " WHERE mail_id = %d AND account_id != 0"
9276 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9277 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9278 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9279 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9281 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9282 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9283 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9284 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9285 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9286 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9287 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9288 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9289 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9290 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9291 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9292 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9293 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9294 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9295 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9296 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9297 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9298 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9299 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9300 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9301 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9302 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9303 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9304 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9305 _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9306 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9307 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9308 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9309 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9310 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9311 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9312 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9313 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9314 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9315 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9316 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9317 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9318 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9319 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9320 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9323 case UPDATE_DATETIME: {
9324 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9325 "UPDATE mail_tbl SET"
9326 " date_time = '%ld'"
9327 " WHERE mail_id = %d AND account_id != 0"
9331 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9332 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9333 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9337 case UPDATE_FROM_CONTACT_INFO:
9338 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9339 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9340 "UPDATE mail_tbl SET"
9341 " email_address_sender = ?,"
9342 " WHERE mail_id = %d",
9347 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9348 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9349 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9351 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9354 case UPDATE_TO_CONTACT_INFO:
9355 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9356 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9357 "UPDATE mail_tbl SET"
9358 " email_address_recipient = ?,"
9359 " WHERE mail_id = %d",
9364 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9365 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9366 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9368 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9371 case UPDATE_ALL_CONTACT_INFO:
9372 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9373 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9374 "UPDATE mail_tbl SET"
9375 " email_address_sender = ?,"
9376 " email_address_recipient = ?,"
9377 " WHERE mail_id = %d",
9382 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9383 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9384 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9386 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9387 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9391 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9392 case UPDATE_PARTIAL_BODY_DOWNLOAD:
9394 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9395 "UPDATE mail_tbl SET"
9396 " body_download_status = ?"
9397 ", file_path_plain = ?"
9398 ", file_path_html = ?"
9399 ", file_path_mime_entity = ?"
9400 ", attachment_count = ?"
9401 ", inline_content_count = ?"
9402 ", preview_text = ?"
9405 " WHERE mail_id = %d"
9409 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9410 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9411 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9414 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9415 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9416 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9417 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9418 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9419 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9420 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9421 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9422 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9427 case UPDATE_FILE_PATH:
9428 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9429 "UPDATE mail_tbl SET"
9430 ", file_path_plain = ?"
9431 ", file_path_html = ?"
9432 ", file_path_mime_entity = ?"
9433 " WHERE mail_id = %d"
9437 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9438 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9439 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9440 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9442 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9443 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9444 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9448 EM_DEBUG_LOG(" type[%d]", type);
9450 error = EMAIL_ERROR_INVALID_PARAM;
9454 if (hStmt != NULL) {
9456 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9457 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9458 ("sqlite3_step fail:%d", rc));
9459 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9460 ("sqlite3_step fail:%d", rc));
9461 rc = sqlite3_changes(local_db_handle);
9463 EM_DEBUG_LOG(" no matched mail found...");
9464 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9469 if (mail->account_id == 0) {
9470 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9471 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9472 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9475 mail->account_id = mail_for_account_tbl->account_id;
9476 if (mail_for_account_tbl)
9477 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9483 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9485 if (hStmt != NULL) {
9486 rc = sqlite3_finalize(hStmt);
9487 if (rc != SQLITE_OK) {
9488 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9489 error = EMAIL_ERROR_DB_FAILURE;
9494 if (error == EMAIL_ERROR_NONE && move_flag != 1 && transaction) {
9495 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9496 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9498 if (mail->mailbox_id == mailbox_id) {
9499 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9500 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9501 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9503 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9504 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9505 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9509 if (err_code != NULL)
9512 EM_DEBUG_FUNC_END("ret [%d]", ret);
9516 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9518 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9520 int rc, ret = false;
9521 int error = EMAIL_ERROR_NONE;
9522 int latest_mail_id = 0;
9523 sqlite3 *local_db_handle = NULL;
9524 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9525 char **result = NULL;
9527 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9528 _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9529 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9531 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9532 if (ret < 0 || latest_mail_id == 0) {
9533 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9535 local_db_handle = emstorage_get_db_connection(multi_user_name);
9537 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9538 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9539 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9540 if (NULL == result[1])
9543 rc = atoi(result[1]) + 1;
9545 sqlite3_free_table(result);
9546 latest_mail_id = rc;
9551 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9554 *mail_id = latest_mail_id;
9556 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9557 _unlockshm_mutex(mapped_for_generating_mail_id);
9558 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9564 if (err_code != NULL)
9567 EM_DEBUG_FUNC_END("ret [%d]", ret);
9571 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)
9573 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9574 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9576 if (!mail_tbl_data) {
9577 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9578 if (err_code != NULL)
9579 *err_code = EMAIL_ERROR_INVALID_PARAM;
9583 int rc, ret = false;
9584 int error = EMAIL_ERROR_NONE;
9585 char sql_query_string[QUERY_SIZE] = {0, };
9586 DB_STMT hStmt = NULL;
9588 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9590 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9593 /* increase unique id */
9594 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9597 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9598 /* EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9599 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9600 if (rc != SQLITE_OK) {
9601 EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9602 error = EMAIL_ERROR_DB_FAILURE;
9603 sqlite3_free_table(result);
9607 if (NULL == result[1])
9610 rc = atoi(result[1])+1;
9612 sqlite3_free_table(result);
9614 mail_tbl_data->mail_id = rc;
9615 mail_tbl_data->thread_id = rc;
9618 if (mail_tbl_data->date_time == 0)
9619 mail_tbl_data->date_time = time(NULL);
9621 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9622 "INSERT INTO mail_tbl VALUES "
9624 ", ?" /* account_id */
9625 ", ?" /* mailbox_id */
9626 ", ?" /* mailbox_type */
9629 ", ?" /* date_time */
9630 ", ?" /* server_mail_status */
9631 ", ?" /* server_mailbox_name */
9632 ", ?" /* server_mail_id */
9633 ", ?" /* message_id */
9635 ", ?" /* reference_mail_id */
9636 ", ?" /* full_address_from */
9637 ", ?" /* full_address_reply */
9638 ", ?" /* full_address_to */
9639 ", ?" /* full_address_cc */
9641 ", ?" /* full_address_bcc */
9642 ", ?" /* full_address_return */
9643 ", ?" /* email_address_sender */
9644 ", ?" /* email_address_recipient */
9645 ", ?" /* alias_sender */
9647 ", ?" /* alias_recipient */
9648 ", ?" /* body_download_status */
9649 ", ?" /* file_path_plain */
9650 ", ?" /* file_path_html */
9651 ", ?" /* file_path_mime_entity */
9653 ", ?" /* mail_size */
9654 ", ?" /* flags_seen_field */
9655 ", ?" /* flags_deleted_field */
9656 ", ?" /* flags_flagged_field */
9657 ", ?" /* flags_answered_field */
9659 ", ?" /* flags_recent_field */
9660 ", ?" /* flags_draft_field */
9661 ", ?" /* flags_forwarded_field */
9662 ", ?" /* DRM_status */
9663 ", ?" /* priority */
9665 ", ?" /* save_status */
9666 ", ?" /* lock_status */
9667 ", ?" /* report_status */
9668 ", ?" /* attachment_count */
9669 ", ?" /* inline_content_count */
9671 ", ?" /* thread_id */
9672 ", ?" /* thread_item_count */
9673 ", ?" /* preview_text */
9674 ", ?" /* meeting_request_status */
9675 ", ?" /* message_class */
9677 ", ?" /* digest_type */
9678 ", ?" /* smime_type */
9679 ", ?" /* scheduled_sending_time */
9680 ", ?" /* remaining_resend_times */
9683 ", ?" /* replied_time */
9684 ", ?" /* forwarded_time */
9685 ", ?" /* default charset */
9686 ", ?" /* eas_data_length */
9687 ", ?" /* eas_data */
9688 ", ?" /* user_name */
9691 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9693 EM_SAFE_STRLEN(sql_query_string),
9697 if (rc != SQLITE_OK) {
9698 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9699 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9700 error = EMAIL_ERROR_DB_FAILURE;
9704 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9705 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9706 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9707 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9708 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9710 _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9711 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9712 _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);
9713 _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);
9714 _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);
9716 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9717 _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);
9718 _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);
9719 _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);
9720 _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);
9722 _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);
9723 _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);
9724 _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);
9725 _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);
9726 _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);
9728 _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);
9729 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9730 _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);
9731 _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);
9732 _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);
9734 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9735 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9736 _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9737 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9738 _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9740 _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9741 _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9742 _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9743 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9744 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9746 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9747 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9748 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9749 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9750 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9752 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9753 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9754 _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9755 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9756 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9758 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9759 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9760 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9761 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9762 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9764 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9765 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9766 _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);
9767 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9768 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9769 _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);
9771 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9772 if (rc == SQLITE_FULL) {
9773 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9774 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9777 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9778 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9779 error = EMAIL_ERROR_DB_FAILURE;
9785 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9787 if (hStmt != NULL) {
9788 rc = sqlite3_finalize(hStmt);
9789 if (rc != SQLITE_OK) {
9790 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9791 error = EMAIL_ERROR_DB_FAILURE;
9795 if (err_code != NULL)
9798 EM_PROFILE_END(profile_emstorage_add_mail);
9799 EM_DEBUG_FUNC_END("ret [%d]", ret);
9803 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)
9805 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);
9807 int ret = false, i, cur_conditional_clause = 0;
9808 int error = EMAIL_ERROR_NONE;
9809 int target_account_id;
9810 int conditional_clause_len = 0;
9811 char *sql_query_string = NULL, *conditional_clause = NULL;
9812 emstorage_mailbox_tbl_t *result_mailbox = NULL;
9813 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9814 char* target_mailbox_name = NULL;
9816 if (!mail_ids || input_mailbox_id <= 0) {
9817 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9818 if (err_code != NULL)
9819 *err_code = EMAIL_ERROR_INVALID_PARAM;
9823 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9825 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9826 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9827 if (err_code != NULL)
9832 if (result_mailbox->mailbox_name) {
9833 if (strstr(result_mailbox->mailbox_name, "'")) {
9834 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9836 target_mailbox_name = strdup(result_mailbox->mailbox_name);
9840 target_mailbox_type = result_mailbox->mailbox_type;
9841 target_account_id = result_mailbox->account_id;
9842 emstorage_free_mailbox(&result_mailbox, 1, NULL);
9844 conditional_clause_len = (sizeof(char) * 8 * number_of_mails) + 512;
9845 conditional_clause = em_malloc(conditional_clause_len);
9846 if (conditional_clause == NULL) {
9847 EM_DEBUG_EXCEPTION("em_mallocfailed");
9848 error = EMAIL_ERROR_OUT_OF_MEMORY;
9851 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9853 for (i = 0; i < number_of_mails; i++)
9854 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9857 char *last_comma = rindex(conditional_clause, ',');
9858 if (last_comma) *last_comma = ')'; /* replace , with) */
9860 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9862 /* Updating a mail_tbl */
9864 sql_query_string = em_malloc(conditional_clause_len);
9865 if (sql_query_string == NULL) {
9866 EM_DEBUG_EXCEPTION("em_mallocfailed");
9867 error = EMAIL_ERROR_OUT_OF_MEMORY;
9870 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);
9871 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9873 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9874 if (error != EMAIL_ERROR_NONE) {
9875 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9879 /* Updating a mail_attachment_tbl */
9880 memset(sql_query_string, 0x00, conditional_clause_len);
9881 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);
9882 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9883 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9884 if (error != EMAIL_ERROR_NONE) {
9885 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9889 /* Updating a mail_meeting_tbl */
9890 memset(sql_query_string, 0x00, conditional_clause_len);
9891 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);
9892 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9893 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9894 if (error != EMAIL_ERROR_NONE) {
9895 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9899 #ifdef __FEATURE_BODY_SEARCH__
9900 /* Updating mail_text_tbl */
9901 memset(sql_query_string, 0x00, conditional_clause_len);
9902 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);
9903 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9904 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9905 if (error != EMAIL_ERROR_NONE) {
9906 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9911 /* Updating a mail_read_mail_uid_tbl */
9912 memset(conditional_clause, 0x00, conditional_clause_len);
9913 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9915 for (i = 0; i < number_of_mails; i++)
9916 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9919 last_comma = rindex(conditional_clause, ',');
9920 if (last_comma) *last_comma = ')'; /* replace , with) */
9922 memset(sql_query_string, 0x00, conditional_clause_len);
9923 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);
9924 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9925 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9926 if (error != EMAIL_ERROR_NONE) {
9927 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9934 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9936 EM_SAFE_FREE(target_mailbox_name);
9937 EM_SAFE_FREE(conditional_clause);
9938 EM_SAFE_FREE(sql_query_string);
9940 if (err_code != NULL)
9943 EM_DEBUG_FUNC_END("ret [%d]", ret);
9947 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9949 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9952 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9953 if (err_code != NULL)
9954 *err_code = EMAIL_ERROR_INVALID_PARAM;
9959 int error = EMAIL_ERROR_NONE;
9960 char sql_query_string[QUERY_SIZE] = {0, };
9961 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9962 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9964 memset(sql_query_string, 0x00, sizeof(sql_query_string));
9966 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9967 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9968 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9969 if (error != EMAIL_ERROR_NONE) {
9970 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9977 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9979 if (err_code != NULL)
9982 EM_DEBUG_FUNC_END("ret [%d]", ret);
9986 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
9988 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
9990 int ret = false, i, cur_sql_query_string = 0;
9991 int error = EMAIL_ERROR_NONE;
9993 char *sql_query_string = NULL;
9996 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9997 if (err_code != NULL)
9998 *err_code = EMAIL_ERROR_INVALID_PARAM;
10002 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10003 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10005 query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10006 sql_query_string = em_malloc(query_size);
10007 if (sql_query_string == NULL) {
10008 EM_DEBUG_EXCEPTION("em_mallocfailed");
10009 error = EMAIL_ERROR_OUT_OF_MEMORY;
10013 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10015 for (i = 0; i < number_of_mails; i++)
10016 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10018 /* prevent 34414 */
10019 char *last_comma = rindex(sql_query_string, ',');
10020 if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10022 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10023 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10024 if (error != EMAIL_ERROR_NONE) {
10025 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10029 #ifdef __FEATURE_BODY_SEARCH__
10030 /* delete mail_text from mail_text_tbl */
10031 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10033 for (i = 0; i < number_of_mails; i++)
10034 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10036 last_comma = rindex(sql_query_string, ',');
10037 *last_comma = ')'; /* replace , with) */
10039 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10040 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10041 if (error != EMAIL_ERROR_NONE) {
10042 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10050 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10052 EM_SAFE_FREE(sql_query_string);
10053 if (err_code != NULL)
10056 EM_DEBUG_FUNC_END("ret [%d]", ret);
10060 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10062 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10064 if (account_id < FIRST_ACCOUNT_ID) {
10065 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10067 if (err_code != NULL)
10068 *err_code = EMAIL_ERROR_INVALID_PARAM;
10072 int rc, ret = false;
10073 int error = EMAIL_ERROR_NONE;
10074 char sql_query_string[QUERY_SIZE] = {0, };
10076 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10077 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10079 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10080 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10081 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10082 if (error != EMAIL_ERROR_NONE) {
10083 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10087 rc = sqlite3_changes(local_db_handle);
10089 EM_DEBUG_LOG(" no mail found...");
10090 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10093 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
10094 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10095 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10096 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10097 if (error != EMAIL_ERROR_NONE) {
10098 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10102 rc = sqlite3_changes(local_db_handle);
10104 EM_DEBUG_LOG("No mail found...");
10105 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10108 #ifdef __FEATURE_BODY_SEARCH__
10109 /* Delete all mail_text in mail_text_tbl table based on account id */
10110 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10111 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10112 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10113 if (error != EMAIL_ERROR_NONE) {
10114 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10118 rc = sqlite3_changes(local_db_handle);
10120 EM_DEBUG_LOG("No mail found...");
10121 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10128 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10130 if (error == EMAIL_ERROR_NONE) {
10131 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10132 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10135 if (err_code != NULL)
10138 EM_DEBUG_FUNC_END("ret [%d]", ret);
10142 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10144 EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10146 if (mailbox == NULL) {
10147 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10148 if (err_code != NULL)
10149 *err_code = EMAIL_ERROR_INVALID_PARAM;
10154 int error = EMAIL_ERROR_NONE;
10155 char sql_query_string[QUERY_SIZE] = {0, };
10156 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10157 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10159 if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10160 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);
10161 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10162 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10163 if (error != EMAIL_ERROR_NONE) {
10164 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10168 /* Delete Mails from mail_read_mail_uid_tbl */
10169 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);
10170 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10171 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10172 if (error != EMAIL_ERROR_NONE) {
10173 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10177 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);
10178 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10179 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10180 if (error != EMAIL_ERROR_NONE) {
10181 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10185 /* Delete Mails from mail_read_mail_uid_tbl */
10186 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);
10187 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10188 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10189 if (error != EMAIL_ERROR_NONE) {
10190 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10194 #ifdef __FEATURE_BODY_SEARCH__
10195 /* Delete Mails from mail_text_tbl */
10196 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);
10197 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10198 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10199 if (error != EMAIL_ERROR_NONE) {
10200 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10208 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10210 if (error == EMAIL_ERROR_NONE) {
10211 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10212 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10215 if (err_code != NULL)
10218 EM_DEBUG_FUNC_END("ret [%d]", ret);
10222 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10224 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10227 if ((mail_list == NULL) || (*mail_list == NULL)) {
10228 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10231 *err_code = EMAIL_ERROR_INVALID_PARAM;
10235 emstorage_mail_tbl_t* p = *mail_list;
10238 for (; i < count; i++, p++) {
10239 EM_SAFE_FREE(p->server_mailbox_name);
10240 EM_SAFE_FREE(p->server_mail_id);
10241 EM_SAFE_FREE(p->full_address_from);
10242 EM_SAFE_FREE(p->full_address_reply);
10243 EM_SAFE_FREE(p->full_address_to);
10244 EM_SAFE_FREE(p->full_address_cc);
10245 EM_SAFE_FREE(p->full_address_bcc);
10246 EM_SAFE_FREE(p->full_address_return);
10247 EM_SAFE_FREE(p->subject);
10248 EM_SAFE_FREE(p->file_path_plain);
10249 EM_SAFE_FREE(p->file_path_html);
10250 EM_SAFE_FREE(p->file_path_mime_entity);
10251 EM_SAFE_FREE(p->message_id);
10252 EM_SAFE_FREE(p->email_address_sender);
10253 EM_SAFE_FREE(p->email_address_recipient);
10254 EM_SAFE_FREE(p->preview_text);
10255 EM_SAFE_FREE(p->alias_sender);
10256 EM_SAFE_FREE(p->alias_recipient);
10257 EM_SAFE_FREE(p->default_charset);
10258 EM_SAFE_FREE(p->pgp_password);
10259 EM_SAFE_FREE(p->eas_data);
10260 EM_SAFE_FREE(p->user_name);
10262 EM_SAFE_FREE(*mail_list);
10265 if (err_code != NULL)
10266 *err_code = EMAIL_ERROR_NONE;
10268 EM_DEBUG_FUNC_END();
10272 #ifdef __FEATURE_BODY_SEARCH__
10273 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10275 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10278 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10279 EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10283 emstorage_mail_text_tbl_t *p = *mail_text_list;
10286 for (; i < count; i++, p++) {
10287 EM_SAFE_FREE(p->body_text);
10289 EM_SAFE_FREE(*mail_text_list);
10292 EM_DEBUG_FUNC_END();
10296 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10298 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10300 if (mail_id <= 0 || !count) {
10301 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10302 if (err_code != NULL)
10303 *err_code = EMAIL_ERROR_INVALID_PARAM;
10307 int rc = -1, ret = false;
10308 int error = EMAIL_ERROR_NONE;
10309 char sql_query_string[QUERY_SIZE] = {0, };
10311 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10312 EMSTORAGE_START_READ_TRANSACTION(transaction);
10314 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10318 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10319 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10320 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10322 *count = atoi(result[1]);
10323 sqlite3_free_table(result);
10328 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10330 if (err_code != NULL)
10333 EM_DEBUG_FUNC_END("ret [%d]", ret);
10337 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)
10339 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);
10341 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10342 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10343 return EMAIL_ERROR_INVALID_PARAM;
10346 int error = EMAIL_ERROR_NONE;
10349 char **result = NULL;
10350 char sql_query_string[QUERY_SIZE] = {0, };
10351 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10352 DB_STMT hStmt = NULL;
10353 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10355 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10356 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10357 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10358 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10359 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10361 *output_attachment_count = atoi(result[1]);
10362 sqlite3_free_table(result);
10364 if (*output_attachment_count == 0) {
10365 error = EMAIL_ERROR_NONE;
10369 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10372 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10373 error = EMAIL_ERROR_OUT_OF_MEMORY;
10377 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10378 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10380 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10381 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)));
10383 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10384 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10386 if (rc == SQLITE_DONE) {
10387 EM_DEBUG_EXCEPTION("no matched attachment found...");
10388 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10391 for (i = 0; i < *output_attachment_count; i++) {
10392 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10393 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10394 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10395 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10396 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10397 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10398 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10399 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10400 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10401 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10402 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10403 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10404 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10405 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10406 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10407 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10408 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10413 if (error == EMAIL_ERROR_NONE)
10414 *output_attachment_list = p_data_tbl;
10415 else if (p_data_tbl != NULL)
10416 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10419 rc = sqlite3_finalize(hStmt);
10420 if (rc != SQLITE_OK) {
10421 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10422 error = EMAIL_ERROR_DB_FAILURE;
10423 if (*output_attachment_list)
10424 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10428 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10430 EM_DEBUG_FUNC_END("error [%d]", error);
10434 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10436 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10438 if (attachment_id <= 0 || !attachment) {
10439 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10440 if (err_code != NULL)
10441 *err_code = EMAIL_ERROR_INVALID_PARAM;
10445 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10446 int rc, ret = false;
10447 int error = EMAIL_ERROR_NONE;
10448 char sql_query_string[QUERY_SIZE] = {0, };
10450 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10451 EMSTORAGE_START_READ_TRANSACTION(transaction);
10453 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10455 sqlite3_stmt* hStmt = NULL;
10457 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10458 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10460 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10461 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10464 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10465 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10466 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10468 if (rc == SQLITE_DONE) {
10469 EM_DEBUG_LOG("no matched attachment found...");
10470 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10474 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10475 EM_DEBUG_EXCEPTION("malloc failed...");
10476 error = EMAIL_ERROR_OUT_OF_MEMORY;
10480 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10481 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10482 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10483 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10484 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10485 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10486 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10487 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10488 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10489 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10490 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10491 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10492 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10494 #ifdef __ATTACHMENT_OPTI__
10495 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10496 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10503 *attachment = p_data_tbl;
10505 if (hStmt != NULL) {
10506 rc = sqlite3_finalize(hStmt);
10507 if (rc != SQLITE_OK) {
10508 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10509 error = EMAIL_ERROR_DB_FAILURE;
10513 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10515 if (err_code != NULL)
10518 EM_DEBUG_FUNC_END("ret [%d]", ret);
10522 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)
10524 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);
10526 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10527 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10529 if (err_code != NULL)
10530 *err_code = EMAIL_ERROR_INVALID_PARAM;
10534 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10536 int rc, ret = false;
10537 int error = EMAIL_ERROR_NONE;
10538 char sql_query_string[QUERY_SIZE] = {0, };
10540 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10541 EMSTORAGE_START_READ_TRANSACTION(transaction);
10543 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));
10544 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10546 DB_STMT hStmt = NULL;
10548 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10549 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10550 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10553 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10554 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10555 ("sqlite3_step fail:%d", rc));
10557 if (rc == SQLITE_DONE) {
10558 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10559 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10563 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10564 EM_DEBUG_EXCEPTION(" malloc failed...");
10565 error = EMAIL_ERROR_OUT_OF_MEMORY;
10569 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10570 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10571 p_data_tbl->attachment_name = cpy_str(p);
10572 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10573 p_data_tbl->attachment_path = cpy_str(p);
10574 if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10575 p_data_tbl->content_id = cpy_str(p);
10576 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10577 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10578 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10579 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10580 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10581 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10582 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10583 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10584 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10585 p_data_tbl->attachment_mime_type = cpy_str(p);
10586 #ifdef __ATTACHMENT_OPTI__
10587 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10588 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10589 p_data_tbl->section = cpy_str(p);
10595 *attachment_tbl = p_data_tbl;
10597 if (hStmt != NULL) {
10598 rc = sqlite3_finalize(hStmt);
10599 if (rc != SQLITE_OK) {
10600 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10601 error = EMAIL_ERROR_DB_FAILURE;
10605 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10607 if (err_code != NULL)
10610 EM_DEBUG_FUNC_END("ret [%d]", ret);
10614 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)
10616 EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10618 if (attachment_path == NULL || !attachment) {
10619 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10620 if (err_code != NULL)
10621 *err_code = EMAIL_ERROR_INVALID_PARAM;
10625 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10626 int rc, ret = false;
10627 int error = EMAIL_ERROR_NONE;
10628 char sql_query_string[QUERY_SIZE] = {0, };
10630 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10631 EMSTORAGE_START_READ_TRANSACTION(transaction);
10633 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10635 sqlite3_stmt* hStmt = NULL;
10637 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10638 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10640 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10641 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10644 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10645 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10646 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10648 if (rc == SQLITE_DONE) {
10649 EM_DEBUG_LOG("no matched attachment found...");
10650 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10654 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10655 EM_DEBUG_EXCEPTION("malloc failed...");
10656 error = EMAIL_ERROR_OUT_OF_MEMORY;
10660 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10661 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10662 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10663 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10664 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10665 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10666 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10667 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10668 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10669 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10670 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10671 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10672 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10674 #ifdef __ATTACHMENT_OPTI__
10675 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10676 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10683 *attachment = p_data_tbl;
10685 if (hStmt != NULL) {
10686 rc = sqlite3_finalize(hStmt);
10687 if (rc != SQLITE_OK) {
10688 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10689 error = EMAIL_ERROR_DB_FAILURE;
10693 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10695 if (err_code != NULL)
10698 EM_DEBUG_FUNC_END("ret [%d]", ret);
10702 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)
10704 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10706 if (mail_id <= 0 || !attachment) {
10707 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10708 if (err_code != NULL)
10709 *err_code = EMAIL_ERROR_INVALID_PARAM;
10713 int rc, ret = false;
10714 int error = EMAIL_ERROR_NONE;
10715 DB_STMT hStmt = NULL;
10716 char sql_query_string[QUERY_SIZE] = {0, };
10720 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10722 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10725 case UPDATE_MAILBOX:
10726 EM_DEBUG_LOG("UPDATE_MAILBOX");
10727 if (!attachment->mailbox_id) {
10728 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10729 error = EMAIL_ERROR_INVALID_PARAM;
10732 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10733 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10735 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10736 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10737 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10738 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10740 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10743 case UPDATE_SAVENAME:
10744 EM_DEBUG_LOG("UPDATE_SAVENAME");
10745 if (!attachment->attachment_path) {
10746 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10747 error = EMAIL_ERROR_INVALID_PARAM;
10751 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10752 "UPDATE mail_attachment_tbl SET"
10753 " attachment_size = ?"
10754 ", attachment_save_status = ?"
10755 ", attachment_path = ?"
10756 " WHERE mail_id = %d"
10757 " AND attachment_id = %d"
10758 , attachment->mail_id
10759 , attachment->attachment_id);
10762 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10763 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10764 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10765 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10767 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10768 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10769 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10773 EM_DEBUG_LOG("type[%d]", type);
10774 error = EMAIL_ERROR_INVALID_PARAM;
10777 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10779 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10780 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10781 ("sqlite3_step fail:%d", rc));
10786 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10788 if (hStmt != NULL) {
10789 rc = sqlite3_finalize(hStmt);
10790 if (rc != SQLITE_OK) {
10791 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10792 error = EMAIL_ERROR_DB_FAILURE;
10796 if (err_code != NULL)
10798 EM_DEBUG_FUNC_END("ret [%d]", ret);
10802 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)
10804 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);
10808 int error = EMAIL_ERROR_NONE;
10810 int account_id = 0;
10811 char sql_query_string[QUERY_SIZE] = {0, };
10812 char *replaced_mailbox_name = NULL;
10813 char *replaced_alias = NULL;
10814 sqlite3 *local_db_handle = NULL;
10815 DB_STMT hStmt = NULL;
10816 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10818 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10819 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10820 return EMAIL_ERROR_INVALID_PARAM;
10823 if (strstr(input_new_mailbox_name, "'")) {
10824 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10826 replaced_mailbox_name = strdup(input_new_mailbox_name);
10829 if (strstr(input_new_mailbox_alias, "'")) {
10830 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10832 replaced_alias = strdup(input_new_mailbox_alias);
10835 local_db_handle = emstorage_get_db_connection(multi_user_name);
10837 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10838 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10839 EM_SAFE_FREE(replaced_mailbox_name);
10840 EM_SAFE_FREE(replaced_alias);
10844 if (old_mailbox_data == NULL) {
10845 EM_DEBUG_LOG("old_mailbox_data is NULL");
10846 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10850 account_id = old_mailbox_data->account_id;
10852 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10854 if (input_eas_data && input_eas_data_length > 0) {
10855 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10856 "UPDATE mail_box_tbl SET"
10857 " mailbox_name = ?"
10860 ",eas_data_length = ?"
10861 " WHERE mailbox_id = ?");
10863 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10865 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10866 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10868 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10869 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10870 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10871 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10872 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10874 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10875 "UPDATE mail_box_tbl SET"
10876 " mailbox_name = ?"
10878 " WHERE mailbox_id = ?");
10880 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10882 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10883 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10885 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10886 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10887 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10891 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10892 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10893 ("sqlite3_step fail:%d", rc));
10894 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10895 ("sqlite3_step fail:%d", rc));
10897 if (sqlite3_changes(local_db_handle) == 0)
10898 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10903 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10904 if (error == EMAIL_ERROR_NONE) {
10905 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10906 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10908 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10909 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10912 EM_SAFE_FREE(replaced_mailbox_name);
10913 EM_SAFE_FREE(replaced_alias);
10915 if (old_mailbox_data)
10916 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10918 if (hStmt != NULL) {
10919 rc = sqlite3_finalize(hStmt);
10920 if (rc != SQLITE_OK) {
10921 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10922 error = EMAIL_ERROR_DB_FAILURE;
10926 EM_DEBUG_FUNC_END("error [%d]", error);
10930 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10932 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10933 int rc, ret = false;
10934 int error = EMAIL_ERROR_NONE;
10935 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10938 if (!attachment_no) {
10939 EM_DEBUG_EXCEPTION("Invalid attachment");
10940 if (err_code != NULL)
10941 *err_code = EMAIL_ERROR_INVALID_PARAM;
10945 *attachment_no = -1;
10947 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10950 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10951 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10952 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10954 if (NULL == result[1])
10957 rc = atoi(result[1])+1;
10959 sqlite3_free_table(result);
10961 *attachment_no = rc;
10962 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
10967 if (err_code != NULL)
10970 EM_DEBUG_FUNC_END("ret [%d]", ret);
10974 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
10976 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
10980 int rc, ret = false;
10981 int error = EMAIL_ERROR_NONE;
10982 char sql_query_string[QUERY_SIZE] = {0, };
10984 DB_STMT hStmt = NULL;
10985 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10987 if (!attachment_tbl) {
10988 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
10989 if (err_code != NULL)
10990 *err_code = EMAIL_ERROR_INVALID_PARAM;
10994 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10996 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10998 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10999 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11000 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11002 if (NULL == result[1]) rc = 1;
11003 else rc = atoi(result[1]) + 1;
11004 sqlite3_free_table(result);
11006 attachment_tbl->attachment_id = rc;
11008 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11009 "INSERT INTO mail_attachment_tbl VALUES "
11010 "(?" /* attachment_id */
11011 ", ?" /* attachment_name */
11012 ", ?" /* attachment_path */
11013 ", ?" /* content_id */
11014 ", ?" /* attachment_size */
11015 ", ?" /* mail_id */
11016 ", ?" /* account_id */
11017 ", ?" /* mailbox_id */
11018 ", ?" /* attachment_save_status */
11019 ", ?" /* attachment_drm_type */
11020 ", ?" /* attachment_drm_method */
11021 ", ?" /* attachment_inline_content_status */
11022 ", ?" /* attachment_mime_type */
11023 #ifdef __ATTACHMENT_OPTI__
11030 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11031 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11032 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11034 _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11035 _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);
11036 _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);
11037 _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);
11038 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11039 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11040 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11041 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11042 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11043 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11044 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11045 _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11046 _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);
11047 #ifdef __ATTACHMENT_OPTI__
11048 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11049 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11053 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11054 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11055 ("sqlite3_step fail:%d", rc));
11056 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11057 ("sqlite3_step fail:%d", rc));
11059 rc = sqlite3_changes(local_db_handle);
11061 EM_DEBUG_LOG(" no matched mail found...");
11062 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11069 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11071 if (hStmt != NULL) {
11072 rc = sqlite3_finalize(hStmt);
11073 if (rc != SQLITE_OK) {
11074 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11075 error = EMAIL_ERROR_DB_FAILURE;
11079 if (err_code != NULL)
11082 EM_DEBUG_FUNC_END("ret [%d]", ret);
11086 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11088 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11090 int rc, ret = false, field_idx = 0;
11091 int error = EMAIL_ERROR_NONE;
11092 DB_STMT hStmt = NULL;
11093 char sql_query_string[QUERY_SIZE] = {0, };
11095 if (!attachment_tbl) {
11096 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11098 *err_code = EMAIL_ERROR_INVALID_PARAM;
11102 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11104 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11106 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11107 "UPDATE mail_attachment_tbl SET "
11108 " attachment_name = ?"
11109 ", attachment_path = ?"
11111 ", attachment_size = ?"
11115 ", attachment_save_status = ?"
11116 ", attachment_drm_type = ?"
11117 ", attachment_drm_method = ?"
11118 ", attachment_inline_content_status = ? "
11119 ", attachment_mime_type = ? "
11120 " WHERE attachment_id = ?;");
11123 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11125 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11126 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11128 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11129 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11130 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11131 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11132 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11133 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11134 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11135 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11136 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11137 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11138 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11139 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11140 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11143 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11144 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11145 ("sqlite3_step fail:%d", rc));
11146 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11147 ("sqlite3_step fail:%d", rc));
11149 rc = sqlite3_changes(local_db_handle);
11151 EM_DEBUG_LOG(" no matched mail found...");
11152 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11159 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11160 if (hStmt != NULL) {
11161 rc = sqlite3_finalize(hStmt);
11162 if (rc != SQLITE_OK) {
11163 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11164 error = EMAIL_ERROR_DB_FAILURE;
11168 if (err_code != NULL)
11171 EM_DEBUG_FUNC_END("ret [%d]", ret);
11175 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11177 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11179 if (attachment_id < 0) {
11180 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11181 if (err_code != NULL)
11182 *err_code = EMAIL_ERROR_INVALID_PARAM;
11187 int error = EMAIL_ERROR_NONE;
11188 char sql_query_string[QUERY_SIZE] = {0, };
11189 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11191 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11193 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11195 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11196 if (error != EMAIL_ERROR_NONE) {
11197 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11204 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11209 EM_DEBUG_FUNC_END("ret [%d]", ret);
11213 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11215 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11217 int error = EMAIL_ERROR_NONE;
11218 char sql_query_string[QUERY_SIZE] = {0, };
11219 sqlite3 *local_db_handle = NULL;
11221 if (mail_id <= 0) {
11222 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11223 if (err_code != NULL)
11224 *err_code = EMAIL_ERROR_INVALID_PARAM;
11228 local_db_handle = emstorage_get_db_connection(multi_user_name);
11230 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11232 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11233 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11234 if (error != EMAIL_ERROR_NONE) {
11235 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11242 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11247 EM_DEBUG_FUNC_END("ret [%d]", ret);
11251 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11253 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11255 int error = EMAIL_ERROR_NONE;
11257 char sql_query_string[QUERY_SIZE] = {0, };
11258 char *replaced_mailbox = NULL;
11259 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11261 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11263 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11265 if (account_id != ALL_ACCOUNT) /* '0' means all account */
11266 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);
11268 if (mailbox) /* NULL means all mailbox_name */ {
11269 if (strstr(mailbox, "'")) {
11270 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11272 replaced_mailbox = strdup(mailbox);
11275 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);
11276 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11279 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11280 if (error != EMAIL_ERROR_NONE) {
11281 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11288 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11290 if (err_code != NULL)
11293 EM_DEBUG_FUNC_END("ret [%d]", ret);
11297 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11299 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11302 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11303 EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11304 if (err_code != NULL)
11305 *err_code = EMAIL_ERROR_INVALID_PARAM;
11309 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11312 for (i = 0; i < count; i++) {
11313 EM_SAFE_FREE(p[i].attachment_name);
11314 EM_SAFE_FREE(p[i].attachment_path);
11315 EM_SAFE_FREE(p[i].content_id);
11316 EM_SAFE_FREE(p[i].attachment_mime_type);
11317 #ifdef __ATTACHMENT_OPTI__
11318 EM_SAFE_FREE(p[i].section);
11323 *attachment_tbl_list = NULL;
11326 if (err_code != NULL)
11327 *err_code = EMAIL_ERROR_NONE;
11328 EM_DEBUG_FUNC_END();
11332 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11334 EM_PROFILE_BEGIN(emStorageBeginTransaction);
11337 _timedlock_shm_mutex(mapped_for_db_lock, 2);
11339 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11342 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11343 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11344 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11346 if (ret == false) {
11347 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11350 EM_PROFILE_END(emStorageBeginTransaction);
11351 EM_DEBUG_FUNC_END("ret [%d]", ret);
11355 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11357 EM_DEBUG_FUNC_BEGIN();
11361 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11363 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11364 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11366 if (ret == false && err_code != NULL)
11367 *err_code = EMAIL_ERROR_DB_FAILURE;
11369 _unlockshm_mutex(mapped_for_db_lock);
11371 EM_DEBUG_FUNC_END("ret [%d]", ret);
11375 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11377 EM_DEBUG_FUNC_BEGIN();
11380 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11382 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11383 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11384 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11386 if (ret == false && err_code != NULL)
11387 *err_code = EMAIL_ERROR_DB_FAILURE;
11389 _unlockshm_mutex(mapped_for_db_lock);
11391 EM_DEBUG_FUNC_END("ret [%d]", ret);
11395 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11397 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11399 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11401 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11403 if (err_code != NULL)
11404 *err_code = EMAIL_ERROR_INVALID_PARAM;
11410 int error = EMAIL_ERROR_NONE;
11411 int mail_count = 0;
11413 if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11414 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11419 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11420 if (mail_count >= mailbox->mail_slot_size)
11431 if (err_code != NULL)
11434 EM_DEBUG_FUNC_END("ret [%d]", ret);
11438 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11440 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11443 int error = EMAIL_ERROR_NONE;
11444 char sql_query_string[QUERY_SIZE] = {0, };
11445 const email_db_object_t* tables = _g_db_tables;
11446 const email_db_object_t* indexes = _g_db_indexes;
11448 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11449 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11451 if (!emstorage_delete_dir((char *)MAILHOME, &error)) {
11452 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11457 mkdir(MAILHOME, DIRECTORY_PERMISSION);
11458 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11459 chmod(MAILTEMP, 0777);
11461 /* first clear index. */
11462 while (indexes->object_name) {
11463 if (indexes->data_flag) {
11464 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11465 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11466 if (error != EMAIL_ERROR_NONE) {
11467 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11474 while (tables->object_name) {
11475 if (tables->data_flag) {
11476 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11477 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11478 if (error != EMAIL_ERROR_NONE) {
11479 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11489 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11491 if (err_code != NULL)
11494 EM_DEBUG_FUNC_END("ret [%d]", ret);
11497 /*======================= DB File Utils =============================================*/
11498 #include <dirent.h>
11499 #include <sys/types.h>
11500 #define DIR_SEPERATOR "/"
11502 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11504 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11505 char delims[] = "/";
11506 char *result = NULL;
11507 gchar **token = NULL;
11509 token = g_strsplit_set(file_name, delims, 1);
11511 if (token && token[0]) {
11512 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11513 result = EM_SAFE_STRDUP(token[0]);
11515 EM_DEBUG_LOG(">>>> No Need to create Directory");
11522 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11523 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11525 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);
11526 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11529 int error = EMAIL_ERROR_NONE;
11530 char *dir_name = NULL;
11531 char create_dir[1024] = {0};
11532 char *temp_file = NULL;
11533 char *prefix_path = NULL;
11534 char *modified_fname = NULL;
11536 if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11537 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);
11538 error = EMAIL_ERROR_INVALID_PARAM;
11542 snprintf(path_buf, 512, "%s", MAILHOME);
11543 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11546 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11549 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11552 temp_file = EM_SAFE_STRDUP(fname);
11553 if (temp_file && strstr(temp_file, "/")) {
11554 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11559 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11560 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11561 mkdir(create_dir, DIRECTORY_PERMISSION);
11562 EM_SAFE_FREE(dir_name);
11566 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11568 /* Did not allow the slash */
11569 modified_fname = reg_replace_new(fname, "/", "_");
11570 EM_DEBUG_LOG("modified_fname : [%s]", modified_fname);
11572 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11574 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11575 char *modified_name = NULL;
11576 int remain_len = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11578 if (remain_len <= 0) {
11579 error = EMAIL_ERROR_MAX_EXCEEDED;
11583 if (remain_len > MAX_FILENAME) {
11584 remain_len = MAX_FILENAME;
11587 modified_name = em_shrink_filename(modified_fname, remain_len);
11589 if (!modified_name) {
11590 error = EMAIL_ERROR_MAX_EXCEEDED;
11594 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_name);
11595 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11596 EM_SAFE_FREE(modified_name);
11598 if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11599 char *modified_name = NULL;
11601 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11602 if (!modified_name) {
11603 error = EMAIL_ERROR_MAX_EXCEEDED;
11607 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_name);
11608 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11609 EM_SAFE_FREE(modified_name);
11611 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_fname);
11616 EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11618 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11619 error = emcore_get_container_path(multi_user_name, &prefix_path);
11620 if (error != EMAIL_ERROR_NONE) {
11621 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11624 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11625 EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11627 snprintf(move_buf, 512, "%s", path_buf);
11628 EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11635 EM_SAFE_FREE(temp_file);
11636 EM_SAFE_FREE(prefix_path);
11637 EM_SAFE_FREE(modified_fname);
11639 if (err_code != NULL)
11642 EM_PROFILE_END(profile_emstorage_get_save_name);
11643 EM_DEBUG_FUNC_END("ret [%d]", ret);
11648 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)
11650 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);
11652 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11653 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);
11654 if (err_code != NULL)
11655 *err_code = EMAIL_ERROR_INVALID_PARAM;
11659 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11662 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11667 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11672 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11674 EM_DEBUG_FUNC_END();
11679 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11681 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11682 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11684 int error = EMAIL_ERROR_NONE;
11687 char *prefix_path = NULL;
11688 char errno_buf[ERRNO_BUF_SIZE] = {0};
11690 memset(buf, 0x00, sizeof(buf));
11692 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11693 error = emcore_get_container_path(multi_user_name, &prefix_path);
11694 if (error != EMAIL_ERROR_NONE) {
11695 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11699 prefix_path = strdup("");
11702 if (account_id >= FIRST_ACCOUNT_ID) {
11703 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11709 if (stat(buf, &sbuf) == 0) {
11710 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11711 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11712 error = EMAIL_ERROR_SYSTEM_FAILURE;
11716 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11717 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11718 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11719 error = EMAIL_ERROR_SYSTEM_FAILURE;
11721 error = EMAIL_ERROR_OUT_OF_MEMORY;
11725 if (account_id == EML_FOLDER) {
11732 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11734 if (account_id < FIRST_ACCOUNT_ID) {
11735 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11736 error = EMAIL_ERROR_INVALID_PARAM;
11740 if (space_left_in_buffer + 10 > sizeof(buf)) {
11741 EM_DEBUG_EXCEPTION("Buffer overflowed");
11742 error = EMAIL_ERROR_OUT_OF_MEMORY;
11746 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11748 if (stat(buf, &sbuf) == 0) {
11749 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11750 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11751 error = EMAIL_ERROR_SYSTEM_FAILURE;
11755 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11756 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11757 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11758 error = EMAIL_ERROR_SYSTEM_FAILURE;
11760 error = EMAIL_ERROR_OUT_OF_MEMORY;
11764 if (account_id == EML_FOLDER) {
11771 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11772 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11773 error = EMAIL_ERROR_INVALID_PARAM;
11777 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11779 if (stat(buf, &sbuf) == 0) {
11780 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11781 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11782 error = EMAIL_ERROR_SYSTEM_FAILURE;
11786 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11787 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11788 error = EMAIL_ERROR_SYSTEM_FAILURE;
11790 error = EMAIL_ERROR_OUT_OF_MEMORY;
11794 if (account_id == EML_FOLDER) {
11804 EM_SAFE_FREE(prefix_path);
11806 if (err_code != NULL)
11809 EM_PROFILE_END(profile_emcore_save_create_dir);
11810 EM_DEBUG_FUNC_END("ret [%d]", ret);
11814 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11816 EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11817 EM_DEBUG_LOG("Using the fsync function");
11819 int error = EMAIL_ERROR_NONE;
11820 struct stat st_buf;
11825 char buf[FILE_MAX_BUFFER_SIZE] = {0};
11826 char errno_buf[ERRNO_BUF_SIZE] = {0};
11828 if (!src_file || !dst_file) {
11829 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11831 error = EMAIL_ERROR_INVALID_PARAM;
11835 if (stat(src_file, &st_buf) < 0) {
11836 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11838 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
11842 error = em_open(src_file, O_RDONLY, 0, &fp_src);
11843 if (error != EMAIL_ERROR_NONE) {
11844 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11848 error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11849 if (error != EMAIL_ERROR_NONE) {
11850 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s: %d", dst_file, error);
11854 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11856 while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11857 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11858 EM_DEBUG_LOG("Nread Value [%d]", nread);
11860 ssize_t byte_written = 0;
11861 size_t remain_byte = nread;
11865 while (remain_byte > 0 && buf_ptr && errno == 0) {
11866 byte_written = write(fp_dst, buf_ptr, remain_byte);
11868 if (byte_written < 0) {
11869 /* interrupted by a signal */
11870 if (errno == EINTR) {
11875 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11876 error = EMAIL_ERROR_UNKNOWN;
11879 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11880 remain_byte -= byte_written;
11881 buf_ptr += byte_written;
11885 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11891 EM_SAFE_CLOSE(fp_src);
11893 if (fp_dst >= 0) { /*prevent 24474*/
11895 EM_DEBUG_LOG("Before fsync");
11901 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11904 if (err_code != NULL)
11906 EM_DEBUG_FUNC_END("ret [%d]", ret);
11909 /* create Directory if user has deleted [deepam.p@samsung.com] */
11910 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11912 EM_DEBUG_FUNC_BEGIN();
11914 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11915 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
11916 mkdir(MAILHOME, DIRECTORY_PERMISSION);
11917 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11918 chmod(MAILTEMP, 0777);
11920 EM_DEBUG_FUNC_END();
11923 static int _get_temp_file_name(char **filename, int *err_code)
11925 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11928 int error = EMAIL_ERROR_NONE;
11930 if (filename == NULL) {
11931 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11932 error = EMAIL_ERROR_INVALID_PARAM;
11936 char tempname[512] = {0x00, };
11939 gettimeofday(&tv, NULL);
11941 unsigned int seed = time(NULL);
11942 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand_r(&seed));
11944 char *p = EM_SAFE_STRDUP(tempname);
11946 EM_DEBUG_EXCEPTION(" strdup failed...");
11947 error = EMAIL_ERROR_OUT_OF_MEMORY;
11956 if (err_code != NULL)
11959 EM_DEBUG_FUNC_END("ret [%d]", ret);
11963 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
11965 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
11967 EM_IF_NULL_RETURN_VALUE(file_path, false);
11968 EM_IF_NULL_RETURN_VALUE(char_set, false);
11969 EM_IF_NULL_RETURN_VALUE(err_code, false);
11973 struct stat st_buf;
11975 char *low_char_set = NULL;
11976 char *match_str = NULL;
11979 int error = EMAIL_ERROR_NONE;
11980 int data_count_to_written = 0;
11981 char *temp_file_name = NULL;
11983 FILE* fp_src = NULL;
11984 FILE* fp_dest = NULL;
11988 if (stat(file_path, &st_buf) < 0) {
11989 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
11990 error = EMAIL_ERROR_SYSTEM_FAILURE;
11994 buf_size = st_buf.st_size;
11995 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
11996 buf = (char *)calloc(1, buf_size+1);
11999 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12003 error = em_fopen(file_path, "rb", &fp_src);
12004 if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12005 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12009 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12010 if (nread > 0 && nread <= buf_size) {
12011 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12014 * 1.Add check for whether content type is there.
12015 * 2. If not based on the character set, Append it in File
12018 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12019 if (low_char_set == NULL) {
12020 EM_DEBUG_EXCEPTION("calloc failed");
12021 error = EMAIL_ERROR_OUT_OF_MEMORY;
12025 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
12026 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12027 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
12028 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12029 match_str = strstr(buf, CONTENT_TYPE_DATA);
12031 if (match_str == NULL) {
12032 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12033 if (fp_src != NULL) {
12037 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12038 buf1 = (char *)calloc(1, data_count_to_written);
12041 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
12042 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12043 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
12044 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
12046 /* 1. Create a temporary file name */
12047 if (!_get_temp_file_name(&temp_file_name, &err)) {
12048 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12049 if (err_code != NULL) *err_code = err;
12052 EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12054 /* Open the Temp file in Append mode */
12055 error = em_fopen(temp_file_name, "ab", &fp_dest);
12056 if (error != EMAIL_ERROR_NONE) {
12057 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12061 /* 2. write the Latest data */
12062 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12064 if (nwritten > 0) {
12065 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
12067 /* 3. Append old data */
12068 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12070 if (nwritten <= 0) {
12071 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12072 error = EMAIL_ERROR_SYSTEM_FAILURE;
12075 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12077 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12078 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12084 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12085 error = EMAIL_ERROR_SYSTEM_FAILURE;
12097 EM_SAFE_FREE(buf1);
12098 EM_SAFE_FREE(low_char_set);
12099 EM_SAFE_FREE(temp_file_name);
12101 if (fp_src != NULL) {
12106 if (fp_dest != NULL) {
12114 EM_DEBUG_FUNC_END("ret [%d]", ret);
12119 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12121 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12124 int error = EMAIL_ERROR_NONE;
12125 char errno_buf[ERRNO_BUF_SIZE] = {0};
12127 if (src_file == NULL || dst_file == NULL) {
12128 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12129 error = EMAIL_ERROR_INVALID_PARAM;
12133 EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12135 if (strcmp(src_file, dst_file) != 0) {
12136 if (rename(src_file, dst_file) != 0) {
12137 /* EM_DEBUG_EXCEPTION("%s", strerror(errno)); */
12138 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() */
12139 /* does not work across different mount points, even if the same file system is mounted on both.) */
12140 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12141 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12142 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12146 EM_DEBUG_LOG("src[%s] removed", src_file);
12148 if (errno == ENOENT) {
12149 struct stat temp_file_stat;
12150 if (stat(src_file, &temp_file_stat) < 0) {
12151 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12153 if (stat(dst_file, &temp_file_stat) < 0)
12154 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12156 error = EMAIL_ERROR_FILE_NOT_FOUND;
12159 EM_DEBUG_EXCEPTION("rename failed: %s", EM_STRERROR(errno_buf));
12160 error = EMAIL_ERROR_SYSTEM_FAILURE;
12166 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12172 if (err_code != NULL)
12175 EM_DEBUG_FUNC_END("ret [%d]", ret);
12179 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12181 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12184 int error = EMAIL_ERROR_NONE;
12186 if (src_file == NULL) {
12187 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12189 error = EMAIL_ERROR_INVALID_PARAM;
12193 if (remove(src_file) != 0) {
12194 if (errno != ENOENT) {
12195 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12197 error = EMAIL_ERROR_SYSTEM_FAILURE;
12200 EM_DEBUG_EXCEPTION(" no file found...");
12202 error = EMAIL_ERROR_FILE_NOT_FOUND;
12209 if (err_code != NULL)
12212 EM_DEBUG_FUNC_END("ret [%d]", ret);
12217 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12219 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12221 if (src_dir == NULL) {
12222 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12224 if (err_code != NULL)
12225 *err_code = EMAIL_ERROR_INVALID_PARAM;
12229 int error = EMAIL_ERROR_NONE;
12236 dirp = opendir(src_dir);
12238 if (dirp == NULL) {
12239 if (errno == ENOENT) {
12240 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12241 if (err_code != NULL)
12242 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12245 EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12246 if (err_code != NULL)
12247 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12252 while ((dp = readdir(dirp))) {
12253 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12256 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12258 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12259 /* check directory */
12260 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
12261 /* recursive call */
12262 if (!emstorage_delete_dir(buf, &error)) {
12264 if (err_code != NULL)
12268 } else { /* file */
12269 if (remove(buf) < 0) {
12270 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12272 if (err_code != NULL)
12273 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12278 EM_DEBUG_EXCEPTION("content does not exist...");
12283 EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12285 /* EM_DEBUG_FUNC_BEGIN(); */
12287 if (remove(src_dir) < 0) {
12288 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12289 if (err_code != NULL)
12290 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12294 if (err_code != NULL)
12300 /* faizan.h@samsung.com */
12301 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12303 char *old_server_uid,
12304 char *new_server_uid,
12307 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12309 int error = EMAIL_ERROR_NONE;
12310 int transaction = true;
12311 int temp_strlen = 0;
12312 int and_operation = 0;
12313 char sql_query_string[QUERY_SIZE] = {0, };
12314 char conditional_clause_string[QUERY_SIZE] = {0};
12316 if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12317 EM_DEBUG_EXCEPTION("Invalid parameters");
12319 *err_code = EMAIL_ERROR_INVALID_PARAM;
12323 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12324 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12326 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12329 temp_strlen = strlen(conditional_clause_string);
12330 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12331 "mail_id = %d ", mail_id);
12335 if (old_server_uid) {
12336 temp_strlen = strlen(conditional_clause_string);
12337 if (!and_operation) {
12338 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12339 "server_mail_id = '%q'", old_server_uid);
12341 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12342 "and server_mail_id = '%q'", old_server_uid);
12346 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12347 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12349 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12350 if (error != EMAIL_ERROR_NONE) {
12351 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12359 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12361 if (err_code != NULL)
12364 EM_DEBUG_FUNC_END("ret [%d]", ret);
12369 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)
12371 EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12374 int error = EMAIL_ERROR_NONE;
12375 char sql_query_string[QUERY_SIZE] = {0, };
12376 int transaction = true;
12378 if (!mail_id || !new_server_uid || !mbox_name) {
12379 EM_DEBUG_EXCEPTION("Invalid parameters");
12380 if (err_code != NULL)
12381 *err_code = EMAIL_ERROR_INVALID_PARAM;
12385 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12386 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12389 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12390 "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);
12392 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12393 if (error != EMAIL_ERROR_NONE) {
12394 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12402 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12404 if (err_code != NULL)
12407 EM_DEBUG_FUNC_END("ret [%d]", ret);
12412 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12414 EM_DEBUG_FUNC_BEGIN();
12417 int transaction = true;
12418 int error = EMAIL_ERROR_NONE;
12419 char sql_query_string[QUERY_SIZE] = {0,};
12421 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12423 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12425 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12427 if (account_id <= ALL_ACCOUNT)
12428 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);
12430 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);
12432 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12433 if (error != EMAIL_ERROR_NONE) {
12434 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12441 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12443 if (err_code != NULL)
12446 EM_DEBUG_FUNC_END("ret [%d]", ret);
12451 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12453 EM_DEBUG_FUNC_BEGIN();
12455 if ((!mail_ids) || (account_id <= 0 && account_id != -1)) {
12456 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12457 if (err_code != NULL)
12458 *err_code = EMAIL_ERROR_INVALID_PARAM;
12464 int error = EMAIL_ERROR_NONE;
12468 int *p_mail_ids = NULL;
12469 int transaction = false;
12470 char **result = NULL;
12471 char sql_query_string[QUERY_SIZE] = {0, };
12472 char temp_query_string[QUERY_SIZE] = {0,};
12473 char sql_select_query_string[QUERY_SIZE] = {0, };
12475 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12476 EMSTORAGE_START_READ_TRANSACTION(transaction);
12478 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12480 if (account_id == -1)
12481 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);
12483 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);
12486 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
12488 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12491 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12493 EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12496 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12497 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12499 EM_DEBUG_LOG("Count : %d", count);
12502 EM_DEBUG_EXCEPTION("no Mails found...");
12504 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12508 p_mail_ids = em_malloc(count * sizeof(int));
12509 if (p_mail_ids == NULL) {
12510 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12511 error = EMAIL_ERROR_OUT_OF_MEMORY;
12517 for (i = 0; i < count; i++) {
12518 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12526 sqlite3_free_table(result);
12528 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12530 // sqlite3_db_release_memory(local_db_handle);
12533 if (mail_ids != NULL)
12534 *mail_ids = p_mail_ids;
12536 if (mail_number != NULL)
12537 *mail_number = count;
12539 EM_SAFE_FREE(p_mail_ids);
12542 if (err_code != NULL)
12545 EM_DEBUG_FUNC_END("ret [%d]", ret);
12549 int setting_system_command(const char *command)
12552 int pid = 0, status = 0;
12553 char *const environ[] = { NULL };
12568 argv[2] = (char *)command;
12571 execve("/bin/sh", argv, environ);
12576 if (waitpid(pid, &status, 0) == -1) {
12577 if (errno != EINTR)
12588 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
12590 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12592 if (!total_usage) {
12593 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12595 if (err_code != NULL)
12596 *err_code = EMAIL_ERROR_INVALID_PARAM;
12601 int error = EMAIL_ERROR_NONE;
12602 char syscmd[256] = {0, };
12603 char line[256] = {0, };
12604 char *line_from_file = NULL;
12606 unsigned long total_diskusage = 0;
12608 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12609 if (setting_system_command(syscmd) == -1) {
12610 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12612 error = EMAIL_ERROR_SYSTEM_FAILURE;
12616 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
12617 EM_DEBUG_LOG(" cmd : %s", syscmd);
12618 if (setting_system_command(syscmd) == -1) {
12619 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12621 error = EMAIL_ERROR_SYSTEM_FAILURE;
12625 error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12626 if (error != EMAIL_ERROR_NONE) {
12627 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12631 line_from_file = fgets(line, sizeof(line), fp);
12633 if (line_from_file == NULL) {
12634 EM_DEBUG_EXCEPTION("fgets failed");
12635 error = EMAIL_ERROR_SYSTEM_FAILURE;
12638 total_diskusage = strtoul(line, NULL, 10);
12640 memset(syscmd, 0, sizeof(syscmd));
12641 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12642 if (setting_system_command(syscmd) == -1) {
12643 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12644 error = EMAIL_ERROR_SYSTEM_FAILURE;
12648 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
12653 if (err_code != NULL)
12657 *total_usage = total_diskusage;
12661 if (fp) fclose(fp); /* prevent 32730 */
12663 EM_DEBUG_FUNC_END("ret [%d]", ret);
12666 #define MAILHOME_UTF8 tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12668 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)
12670 DB_STMT hStmt = NULL;
12672 int error = EMAIL_ERROR_NONE;
12674 char sql_query_string[QUERY_SIZE] = {0, };
12676 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12677 "INSERT INTO mail_tbl VALUES "
12679 ", ?" /* account_id */
12680 ", ?" /* mail_size */
12681 ", ?" /* server_mail_status */
12682 ", ?" /* server_mailbox_name */
12683 ", ?" /* server_mail_id */
12684 ", ?" /* reference_mail_id */
12685 ", ?" /* full_address_from */
12686 ", ?" /* full_address_reply */
12687 ", ?" /* full_address_to */
12688 ", ?" /* full_address_cc */
12689 ", ?" /* full_address_bcc */
12690 ", ?" /* full_address_return */
12691 ", ?" /* subject */
12692 ", ?" /* body_download_status */
12693 ", ?" /* file_path_plain */
12694 ", ?" /* file_path_html */
12695 ", ?" /* date_time */
12696 ", ?" /* flags_seen_field */
12697 ", ?" /* flags_deleted_field */
12698 ", ?" /* flags_flagged_field */
12699 ", ?" /* flags_answered_field */
12700 ", ?" /* flags_recent_field */
12701 ", ?" /* flags_draft_field */
12702 ", ?" /* flags_forwarded_field */
12703 ", ?" /* DRM_status */
12704 ", ?" /* priority */
12705 ", ?" /* save_status */
12706 ", ?" /* lock_status */
12707 ", ?" /* message_id */
12708 ", ?" /* report_status */
12709 ", ?" /* email_address_sender */
12710 ", ?" /* email_address_recipient */
12711 ", ?" /* attachment_count */
12712 ", ?" /* inline_content_count */
12713 ", ?" /* preview_text */
12714 ", ?" /* thread_id */
12715 ", ?" /* mailbox_type */
12716 ", ?" /* alias_sender */
12717 ", ?" /* alias_recipient */
12718 ", ?" /* thread_item_count */
12719 ", ?" /* meeting_request_status */
12720 ", ?" /* message_class */
12721 ", ?" /* digest_type */
12722 ", ?" /* smime_type */
12723 ", ?" /* scheduled_sending_time */
12724 ", ?" /* remaining_resend_times */
12726 ", ?" /* replied_time */
12727 ", ?" /* forwarded_time */
12728 ", ?" /* default_charset */
12729 ", ?" /* eas_data_length */
12730 ", ?" /* eas_data */
12733 int transaction = true;
12734 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12736 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12739 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12740 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12741 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12743 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12744 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12745 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12746 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12747 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12748 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12749 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12750 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12751 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12752 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12753 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12754 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12755 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12756 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12757 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12758 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12759 _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);
12760 _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);
12761 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12762 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12763 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12764 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12765 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12766 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12767 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12768 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12769 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12770 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12771 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12772 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12773 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12774 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12775 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12776 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12777 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12778 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12779 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12780 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12781 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12782 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12783 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12784 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12785 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12786 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12787 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12788 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12789 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12790 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12791 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12792 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12793 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12794 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12795 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12796 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12798 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12799 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12800 ("sqlite3_step fail:%d", rc));
12801 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12802 ("sqlite3_step fail:%d", rc));
12806 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12807 if (hStmt != NULL) {
12808 rc = sqlite3_finalize(hStmt);
12809 if (rc != SQLITE_OK) {
12810 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12811 error = EMAIL_ERROR_DB_FAILURE;
12815 if (err_code != NULL)
12818 EM_DEBUG_FUNC_END("ret [%d]", ret);
12823 INTERNAL_FUNC int emstorage_get_max_mail_count()
12825 return EMAIL_MAIL_MAX_COUNT;
12828 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12830 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12831 emstorage_mail_tbl_t *mail_tbl,
12833 int *result_latest_mail_id_in_thread,
12834 int *thread_item_count)
12836 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12837 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12838 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12839 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12841 int rc = 0, query_size = 0, query_size_account = 0;
12843 int search_thread = false;
12844 int account_id = 0;
12845 int err_code = EMAIL_ERROR_NONE;
12846 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12847 time_t latest_date_time = 0;
12848 char *subject = NULL;
12849 char *p_subject = NULL;
12850 char *sql_query_string = NULL, *sql_account = NULL;
12852 int temp_thread_id = -1;
12853 char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12854 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12855 char *sql_format_account = " AND account_id = %d ";
12856 char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12857 char **result = NULL;
12858 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12859 char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12861 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12863 EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12865 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12866 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12867 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12868 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12870 account_id = mail_tbl->account_id;
12871 subject = mail_tbl->subject;
12873 EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12875 if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12876 result_thread_id = mail_tbl->mail_id;
12881 if (em_find_pos_stripped_subject_for_thread_view(subject,
12883 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12884 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12885 err_code = EMAIL_ERROR_UNKNOWN;
12886 result_thread_id = -1;
12890 EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12892 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12893 result_thread_id = -1;
12897 EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
12898 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12900 if (account_id > 0) {
12901 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12902 sql_account = malloc(query_size_account);
12903 if (sql_account == NULL) {
12904 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
12905 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12908 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12911 /* prevent 34362 */
12912 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
12913 sql_query_string = malloc(query_size);
12915 if (sql_query_string == NULL) {
12916 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
12917 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12921 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12923 if (account_id > 0)
12924 strcat(sql_query_string, sql_account);
12926 strcat(sql_query_string, sql_format_order_by);
12927 strcat(sql_query_string, ";");
12929 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12931 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12934 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12935 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12937 EM_DEBUG_LOG("Result rows count : %d", count);
12940 result_thread_id = -1;
12942 for (i = 0; i < count; i++) {
12943 EM_SAFE_FREE(p_subject);
12945 _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
12946 _get_table_field_data_int(result, &result_thread_id, col_index++);
12947 _get_table_field_data_time_t(result, &latest_date_time, col_index++);
12948 _get_table_field_data_string(result, &p_subject, 0, col_index++);
12950 if (temp_thread_id == result_thread_id)
12953 temp_thread_id = result_thread_id;
12955 if (em_find_pos_stripped_subject_for_thread_view(p_subject,
12957 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12958 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12959 err_code = EMAIL_ERROR_UNKNOWN;
12960 result_thread_id = -1;
12964 if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
12965 if (latest_date_time < mail_tbl->date_time)
12966 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12968 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
12970 search_thread = true;
12974 if (search_thread) {
12975 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
12976 latest_mail_id_in_thread, mail_tbl->mail_id);
12978 result_thread_id = -1;
12987 *thread_id = result_thread_id;
12988 *thread_item_count = count;
12990 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
12991 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
12992 EM_DEBUG_LOG("err_code : %d", err_code);
12994 EM_SAFE_FREE(sql_account);
12995 EM_SAFE_FREE(sql_query_string);
12996 EM_SAFE_FREE(p_subject);
12998 sqlite3_free_table(result);
13000 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13005 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)
13007 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);
13008 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13011 int query_size = 0;
13012 int query_size_account = 0;
13013 int err_code = EMAIL_ERROR_NONE;
13015 int result_thread_id = -1;
13016 char *sql_query_string = NULL;
13017 char *sql_account = NULL;
13018 char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13019 char *sql_format_account = " AND account_id = %d ";
13020 char *sql_format_order_by = " ORDER BY date_time DESC ";
13021 char **result = NULL;
13022 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13023 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13025 EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13026 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13027 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13029 EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13031 if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13032 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13033 err_code = EMAIL_ERROR_UNKNOWN;
13034 result_thread_id = -1;
13038 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13039 result_thread_id = -1;
13043 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13045 if (account_id > 0) {
13046 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13047 sql_account = malloc(query_size_account);
13048 if (sql_account == NULL) {
13049 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
13050 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13053 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13056 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; */
13057 sql_query_string = malloc(query_size);
13059 if (sql_query_string == NULL) {
13060 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
13061 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13065 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13067 if (account_id > 0)
13068 strcat(sql_query_string, sql_account);
13070 strcat(sql_query_string, sql_format_order_by);
13071 strcat(sql_query_string, ";");
13073 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13075 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13077 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13078 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13080 EM_DEBUG_LOG("Result rows count : %d", count);
13083 result_thread_id = -1;
13085 _get_table_field_data_int(result, &result_thread_id, 1);
13089 *thread_id = result_thread_id;
13090 *thread_item_count = count;
13092 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13093 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13094 EM_DEBUG_LOG("err_code : %d", err_code);
13096 EM_SAFE_FREE(sql_account);
13097 EM_SAFE_FREE(sql_query_string);
13099 sqlite3_free_table(result);
13101 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13106 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)
13108 EM_DEBUG_FUNC_BEGIN();
13110 int count = 0, ret = false;
13111 int error = EMAIL_ERROR_NONE;
13112 emstorage_mail_tbl_t *p_data_tbl = NULL;
13113 char conditional_clause[QUERY_SIZE] = {0, };
13115 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13117 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13118 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13120 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13121 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13126 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13132 *mail_tbl = p_data_tbl;
13133 else if (p_data_tbl != NULL)
13134 emstorage_free_mail(&p_data_tbl, 1, NULL);
13136 if (err_code != NULL)
13139 EM_DEBUG_FUNC_END("ret [%d]", ret);
13143 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)
13145 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]"
13146 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13148 if ((!sender_list) || (!sender_count)) {
13149 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13150 if (err_code != NULL)
13151 *err_code = EMAIL_ERROR_INVALID_PARAM;
13155 int rc = -1, ret = false;
13156 int error = EMAIL_ERROR_NONE;
13158 int i, col_index = 0;
13159 int read_count = 0;
13160 email_sender_list_t *p_sender_list = NULL;
13161 char sql_query_string[QUERY_SIZE] = {0, };
13162 char **result = NULL;
13163 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13165 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13166 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13171 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);
13172 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13173 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) ");
13176 /* '0' (ALL_ACCOUNT) means all account */
13177 if (account_id > ALL_ACCOUNT)
13178 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);
13180 if (search_value) {
13181 switch (search_type) {
13182 case EMAIL_SEARCH_FILTER_SUBJECT:
13183 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13184 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13186 case EMAIL_SEARCH_FILTER_SENDER:
13187 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13188 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13189 ") ", search_value);
13191 case EMAIL_SEARCH_FILTER_RECIPIENT:
13192 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13193 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13194 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13195 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13196 ") ", search_value, search_value, search_value);
13198 case EMAIL_SEARCH_FILTER_ALL:
13199 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13200 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13201 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13202 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13203 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13204 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13207 ")", search_value, search_value, search_value, search_value, search_value);
13213 /* sorting option is not available now. The order of sender list is ascending order by display name */
13214 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13215 "GROUP BY email_address_sender "
13216 "ORDER BY UPPER(alias_sender) ");
13218 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13220 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13221 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13222 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13224 EM_DEBUG_LOG("Count of Sender [%d]", count);
13226 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13227 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13228 error = EMAIL_ERROR_OUT_OF_MEMORY;
13234 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13235 for (i = 0; i < count; i++) {
13236 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13237 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13238 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13239 _get_table_field_data_int(result, &(read_count), col_index++);
13240 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
13242 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13244 sqlite3_free_table(result);
13251 *sender_list = p_sender_list;
13252 *sender_count = count;
13253 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13257 if (err_code != NULL)
13260 EM_DEBUG_FUNC_END("ret [%d]", ret);
13264 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13266 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13268 int err = EMAIL_ERROR_NONE;
13271 if (!sender_list || !*sender_list) {
13272 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13273 err = EMAIL_ERROR_INVALID_PARAM;
13277 email_sender_list_t* p = *sender_list;
13280 for (; i < count; i++) {
13281 EM_SAFE_FREE(p[i].address);
13282 EM_SAFE_FREE(p[i].display_name);
13286 *sender_list = NULL;
13293 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13295 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13297 int err = EMAIL_ERROR_NONE;
13298 email_address_info_t *p_address_info = NULL;
13299 GList *list = NULL;
13300 GList *node = NULL;
13303 if (!address_info_list || !*address_info_list) {
13304 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13305 err = EMAIL_ERROR_INVALID_PARAM;
13309 /* delete GLists */
13310 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13312 case EMAIL_ADDRESS_TYPE_FROM:
13313 list = (*address_info_list)->from;
13315 case EMAIL_ADDRESS_TYPE_TO:
13316 list = (*address_info_list)->to;
13318 case EMAIL_ADDRESS_TYPE_CC:
13319 list = (*address_info_list)->cc;
13321 case EMAIL_ADDRESS_TYPE_BCC:
13322 list = (*address_info_list)->bcc;
13326 /* delete dynamic-allocated memory for each item */
13327 node = g_list_first(list);
13328 while (node != NULL) {
13329 p_address_info = (email_address_info_t*)node->data;
13330 EM_SAFE_FREE(p_address_info->address);
13331 EM_SAFE_FREE(p_address_info->display_name);
13332 EM_SAFE_FREE(node->data);
13334 node = g_list_next(node);
13339 EM_SAFE_FREE(*address_info_list);
13340 *address_info_list = NULL;
13342 EM_DEBUG_FUNC_END("err [%d]", err);
13345 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13347 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)
13349 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13351 if (!local_activity || !activity_id) {
13352 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13353 if (err_code != NULL)
13354 *err_code = EMAIL_ERROR_INVALID_PARAM;
13360 int error = EMAIL_ERROR_NONE;
13363 char sql_query_string[QUERY_SIZE] = {0, };
13364 DB_STMT hStmt = NULL;
13365 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13367 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13368 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13369 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13370 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13372 "? " /* Account ID */
13373 ",?" /* Local Mail ID */
13374 ",?" /* Server mail ID */
13375 ",?" /* Activity ID */
13376 ",?" /* Activity type*/
13377 ",?" /* Mailbox ID*/
13378 ",?" /* Mailbox name*/
13379 ",?" /* Multi User Name */
13382 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13383 char **result = NULL;
13386 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13388 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13389 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13391 if (NULL == result[1]) rc = 1;
13392 else rc = atoi(result[1])+1;
13393 sqlite3_free_table(result);
13396 *activity_id = local_activity->activity_id = rc;
13398 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13399 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13401 if (local_activity->mailbox_id)
13402 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13405 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13406 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13407 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13410 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13411 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13412 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13413 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13414 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13415 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13416 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13417 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13420 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13422 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13423 ("sqlite3_step fail:%d", rc));
13424 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13425 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13430 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13431 if (hStmt != NULL) {
13432 rc = sqlite3_finalize(hStmt);
13434 if (rc != SQLITE_OK) {
13435 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13436 error = EMAIL_ERROR_DB_FAILURE;
13440 if (err_code != NULL)
13443 EM_DEBUG_FUNC_END("ret [%d]", ret);
13447 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)
13449 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13451 if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13452 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13453 if (err_code != NULL)
13454 *err_code = EMAIL_ERROR_INVALID_PARAM;
13459 int error = EMAIL_ERROR_NONE;
13461 int i = 0, rc = -1;
13462 int *mbox_list = NULL;
13463 DB_STMT hStmt = NULL;
13464 char sql_query_string[QUERY_SIZE] = {0, };
13466 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13467 EMSTORAGE_START_READ_TRANSACTION(transaction);
13469 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13470 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);
13473 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13474 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13475 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13477 *count = atoi(result[1]);
13478 sqlite3_free_table(result);
13481 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13482 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13486 EM_DEBUG_LOG("Mailbox count = %d", *count);
13488 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13490 /* 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); */
13491 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);
13493 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13496 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13499 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13500 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13503 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13504 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13505 ("sqlite3_step fail:%d", rc));
13507 mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13508 if (NULL == mbox_list) {
13509 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13510 error = EMAIL_ERROR_OUT_OF_MEMORY;
13514 memset(mbox_list, 0x00, sizeof(int) * (*count));
13516 for (i = 0; i < (*count); i++) {
13517 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13519 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13520 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13521 ("sqlite3_step fail:%d", rc));
13522 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13529 *mailbox_list = mbox_list;
13531 EM_SAFE_FREE(mbox_list);
13533 if (hStmt != NULL) {
13534 rc = sqlite3_finalize(hStmt);
13536 if (rc != SQLITE_OK) {
13537 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13538 error = EMAIL_ERROR_DB_FAILURE;
13542 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13543 if (err_code != NULL)
13545 EM_DEBUG_FUNC_END("ret [%d]", ret);
13549 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13551 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13553 if (NULL == account_list || NULL == count) {
13554 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13555 if (err_code != NULL)
13556 *err_code = EMAIL_ERROR_INVALID_PARAM;
13561 int error = EMAIL_ERROR_NONE;
13562 char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13564 int i = 0, rc = -1;
13565 int *result_account_list = NULL;
13566 DB_STMT hStmt = NULL;
13567 char sql_query_string[QUERY_SIZE] = {0, };
13569 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13571 EMSTORAGE_START_READ_TRANSACTION(transaction);
13573 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13574 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13575 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13577 *count = atoi(result[1]);
13578 sqlite3_free_table(result);
13581 EM_DEBUG_EXCEPTION("no account found...");
13582 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13587 EM_DEBUG_LOG("Account count [%d]", *count);
13589 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13591 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13593 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13596 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13598 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13599 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13600 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13603 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13604 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13605 ("sqlite3_step fail:%d", rc));
13607 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13608 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13609 error = EMAIL_ERROR_OUT_OF_MEMORY;
13613 memset(result_account_list, 0x00, sizeof(int) * (*count));
13615 for (i = 0; i < (*count); i++) {
13616 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13618 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13619 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13620 ("sqlite3_step fail:%d", rc));
13621 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13628 *account_list = result_account_list;
13630 EM_SAFE_FREE(result_account_list);
13632 if (hStmt != NULL) {
13633 rc = sqlite3_finalize(hStmt);
13635 if (rc != SQLITE_OK) {
13636 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13637 error = EMAIL_ERROR_DB_FAILURE;
13641 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13642 if (err_code != NULL)
13644 EM_DEBUG_FUNC_END("ret [%d]", ret);
13648 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)
13650 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13652 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13653 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);
13655 if (err_code != NULL)
13656 *err_code = EMAIL_ERROR_INVALID_PARAM;
13663 int error = EMAIL_ERROR_NONE;
13665 DB_STMT hStmt = NULL;
13666 email_event_partial_body_thd* event_list = NULL;
13667 char sql_query_string[QUERY_SIZE] = {0, };
13669 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13671 EMSTORAGE_START_READ_TRANSACTION(transaction);
13673 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13674 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);
13677 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13678 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13679 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13681 *count = atoi(result[1]);
13682 sqlite3_free_table(result);
13684 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13687 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13688 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13692 EM_DEBUG_LOG("Activity Count = %d", *count);
13694 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13695 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);
13697 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13700 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13702 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13703 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13704 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13708 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13709 ("sqlite3_step fail:%d", rc));
13711 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13712 EM_DEBUG_EXCEPTION("Malloc failed");
13714 error = EMAIL_ERROR_OUT_OF_MEMORY;
13717 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13719 for (i = 0; i < (*count); i++) {
13720 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13721 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13722 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13723 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13724 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13725 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13726 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13727 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13729 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13730 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13731 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13732 ("sqlite3_step fail:%d", rc));
13734 event_list[i].event_type = 0;
13741 *event_start = event_list;
13743 for (i = 0; i < (*count); i++)
13744 emcore_free_partial_body_thd_event(event_list, NULL);
13745 EM_SAFE_FREE(event_list); /*prevent 54559*/
13746 *event_start = NULL;
13750 if (hStmt != NULL) {
13751 rc = sqlite3_finalize(hStmt);
13753 if (rc != SQLITE_OK) {
13754 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13755 error = EMAIL_ERROR_DB_FAILURE;
13759 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13760 if (err_code != NULL)
13763 EM_DEBUG_FUNC_END("ret [%d]", ret);
13767 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)
13769 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);
13772 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13773 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);
13775 if (err_code != NULL)
13776 *err_code = EMAIL_ERROR_INVALID_PARAM;
13782 int error = EMAIL_ERROR_NONE;
13783 char sql_query_string[QUERY_SIZE] = {0, };
13784 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13786 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13787 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13789 if (activity_id == 0)
13790 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);
13792 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);
13794 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13795 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13796 if (error != EMAIL_ERROR_NONE) {
13797 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13801 /* validate activity existence */
13802 rc = sqlite3_changes(local_db_handle);
13804 EM_DEBUG_EXCEPTION("No matching activity found");
13805 error = EMAIL_ERROR_DATA_NOT_FOUND;
13813 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13815 if (err_code != NULL)
13818 EM_DEBUG_FUNC_END("ret [%d]", ret);
13822 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)
13824 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13826 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13827 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13828 if (err_code != NULL)
13829 *err_code = EMAIL_ERROR_INVALID_PARAM;
13834 int error = EMAIL_ERROR_NONE;
13835 char sql_query_string[QUERY_SIZE] = {0, };
13837 DB_STMT hStmt = NULL;
13839 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13841 EMSTORAGE_START_READ_TRANSACTION(transaction);
13842 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13844 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);
13846 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13849 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13850 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13851 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13852 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13855 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13856 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13857 ("sqlite3_step fail:%d", rc));
13859 _get_stmt_field_data_int(hStmt, activity_count, 0);
13861 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13867 if (hStmt != NULL) {
13868 rc = sqlite3_finalize(hStmt);
13870 if (rc != SQLITE_OK) {
13871 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13872 error = EMAIL_ERROR_DB_FAILURE;
13876 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13878 if (err_code != NULL)
13881 EM_DEBUG_FUNC_END("ret [%d]", ret);
13885 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13887 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13889 if (NULL == activity_count || NULL == err_code) {
13890 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13891 if (err_code != NULL)
13892 *err_code = EMAIL_ERROR_INVALID_PARAM;
13897 int error = EMAIL_ERROR_NONE;
13898 DB_STMT hStmt = NULL;
13899 char sql_query_string[QUERY_SIZE] = {0, };
13901 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13903 EMSTORAGE_START_READ_TRANSACTION(transaction);
13904 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13906 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13908 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13910 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13911 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
13912 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13913 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13915 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13916 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13917 ("sqlite3_step fail:%d", rc));
13919 _get_stmt_field_data_int(hStmt, activity_count, 0);
13921 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13928 if (hStmt != NULL) {
13929 rc = sqlite3_finalize(hStmt);
13931 if (rc != SQLITE_OK) {
13932 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13933 error = EMAIL_ERROR_DB_FAILURE;
13937 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13938 if (err_code != NULL)
13941 EM_DEBUG_FUNC_END("ret [%d]", ret);
13945 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13947 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13948 if (account_id < FIRST_ACCOUNT_ID) {
13949 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13950 if (err_code != NULL)
13951 *err_code = EMAIL_ERROR_INVALID_PARAM;
13957 int error = EMAIL_ERROR_NONE;
13958 char sql_query_string[QUERY_SIZE] = {0, };
13960 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13962 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13963 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13964 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13966 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13967 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13968 if (error != EMAIL_ERROR_NONE) {
13969 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13973 rc = sqlite3_changes(local_db_handle);
13975 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
13976 error = EMAIL_ERROR_DATA_NOT_FOUND;
13985 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13987 if (err_code != NULL)
13990 EM_DEBUG_FUNC_END("ret [%d]", ret);
13994 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
13995 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*/
13997 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)
13999 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14001 int rc = -1, ret = false;
14002 int error = EMAIL_ERROR_NONE;
14003 char sql_query_string[QUERY_SIZE] = {0, };
14005 int transaction = true;
14007 if (!old_server_uid || !new_server_uid || !mbox_name) {
14008 EM_DEBUG_EXCEPTION("Invalid parameters");
14009 error = EMAIL_ERROR_INVALID_PARAM;
14013 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14015 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14016 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14017 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14018 "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);
14020 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14021 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14022 if (error != EMAIL_ERROR_NONE) {
14023 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14027 rc = sqlite3_changes(local_db_handle);
14029 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14035 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14037 if (err_code != NULL)
14040 EM_DEBUG_FUNC_END("ret [%d]", ret);
14045 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14047 EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14050 int error = EMAIL_ERROR_NONE;
14051 FILE* fp_dst = NULL;
14052 char errno_buf[ERRNO_BUF_SIZE] = {0};
14054 if (!data_string || !dst_file_name) {
14055 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14056 error = EMAIL_ERROR_INVALID_PARAM;
14060 error = em_fopen(dst_file_name, "w", &fp_dst);
14061 if (error != EMAIL_ERROR_NONE) {
14062 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14066 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14067 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14068 error = EMAIL_ERROR_UNKNOWN;
14076 if (fp_dst != NULL)
14079 if (err_code != NULL)
14082 EM_DEBUG_FUNC_END("ret [%d]", ret);
14086 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14090 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14091 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)
14093 EM_DEBUG_FUNC_BEGIN();
14096 int error = EMAIL_ERROR_NONE;
14097 char sql_query_string[QUERY_SIZE] = {0, };
14099 int transaction = true;
14101 if (!old_server_uid || !new_server_uid || !mbox_name) {
14102 EM_DEBUG_EXCEPTION("Invalid parameters");
14103 if (err_code != NULL)
14104 *err_code = EMAIL_ERROR_INVALID_PARAM;
14108 EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14110 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14111 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14114 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14115 "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);
14117 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14118 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14119 if (error != EMAIL_ERROR_NONE) {
14120 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14124 rc = sqlite3_changes(local_db_handle);
14126 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14127 error = EMAIL_ERROR_DATA_NOT_FOUND;
14134 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14136 if (err_code != NULL)
14139 EM_DEBUG_FUNC_END("ret [%d]", ret);
14146 * @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);
14147 * Prepare an array of mail_id and corresponding server mail id.
14149 *@author h.gahlaut@samsung.com
14150 * @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)
14151 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14152 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14153 * @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
14154 * @param[out] err_code Returns the error code.
14155 * @remarks An Example of Query to be exexuted in this API:
14156 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14157 * @return This function returns true on success or false on failure.
14159 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14161 email_id_set_t** idset,
14165 EM_DEBUG_FUNC_BEGIN();
14166 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14168 int error = EMAIL_ERROR_NONE;
14170 email_id_set_t* p_id_set = NULL;
14172 const int buf_size = QUERY_SIZE;
14173 char sql_query_string[QUERY_SIZE] = {0, };
14174 int space_left_in_query_buffer = buf_size;
14177 char *server_mail_id = NULL;
14178 char **result = NULL;
14182 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14183 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14184 mail_ids, idset, id_set_count);
14185 if (err_code != NULL)
14186 *err_code = EMAIL_ERROR_INVALID_PARAM;
14190 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14192 SNPRINTF(sql_query_string, space_left_in_query_buffer,
14193 "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14196 EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14198 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14199 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14200 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14201 goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s",
14202 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14204 EM_DEBUG_LOG(" Count of mails [%d ]", count);
14207 EM_DEBUG_EXCEPTION("Can't find proper mail");
14208 error = EMAIL_ERROR_DATA_NOT_FOUND;
14212 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14213 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14214 error = EMAIL_ERROR_OUT_OF_MEMORY;
14220 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14221 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14222 for (i = 0; i < count; i++) {
14223 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14224 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14225 if (server_mail_id) {
14226 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14227 EM_SAFE_FREE(server_mail_id);
14230 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14231 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14233 sqlite3_free_table(result);
14242 *id_set_count = count;
14243 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14245 EM_SAFE_FREE(p_id_set);
14248 if (err_code != NULL)
14251 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14253 EM_DEBUG_FUNC_END("ret [%d]", ret);
14261 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14263 EM_DEBUG_FUNC_BEGIN();
14264 int ret = true, transaction = true;
14265 int error = EMAIL_ERROR_NONE;
14266 char sql_query_string[QUERY_SIZE] = {0, };
14267 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14268 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14270 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14271 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14272 if (error != EMAIL_ERROR_NONE) {
14273 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14277 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14278 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14279 if (error != EMAIL_ERROR_NONE) {
14280 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14284 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14285 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14286 if (error != EMAIL_ERROR_NONE) {
14287 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14294 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14296 EM_DEBUG_FUNC_END("ret [%d]", ret);
14300 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14302 EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14304 int error = EMAIL_ERROR_NONE;
14305 char sql_query_string[QUERY_SIZE] = {0, };
14306 int transaction = true;
14308 if (old_filter_id < 0 || new_filter_id < 0) {
14309 EM_DEBUG_EXCEPTION("Invalid parameters");
14310 if (err_code != NULL)
14311 *err_code = EMAIL_ERROR_INVALID_PARAM;
14315 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14317 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14319 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14320 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14322 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14323 if (error != EMAIL_ERROR_NONE) {
14324 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14331 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14333 if (err_code != NULL)
14336 EM_DEBUG_FUNC_END("ret [%d]", ret);
14340 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)
14342 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);
14344 if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14345 EM_DEBUG_EXCEPTION("Invalid Parameter");
14347 if (err_code != NULL)
14348 *err_code = EMAIL_ERROR_INVALID_PARAM;
14352 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14353 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14354 int tag_id = rule->rule_id;
14355 char **result = NULL, *where_pararaph = NULL;
14356 char sql_query_string[QUERY_SIZE] = {0, };
14357 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14359 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14361 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14362 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14363 if (where_pararaph == NULL) {
14364 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14365 error = EMAIL_ERROR_OUT_OF_MEMORY;
14369 if (account_id != ALL_ACCOUNT)
14370 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14372 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14374 if (rule->type & EMAIL_FILTER_SUBJECT) {
14375 if (rule->flag2 == RULE_TYPE_INCLUDES)
14376 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14377 else /* RULE_TYPE_EXACTLY */
14378 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14381 if (rule->type & EMAIL_FILTER_FROM) {
14382 if (rule->flag2 == RULE_TYPE_INCLUDES)
14383 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);
14384 #ifdef __FEATURE_COMPARE_DOMAIN__
14385 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14386 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);
14387 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14388 else /* RULE_TYPE_EXACTLY */
14389 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);
14392 if (rule->type == EMAIL_PRIORITY_SENDER) {
14393 if (rule->flag2 == RULE_TYPE_INCLUDES)
14394 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);
14395 else /* RULE_TYPE_EXACTLY */
14396 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);
14398 tag_id = PRIORITY_SENDER_TAG_ID;
14401 /* prevent 34361 */
14402 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14403 strcat(sql_query_string, where_pararaph);
14405 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14407 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14408 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14409 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14410 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14412 EM_DEBUG_LOG("Count of mails [%d]", count);
14415 mail_list = malloc(sizeof(int) * count);
14416 if (mail_list == NULL) {
14417 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14418 error = EMAIL_ERROR_OUT_OF_MEMORY;
14424 for (i = 0; i < count; i++)
14425 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14427 memset(sql_query_string, 0x00, QUERY_SIZE);
14429 switch (rule->action_type) {
14430 case EMAIL_FILTER_MOVE:
14431 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14433 case EMAIL_FILTER_BLOCK:
14435 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14440 switch (rule->action_type) {
14441 case EMAIL_FILTER_MOVE:
14442 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14444 case EMAIL_FILTER_BLOCK:
14445 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14448 EM_DEBUG_LOG("Not support");
14453 /* prevent 34361 */
14454 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14455 strcat(sql_query_string, where_pararaph);
14457 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14458 if (error != EMAIL_ERROR_NONE) {
14459 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14463 #ifdef __FEATURE_BODY_SEARCH__
14464 /* Updating mail_text_tbl */
14465 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14466 memset(sql_query_string, 0x00, QUERY_SIZE);
14467 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14468 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14469 strcat(sql_query_string, where_pararaph);
14471 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14472 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14473 if (error != EMAIL_ERROR_NONE) {
14474 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14486 if (filtered_mail_id_list)
14487 *filtered_mail_id_list = mail_list;
14489 if (count_of_mails)
14490 *count_of_mails = count;
14492 EM_SAFE_FREE(mail_list);
14494 sqlite3_free_table(result);
14498 EM_SAFE_FREE(where_pararaph);
14500 if (err_code != NULL)
14503 EM_DEBUG_FUNC_END("ret [%d]", ret);
14507 #define EMAIL_SLOT_UNIT 25
14508 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)
14510 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);
14511 int ret = false, err = EMAIL_ERROR_NONE;
14512 int where_pararaph_length = 0;
14513 char *where_pararaph = NULL;
14514 char sql_query_string[QUERY_SIZE] = {0, };
14516 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14518 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14520 if (new_slot_size > 0)
14521 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14522 else if (new_slot_size == 0)
14523 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14525 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14529 where_pararaph_length = 80;
14531 where_pararaph_length = 50;
14533 if (new_slot_size == 0)
14534 where_pararaph_length += 70;
14536 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14537 if (where_pararaph == NULL) {
14538 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14539 err = EMAIL_ERROR_OUT_OF_MEMORY;
14542 memset(where_pararaph, 0x00, where_pararaph_length);
14544 if (account_id > ALL_ACCOUNT) {
14547 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14549 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14552 if (new_slot_size == 0)
14553 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"));
14555 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14556 strcat(sql_query_string, where_pararaph);
14558 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14559 err = EMAIL_ERROR_OUT_OF_MEMORY;
14563 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14564 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14565 if (err != EMAIL_ERROR_NONE) {
14566 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14573 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14575 EM_SAFE_FREE(where_pararaph);
14577 if (err_code != NULL)
14580 EM_DEBUG_FUNC_END("ret [%d]", ret);
14584 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)
14586 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);
14588 if (!meeting_req || meeting_req->mail_id <= 0) {
14590 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14592 if (err_code != NULL)
14593 *err_code = EMAIL_ERROR_INVALID_PARAM;
14600 int error = EMAIL_ERROR_NONE;
14601 DB_STMT hStmt = NULL;
14602 char sql_query_string[QUERY_SIZE] = {0, };
14604 time_t temp_unix_time = 0;
14606 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14607 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14609 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14610 "INSERT INTO mail_meeting_tbl VALUES "
14612 ", ?" /* account_id */
14613 ", ?" /* mailbox_id */
14614 ", ?" /* meeting_response */
14615 ", ?" /* start_time */
14616 ", ?" /* end_time */
14617 ", ?" /* location */
14618 ", ?" /* global_object_id */
14620 ", ?" /* standard_name */
14621 ", ?" /* standard_time_start_date */
14622 ", ?" /* standard_biad */
14623 ", ?" /* daylight_name */
14624 ", ?" /* daylight_time_start_date */
14625 ", ?" /* daylight_bias */
14628 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14629 if (rc != SQLITE_OK) {
14630 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14631 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14633 error = EMAIL_ERROR_DB_FAILURE;
14639 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14640 EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14641 EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14642 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14643 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14644 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14645 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14646 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14647 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14648 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14649 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14650 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14651 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14652 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14653 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14655 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14656 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14657 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14658 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14660 temp_unix_time = timegm(&(meeting_req->start_time));
14661 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14662 temp_unix_time = timegm(&(meeting_req->end_time));
14663 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14665 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14666 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14668 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14669 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14670 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14671 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14672 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14674 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14675 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14676 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14677 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14680 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14681 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14682 ("sqlite3_step fail:%d", rc));
14683 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14684 ("sqlite3_step fail:%d", rc));
14689 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14690 if (hStmt != NULL) {
14691 rc = sqlite3_finalize(hStmt);
14692 if (rc != SQLITE_OK) {
14693 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14694 error = EMAIL_ERROR_DB_FAILURE;
14698 if (err_code != NULL)
14701 EM_DEBUG_FUNC_END("ret [%d]", ret);
14705 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)
14707 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);
14714 int err = EMAIL_ERROR_NONE;
14715 char **result = NULL;
14716 char sql_query_string[QUERY_SIZE] = {0, };
14717 email_meeting_request_t* p_temp_meeting_req = NULL;
14718 sqlite3 *local_db_handle = NULL;
14719 time_t temp_unix_time;
14721 if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14722 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14723 err = EMAIL_ERROR_INVALID_PARAM;
14727 col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14728 EM_DEBUG_LOG("col_index [%d]", col_index);
14730 local_db_handle = emstorage_get_db_connection(multi_user_name);
14732 EMSTORAGE_START_READ_TRANSACTION(transaction);
14734 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14736 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14738 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14739 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14740 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14743 EM_DEBUG_EXCEPTION("No meeting_request found...");
14744 err = EMAIL_ERROR_DATA_NOT_FOUND;
14748 EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14749 if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14750 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14751 err = EMAIL_ERROR_OUT_OF_MEMORY;
14755 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14757 for (i = 0; i < count; i++) {
14758 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14759 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14760 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14761 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14762 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14763 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14764 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14765 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14766 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14767 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14768 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14769 _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++);
14770 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14771 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14772 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14773 _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++);
14774 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14775 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14776 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14782 sqlite3_free_table(result);
14784 if (err == EMAIL_ERROR_NONE) {
14785 if (p_temp_meeting_req)
14786 *output_meeting_req = p_temp_meeting_req;
14787 *output_result_count = count;
14789 EM_SAFE_FREE(p_temp_meeting_req);
14791 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14793 // sqlite3_db_release_memory(local_db_handle);
14795 EM_DEBUG_FUNC_END("err [%d]", err);
14799 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)
14801 EM_DEBUG_FUNC_BEGIN();
14805 int error = EMAIL_ERROR_NONE;
14806 char conditional_clause[QUERY_SIZE] = {0, };
14808 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14811 SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14812 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14814 if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14815 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14819 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14820 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14821 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14822 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14823 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14824 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14825 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14826 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14827 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14828 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14829 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14830 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14831 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14836 if (err_code != NULL)
14839 EM_DEBUG_FUNC_END("ret [%d]", ret);
14843 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14845 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14848 int error = EMAIL_ERROR_NONE;
14850 DB_STMT hStmt = NULL;
14851 char sql_query_string[QUERY_SIZE] = {0, };
14852 time_t temp_unix_time = 0;
14854 if (!meeting_req) {
14855 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14856 if (err_code != NULL)
14857 *err_code = EMAIL_ERROR_INVALID_PARAM;
14861 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14862 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14864 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14865 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14866 "UPDATE mail_meeting_tbl "
14868 /* " account_id = ?, " // not update here, this can be changed when move or copy */
14869 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
14870 " meeting_response = ?, "
14871 " start_time = ?, "
14874 " global_object_id = ?, "
14876 " standard_name = ?, "
14877 " standard_time_start_date = ?, "
14878 " standard_bias = ?, "
14879 " daylight_name = ?, "
14880 " daylight_time_start_date = ?, "
14881 " daylight_bias = ? "
14882 "WHERE mail_id = %d",
14883 meeting_req->mail_id);
14885 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14887 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14888 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14889 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14891 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14892 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14893 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14894 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14895 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14896 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14897 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14898 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14899 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14900 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14901 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14902 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14903 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14907 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14908 temp_unix_time = timegm(&(meeting_req->start_time));
14909 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14910 temp_unix_time = timegm(&(meeting_req->end_time));
14911 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14912 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14913 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14914 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14915 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14916 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14917 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14918 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14919 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14920 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14921 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14922 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14925 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14926 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14927 ("sqlite3_step fail:%d", rc));
14932 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14934 if (hStmt != NULL) {
14935 rc = sqlite3_finalize(hStmt);
14936 if (rc != SQLITE_OK) {
14937 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14938 error = EMAIL_ERROR_DB_FAILURE;
14942 if (err_code != NULL)
14945 EM_DEBUG_FUNC_END("ret [%d]", ret);
14949 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)
14951 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);
14953 if (account_id < ALL_ACCOUNT || mail_id < 0) {
14954 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14956 if (err_code != NULL)
14957 *err_code = EMAIL_ERROR_INVALID_PARAM;
14962 int error = EMAIL_ERROR_NONE;
14964 char sql_query_string[QUERY_SIZE] = {0, };
14966 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14967 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14969 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
14971 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
14972 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);
14976 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);
14979 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
14980 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);
14983 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14984 if (error != EMAIL_ERROR_NONE) {
14985 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14992 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14997 EM_DEBUG_FUNC_END("ret [%d]", ret);
15002 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15004 EM_DEBUG_FUNC_BEGIN();
15006 if (!meeting_req) return;
15008 EM_SAFE_FREE(meeting_req->location);
15009 EM_SAFE_FREE(meeting_req->global_object_id);
15011 EM_DEBUG_FUNC_END();
15014 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)
15016 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);
15017 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15018 char sql_query_string[QUERY_SIZE] = {0, };
15019 char **result = NULL;
15020 int rc = -1, ret = false;
15021 int error = EMAIL_ERROR_NONE;
15022 int counter = 0, col_index = 0;
15023 int result_mail_id_count = 0;
15024 int *result_mail_id_list = NULL;
15026 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15027 EM_DEBUG_EXCEPTION("Invalid Parameter");
15029 *err_code = EMAIL_ERROR_INVALID_PARAM;
15033 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);
15035 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15037 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15038 EMSTORAGE_START_READ_TRANSACTION(transaction);
15040 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15041 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15042 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15044 if (!result_mail_id_count) {
15045 EM_DEBUG_LOG("No mail found...");
15047 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15051 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15053 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15054 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15055 error = EMAIL_ERROR_OUT_OF_MEMORY;
15056 sqlite3_free_table(result);
15060 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15064 for (counter = 0; counter < result_mail_id_count; counter++)
15065 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15070 EM_DEBUG_LOG("finish off [%d]", ret);
15073 sqlite3_free_table(result);
15076 *mail_id_list = result_mail_id_list;
15077 *mail_id_count = result_mail_id_count;
15079 EM_SAFE_FREE(result_mail_id_list);
15081 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15083 if (err_code != NULL)
15086 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15087 EM_DEBUG_FUNC_END("ret [%d]", ret);
15091 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15093 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15095 int rc = -1, ret = false;
15096 int err = EMAIL_ERROR_NONE;
15097 char sql_query_string[QUERY_SIZE] = {0, };
15099 int result_count = 0;
15101 if (mail_id == 0 || thread_id == NULL) {
15102 EM_DEBUG_EXCEPTION("Invalid Parameter");
15104 *err_code = EMAIL_ERROR_INVALID_PARAM;
15108 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15110 memset(sql_query_string, 0, QUERY_SIZE);
15111 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15113 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15114 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15115 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15116 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15118 if (!result_count) {
15119 EM_DEBUG_LOG("No mail found...");
15121 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15122 /* sqlite3_free_table(result); */
15126 _get_table_field_data_int(result, thread_id, 1);
15128 sqlite3_free_table(result);
15134 if (err_code != NULL)
15137 EM_DEBUG_FUNC_END("ret [%d]", ret);
15141 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15146 int *updated_thread_id,
15147 int latest_mail_id,
15148 int thread_item_count,
15153 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15154 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15155 account_id, mailbox_id, thread_id, updated_thread_id,
15156 latest_mail_id, thread_item_count, err_code);
15158 int rc = -1, ret = false;
15159 int err = EMAIL_ERROR_NONE;
15160 char sql_query_string[QUERY_SIZE] = {0, };
15161 char **result = NULL;
15162 int result_count = 0;
15164 if (thread_id == 0) {
15165 EM_DEBUG_EXCEPTION("Invalid Parameter");
15167 *err_code = EMAIL_ERROR_INVALID_PARAM;
15171 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15173 if (thread_item_count == 0 && latest_mail_id == 0) {
15174 memset(sql_query_string, 0, QUERY_SIZE);
15175 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);
15177 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15178 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15179 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15180 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15181 EM_DEBUG_LOG("result_count[%d]", result_count);
15182 if (result_count == 0) {
15183 EM_DEBUG_LOG("No mail found...");
15186 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND;
15187 sqlite3_free_table(result);
15191 _get_table_field_data_int(result, &latest_mail_id, 2);
15192 _get_table_field_data_int(result, &thread_item_count, 3);
15194 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15195 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15197 sqlite3_free_table(result);
15200 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15202 if (thread_item_count < 0) {
15203 memset(sql_query_string, 0, QUERY_SIZE);
15204 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);
15205 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15206 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15207 if (err != EMAIL_ERROR_NONE) {
15208 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15211 } else if (thread_id != latest_mail_id) {
15212 /* Initialize the thread id */
15213 memset(sql_query_string, 0, QUERY_SIZE);
15214 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);
15215 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15216 if (err != EMAIL_ERROR_NONE) {
15217 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15221 /* update the thread item count */
15222 memset(sql_query_string, 0, QUERY_SIZE);
15223 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);
15224 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15225 if (err != EMAIL_ERROR_NONE) {
15226 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15230 memset(sql_query_string, 0, QUERY_SIZE);
15231 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);
15232 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15233 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15234 if (err != EMAIL_ERROR_NONE) {
15235 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15242 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15244 if (thread_id != latest_mail_id) {
15245 if (err == EMAIL_ERROR_NONE) {
15246 EM_DEBUG_LOG("noti_type[%d]", noti_type);
15248 if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15249 char mailbox_id_str[25] = {0,};
15250 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15251 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15252 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15254 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15257 } else if (thread_item_count >= 0) {
15258 if (err == EMAIL_ERROR_NONE) {
15259 char parameter_string[500] = {0,};
15260 SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15261 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15262 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15266 if (err_code != NULL)
15269 EM_DEBUG_FUNC_END("ret [%d]", ret);
15273 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)
15275 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);
15278 int err = EMAIL_ERROR_NONE;
15279 char sql_query_string[QUERY_SIZE] = {0, };
15281 if (thread_id == 0) {
15282 EM_DEBUG_EXCEPTION("Invalid Parameter");
15284 *err_code = EMAIL_ERROR_INVALID_PARAM;
15288 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15290 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15292 memset(sql_query_string, 0, QUERY_SIZE);
15293 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);
15294 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15295 if (err != EMAIL_ERROR_NONE) {
15296 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15303 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15305 if (err_code != NULL)
15308 EM_DEBUG_FUNC_END("ret [%d]", ret);
15312 #ifdef __FEATURE_LOCAL_ACTIVITY__
15314 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15317 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15319 EM_DEBUG_FUNC_BEGIN();
15321 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15323 int rc = -1, ret = false;
15324 int error = EMAIL_ERROR_NONE;
15325 DB_STMT hStmt = NULL;
15326 char sql_query_string[8192] = { 0x00, };
15329 if (!local_activity) {
15330 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15331 if (err_code != NULL)
15332 *err_code = EMAIL_ERROR_INVALID_PARAM;
15336 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15338 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15340 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15341 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15343 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15344 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15345 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15346 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15347 EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15348 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15349 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
15351 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15354 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15355 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15356 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15358 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15360 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15361 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15362 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15363 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15364 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15365 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15366 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15369 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15371 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15372 ("sqlite3_step fail:%d", rc));
15373 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15374 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15379 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15381 if (hStmt != NULL) {
15382 rc = sqlite3_finalize(hStmt);
15383 if (rc != SQLITE_OK) {
15384 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15385 error = EMAIL_ERROR_DB_FAILURE;
15389 if (err_code != NULL)
15392 EM_DEBUG_FUNC_END("ret [%d]", ret);
15397 * emstorage_get_activity - Get the Local activity Information
15401 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)
15403 EM_DEBUG_FUNC_BEGIN();
15405 int i = 0, count = 0, rc = -1, ret = false;
15406 int error = EMAIL_ERROR_NONE;
15407 emstorage_activity_tbl_t *p_activity_tbl = NULL;
15408 char sql_query_string[1024] = {0x00, };
15409 char **result = NULL;
15412 EM_IF_NULL_RETURN_VALUE(activity_list, false);
15413 EM_IF_NULL_RETURN_VALUE(select_num, false);
15416 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15417 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15419 *err_code = EMAIL_ERROR_INVALID_PARAM;
15423 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15425 EMSTORAGE_START_READ_TRANSACTION(transaction);
15427 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15429 if (activityid == ALL_ACTIVITIES) {
15430 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15432 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);
15435 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15439 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15440 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15441 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15445 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15446 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15447 error = EMAIL_ERROR_OUT_OF_MEMORY;
15452 for (i = 0; i < count; i++) {
15453 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15454 if (result[col_index])
15455 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15457 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15458 if (result[col_index])
15459 p_activity_tbl[i].account_id = atoi(result[col_index++]);
15461 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15462 if (result[col_index])
15463 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15465 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15466 if (result[col_index])
15467 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15470 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15471 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15472 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15476 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15477 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15478 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15482 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15483 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15484 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15491 sqlite3_free_table(result);
15497 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15500 *activity_list = p_activity_tbl;
15501 *select_num = count;
15502 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15503 } else if (p_activity_tbl != NULL) {
15504 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15508 if (err_code != NULL)
15511 EM_DEBUG_FUNC_END("ret [%d]", ret);
15516 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15519 EM_DEBUG_FUNC_BEGIN();
15522 int err = EMAIL_ERROR_NONE;
15525 char **result = NULL;
15527 if (NULL == activity_id) {
15528 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15530 *err_code = EMAIL_ERROR_INVALID_PARAM;
15534 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15536 /* increase unique id */
15538 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15540 /* 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); */
15541 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15542 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15544 if (NULL == result[1])
15547 rc = atoi(result[1])+1;
15552 sqlite3_free_table(result);
15561 EM_DEBUG_FUNC_END("ret [%d]", ret);
15566 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)
15569 EM_DEBUG_FUNC_BEGIN();
15571 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);
15573 if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15574 if (err_code != NULL)
15575 *err_code = EMAIL_ERROR_INVALID_PARAM;
15580 int error = EMAIL_ERROR_NONE;
15581 int i = 0, rc = -1, count = 0;
15582 char sql_query_string[1024] = {0x00, };
15583 int *activity_ids = NULL;
15585 char **result = NULL;
15586 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15587 EMSTORAGE_START_READ_TRANSACTION(transaction);
15589 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15591 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);
15593 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15596 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15597 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15598 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15602 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15604 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15605 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15606 error = EMAIL_ERROR_OUT_OF_MEMORY;
15610 for (i = 0; i < count; i++) {
15611 activity_ids[i] = atoi(result[col_index]);
15613 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15622 *activity_id_count = count;
15623 *activity_id_list = activity_ids;
15624 } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15625 EM_SAFE_FREE(activity_ids);
15628 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15629 if (err_code != NULL) {
15633 EM_DEBUG_FUNC_END("ret [%d]", ret);
15637 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15639 EM_DEBUG_FUNC_BEGIN();
15641 int error = EMAIL_ERROR_NONE;
15644 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15646 if (NULL == activity_id_list) {
15647 error = EMAIL_ERROR_INVALID_PARAM;
15650 EM_SAFE_FREE(activity_id_list);
15658 if (NULL != error_code) {
15659 *error_code = error;
15662 EM_DEBUG_FUNC_END("ret [%d]", ret);
15667 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15668 * or based on server mail id
15671 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15673 EM_DEBUG_FUNC_BEGIN();
15676 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15678 if (!local_activity) {
15679 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15680 if (err_code != NULL)
15681 *err_code = EMAIL_ERROR_INVALID_PARAM;
15685 int rc = -1, ret = false; /* Prevent_FIX */
15686 int err = EMAIL_ERROR_NONE;
15688 int query_where = 0;
15689 char sql_query_string[8192] = { 0x00, };
15690 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15691 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15693 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15695 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15697 EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15699 if (local_activity->account_id) {
15700 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15701 " WHERE account_id = %d ", local_activity->account_id);
15706 if (local_activity->server_mailid) {
15707 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15708 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15714 if (local_activity->mail_id) {
15715 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15717 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15718 " %s %s mail_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15725 if (local_activity->activity_type > 0) {
15726 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15727 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15730 if (local_activity->activity_id > 0) {
15731 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15732 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15736 EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15737 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15738 if (error != EMAIL_ERROR_NONE) {
15739 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15743 rc = sqlite3_changes(local_db_handle);
15745 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15746 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15752 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15754 if (hStmt != NULL) {
15755 rc = sqlite3_finalize(hStmt);
15756 if (rc != SQLITE_OK) {
15757 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15758 error = EMAIL_ERROR_DB_FAILURE;
15762 if (err_code != NULL)
15764 EM_DEBUG_FUNC_END("ret [%d]", ret);
15769 * emstorage_free_local_activity - Free the Local Activity data
15771 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15773 EM_DEBUG_FUNC_BEGIN();
15775 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15778 int error = EMAIL_ERROR_INVALID_PARAM;
15781 if (!local_activity_list || !*local_activity_list) {
15782 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15784 error = EMAIL_ERROR_INVALID_PARAM;
15788 emstorage_activity_tbl_t* p = *local_activity_list;
15791 for (; i < count; i++) {
15792 EM_SAFE_FREE(p[i].dest_mbox);
15793 EM_SAFE_FREE(p[i].src_mbox);
15794 EM_SAFE_FREE(p[i].server_mailid);
15798 *local_activity_list = NULL;
15806 if (err_code != NULL)
15809 EM_DEBUG_FUNC_END("ret [%d]", ret);
15813 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15816 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15818 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15820 int ret = EMAIL_ERROR_NONE;
15821 char key_value_string[QUERY_SIZE] = { 0, };
15822 char *temp_key_value_1 = NULL;
15823 char *temp_key_value_2 = NULL;
15825 if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15826 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15827 return EMAIL_ERROR_INVALID_PARAM;
15830 switch (input_list_filter_rule->target_attribute) {
15831 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15832 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15833 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15834 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15835 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15836 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15837 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15838 case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15839 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15840 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15841 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15842 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15843 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15844 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15845 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15846 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15847 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15848 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15849 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15850 case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15851 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15852 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15853 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15854 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15855 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15856 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15857 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15858 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15859 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15860 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15861 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15862 case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15863 case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15864 case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15865 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15868 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15869 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15870 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15871 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15872 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15873 case EMAIL_MAIL_ATTRIBUTE_FROM:
15874 case EMAIL_MAIL_ATTRIBUTE_TO:
15875 case EMAIL_MAIL_ATTRIBUTE_CC:
15876 case EMAIL_MAIL_ATTRIBUTE_BCC:
15877 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15878 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15879 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15880 ret = EMAIL_ERROR_INVALID_PARAM;
15884 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15886 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15887 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15889 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15890 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15892 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15895 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15896 case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15897 case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15898 case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15899 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15903 ret = EMAIL_ERROR_INVALID_PARAM;
15904 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15908 if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
15909 *output_key_value_string = strdup(key_value_string);
15914 EM_SAFE_FREE(temp_key_value_1);
15915 EM_SAFE_FREE(temp_key_value_2);
15917 EM_DEBUG_FUNC_END("ret [%d]", ret);
15921 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15922 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15924 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15926 if (index <= 0 || !cert_password_file_name) {
15927 EM_DEBUG_EXCEPTION("Invalid parameter");
15928 return EMAIL_ERROR_INVALID_PARAM;
15931 sprintf(cert_password_file_name, ".email_cert_%d", index);
15933 EM_DEBUG_FUNC_END();
15934 return EMAIL_ERROR_NONE;
15938 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15940 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15942 int ret = EMAIL_ERROR_NONE;
15944 int length_field_name = 0;
15945 int length_value = 0;
15946 char result_rule_string[QUERY_SIZE] = { 0 , };
15947 char *mod_field_name_string = NULL;
15948 char *mod_value_string = NULL;
15949 char *temp_field_name_string = NULL;
15950 char *temp_key_value_string = NULL;
15952 if (input_list_filter_rule == NULL || output_string == NULL) {
15953 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15954 return EMAIL_ERROR_INVALID_PARAM;
15957 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15959 if (temp_field_name_string == NULL) {
15960 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15961 return EMAIL_ERROR_INVALID_PARAM;
15964 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) {
15965 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15966 return EMAIL_ERROR_INVALID_PARAM;
15969 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
15970 length_value = EM_SAFE_STRLEN(temp_key_value_string);
15972 switch (input_list_filter_rule->target_attribute) {
15973 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15974 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15975 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15976 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15977 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15978 case EMAIL_MAIL_ATTRIBUTE_FROM:
15979 case EMAIL_MAIL_ATTRIBUTE_TO:
15980 case EMAIL_MAIL_ATTRIBUTE_CC:
15981 case EMAIL_MAIL_ATTRIBUTE_BCC:
15982 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15990 if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
15991 length_field_name += strlen("UPPER() ");
15992 length_value += strlen("UPPER() ");
15993 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
15994 if (mod_field_name_string == NULL) {
15995 EM_DEBUG_EXCEPTION("em_mallocfailed");
15996 EM_SAFE_FREE(temp_field_name_string);
15997 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16001 mod_value_string = em_malloc(sizeof(char) * length_value);
16002 if (mod_value_string == NULL) {
16003 EM_DEBUG_EXCEPTION("em_mallocfailed");
16004 EM_SAFE_FREE(temp_field_name_string);
16005 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16009 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16010 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
16011 EM_SAFE_FREE(temp_key_value_string);
16013 mod_field_name_string = strdup(temp_field_name_string);
16014 mod_value_string = temp_key_value_string;
16017 switch (input_list_filter_rule->rule_type) {
16018 case EMAIL_LIST_FILTER_RULE_EQUAL:
16019 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16021 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16022 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16024 case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16025 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16027 case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16028 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16030 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16031 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16033 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16034 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16036 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16037 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16039 case EMAIL_LIST_FILTER_RULE_IN:
16040 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16042 case EMAIL_LIST_FILTER_RULE_NOT_IN:
16043 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16046 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16047 ret = EMAIL_ERROR_INVALID_PARAM;
16051 *output_string = strdup(result_rule_string);
16054 EM_SAFE_FREE(mod_field_name_string);
16055 EM_SAFE_FREE(mod_value_string);
16057 EM_DEBUG_FUNC_END("ret [%d]", ret);
16061 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16063 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16065 char *field_name_string = NULL;
16066 char key_value_string[QUERY_SIZE] = {0,};
16067 char result_rule_string[QUERY_SIZE] = {0,};
16070 int query_size = 0;
16073 int error = EMAIL_ERROR_NONE;
16074 char **result = NULL;
16075 char sql_query_string[QUERY_SIZE] = {0,};
16076 char *sql_query_string2 = NULL;
16077 sqlite3 *local_db_handle = NULL;
16078 int *mail_ids = NULL;
16080 if (input_list_filter_rule == NULL || output_string == NULL) {
16081 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16082 return EMAIL_ERROR_INVALID_PARAM;
16085 field_name_string = EM_SAFE_STRDUP("attachment_name");
16086 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16088 switch (input_list_filter_rule->rule_type) {
16090 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16091 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16094 case EMAIL_LIST_FILTER_RULE_MATCH:
16095 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16099 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16100 error = EMAIL_ERROR_INVALID_PARAM;
16104 local_db_handle = emstorage_get_db_connection(multi_user_name);
16106 EMSTORAGE_START_READ_TRANSACTION(true);
16107 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16109 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16110 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16111 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16116 EM_DEBUG_LOG("No mail found...");
16117 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16118 *output_string = strdup("mail_id IN () ");
16122 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16124 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16125 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16126 error = EMAIL_ERROR_OUT_OF_MEMORY;
16130 for (i = 0; i < count; i++) {
16131 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16132 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16135 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16136 sqlite3_free_table(result);
16137 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16139 // sqlite3_db_release_memory(local_db_handle);
16142 query_size = (10 * count) + strlen("mail_id IN () ");
16144 sql_query_string2 = em_malloc(query_size);
16145 if (sql_query_string2 == NULL) {
16146 EM_DEBUG_EXCEPTION("em_mallocfailed");
16147 error = EMAIL_ERROR_OUT_OF_MEMORY;
16151 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16152 for (i = 0; i < count-1; i++) {
16153 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16155 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16157 *output_string = strdup(sql_query_string2);
16160 EM_SAFE_FREE(mail_ids); /* prevent */
16161 EM_SAFE_FREE(sql_query_string2);
16162 EM_SAFE_FREE(field_name_string);
16163 EM_DEBUG_FUNC_END("error [%d]", error);
16167 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16169 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16170 char *field_name_string = NULL;
16171 char key_value_string[QUERY_SIZE] = {0,};
16172 char result_rule_string[QUERY_SIZE] = {0,};
16176 int query_size = 0;
16177 int error = EMAIL_ERROR_NONE;
16178 char **result = NULL;
16179 char sql_query_string[QUERY_SIZE] = {0,};
16180 char *sql_query_string2 = NULL;
16181 sqlite3 *local_db_handle = NULL;
16182 int *mail_ids = NULL;
16184 if (input_list_filter_rule == NULL || output_string == NULL) {
16185 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16186 return EMAIL_ERROR_INVALID_PARAM;
16189 field_name_string = EM_SAFE_STRDUP("body_text");
16190 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16192 switch (input_list_filter_rule->rule_type) {
16194 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16195 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16198 case EMAIL_LIST_FILTER_RULE_MATCH:
16199 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16203 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16204 error = EMAIL_ERROR_INVALID_PARAM;
16208 local_db_handle = emstorage_get_db_connection(multi_user_name);
16210 EMSTORAGE_START_READ_TRANSACTION(true);
16211 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16213 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16214 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16215 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16220 EM_DEBUG_LOG("No mail found...");
16221 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16222 *output_string = strdup("mail_id IN () ");
16226 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16229 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16230 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16231 error = EMAIL_ERROR_OUT_OF_MEMORY;
16235 for (i = 0; i < count; i++) {
16236 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16237 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16240 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16241 sqlite3_free_table(result);
16242 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16244 // sqlite3_db_release_memory(local_db_handle);
16247 query_size = (10 * count) + strlen("mail_id IN () ");
16248 sql_query_string2 = em_malloc(query_size);
16249 if (sql_query_string2 == NULL) {
16250 EM_DEBUG_EXCEPTION("em_mallocfailed");
16251 error = EMAIL_ERROR_OUT_OF_MEMORY;
16255 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16256 for (i = 0; i < count-1; i++) {
16257 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16259 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16261 *output_string = strdup(sql_query_string2);
16264 EM_SAFE_FREE(mail_ids); /* prevent */
16265 EM_SAFE_FREE(sql_query_string2);
16266 EM_SAFE_FREE(field_name_string);
16267 EM_DEBUG_FUNC_END("error [%d]", error);
16271 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16273 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16275 char result_rule_string[QUERY_SIZE] = { 0 , };
16276 int ret = EMAIL_ERROR_NONE;
16278 emstorage_account_tbl_t *account_tbl_array = NULL;
16281 char *result_str = NULL;
16282 char *tmp_str1 = NULL;
16283 char *tmp_str2 = NULL;
16284 char query_per_account[QUERY_SIZE] = { 0 , };
16286 if (input_sorting_rule->force_boolean_check) {
16287 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16289 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16291 switch (input_sorting_rule->sort_order) {
16292 case EMAIL_SORT_ORDER_ASCEND:
16293 EM_SAFE_STRCAT(result_rule_string, " ASC ");
16296 case EMAIL_SORT_ORDER_DESCEND:
16297 EM_SAFE_STRCAT(result_rule_string, " DESC ");
16300 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16301 EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
16304 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16305 EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
16308 case EMAIL_SORT_ORDER_TO_CCBCC:
16309 memset(result_rule_string, 0, QUERY_SIZE);
16310 if (input_sorting_rule->key_value.string_type_value)
16311 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16312 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16313 input_sorting_rule->key_value.string_type_value);
16316 case EMAIL_SORT_ORDER_TO_CC_BCC:
16317 memset(result_rule_string, 0, QUERY_SIZE);
16318 if (input_sorting_rule->key_value.string_type_value)
16319 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16320 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16321 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16324 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16325 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16326 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16331 EM_DEBUG_LOG("No account exist");
16332 ret = EMAIL_ERROR_INVALID_PARAM;
16336 for (i = 0; i < count; i++) {
16337 if (i > 0 && result_str) {
16338 tmp_str2 = result_str;
16339 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16340 EM_SAFE_FREE(tmp_str2);
16343 memset(query_per_account, 0, QUERY_SIZE);
16344 snprintf(query_per_account, QUERY_SIZE,
16345 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16346 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16348 tmp_str1 = result_str;
16350 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16352 result_str = g_strdup(query_per_account);
16353 EM_SAFE_FREE(tmp_str1);
16356 snprintf(result_rule_string, QUERY_SIZE,
16357 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16359 EM_SAFE_FREE(result_str);
16360 if (account_tbl_array)
16361 emstorage_free_account(&account_tbl_array, count, NULL);
16364 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16365 memset(result_rule_string, 0, QUERY_SIZE);
16366 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16367 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16368 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16369 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16372 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16373 memset(result_rule_string, 0, QUERY_SIZE);
16374 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16375 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16376 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16377 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16381 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16382 ret = EMAIL_ERROR_INVALID_PARAM;
16386 *output_string = strdup(result_rule_string);
16389 EM_DEBUG_FUNC_END("ret [%d]", ret);
16393 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)
16395 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);
16396 int ret = EMAIL_ERROR_NONE;
16398 int string_offset = 0;
16399 int query_size = 0;
16400 int new_query_size = 0;
16401 char *conditional_clause_string = NULL;
16402 char *result_string_for_a_item = NULL;
16404 if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16405 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16406 return EMAIL_ERROR_INVALID_PARAM;
16409 conditional_clause_string = em_malloc(QUERY_SIZE);
16410 if (conditional_clause_string == NULL) {
16411 EM_DEBUG_EXCEPTION("Memory is full");
16412 return EMAIL_ERROR_OUT_OF_MEMORY;
16415 if (input_filter_count > 0) {
16416 query_size = QUERY_SIZE;
16417 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16419 for (i = 0; i < input_filter_count; i++) {
16420 switch (input_filter_list[i].list_filter_item_type) {
16421 case EMAIL_LIST_FILTER_ITEM_RULE:
16422 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16423 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16426 case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16427 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16428 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16431 case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16432 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16433 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16436 case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16437 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16438 switch (input_filter_list[i].list_filter_item.operator_type) {
16439 case EMAIL_LIST_FILTER_OPERATOR_AND:
16440 result_string_for_a_item = strdup("AND ");
16442 case EMAIL_LIST_FILTER_OPERATOR_OR:
16443 result_string_for_a_item = strdup("OR ");
16445 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16446 result_string_for_a_item = strdup(" (");
16448 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16449 result_string_for_a_item = strdup(") ");
16455 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16456 ret = EMAIL_ERROR_INVALID_PARAM;
16460 if (result_string_for_a_item == NULL) {
16461 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16462 ret = EMAIL_ERROR_INVALID_PARAM;
16466 if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16467 EM_DEBUG_LOG("QUERY is too long");
16468 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16469 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16470 if (conditional_clause_string == NULL) {
16471 EM_DEBUG_EXCEPTION("realloc failed");
16472 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16476 query_size = new_query_size;
16479 strcat(conditional_clause_string, result_string_for_a_item);
16480 EM_SAFE_FREE(result_string_for_a_item);
16484 if (input_sorting_rule_count > 0) {
16485 strcat(conditional_clause_string, "ORDER BY ");
16487 for (i = 0; i < input_sorting_rule_count; i++) {
16488 if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16489 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16493 strcat(conditional_clause_string, ", ");
16494 strcat(conditional_clause_string, result_string_for_a_item);
16495 EM_SAFE_FREE(result_string_for_a_item);
16499 if (input_start_index != -1 && input_limit_count != -1) {
16500 string_offset = strlen(conditional_clause_string);
16501 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16504 *output_conditional_clause = strdup(conditional_clause_string);
16507 EM_SAFE_FREE(result_string_for_a_item);
16508 EM_SAFE_FREE(conditional_clause_string);
16510 EM_DEBUG_FUNC_END("ret [%d]", ret);
16514 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16516 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16517 int err = EMAIL_ERROR_NONE;
16519 email_list_filter_t *temp_filter_list = NULL;
16521 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16523 for (i = 0; i < input_filter_count; i++) {
16524 temp_filter_list = (*input_filter_list) + i;
16525 if (!temp_filter_list) {
16529 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16530 switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16531 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16532 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16533 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16534 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16535 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16536 case EMAIL_MAIL_ATTRIBUTE_FROM:
16537 case EMAIL_MAIL_ATTRIBUTE_TO:
16538 case EMAIL_MAIL_ATTRIBUTE_CC:
16539 case EMAIL_MAIL_ATTRIBUTE_BCC:
16540 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16541 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16542 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16543 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16548 } 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) {
16549 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16550 } 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) {
16551 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16555 EM_SAFE_FREE(*input_filter_list);
16557 EM_DEBUG_FUNC_END("err [%d]", err);
16561 /* Tasks --------------------------------------------------------------------------*/
16562 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)
16564 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);
16568 int err = EMAIL_ERROR_NONE;
16570 DB_STMT hStmt = NULL;
16571 char sql_query_string[QUERY_SIZE] = {0, };
16572 sqlite3 *local_db_handle = NULL;
16573 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16574 char **result = NULL;
16576 if (input_task_parameter == NULL || output_task_id == NULL) {
16577 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16578 return EMAIL_ERROR_INVALID_PARAM;
16581 local_db_handle = emstorage_get_db_connection(multi_user_name);
16582 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16584 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16585 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16586 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16588 if (NULL == result[1])
16591 task_id = atoi(result[1])+1;
16593 *output_task_id = task_id;
16595 sqlite3_free_table(result);
16598 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16599 "INSERT INTO mail_task_tbl VALUES "
16601 " ? " /* task_id */
16602 " , ? " /* task_type */
16603 " , ? " /* task_status */
16604 " , ? " /* task_priority */
16605 " , ? " /* task_parameter_length */
16606 " , ? " /* task_parameter */
16607 " , ? " /* date_time */
16610 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16611 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16612 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16614 EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16617 _bind_stmt_field_data_int(hStmt, i++, task_id);
16618 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16619 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16620 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16621 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16622 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16623 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16625 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16627 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16628 ("sqlite3_step fail:%d", rc));
16629 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16630 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16632 ret = (err == EMAIL_ERROR_NONE);
16635 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16637 if (hStmt != NULL) {
16638 rc = sqlite3_finalize(hStmt);
16639 if (rc != SQLITE_OK) {
16640 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16641 err = EMAIL_ERROR_DB_FAILURE;
16645 EM_DEBUG_FUNC_END("err [%d]", err);
16649 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16651 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16653 int err = EMAIL_ERROR_NONE;
16654 char sql_query_string[QUERY_SIZE] = {0, };
16656 sqlite3 *local_db_handle = NULL;
16659 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16660 return EMAIL_ERROR_INVALID_PARAM;
16663 local_db_handle = emstorage_get_db_connection(multi_user_name);
16665 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16667 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16668 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16669 if (err != EMAIL_ERROR_NONE) {
16670 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16677 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16679 EM_DEBUG_FUNC_END("err [%d]", err);
16683 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16685 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16687 int err = EMAIL_ERROR_NONE;
16688 char sql_query_string[QUERY_SIZE] = {0, };
16690 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16691 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16693 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16694 "UPDATE mail_task_tbl SET"
16695 " task_status = %d"
16696 " WHERE task_id = %d"
16699 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16700 if (err != EMAIL_ERROR_NONE) {
16701 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16708 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16710 EM_DEBUG_FUNC_END("err [%d]", err);
16714 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16715 const char *input_conditional_clause,
16716 const char *input_ordering_clause,
16717 email_task_t **output_task_list,
16718 int *output_task_count)
16720 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16721 "output_task_list[%p], output_task_count[%d]",
16722 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16723 int i = 0, count = 0, rc = -1;
16725 int field_index = 0;
16726 int err = EMAIL_ERROR_NONE;
16727 email_task_t *task_item_from_tbl = NULL;
16728 char sql_query_string[QUERY_SIZE] = {0, };
16729 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16731 sqlite3 *local_db_handle = NULL;
16732 DB_STMT hStmt = NULL;
16734 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16735 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16737 local_db_handle = emstorage_get_db_connection(multi_user_name);
16739 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16740 "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16741 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16743 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16744 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16745 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16747 count = atoi(result[1]);
16748 sqlite3_free_table(result);
16750 EM_DEBUG_LOG("count = %d", rc);
16753 EM_DEBUG_EXCEPTION("no task found...");
16754 err = EMAIL_ERROR_TASK_NOT_FOUND;
16758 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16759 "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16760 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16762 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16764 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16765 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16766 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16768 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16769 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16770 ("sqlite3_step fail:%d", rc));
16772 if (rc == SQLITE_DONE) {
16773 EM_DEBUG_EXCEPTION("no task found...");
16774 err = EMAIL_ERROR_TASK_NOT_FOUND;
16779 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16780 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16781 err = EMAIL_ERROR_OUT_OF_MEMORY;
16785 for (i = 0; i < count; i++) {
16786 /* get recordset */
16789 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16790 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16791 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16792 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16793 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16794 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16796 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16797 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
16798 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16799 ("sqlite3_step fail:%d", rc));
16804 if (err == EMAIL_ERROR_NONE) {
16805 if (output_task_list)
16806 *output_task_list = task_item_from_tbl;
16807 *output_task_count = count;
16809 if (task_item_from_tbl) {
16810 for (i = 0; i < count; i++)
16811 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16813 free(task_item_from_tbl);
16817 if (hStmt != NULL) {
16818 rc = sqlite3_finalize(hStmt);
16820 if (rc != SQLITE_OK) {
16821 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16822 err = EMAIL_ERROR_DB_FAILURE;
16826 EM_DEBUG_FUNC_END("err [%d]", err);
16830 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)
16832 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);
16833 int err = EMAIL_ERROR_NONE;
16835 if (message_id == NULL) {
16836 EM_DEBUG_EXCEPTION("Invalid parameter");
16837 err = EMAIL_ERROR_INVALID_PARAM;
16843 int temp_mail_id = 0;
16844 int where_pararaph_length = 0;
16845 char *where_pararaph = NULL;
16846 char sql_query_string[QUERY_SIZE] = {0, };
16847 char **result = NULL;
16848 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16850 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16852 where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16853 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16854 if (where_pararaph == NULL) {
16855 EM_DEBUG_EXCEPTION("em_mallocfailed");
16856 err = EMAIL_ERROR_OUT_OF_MEMORY;
16860 if (account_id != ALL_ACCOUNT)
16861 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);
16863 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16865 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16866 strcat(sql_query_string, where_pararaph);
16868 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16870 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16871 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16872 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16875 EM_DEBUG_LOG("Count of mails [%d]", count);
16878 _get_table_field_data_int(result, &temp_mail_id, 1);
16879 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16881 memset(sql_query_string, 0x00, QUERY_SIZE);
16882 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16884 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16885 strcat(sql_query_string, where_pararaph);
16887 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16888 if (err != EMAIL_ERROR_NONE) {
16889 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16894 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16899 sqlite3_free_table(result);
16903 EM_SAFE_FREE(where_pararaph);
16905 if (mail_id != NULL)
16906 *mail_id = temp_mail_id;
16908 EM_DEBUG_FUNC_END("err : [%d]", err);
16911 /* Tasks --------------------------------------------------------------------------*/
16913 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16914 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)
16916 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16918 if (!local_activity || !activity_id) {
16919 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16920 if (err_code != NULL)
16921 *err_code = EMAIL_ERROR_INVALID_PARAM;
16927 int error = EMAIL_ERROR_NONE;
16930 char sql_query_string[QUERY_SIZE] = {0, };
16931 DB_STMT hStmt = NULL;
16933 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16934 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16936 memset(sql_query_string, 0x00, sizeof(sql_query_string));
16937 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16938 "INSERT INTO mail_auto_download_activity_tbl VALUES "
16940 "? " /* Activity ID */
16942 ",?" /* Account ID */
16943 ",?" /* Local Mail ID */
16944 ",?" /* Server mail ID */
16945 ",?" /* Mailbox ID*/
16946 ",?" /* Multi USER NAME */
16949 char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
16950 char **result = NULL;
16953 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16955 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16956 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16958 if (NULL == result[1]) rc = 1;
16959 else rc = atoi(result[1])+1;
16960 sqlite3_free_table(result);
16963 *activity_id = local_activity->activity_id = rc;
16965 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
16966 local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
16968 if (local_activity->mailbox_id)
16969 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
16972 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16973 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16974 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16977 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
16978 _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
16979 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
16980 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
16981 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
16982 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
16983 _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
16986 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16988 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16989 ("sqlite3_step fail:%d", rc));
16990 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16991 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16996 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
16997 if (hStmt != NULL) {
16998 rc = sqlite3_finalize(hStmt);
17000 if (rc != SQLITE_OK) {
17001 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17002 error = EMAIL_ERROR_DB_FAILURE;
17006 if (err_code != NULL)
17009 EM_DEBUG_FUNC_END("ret [%d]", ret);
17014 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)
17016 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);
17018 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17019 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);
17021 if (err_code != NULL)
17022 *err_code = EMAIL_ERROR_INVALID_PARAM;
17028 int error = EMAIL_ERROR_NONE;
17029 char sql_query_string[QUERY_SIZE] = {0, };
17030 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17032 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17033 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17035 if (activity_id == 0)
17036 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);
17038 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);
17040 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17041 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17042 if (error != EMAIL_ERROR_NONE) {
17043 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17047 /* validate activity existence */
17048 rc = sqlite3_changes(local_db_handle);
17050 EM_DEBUG_EXCEPTION("No matching activity found");
17051 error = EMAIL_ERROR_DATA_NOT_FOUND;
17059 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17061 if (err_code != NULL)
17064 EM_DEBUG_FUNC_END("ret [%d]", ret);
17068 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17070 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17072 if (account_id < FIRST_ACCOUNT_ID) {
17073 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17074 if (err_code != NULL)
17075 *err_code = EMAIL_ERROR_INVALID_PARAM;
17081 int error = EMAIL_ERROR_NONE;
17082 char sql_query_string[QUERY_SIZE] = {0, };
17084 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17086 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17087 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17088 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17090 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17091 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17092 if (error != EMAIL_ERROR_NONE) {
17093 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17097 rc = sqlite3_changes(local_db_handle);
17099 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17100 error = EMAIL_ERROR_DATA_NOT_FOUND;
17108 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17110 if (err_code != NULL)
17113 EM_DEBUG_FUNC_END("ret [%d]", ret);
17118 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)
17120 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17122 if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17123 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17124 if (err_code != NULL)
17125 *err_code = EMAIL_ERROR_INVALID_PARAM;
17131 int error = EMAIL_ERROR_NONE;
17132 char sql_query_string[QUERY_SIZE] = {0, };
17134 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17136 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17137 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17138 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);
17140 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17141 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17142 if (error != EMAIL_ERROR_NONE) {
17143 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17147 rc = sqlite3_changes(local_db_handle);
17149 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17150 error = EMAIL_ERROR_DATA_NOT_FOUND;
17158 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17160 if (err_code != NULL)
17163 EM_DEBUG_FUNC_END("ret [%d]", ret);
17168 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)
17170 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17172 if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17173 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);
17175 if (err_code != NULL)
17176 *err_code = EMAIL_ERROR_INVALID_PARAM;
17183 int error = EMAIL_ERROR_NONE;
17185 DB_STMT hStmt = NULL;
17186 email_event_auto_download *event_list = NULL;
17187 char sql_query_string[QUERY_SIZE] = {0, };
17189 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17191 EMSTORAGE_START_READ_TRANSACTION(transaction);
17193 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17194 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);
17197 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17198 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17199 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17201 *count = atoi(result[1]);
17202 sqlite3_free_table(result);
17204 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17207 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17208 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17212 EM_DEBUG_LOG("Activity Count = %d", *count);
17214 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17215 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);
17217 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17220 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17222 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17223 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17224 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17227 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17228 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17229 ("sqlite3_step fail:%d", rc));
17231 if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17232 EM_DEBUG_EXCEPTION("Malloc failed");
17234 error = EMAIL_ERROR_OUT_OF_MEMORY;
17238 for (i = 0; i < (*count); i++) {
17239 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17240 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17241 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17242 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17243 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17244 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17246 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17247 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17248 ("sqlite3_step fail:%d", rc));
17256 *event_start = event_list;
17258 EM_SAFE_FREE(event_list);
17259 *event_start = NULL;
17263 if (hStmt != NULL) {
17264 rc = sqlite3_finalize(hStmt);
17266 if (rc != SQLITE_OK) {
17267 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17268 error = EMAIL_ERROR_DB_FAILURE;
17272 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17273 if (err_code != NULL)
17276 EM_DEBUG_FUNC_END("ret [%d]", ret);
17281 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17283 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17285 if (!activity_count || !err_code) {
17286 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17287 if (err_code != NULL)
17288 *err_code = EMAIL_ERROR_INVALID_PARAM;
17294 int error = EMAIL_ERROR_NONE;
17295 DB_STMT hStmt = NULL;
17296 char sql_query_string[QUERY_SIZE] = {0, };
17298 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17300 EMSTORAGE_START_READ_TRANSACTION(transaction);
17301 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17303 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17305 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17308 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17309 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17310 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17311 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17314 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17315 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17316 ("sqlite3_step fail:%d", rc));
17318 _get_stmt_field_data_int(hStmt, activity_count, 0);
17320 EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17326 if (hStmt != NULL) {
17327 rc = sqlite3_finalize(hStmt);
17329 if (rc != SQLITE_OK) {
17330 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17331 error = EMAIL_ERROR_DB_FAILURE;
17335 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17336 if (err_code != NULL)
17339 EM_DEBUG_FUNC_END("ret [%d]", ret);
17344 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17346 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17348 if (!account_list || !count) {
17349 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17350 if (err_code != NULL)
17351 *err_code = EMAIL_ERROR_INVALID_PARAM;
17356 int error = EMAIL_ERROR_NONE;
17357 char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17359 int i = 0, rc = -1;
17360 int *result_account_list = NULL;
17361 DB_STMT hStmt = NULL;
17362 char sql_query_string[QUERY_SIZE] = {0, };
17363 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17365 EMSTORAGE_START_READ_TRANSACTION(transaction);
17367 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17368 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17369 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17371 *count = atoi(result[1]);
17372 sqlite3_free_table(result);
17375 EM_DEBUG_EXCEPTION("no account found...");
17376 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17381 EM_DEBUG_LOG("Account count [%d]", *count);
17383 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17385 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17387 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17390 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17392 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17393 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17394 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17397 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17398 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17399 ("sqlite3_step fail:%d", rc));
17401 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17402 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17403 error = EMAIL_ERROR_OUT_OF_MEMORY;
17407 for (i = 0; i < (*count); i++) {
17408 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17411 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17412 ("sqlite3_step fail:%d", rc));
17413 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17421 *account_list = result_account_list;
17423 EM_SAFE_FREE(result_account_list);
17425 if (hStmt != NULL) {
17426 rc = sqlite3_finalize(hStmt);
17428 if (rc != SQLITE_OK) {
17429 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17430 error = EMAIL_ERROR_DB_FAILURE;
17434 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17435 if (err_code != NULL)
17437 EM_DEBUG_FUNC_END("ret [%d]", ret);
17442 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)
17444 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17446 if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17447 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17448 if (err_code != NULL)
17449 *err_code = EMAIL_ERROR_INVALID_PARAM;
17454 int error = EMAIL_ERROR_NONE;
17456 int i = 0, rc = -1;
17457 int *mbox_list = NULL;
17458 DB_STMT hStmt = NULL;
17459 char sql_query_string[QUERY_SIZE] = {0, };
17461 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17463 EMSTORAGE_START_READ_TRANSACTION(transaction);
17465 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17466 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);
17469 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17470 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17471 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17473 *count = atoi(result[1]);
17474 sqlite3_free_table(result);
17477 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17478 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17482 EM_DEBUG_LOG("Mailbox count = %d", *count);
17484 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17486 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);
17488 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17491 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17494 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17495 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17498 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17499 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17500 ("sqlite3_step fail:%d", rc));
17502 mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17503 if (mbox_list == NULL) {
17504 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17505 error = EMAIL_ERROR_OUT_OF_MEMORY;
17509 for (i = 0; i < (*count); i++) {
17510 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17511 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17513 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17514 ("sqlite3_step fail:%d", rc));
17515 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17523 *mailbox_list = mbox_list;
17525 EM_SAFE_FREE(mbox_list);
17527 if (hStmt != NULL) {
17528 rc = sqlite3_finalize(hStmt);
17530 if (rc != SQLITE_OK) {
17531 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17532 error = EMAIL_ERROR_DB_FAILURE;
17536 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17537 if (err_code != NULL)
17539 EM_DEBUG_FUNC_END("ret [%d]", ret);
17544 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)
17546 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17548 if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17549 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17550 if (err_code != NULL)
17551 *err_code = EMAIL_ERROR_INVALID_PARAM;
17557 int error = EMAIL_ERROR_NONE;
17558 char sql_query_string[QUERY_SIZE] = {0, };
17559 DB_STMT hStmt = NULL;
17561 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17563 EMSTORAGE_START_READ_TRANSACTION(transaction);
17564 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17565 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);
17567 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17569 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17570 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17571 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17572 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17574 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17575 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17576 ("sqlite3_step fail:%d", rc));
17578 _get_stmt_field_data_int(hStmt, activity_count, 0);
17580 EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17586 if (hStmt != NULL) {
17587 rc = sqlite3_finalize(hStmt);
17589 if (rc != SQLITE_OK) {
17590 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17591 error = EMAIL_ERROR_DB_FAILURE;
17595 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17597 if (err_code != NULL)
17600 EM_DEBUG_FUNC_END("ret [%d]", ret);
17605 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)
17607 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17609 int rc = -1, ret = false;
17610 int error = EMAIL_ERROR_NONE;
17611 char sql_query_string[QUERY_SIZE] = {0, };
17612 int transaction = true;
17614 if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17615 EM_DEBUG_EXCEPTION("Invalid parameters");
17616 error = EMAIL_ERROR_INVALID_PARAM;
17620 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17622 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17623 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17625 if (mailbox_id > 0)
17626 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17627 "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);
17628 else if (mailbox_name)
17629 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17630 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17632 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17633 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17634 if (error != EMAIL_ERROR_NONE) {
17635 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17639 rc = sqlite3_changes(local_db_handle);
17641 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17648 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17650 if (err_code != NULL)
17653 EM_DEBUG_FUNC_END("ret [%d]", ret);
17659 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17664 } email_column_info_t;
17666 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17668 EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17672 char *column_name = NULL;
17673 char *column_type = NULL;
17674 GList *new_list = *((GList**)arg1);
17675 email_column_info_t *column_info_item = NULL;
17677 for (i = 0; i < argc; ++i) {
17678 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17679 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17681 EM_SAFE_FREE(column_name);
17683 column_name = EM_SAFE_STRDUP(argv[i]);
17685 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17687 EM_SAFE_FREE(column_type);
17689 column_type = EM_SAFE_STRDUP(argv[i]);
17694 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17695 column_info_item = em_malloc(sizeof(email_column_info_t));
17696 if (column_info_item == NULL) {
17697 EM_DEBUG_EXCEPTION("em_mallocfailed");
17701 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17702 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17703 new_list = g_list_append(new_list, (gpointer)column_info_item);
17704 *((GList**)arg1) = new_list;
17709 EM_SAFE_FREE(column_name);
17710 EM_SAFE_FREE(column_type);
17712 EM_DEBUG_FUNC_END();
17716 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17718 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17719 int err = EMAIL_ERROR_NONE;
17720 int result_from_sqlite = 0;
17721 char *error_message_from_sqlite = NULL;
17722 char sql_query_string[QUERY_SIZE] = {0, };
17723 GList *new_list = NULL;
17724 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17726 SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17728 result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17730 if (result_from_sqlite != SQLITE_OK)
17731 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17733 EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17735 if (new_list && output_column_info) {
17736 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17737 *output_column_info = new_list;
17740 EM_DEBUG_FUNC_END("err [%d]", err);
17744 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)
17746 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);
17747 int error = EMAIL_ERROR_NONE;
17749 sqlite3 *local_db_handle = NULL;
17750 char sql_query_string[QUERY_SIZE] = {0, };
17752 if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17753 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17754 error = EMAIL_ERROR_INVALID_PARAM;
17758 local_db_handle = emstorage_get_db_connection(multi_user_name);
17760 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17761 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17763 EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17765 EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17766 reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17768 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17771 if (error == EMAIL_ERROR_NONE) {
17772 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17774 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17777 EM_DEBUG_FUNC_END("error [%d]", error);
17781 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17783 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17784 int error = EMAIL_ERROR_NONE;
17786 sqlite3 *local_db_handle = NULL;
17787 char sql_query_string[QUERY_SIZE] = {0, };
17789 if (input_table_name == NULL || input_new_column == NULL) {
17790 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17791 error = EMAIL_ERROR_INVALID_PARAM;
17795 local_db_handle = emstorage_get_db_connection(multi_user_name);
17797 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17798 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17799 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);
17800 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17803 if (error == EMAIL_ERROR_NONE) {
17804 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17806 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17809 EM_DEBUG_FUNC_END("error [%d]", error);
17813 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17815 EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17816 int error = EMAIL_ERROR_NONE;
17818 sqlite3 *local_db_handle = NULL;
17819 char sql_query_string[QUERY_SIZE] = {0, };
17821 if (input_table_name == NULL) {
17822 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17823 error = EMAIL_ERROR_INVALID_PARAM;
17827 local_db_handle = emstorage_get_db_connection(multi_user_name);
17829 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17830 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17831 SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17832 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17835 if (error == EMAIL_ERROR_NONE) {
17836 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17838 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17841 EM_DEBUG_FUNC_END("error [%d]", error);
17845 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17847 EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17848 email_column_info_t *left_one = (email_column_info_t*)old_column_info;
17849 email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17851 if (old_column_info == NULL || new_column_info == NULL)
17854 return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17857 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17859 EM_DEBUG_FUNC_BEGIN();
17862 int error = EMAIL_ERROR_NONE;
17864 email_column_info_t *new_column_info = NULL;
17865 email_column_info_t *p_column_info = NULL;
17866 char **create_table_query = NULL;
17867 GList *found_data = NULL;
17868 GList *column_list_of_old_table = NULL;
17869 GList *column_list_of_new_table = NULL;
17870 char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17871 { "mail_box_tbl", "mail_box_tbl_new" },
17872 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17873 { "mail_rule_tbl", "mail_rule_tbl_new" },
17874 { "mail_tbl", "mail_tbl_new" },
17875 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17876 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17877 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17881 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17882 #ifdef __FEATURE_LOCAL_ACTIVITY__
17883 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17887 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17888 { "mail_task_tbl", "mail_task_tbl_new" },
17889 #ifdef __FEATURE_BODY_SEARCH__
17890 { "mail_text_tbl", "mail_text_tbl_new" },
17895 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17896 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17903 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17905 if (error != EMAIL_ERROR_NONE) {
17906 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17910 if (query_len < CREATE_TABLE_MAX) {
17911 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17912 error = EMAIL_ERROR_SYSTEM_FAILURE;
17916 for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17917 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17918 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17919 /* Check existing of _new table */
17920 emstorage_drop_table(multi_user_name, table_names[i][1]);
17921 error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17922 if (error != EMAIL_ERROR_NONE) {
17923 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17927 emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17928 emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17930 /* Compare fields and add new field */
17931 for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17932 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
17933 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
17934 if (found_data == NULL) {
17936 emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
17940 emstorage_drop_table(multi_user_name, table_names[i][1]);
17942 EM_DEBUG_LOG("Skipped");
17946 if (create_table_query) {
17948 for (i = 0; i < query_len; i++) {
17949 if (create_table_query[i]) {
17950 EM_SAFE_FREE(create_table_query[i]);
17953 EM_SAFE_FREE(create_table_query);
17956 found_data = g_list_first(column_list_of_old_table);
17957 while (found_data != NULL) {
17958 p_column_info = (email_column_info_t *)found_data->data;
17959 EM_SAFE_FREE(p_column_info->column_name);
17960 EM_SAFE_FREE(p_column_info->column_type);
17961 EM_SAFE_FREE(p_column_info);
17963 found_data = g_list_next(found_data);
17965 g_list_free(column_list_of_old_table);
17967 found_data = g_list_first(column_list_of_new_table);
17968 while (found_data != NULL) {
17969 p_column_info = (email_column_info_t *)found_data->data;
17970 EM_SAFE_FREE(p_column_info->column_name);
17971 EM_SAFE_FREE(p_column_info->column_type);
17972 EM_SAFE_FREE(p_column_info);
17974 found_data = g_list_next(found_data);
17976 g_list_free(column_list_of_new_table);
17978 EM_DEBUG_FUNC_END("error [%d]", error);
17981 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */