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>
57 #include "email-internal-types.h"
58 #include "email-convert.h"
59 #include "email-core-utils.h"
60 #include "email-utilities.h"
61 #include "email-storage.h"
62 #include "email-debug-log.h"
63 #include "email-types.h"
64 #include "email-convert.h"
65 #include "email-core-signal.h"
66 #include "email-core-event.h"
67 #include "email-core-container.h"
68 #include "email-core-gmime.h"
69 #include "email-core-key-manager.h"
71 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
72 #define CONTENT_DATA "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define CONTENT_TYPE_DATA "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
75 #define FLAG_SEEN 0x01
76 #define FLAG_DELETED 0x02
77 #define FLAG_FLAGGED 0x04
78 #define FLAG_ANSWERED 0x08
80 #define FLAG_DRAFT 0x20
84 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL 256
85 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL 128
86 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL 1027
87 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL 256
89 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
90 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
91 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
92 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
93 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
94 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
95 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL 50
96 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
97 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL 30
98 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
99 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
100 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
101 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL 256
102 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL 256
103 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL 128
104 #define ALIAS_LEN_IN_MAIL_BOX_TBL 128
105 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
106 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
107 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
108 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL 256
109 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL 256
110 #define VALUE_LEN_IN_MAIL_RULE_TBL 256
111 #define VALUE2_LEN_IN_MAIL_RULE_TBL 256
112 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL 128
113 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 128
114 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL 256
115 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 256
116 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL 256
117 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
118 #define MAILBOX_LEN_IN_MAIL_TBL 128
119 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL 128
120 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL 128
121 #define FROM_LEN_IN_MAIL_TBL 256
122 #define SENDER_LEN_IN_MAIL_TBL 256
123 #define REPLY_TO_LEN_IN_MAIL_TBL 256
124 #define TO_LEN_IN_MAIL_TBL 3999
125 #define CC_LEN_IN_MAIL_TBL 3999
126 #define BCC_LEN_IN_MAIL_TBL 3999
127 #define RETURN_PATH_LEN_IN_MAIL_TBL 3999
128 #define SUBJECT_LEN_IN_MAIL_TBL 1027
129 #define THREAD_TOPIC_LEN_IN_MAIL_TBL 256
130 #define TEXT_1_LEN_IN_MAIL_TBL 256
131 #define TEXT_2_LEN_IN_MAIL_TBL 256
132 #define MIME_ENTITY_LEN_IN_MAIL_TBL 256
133 #define DATETIME_LEN_IN_MAIL_TBL 128
134 #define MESSAGE_ID_LEN_IN_MAIL_TBL 256
135 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
136 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
137 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
138 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
139 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL 128
140 #define LOCATION_LEN_IN_MAIL_MEETING_TBL 1024
141 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL 512
142 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL 32
143 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL 32
144 #define PREVIEWBODY_LEN_IN_MAIL_TBL 512
145 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
146 #define UID "root" /* UID = root */
147 #define UID_DEFAULT 0 /* UID = root */
148 #define GID "priv_email" /* GID = priv_email */
149 #define GID_DEFAULT 10901 /* GID = priv_email */
150 #define MAX_GRP_BUF_SIZE (1024 * 4)
152 /* this define is used for query to change data (delete, insert, update) */
153 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
155 if (transaction_flag) { \
156 if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
157 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
163 /* this define is used for query to change data (delete, insert, update) */
164 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
166 if (transaction_flag) { \
167 if (result_code == true) { \
168 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169 EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
170 error_code = EMAIL_ERROR_DB_FAILURE; \
171 result_code = false; \
175 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
176 EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
177 error_code = EMAIL_ERROR_DB_FAILURE; \
183 /* this define is used for query to read (select) */
184 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
185 if (transaction_flag) { \
186 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
189 /* this define is used for query to read (select) */
190 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
191 if (transaction_flag) { \
192 /*_unlockshm_mutex(mapped_for_db_lock);*/\
195 /* for safety DB operation */
196 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
199 #define _MULTIPLE_DB_HANDLE
201 #ifdef _MULTIPLE_DB_HANDLE
209 #define MAX_DB_CLIENT 100
211 /* static int _db_handle_count = 0; */
212 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
215 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
217 EM_DEBUG_FUNC_BEGIN();
219 pthread_t current_thread_id = THREAD_SELF();
220 sqlite3 *result_db_handle = NULL;
222 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
223 ENTER_CRITICAL_SECTION(_db_handle_lock);
224 for (i = 0; i < MAX_DB_CLIENT; i++) {
225 if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
228 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
229 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
230 result_db_handle = _db_handle_list[i].db_handle;
234 LEAVE_CRITICAL_SECTION(_db_handle_lock);
236 ENTER_CRITICAL_SECTION(_db_handle_lock);
237 for (i = 0; i < MAX_DB_CLIENT; i++) {
238 if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
241 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
242 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
243 result_db_handle = _db_handle_list[i].db_handle;
247 LEAVE_CRITICAL_SECTION(_db_handle_lock);
250 if (!result_db_handle)
251 EM_DEBUG_LOG("no db_handle for [%lu] found", current_thread_id);
254 return result_db_handle;
257 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
259 EM_DEBUG_FUNC_BEGIN();
260 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
261 pthread_t current_thread_id = THREAD_SELF();
263 ENTER_CRITICAL_SECTION(_db_handle_lock);
264 for (i = 0; i < MAX_DB_CLIENT; i++) {
265 if (_db_handle_list[i].thread_id == 0) {
266 _db_handle_list[i].thread_id = current_thread_id;
267 _db_handle_list[i].db_handle = db_handle;
268 /* Only distinguished container and host */
269 _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
270 EM_DEBUG_LOG("current_thread_id [%lu], index [%d]", current_thread_id, i);
271 error_code = EMAIL_ERROR_NONE;
275 LEAVE_CRITICAL_SECTION(_db_handle_lock);
277 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
278 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
280 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
284 int emstorage_remove_db_handle()
286 EM_DEBUG_FUNC_BEGIN();
287 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
288 ENTER_CRITICAL_SECTION(_db_handle_lock);
289 for (i = 0; i < MAX_DB_CLIENT; i++) {
290 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
291 _db_handle_list[i].thread_id = 0;
292 _db_handle_list[i].db_handle = NULL;
293 EM_SAFE_FREE(_db_handle_list[i].user_name);
295 EM_DEBUG_LOG("index [%d]", i);
296 error_code = EMAIL_ERROR_NONE;
300 LEAVE_CRITICAL_SECTION(_db_handle_lock);
302 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
303 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
305 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
309 int emstorage_reset_db_handle_list()
311 EM_DEBUG_FUNC_BEGIN();
314 ENTER_CRITICAL_SECTION(_db_handle_lock);
315 for (i = 0; i < MAX_DB_CLIENT; i++) {
316 _db_handle_list[i].thread_id = 0;
317 _db_handle_list[i].db_handle = NULL;
318 EM_SAFE_FREE(_db_handle_list[i].user_name);
320 LEAVE_CRITICAL_SECTION(_db_handle_lock)
323 return EMAIL_ERROR_NONE;
327 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
329 return emstorage_db_open(multi_user_name, NULL);
332 #else /* _MULTIPLE_DB_HANDLE */
334 sqlite3 *_db_handle = NULL;
336 sqlite3 *emstorage_get_db_connection()
338 if (NULL == _db_handle)
339 emstorage_db_open(NULL);
342 #endif /* _MULTIPLE_DB_HANDLE */
344 /* ------------------------------------------------------------------------------ */
345 /* Mutex using shared memory */
347 pthread_mutex_t mutex;
351 mmapped_t *mapped_for_db_lock = NULL;
352 int shm_fd_for_db_lock = 0;
354 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
355 mmapped_t *mapped_for_generating_mail_id = NULL;
356 int shm_fd_for_generating_mail_id = 0;
357 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
359 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
360 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
362 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
363 return_value = function_call;\
364 _unlockshm_mutex(mapped_for_db_lock);\
367 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
368 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
369 { return_value = function_call; }
370 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
373 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
375 EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
376 int error = EMAIL_ERROR_NONE;
378 DB_STMT db_statement = NULL;
380 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
381 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
382 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
384 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
385 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
386 ("sqlite3_step failed [%d] [%s]", rc, query_string));
390 if (db_statement != NULL) {
391 rc = sqlite3_finalize(db_statement);
392 if (rc != SQLITE_OK) {
393 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
394 error = EMAIL_ERROR_DB_FAILURE;
401 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
403 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
404 char errno_buf[ERRNO_BUF_SIZE] = {0};
406 if (!shm_file_name) {
407 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
408 return EMAIL_ERROR_INVALID_PARAM;
411 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
413 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
414 return EMAIL_ERROR_SYSTEM_FAILURE;
418 EM_DEBUG_LOG("** Create SHM FILE **");
419 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
420 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
421 return EMAIL_ERROR_SYSTEM_FAILURE;
424 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
425 if (m == MAP_FAILED) {
426 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
428 return EMAIL_ERROR_SYSTEM_FAILURE;
433 pthread_mutexattr_t mattr;
434 pthread_mutexattr_init(&mattr);
435 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
436 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
437 pthread_mutex_init(&(m->mutex), &mattr);
438 pthread_mutexattr_destroy(&mattr);
440 pthread_mutex_destroy(&(m->mutex));
441 munmap(m, sizeof(mmapped_t));
445 return EMAIL_ERROR_NONE;
448 int emstorage_shm_file_destroy(const char *shm_file_name)
450 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
451 char errno_buf[ERRNO_BUF_SIZE] = {0};
453 if (!shm_file_name) {
454 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
455 return EMAIL_ERROR_INVALID_PARAM;
458 if (shm_unlink(shm_file_name) != 0)
459 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
461 return EMAIL_ERROR_NONE;
464 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
466 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
467 char errno_buf[ERRNO_BUF_SIZE] = {0};
469 if (!shm_file_name || !param_shm_fd || !param_mapped) {
470 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
471 return EMAIL_ERROR_INVALID_PARAM;
474 if (!(*param_mapped)) {
475 EM_DEBUG_LOG("** mapping begin **");
476 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
477 *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
478 if ((*param_shm_fd) == -1) {
479 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
480 return EMAIL_ERROR_SYSTEM_FAILURE;
484 fchmod((*param_shm_fd), 0666);
485 EM_DEBUG_LOG("** Create SHM FILE **");
486 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
487 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
488 return EMAIL_ERROR_SYSTEM_FAILURE;
491 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
492 if (tmp == MAP_FAILED) {
493 EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
494 return EMAIL_ERROR_SYSTEM_FAILURE;
499 pthread_mutexattr_t mattr;
500 pthread_mutexattr_init(&mattr);
501 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
502 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
503 pthread_mutex_init(&(tmp->mutex), &mattr);
504 pthread_mutexattr_destroy(&mattr);
510 return EMAIL_ERROR_NONE;
513 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
515 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
518 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
519 return EMAIL_ERROR_INVALID_PARAM;
522 struct timespec abs_time;
523 clock_gettime(CLOCK_REALTIME, &abs_time);
524 abs_time.tv_sec += sec;
525 char errno_buf[ERRNO_BUF_SIZE] = {0};
527 int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
529 if (err == EOWNERDEAD) {
530 err = pthread_mutex_consistent(&(param_mapped->mutex));
531 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
532 } else if (err != 0) {
533 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
538 return EMAIL_ERROR_NONE;
541 void _unlockshm_mutex(mmapped_t *param_mapped)
543 EM_DEBUG_FUNC_BEGIN();
544 pthread_mutex_unlock(&(param_mapped->mutex));
547 /* ------------------------------------------------------------------------------ */
550 static int _open_counter = 0;
552 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
553 static int _read_password_from_secure_storage(char *file_name, char **password);
555 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
556 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
560 const char *object_name;
561 unsigned int data_flag;
564 static const email_db_object_t _g_db_tables[] = {
565 { "mail_read_mail_uid_tbl", 1},
567 { "mail_attachment_tbl", 1},
571 static const email_db_object_t _g_db_indexes[] = {
572 { "mail_read_mail_uid_idx1", 1},
574 { "mail_attachment_idx1", 1},
579 CREATE_TABLE_MAIL_ACCOUNT_TBL,
580 CREATE_TABLE_MAIL_BOX_TBL,
581 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
582 CREATE_TABLE_MAIL_RULE_TBL,
583 CREATE_TABLE_MAIL_TBL,
584 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
585 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
586 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
588 CREATE_TABLE_DUMMY_INDEX1,
590 CREATE_TABLE_MAIL_MEETING_TBL,
591 #ifdef __FEATURE_LOCAL_ACTIVITY__
592 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
594 CREATE_TABLE_DUMMY_INDEX2,
596 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
597 CREATE_TABLE_MAIL_TASK_TBL,
598 #ifdef __FEATURE_BODY_SEARCH__
599 CREATE_TABLE_MAIL_TEXT_TBL,
601 CREATE_TABLE_DUMMY_INDEX3,
604 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
605 CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
607 CREATE_TABLE_DUMMY_INDEX4,
611 CREATE_TABLE_MAIL_ACCOUNT_IDX,
612 CREATE_TABLE_MAIL_BOX_IDX,
613 CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
614 CREATE_TABLE_MAIL_IDX,
615 CREATE_TABLE_MAIL_ATTACHMENT_IDX,
616 CREATE_TABLE_MAIL_MEETING_IDX,
617 CREATE_TABLE_MAIL_TASK_IDX,
618 CREATE_TABLE_MAIL_DATETIME_IDX,
619 CREATE_TABLE_MAIL_THREAD_IDX,
624 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
625 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
626 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
630 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
631 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
632 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
633 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
634 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
635 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
636 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
637 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
638 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
639 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
640 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
649 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
650 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
651 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
652 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
653 ALIAS_IDX_IN_MAIL_BOX_TBL,
654 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
655 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
656 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
657 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
658 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
662 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
663 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
666 SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
667 RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
668 SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
669 FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
670 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
671 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
674 #ifdef __FEATURE_BODY_SEARCH__
676 MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
677 ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
678 MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
679 BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
680 FIELD_COUNT_OF_MAIL_TEXT_TBL,
685 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
686 RULE_ID_IDX_IN_MAIL_RULE_TBL,
687 FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
688 TYPE_IDX_IN_MAIL_RULE_TBL,
689 VALUE_IDX_IN_MAIL_RULE_TBL,
690 VALUE2_IDX_IN_MAIL_RULE_TBL,
691 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
692 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
693 FLAG1_IDX_IN_MAIL_RULE_TBL,
694 FLAG2_IDX_IN_MAIL_RULE_TBL,
698 MAIL_ID_IDX_IN_MAIL_TBL = 0,
699 ACCOUNT_ID_IDX_IN_MAIL_TBL,
700 MAILBOX_ID_IDX_IN_MAIL_TBL,
701 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
702 SUBJECT_IDX_IN_MAIL_TBL,
703 DATETIME_IDX_IN_MAIL_TBL,
704 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
705 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
706 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
707 MESSAGE_ID_IDX_IN_MAIL_TBL,
708 REFERENCE_ID_IDX_IN_MAIL_TBL,
709 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
710 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
711 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
712 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
713 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
714 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
715 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
716 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
717 ALIAS_SENDER_IDX_IN_MAIL_TBL,
718 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
719 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
720 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
721 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
722 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
723 MAIL_SIZE_IDX_IN_MAIL_TBL,
724 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
725 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
726 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
727 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
728 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
729 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
730 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
731 DRM_STATUS_IDX_IN_MAIL_TBL,
732 PRIORITY_IDX_IN_MAIL_TBL,
733 SAVE_STATUS_IDX_IN_MAIL_TBL,
734 LOCK_STATUS_IDX_IN_MAIL_TBL,
735 REPORT_STATUS_IDX_IN_MAIL_TBL,
736 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
737 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
738 THREAD_ID_IDX_IN_MAIL_TBL,
739 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
740 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
741 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
742 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
743 DIGEST_TYPE_IDX_IN_MAIL_TBL,
744 SMIME_TYPE_IDX_IN_MAIL_TBL,
745 SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
746 REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
747 TAG_ID_IDX_IN_MAIL_TBL,
748 REPLIED_TIME_IDX_IN_MAIL_TBL,
749 FORWARDED_TIME_IDX_IN_MAIL_TBL,
750 DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
751 EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
752 EAS_DATA_IDX_IN_MAIL_TBL,
753 USER_NAME_IDX_IN_MAIL_TBL,
754 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
758 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
759 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
760 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
761 CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
762 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
763 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
764 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
765 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
766 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
767 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
768 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
769 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
770 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
771 #ifdef __ATTACHMENT_OPTI__
772 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
773 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
778 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
779 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
780 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
785 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
788 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
789 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
790 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
791 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
795 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
797 ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
798 STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
799 ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
800 MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801 SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
802 MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
803 MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
807 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
808 typedef struct _em_mail_id_list {
810 struct _em_mail_id_list *next;
813 static char *g_test_query[] = {
814 /* 1. select mail_account_tbl */
817 " incoming_server_type, "
818 " incoming_server_address, "
819 " user_email_address, "
820 " incoming_server_user_name, "
821 " incoming_server_password, "
823 " incoming_server_port_number, "
824 " incoming_server_secure_connection, "
825 " incoming_server_authentication_method,"
826 " outgoing_server_type, "
827 " outgoing_server_address, "
828 " outgoing_server_port_number, "
829 " outgoing_server_need_authentication, "
830 " outgoing_server_secure_connection, "
831 " outgoing_server_user_name, "
832 " outgoing_server_password, "
837 " keep_mails_on_pop_server_after_download, "
838 " auto_resend_times, "
839 " outgoing_server_size_limit, "
840 " wifi_auto_download, "
842 " incoming_server_requires_apop,"
844 " is_preset_account, "
848 " req_delivery_receipt, "
849 " req_read_receipt, "
853 " display_name_from, "
855 " forward_with_files, "
859 ", add_my_address_to_bcc"
860 ", notification_status "
862 ", display_content_status "
863 ", default_ringtone_status "
864 ", alert_ringtone_path "
873 " FROM mail_account_tbl",
874 /* 2. select mail_box_tbl */
884 " total_mail_count_on_server, "
885 " has_archived_mails, "
889 " FROM mail_box_tbl ",
890 /* 3. select mail_read_mail_uid_tbl */
899 " flags_seen_field, "
901 " FROM mail_read_mail_uid_tbl ",
902 /* 4. select mail_rule_tbl */
911 " target_mailbox_id, "
914 " FROM mail_rule_tbl ",
915 /* 5. select mail_tbl */
924 " server_mail_status, "
925 " server_mailbox_name, "
928 " reference_mail_id, "
929 " full_address_from, "
930 " full_address_reply, "
933 " full_address_bcc, "
934 " full_address_return, "
935 " email_address_sender, "
936 " email_address_recipient, "
939 " body_download_status, "
942 " file_path_mime_entity, "
944 " flags_seen_field ,"
945 " flags_deleted_field ,"
946 " flags_flagged_field ,"
947 " flags_answered_field ,"
948 " flags_recent_field ,"
949 " flags_draft_field ,"
950 " flags_forwarded_field,"
956 " attachment_count, "
957 " inline_content_count, "
959 " thread_item_count, "
961 " meeting_request_status, "
966 /* 6. select mail_attachment_tbl */
975 " attachment_save_status, "
976 " attachment_drm_type, "
977 " attachment_drm_method, "
978 " attachment_inline_content_status, "
979 " attachment_mime_type "
980 " FROM mail_attachment_tbl ",
982 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
992 " FROM mail_partial_body_activity_tbl ",
999 " meeting_response, "
1003 " global_object_id, "
1006 " standard_time_start_date, "
1009 " daylight_time_start_date, "
1011 " FROM mail_meeting_tbl ",
1013 #ifdef __FEATURE_LOCAL_ACTIVITY__
1022 " FROM mail_local_activity_tbl ",
1029 " expiration_year, "
1030 " expiration_month, "
1032 " issue_organization_name, "
1037 " FROM mail_certificate_tbl ",
1043 " task_parameter_length, "
1044 " task_parameter , "
1046 " FROM mail_task_tbl ",
1047 #ifdef __FEATURE_BODY_SEARCH__
1053 " FROM mail_text_tbl ",
1055 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1063 " multi_user_name, "
1064 " FROM mail_auto_download_activity_tbl ",
1069 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1071 static int _get_table_field_data_char(char **table, char *buf, int index)
1073 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1074 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1078 if (table[index] != NULL) {
1079 *buf = (char)atoi(table[index]);
1083 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1089 static int _get_table_field_data_int(char **table, int *buf, int index)
1091 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1092 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1096 if (table[index] != NULL) {
1097 *buf = atoi(table[index]);
1101 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1107 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
1109 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1110 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1114 if (table[index] != NULL) {
1115 *buf = (time_t)atol(table[index]);
1119 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1125 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1129 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1130 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1134 char *pTemp = table[index];
1139 sLen = EM_SAFE_STRLEN(pTemp);
1141 *buf = (char *) em_malloc(sLen + 1);
1143 EM_DEBUG_EXCEPTION("malloc is failed");
1146 strncpy(*buf, pTemp, sLen+1);
1147 (*buf)[sLen] = '\0';
1151 #ifdef _PRINT_STORAGE_LOG_
1153 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1155 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1163 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1165 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1166 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1170 char *pTemp = table[index];
1175 memset(buf, 0, buffer_size);
1176 strncpy(buf, pTemp, buffer_size - 1);
1178 #ifdef _PRINT_STORAGE_LOG_
1180 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1182 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1188 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1190 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1191 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1195 char *temp_buffer = table[index];
1197 if (temp_buffer == NULL)
1200 *buffer = malloc(buffer_size);
1201 if (*buffer == NULL) {
1202 EM_DEBUG_EXCEPTION("allocation failed.");
1205 memset(*buffer, 0, buffer_size);
1206 memcpy(*buffer, temp_buffer, buffer_size);
1208 #ifdef _PRINT_STORAGE_LOG_
1210 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1212 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1218 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1220 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1221 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1225 if (sqlite3_column_text(hStmt, index) != NULL) {
1226 *buf = (char)sqlite3_column_int(hStmt, index);
1227 #ifdef _PRINT_STORAGE_LOG_
1228 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1233 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1238 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1240 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1241 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1245 if (sqlite3_column_text(hStmt, index) != NULL) {
1246 *buf = sqlite3_column_int(hStmt, index);
1247 #ifdef _PRINT_STORAGE_LOG_
1248 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1253 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1258 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1260 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1261 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1265 if (sqlite3_column_text(hStmt, index) != NULL) {
1266 *buf = (time_t)sqlite3_column_int(hStmt, index);
1267 #ifdef _PRINT_STORAGE_LOG_
1268 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1273 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1277 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1279 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1280 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1285 sLen = sqlite3_column_bytes(hStmt, index);
1287 #ifdef _PRINT_STORAGE_LOG_
1288 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1292 *buf = (char *) em_malloc(sLen + 1);
1294 EM_DEBUG_EXCEPTION("em_mallocfailed");
1298 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1302 #ifdef _PRINT_STORAGE_LOG_
1304 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1306 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1312 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1314 if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1315 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1320 sLen = sqlite3_column_bytes(hStmt, index);
1322 #ifdef _PRINT_STORAGE_LOG_
1323 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1327 *buf = (char *) em_malloc(sLen);
1329 EM_DEBUG_EXCEPTION("em_mallocfailed");
1333 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1339 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1341 if ((hStmt == NULL) || (index < 0)) {
1342 EM_DEBUG_EXCEPTION("index[%d]", index);
1346 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1348 if (ret != SQLITE_OK) {
1349 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1356 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1358 if ((hStmt == NULL) || (index < 0)) {
1359 EM_DEBUG_EXCEPTION("index[%d]", index);
1363 int ret = sqlite3_bind_int(hStmt, index+1, value);
1365 if (ret != SQLITE_OK) {
1366 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1373 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1375 if ((hStmt == NULL) || (index < 0)) {
1376 EM_DEBUG_EXCEPTION("index[%d]", index);
1380 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1382 if (ret != SQLITE_OK) {
1383 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1390 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1392 if ((hStmt == NULL) || (index < 0)) {
1393 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1397 #ifdef _PRINT_STORAGE_LOG_
1398 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1403 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1405 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1407 if (ret != SQLITE_OK) {
1408 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
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, SQLITE_TRANSIENT);
1440 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1442 if (ret != SQLITE_OK) {
1443 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1450 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1452 if ((hStmt == NULL) || (index < 0)) {
1453 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1457 #ifdef _PRINT_STORAGE_LOG_
1458 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1463 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1465 ret = sqlite3_bind_null(hStmt, index+1);
1467 if (ret != SQLITE_OK) {
1468 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1474 static int emstorage_exec_query_by_prepare_v2_stmt(sqlite3 *local_db_handle, char *query_string, char * target_mailbox_name, int input_mailbox_id, int target_account_id)
1477 EM_DEBUG_FUNC_BEGIN("Prepared Statement: local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
1478 int error = EMAIL_ERROR_NONE;
1479 int rc = 0, count = 0;
1480 DB_STMT db_statement = NULL;
1482 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
1484 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1485 ("sqlite3_prepare_stmt failed [%d] [%s]", rc, query_string));
1487 _bind_stmt_field_data_string(db_statement, count++, target_mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
1488 _bind_stmt_field_data_int(db_statement, count++, input_mailbox_id);
1489 _bind_stmt_field_data_int(db_statement, count++, target_account_id);
1493 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
1494 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1495 ("sqlite3_step_stmt failed [%d] [%s]", rc, query_string));
1499 if (db_statement != NULL) {
1500 rc = sqlite3_finalize(db_statement);
1501 if (rc != SQLITE_OK) {
1502 EM_DEBUG_EXCEPTION("sqlite3_finalize_stmt error [%d]", rc);
1503 error = EMAIL_ERROR_DB_FAILURE;
1512 static int _delete_temp_file(const char *path)
1514 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1517 struct dirent *entry = NULL;
1519 char buf[1024] = {0x00, };
1521 if ((dp = opendir(path)) == NULL) {
1522 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1526 while ((entry = readdir(dp)) != NULL) {
1527 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1528 if (remove(buf) != 0) {
1529 EM_DEBUG_EXCEPTION("File Deletion Failed`");
1534 EM_DEBUG_FUNC_END();
1538 char *cpy_str(char *src)
1543 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1544 EM_DEBUG_EXCEPTION("mailoc failed...");
1547 strncpy(p, src, EM_SAFE_STRLEN(src)+1);
1553 static void _emstorage_close_once(void)
1555 EM_DEBUG_FUNC_BEGIN();
1557 EM_DEBUG_FUNC_END();
1560 INTERNAL_FUNC int emstorage_close(int *err_code)
1562 EM_DEBUG_FUNC_BEGIN();
1565 int error = EMAIL_ERROR_NONE;
1567 if (!emstorage_db_close(NULL, &error))
1569 if (--_open_counter == 0)
1570 _emstorage_close_once();
1574 if (err_code != NULL)
1577 EM_DEBUG_FUNC_END("ret [%d]", ret);
1581 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1583 EM_DEBUG_FUNC_BEGIN();
1585 int error = EMAIL_ERROR_NONE;
1587 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1588 char buf[MAX_PATH] = {0};
1589 char *prefix_path = NULL;
1591 error = emcore_get_container_path(multi_user_name, &prefix_path);
1592 if (error != EMAIL_ERROR_NONE) {
1593 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1597 memset(buf, 0x00, sizeof(buf));
1598 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1599 mkdir(buf, DIRECTORY_PERMISSION);
1601 memset(buf, 0x00, sizeof(buf));
1602 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1603 mkdir(buf, DIRECTORY_PERMISSION);
1605 memset(buf, 0x00, sizeof(buf));
1606 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1607 mkdir(buf, DIRECTORY_PERMISSION);
1609 _delete_temp_file(buf);
1610 EM_SAFE_FREE(prefix_path);
1612 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1613 mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
1614 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
1615 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
1617 _delete_temp_file(MAIL_TEMP);
1620 if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1621 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1628 if (err_code != NULL)
1634 /* pData : a parameter which is registered when busy handler is registerd */
1635 /* count : retry count */
1636 static int _callback_sqlite_busy_handler(void *pData, int count)
1638 if (10 - count > 0) {
1639 struct timespec time = {
1641 .tv_nsec = (count + 1) * 100 * 1000 * 1000
1643 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1644 nanosleep(&time, NULL);
1647 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1652 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1653 int length_text_b, const void *text_b)
1655 EM_DEBUG_FUNC_BEGIN();
1657 char *converted_string_a = NULL;
1658 char *converted_string_b = NULL;
1660 EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1661 converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1662 EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1664 EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1665 converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1666 EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1668 if (converted_string_a && converted_string_b)
1669 result = strcmp(converted_string_a, converted_string_b);
1671 EM_SAFE_FREE(converted_string_a);
1672 EM_SAFE_FREE(converted_string_b);
1674 EM_DEBUG_FUNC_END();
1678 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1680 EM_DEBUG_FUNC_BEGIN();
1682 int error = EMAIL_ERROR_NONE;
1685 if (!emstorage_delete_file(db_file_path, &error)) {
1686 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1687 EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1692 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1693 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1702 EM_DEBUG_FUNC_END();
1706 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1708 EM_DEBUG_FUNC_BEGIN();
1710 int error = EMAIL_ERROR_NONE;
1713 /* Delete all files and directories */
1714 if (!_delete_all_files_and_directories(db_file_path, &error)) {
1715 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1719 /* Delete all accounts on MyAccount */
1721 /* Delete all managed connection to DB */
1722 emstorage_reset_db_handle_list();
1729 EM_DEBUG_FUNC_END();
1733 int _xsystem(const char *argv[])
1741 perror("fork failed");
1744 if (execvp(argv[0], (char *const *)argv) == -1) {
1745 perror("execute init db script");
1754 if (waitpid(pid, &status, 0) == -1) {
1755 perror("waitpid failed");
1759 if (WIFSIGNALED(status)) {
1764 if (!WIFEXITED(status)) {
1765 perror("should not happen");
1769 return WEXITSTATUS(status);
1772 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1774 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1776 EM_DEBUG_FUNC_BEGIN();
1777 int err = EMAIL_ERROR_NONE;
1778 char *prefix_path = NULL;
1779 char *output_file_path = NULL;
1780 char temp_file_path[MAX_PATH] = {0};
1781 gid_t email_gid = -1;
1782 uid_t email_uid = -1;
1784 struct group buf_group;
1786 struct passwd buf_passwd;
1787 char buf_gr[MAX_GRP_BUF_SIZE];
1788 char buf_pw[MAX_GRP_BUF_SIZE];
1791 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1792 err = emcore_get_container_path(multi_user_name, &prefix_path);
1793 if (err != EMAIL_ERROR_NONE) {
1794 if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1795 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1800 prefix_path = strdup("");
1803 if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1804 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1805 if (err != EMAIL_ERROR_NONE) {
1806 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1810 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1812 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1815 EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1817 if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1819 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1820 ret = _xsystem(argv_script);
1823 EM_DEBUG_EXCEPTION("_xsystem failed");
1824 err = EMAIL_ERROR_SYSTEM_FAILURE;
1827 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1828 if (result == 0 && NULL != gr)
1829 email_gid = gr->gr_gid;
1831 email_gid = GID_DEFAULT;
1834 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1835 if (result == 0 && NULL != pw)
1836 email_uid = pw->pw_uid;
1838 email_uid = UID_DEFAULT;
1841 chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660);
1842 chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660);
1843 chmod(EMAIL_SQL_PATH, 0660);
1845 ret = chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1847 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1848 ret = chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1850 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1851 ret = chown(EMAIL_SQL_PATH, email_uid, email_gid);
1853 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1859 EM_SAFE_FREE(prefix_path);
1860 EM_SAFE_FREE(output_file_path);
1862 EM_DEBUG_FUNC_END();
1866 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1868 EM_DEBUG_FUNC_BEGIN();
1870 int error = EMAIL_ERROR_NONE;
1873 EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1875 if (*sqlite_handle) { /*prevent 33351*/
1876 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1877 if (err_code != NULL)
1882 EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1885 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1886 if (SQLITE_OK != rc) {
1887 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1888 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc)
1889 error = EMAIL_ERROR_PERMISSION_DENIED;
1891 error = EMAIL_ERROR_DB_FAILURE;
1893 sqlite3_close(*sqlite_handle);
1894 *sqlite_handle = NULL;
1896 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1897 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1898 if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1899 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1903 EM_DEBUG_LOG("Open DB again");
1904 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1905 if (SQLITE_OK != rc) {
1906 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1907 if (SQLITE_PERM == rc)
1908 error = EMAIL_ERROR_PERMISSION_DENIED;
1910 error = EMAIL_ERROR_DB_FAILURE;
1912 sqlite3_close(*sqlite_handle);
1913 *sqlite_handle = NULL;
1914 goto FINISH_OFF; /*prevent 33351*/
1920 /* register busy handler */
1921 EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1922 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1923 if (SQLITE_OK != rc) {
1924 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1925 error = EMAIL_ERROR_DB_FAILURE;
1926 sqlite3_close(*sqlite_handle);
1927 *sqlite_handle = NULL;
1931 /* Register collation callback function */
1932 rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1933 if (SQLITE_OK != rc) {
1934 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1935 error = EMAIL_ERROR_DB_FAILURE;
1936 sqlite3_close(*sqlite_handle);
1937 *sqlite_handle = NULL;
1944 if (err_code != NULL)
1947 EM_DEBUG_FUNC_END("ret [%d]", ret);
1951 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1953 EM_DEBUG_FUNC_BEGIN();
1955 sqlite3 *_db_handle = NULL;
1957 int error = EMAIL_ERROR_NONE;
1958 char *prefix_path = NULL;
1960 _db_handle = emstorage_get_db_handle(multi_user_name);
1962 if (_db_handle == NULL) {
1963 char *output_file_path = NULL;
1964 char temp_file_path[MAX_PATH] = {0};
1966 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1967 error = emcore_get_container_path(multi_user_name, &prefix_path);
1968 if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1969 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1973 prefix_path = strdup("");
1976 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1977 if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1978 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1982 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1983 EM_SAFE_FREE(output_file_path);
1985 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1988 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1989 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1993 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1995 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1996 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1997 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1999 emstorage_set_db_handle(multi_user_name, _db_handle);
2001 emstorage_initialize_field_count();
2006 EM_SAFE_FREE(prefix_path);
2008 if (err_code != NULL)
2011 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
2015 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
2017 EM_DEBUG_FUNC_BEGIN();
2018 #ifdef _MULTIPLE_DB_HANDLE
2019 sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
2022 int error = EMAIL_ERROR_NONE;
2026 ret = sqlite3_close(_db_handle);
2027 if (ret != SQLITE_OK) {
2028 EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
2029 error = EMAIL_ERROR_DB_FAILURE;
2033 #ifdef _MULTIPLE_DB_HANDLE
2034 emstorage_remove_db_handle();
2042 if (err_code != NULL)
2045 EM_DEBUG_FUNC_END("ret [%d]", ret);
2049 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2051 EM_DEBUG_FUNC_BEGIN();
2054 int error = EMAIL_ERROR_NONE;
2056 char *prefix_path = NULL;
2057 char buf[MAX_PATH] = {0};
2059 if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2060 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2062 error = emcore_get_container_path(multi_user_name, &prefix_path);
2063 if (error != EMAIL_ERROR_NONE) {
2064 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2067 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2070 if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2071 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2073 EM_DEBUG_LOG("mkdir return- %d", retValue);
2074 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2077 if (emstorage_db_open(multi_user_name, &error) == NULL) {
2078 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2082 if (_open_counter++ == 0)
2083 _emstorage_open_once(multi_user_name, &error);
2089 EM_SAFE_FREE(prefix_path);
2091 if (err_code != NULL)
2094 EM_DEBUG_FUNC_END("ret [%d]", ret);
2098 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2100 EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2101 int err = EMAIL_ERROR_NONE;
2102 int field_count = 0;
2105 if (input_create_table_query == NULL || output_field_count == NULL) {
2106 err = EMAIL_ERROR_INVALID_PARAM;
2110 pos = input_create_table_query;
2114 if (pos == NULL || *pos == (char)0)
2117 pos = strchr(pos, ',');
2120 *output_field_count = field_count;
2122 EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2126 EM_DEBUG_FUNC_END("err [%d]", err);
2130 INTERNAL_FUNC int emstorage_initialize_field_count()
2132 EM_DEBUG_FUNC_BEGIN();
2133 int err = EMAIL_ERROR_NONE;
2135 char **create_table_query = NULL;
2137 if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2138 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2142 err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2143 if (err != EMAIL_ERROR_NONE) {
2144 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2148 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]));
2149 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]));
2150 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]));
2151 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]));
2152 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2153 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]));
2154 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]));
2155 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]));
2156 #ifdef __FEATURE_LOCAL_ACTIVITY__
2157 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]));
2158 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2159 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]));
2160 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]));
2161 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]));
2164 if (create_table_query) {
2166 for (i = 0; i < query_len; i++) {
2167 if (create_table_query[i])
2168 EM_SAFE_FREE(create_table_query[i]);
2170 EM_SAFE_FREE(create_table_query);
2173 EM_DEBUG_FUNC_END("err [%d]", err);
2177 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2179 EM_DEBUG_FUNC_BEGIN();
2181 int error = EMAIL_ERROR_NONE;
2182 int rc = -1, ret = false;
2184 char sql_query_string[QUERY_SIZE] = {0, };
2185 char **create_table_query = NULL;
2187 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2189 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2190 if (error != EMAIL_ERROR_NONE) {
2191 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2195 if (query_len < CREATE_TABLE_MAX) {
2196 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2197 error = EMAIL_ERROR_SYSTEM_FAILURE;
2201 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2204 char **result = NULL;
2206 /* 1. create mail_account_tbl */
2207 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2208 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2209 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2210 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2212 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %s", result[1], result[1]);
2214 if (atoi(result[1]) < 1) {
2215 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2216 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2217 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2219 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2220 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2221 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2222 if (error != EMAIL_ERROR_NONE) {
2223 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2227 /* create mail_account_tbl unique index */
2228 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2229 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2230 if (error != EMAIL_ERROR_NONE) {
2231 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2235 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2236 } /* mail_account_tbl */
2237 else if (type == EMAIL_CREATE_DB_CHECK) {
2238 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2239 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)));
2242 sqlite3_free_table(result);
2245 /* 2. create mail_box_tbl */
2246 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2248 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2249 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2250 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2252 if (atoi(result[1]) < 1) {
2253 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2254 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2255 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2257 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2259 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2260 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2261 if (error != EMAIL_ERROR_NONE) {
2262 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2266 /* create mail_local_mailbox_tbl unique index */
2267 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2268 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2269 if (error != EMAIL_ERROR_NONE) {
2270 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2274 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2275 } /* mail_box_tbl */
2276 else if (type == EMAIL_CREATE_DB_CHECK) {
2277 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2278 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2279 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2281 sqlite3_free_table(result);
2284 /* 3. create mail_read_mail_uid_tbl */
2285 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2286 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2287 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2288 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2291 if (atoi(result[1]) < 1) {
2292 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2293 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2294 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2296 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2298 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2299 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2300 if (error != EMAIL_ERROR_NONE) {
2301 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2305 /* create mail_read_mail_uid_tbl unique index */
2306 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2307 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2308 if (error != EMAIL_ERROR_NONE) {
2309 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2313 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2314 } /* mail_read_mail_uid_tbl */
2315 else if (type == EMAIL_CREATE_DB_CHECK) {
2316 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2317 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2318 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2320 sqlite3_free_table(result);
2323 /* 4. create mail_rule_tbl */
2324 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2326 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2327 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2328 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2330 if (atoi(result[1]) < 1) {
2331 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2332 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2333 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2335 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2337 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2338 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2339 if (error != EMAIL_ERROR_NONE) {
2340 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2344 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2345 } /* mail_rule_tbl */
2346 else if (type == EMAIL_CREATE_DB_CHECK) {
2347 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2348 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2349 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2351 sqlite3_free_table(result);
2354 /* 5. create mail_tbl */
2355 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2356 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2357 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2358 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2360 if (atoi(result[1]) < 1) {
2361 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2362 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2363 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2364 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2366 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2367 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2368 if (error != EMAIL_ERROR_NONE) {
2369 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2373 /* create mail_tbl unique index */
2374 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2375 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2376 if (error != EMAIL_ERROR_NONE) {
2377 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2381 /* create mail_tbl index for date_time */
2382 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2383 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2384 if (error != EMAIL_ERROR_NONE) {
2385 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2389 /* create mail_tbl index for thread_item_count */
2390 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2391 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2392 if (error != EMAIL_ERROR_NONE) {
2393 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2397 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2398 /* just one time call */
2399 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2401 else if (type == EMAIL_CREATE_DB_CHECK) {
2402 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2403 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2404 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2406 sqlite3_free_table(result);
2409 /* 6. create mail_attachment_tbl */
2410 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2411 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2412 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2413 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2415 if (atoi(result[1]) < 1) {
2416 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2417 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2418 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2420 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2422 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2423 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2424 if (error != EMAIL_ERROR_NONE) {
2425 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2429 /* create mail_attachment_tbl unique index */
2430 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2431 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2432 if (error != EMAIL_ERROR_NONE) {
2433 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2437 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2438 } /* mail_attachment_tbl */
2439 else if (type == EMAIL_CREATE_DB_CHECK) {
2440 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2441 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2442 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2444 sqlite3_free_table(result);
2447 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2449 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2450 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2451 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2452 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2454 if (atoi(result[1]) < 1) {
2456 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2457 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2458 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2460 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2462 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2463 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2464 if (error != EMAIL_ERROR_NONE) {
2465 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2469 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2470 } /* mail_rule_tbl */
2471 else if (type == EMAIL_CREATE_DB_CHECK) {
2472 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2473 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2474 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2476 sqlite3_free_table(result);
2479 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2481 /* create mail_meeting_tbl */
2482 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2483 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2484 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2485 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2487 if (atoi(result[1]) < 1) {
2488 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2489 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2490 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2492 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2494 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2495 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2496 if (error != EMAIL_ERROR_NONE) {
2497 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2501 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2502 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2503 if (error != EMAIL_ERROR_NONE) {
2504 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2508 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2509 } /* mail_contact_sync_tbl */
2510 else if (type == EMAIL_CREATE_DB_CHECK) {
2511 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2512 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2513 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2515 sqlite3_free_table(result);
2518 #ifdef __FEATURE_LOCAL_ACTIVITY__
2520 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2521 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2522 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2523 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2525 if (atoi(result[1]) < 1) {
2527 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2528 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2529 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2531 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2533 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2534 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2535 if (error != EMAIL_ERROR_NONE) {
2536 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2540 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2541 } /* mail_rule_tbl */
2542 else if (type == EMAIL_CREATE_DB_CHECK) {
2543 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2544 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2545 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2546 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2548 sqlite3_free_table(result);
2550 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2551 /* create mail_certificate_tbl */
2552 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2553 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2554 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2555 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)));
2557 if (atoi(result[1]) < 1) {
2558 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2559 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2561 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2563 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2564 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2565 if (error != EMAIL_ERROR_NONE) {
2566 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2570 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2571 } /* mail_contact_sync_tbl */
2572 else if (type == EMAIL_CREATE_DB_CHECK) {
2573 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2574 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)));
2577 sqlite3_free_table(result);
2580 /* create mail_task_tbl */
2581 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2582 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2583 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)));
2585 if (atoi(result[1]) < 1) {
2586 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2587 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2589 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2591 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2592 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2593 if (error != EMAIL_ERROR_NONE) {
2594 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2598 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2599 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2600 if (error != EMAIL_ERROR_NONE) {
2601 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2605 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2606 } /* mail_task_tbl */
2607 else if (type == EMAIL_CREATE_DB_CHECK) {
2608 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2609 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)));
2612 sqlite3_free_table(result);
2615 #ifdef __FEATURE_BODY_SEARCH__
2616 /* create mail_text_tbl */
2617 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2618 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2619 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)));
2621 if (atoi(result[1]) < 1) {
2622 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2623 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2625 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2627 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2628 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2629 if (error != EMAIL_ERROR_NONE) {
2630 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2634 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2635 } /* mail_text_tbl */
2636 else if (type == EMAIL_CREATE_DB_CHECK) {
2637 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2638 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)));
2641 sqlite3_free_table(result);
2646 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2647 /* create mail_auto_download_activity_tbl */
2648 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2649 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2650 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)));
2652 if (atoi(result[1]) < 1) {
2653 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2654 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2656 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2658 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2659 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2660 if (error != EMAIL_ERROR_NONE) {
2661 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2665 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2666 } /* mail_auto_download_activity_tbl */
2667 else if (type == EMAIL_CREATE_DB_CHECK) {
2668 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2669 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)));
2672 sqlite3_free_table(result);
2681 if (result) sqlite3_free_table(result);
2684 EMSTORAGE_PROTECTED_FUNC_CALL(
2685 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2687 EMSTORAGE_PROTECTED_FUNC_CALL(
2688 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2692 if (create_table_query) {
2694 for (i = 0; i < query_len; i++) {
2695 if (create_table_query[i])
2696 EM_SAFE_FREE(create_table_query[i]);
2698 EM_SAFE_FREE(create_table_query);
2701 if (err_code != NULL)
2704 EM_DEBUG_FUNC_END("ret [%d]", ret);
2708 /* Query series --------------------------------------------------------------*/
2709 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)
2711 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);
2714 int error = EMAIL_ERROR_NONE;
2715 DB_STMT hStmt = NULL;
2716 char *sql_query_string = NULL;
2718 sqlite3 *local_db_handle = NULL;
2720 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2721 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2722 return EMAIL_ERROR_INVALID_PARAM;
2725 query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2726 sql_query_string = em_malloc(query_size);
2727 if (sql_query_string == NULL) {
2728 EM_DEBUG_EXCEPTION("em_mallocfailed");
2729 error = EMAIL_ERROR_OUT_OF_MEMORY;
2733 local_db_handle = emstorage_get_db_connection(multi_user_name);
2735 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2737 SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2738 EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2740 if (output_total_mail_count) {
2741 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2742 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2743 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2745 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2746 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2747 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2748 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2751 if (output_unseen_mail_count) {
2752 EM_SAFE_STRNCAT(sql_query_string, " AND flags_seen_field = 0 ", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2754 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2755 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2756 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2758 *output_unseen_mail_count = atoi(result[1]);
2759 sqlite3_free_table(result);
2764 if (hStmt != NULL) {
2765 rc = sqlite3_finalize(hStmt);
2766 if (rc != SQLITE_OK) {
2767 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2768 error = EMAIL_ERROR_DB_FAILURE;
2772 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2774 EM_SAFE_FREE(sql_query_string);
2776 EM_DEBUG_FUNC_END("error [%d]", error);
2780 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)
2782 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);
2790 int error = EMAIL_ERROR_NONE;
2791 int *result_mail_id_list = NULL;
2792 char **result = NULL;
2793 char *sql_query_string = NULL;
2794 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2796 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2797 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2798 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2800 query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10; // 10 is extra space
2801 sql_query_string = em_malloc(query_size);
2802 if (sql_query_string == NULL) {
2803 EM_DEBUG_EXCEPTION("em_mallocfailed");
2804 error = EMAIL_ERROR_OUT_OF_MEMORY;
2808 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2810 /* Composing query */
2811 SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2812 EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2814 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2816 /* Performing query */
2817 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2818 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2819 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2823 /* to get mail list */
2825 EM_DEBUG_LOG("No mail found...");
2826 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2830 EM_DEBUG_LOG("There are [%d] mails.", count);
2832 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2833 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2834 error = EMAIL_ERROR_OUT_OF_MEMORY;
2838 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2840 for (i = 0; i < count; i++)
2841 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2843 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2845 *output_mail_id_list = result_mail_id_list;
2846 *output_mail_id_count = count;
2851 sqlite3_free_table(result);
2853 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2855 EM_SAFE_FREE(sql_query_string);
2857 if (error != EMAIL_ERROR_NONE)
2858 EM_SAFE_FREE(result_mail_id_list);
2860 EM_DEBUG_FUNC_END("error [%d]", error);
2864 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)
2866 EM_DEBUG_FUNC_BEGIN();
2867 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2869 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2870 int sql_query_string_length = 0;
2871 int local_inline_content_count = 0, local_attachment_count = 0;
2872 int cur_query = 0, base_count = 0, col_index;
2873 int ret = false, error = EMAIL_ERROR_NONE;
2874 char *date_time_string = NULL;
2875 char **result = NULL;
2876 char *field_mail_id = "mail_id";
2877 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 ";
2878 char *select_query_form = "SELECT %s FROM mail_tbl ";
2879 char *target_field = NULL;
2880 char *sql_query_string = NULL;
2881 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2882 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2884 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2885 EM_IF_NULL_RETURN_VALUE(result_count, false);
2887 EMSTORAGE_START_READ_TRANSACTION(transaction);
2890 if (to_get_count) /* count only */
2891 target_field = field_mail_id;
2892 else /* mail list in plain form */
2893 target_field = field_all;
2895 sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2897 if (sql_query_string_length)
2898 sql_query_string = em_malloc(sql_query_string_length);
2900 if (sql_query_string == NULL) {
2901 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2902 error = EMAIL_ERROR_OUT_OF_MEMORY;
2906 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2908 strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2910 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2912 /* performing query */
2913 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2914 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2915 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2916 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2917 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2923 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2927 col_index = base_count;
2929 EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2934 EM_DEBUG_LOG_DEV("No mail found...");
2936 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2939 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2941 /* to get mail list */
2943 EM_DEBUG_LOG_DEV("No mail found...");
2945 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2949 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2950 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2951 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2952 error = EMAIL_ERROR_OUT_OF_MEMORY;
2956 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2957 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2958 for (i = 0; i < count; i++) {
2959 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2960 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2961 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2962 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2963 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2964 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2965 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2966 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2967 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2968 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2969 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2970 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2971 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2972 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2973 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2974 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2975 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2976 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2977 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2978 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2979 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2980 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2981 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2982 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2983 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2984 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2985 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2986 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2987 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2988 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2989 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2990 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2991 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2992 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2993 _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++);
2995 mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2997 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2998 EM_PROFILE_END(emstorage_query_mail_list_loop);
3004 EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
3007 sqlite3_free_table(result);
3010 *result_count = count;
3013 if (result_mail_list)
3014 *result_mail_list = mail_list_item_from_tbl;
3016 EM_SAFE_FREE(mail_list_item_from_tbl);
3017 *result_count = count;
3019 EM_SAFE_FREE(mail_list_item_from_tbl);
3022 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3024 // sqlite3_db_release_memory(local_db_handle);
3026 EM_SAFE_FREE(sql_query_string);
3027 EM_SAFE_FREE(date_time_string);
3029 if (err_code != NULL)
3032 EM_PROFILE_END(emstorage_query_mail_list_func);
3033 EM_DEBUG_FUNC_END("ret [%d]", ret);
3038 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)
3040 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);
3042 if (!conditional_clause || !result_mail_tbl || !result_count) {
3043 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3044 if (err_code != NULL)
3045 *err_code = EMAIL_ERROR_INVALID_PARAM;
3049 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3050 int error = EMAIL_ERROR_NONE;
3051 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3052 emstorage_mail_tbl_t* p_data_tbl = NULL;
3053 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3055 EMSTORAGE_START_READ_TRANSACTION(transaction);
3057 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3059 EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3061 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3062 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3063 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3066 EM_DEBUG_LOG("No mail found...");
3068 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3072 EM_DEBUG_LOG("There are [%d] mails.", count);
3073 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3074 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3075 error = EMAIL_ERROR_OUT_OF_MEMORY;
3079 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3080 for (i = 0; i < count; i++) {
3081 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3082 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3083 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3084 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3085 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3086 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3087 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3088 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3089 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3090 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3091 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3092 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3093 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3094 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3095 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3096 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3097 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3098 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3099 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3100 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3101 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3102 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3103 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3104 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3105 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3106 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3107 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3108 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3109 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3110 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3111 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3112 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3113 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3114 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3115 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3116 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3117 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3118 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3119 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3120 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3121 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3122 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3123 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3124 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3125 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3126 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3127 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3128 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3129 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3130 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3131 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3132 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3133 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3134 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3135 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3136 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3143 sqlite3_free_table(result);
3146 *result_mail_tbl = p_data_tbl;
3147 *result_count = count;
3149 *result_mail_tbl = NULL;
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);
3164 #ifdef __FEATURE_BODY_SEARCH__
3165 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)
3167 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);
3169 if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3170 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3171 if (err_code != NULL)
3172 *err_code = EMAIL_ERROR_INVALID_PARAM;
3180 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3181 int error = EMAIL_ERROR_NONE;
3182 char **result = NULL;
3183 char sql_query_string[QUERY_SIZE] = {0, };
3184 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3185 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3187 EMSTORAGE_START_READ_TRANSACTION(transaction);
3189 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3191 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3193 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3194 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3195 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3198 EM_DEBUG_LOG("No mail found...");
3200 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3204 EM_DEBUG_LOG("There are [%d] mails.", count);
3205 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3206 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3207 error = EMAIL_ERROR_OUT_OF_MEMORY;
3211 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3212 for (i = 0; i < count; i++) {
3213 _get_table_field_data_int(result, &(p_data_tbl[i].mail_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].mailbox_id), col_index++);
3216 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3223 sqlite3_free_table(result);
3226 *result_mail_text_tbl = p_data_tbl;
3227 *result_count = count;
3229 EM_SAFE_FREE(p_data_tbl);
3231 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3233 // sqlite3_db_release_memory(local_db_handle);
3235 if (err_code != NULL)
3238 EM_DEBUG_FUNC_END("ret [%d]", ret);
3243 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)
3245 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);
3251 int error = EMAIL_ERROR_NONE;
3253 int total_count = 0;
3255 char sql_query_string[QUERY_SIZE] = {0, };
3256 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 ";
3257 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3258 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3260 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3262 if (input_get_mail_count == 0) { /* without mail count */
3264 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);
3265 } else { /* with read count and total count */
3267 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);
3270 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3272 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3273 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3274 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3276 EM_DEBUG_LOG_DEV("result count [%d]", count);
3279 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3280 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3284 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3285 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3286 error = EMAIL_ERROR_OUT_OF_MEMORY;
3290 for (i = 0; i < count; i++) {
3291 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3292 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3293 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3294 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3295 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3296 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3297 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3298 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3299 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3300 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3301 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3302 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3303 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3304 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3305 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3307 if (input_get_mail_count == 1) {
3308 _get_table_field_data_int(result, &(total_count), col_index++);
3309 p_data_tbl[i].total_mail_count_on_local = total_count;
3310 _get_table_field_data_int(result, &(read_count), col_index++);
3311 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
3318 sqlite3_free_table(result);
3320 if (error == EMAIL_ERROR_NONE) {
3321 *output_mailbox_list = p_data_tbl;
3322 *output_mailbox_count = count;
3323 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3326 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3328 // sqlite3_db_release_memory(local_db_handle);
3330 EM_DEBUG_FUNC_END("error [%d]", error);
3334 /* Query series --------------------------------------------------------------*/
3335 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3337 EM_DEBUG_FUNC_BEGIN();
3338 int rc = -1, ret = false;
3339 int error = EMAIL_ERROR_NONE;
3342 char sql_query_string[QUERY_SIZE] = {0, };
3343 sqlite3 *local_db_handle = NULL;
3345 if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3346 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3347 if (err_code != NULL)
3348 *err_code = EMAIL_ERROR_INVALID_PARAM;
3352 local_db_handle = emstorage_get_db_connection(multi_user_name);
3354 EMSTORAGE_START_READ_TRANSACTION(transaction);
3356 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3357 "SELECT COUNT(*) FROM mail_account_tbl "
3359 " user_email_address = '%s' AND "
3360 " incoming_server_user_name = '%s' AND "
3361 " incoming_server_type = %d AND "
3362 " incoming_server_address = '%s' AND "
3363 " outgoing_server_user_name = '%s' AND "
3364 " outgoing_server_type = %d AND "
3365 " outgoing_server_address = '%s'; ",
3366 account->user_email_address,
3367 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3368 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3370 EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3371 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3372 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3373 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3375 count = atoi(result[1]);
3376 sqlite3_free_table(result);
3378 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3380 if (count == 0) { /* not duplicated account */
3382 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3383 } else { /* duplicated account */
3385 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3386 error = EMAIL_ERROR_ALREADY_EXISTS;
3391 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3393 if (err_code != NULL)
3396 EM_DEBUG_FUNC_END("ret [%d]", ret);
3401 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3403 EM_DEBUG_FUNC_BEGIN();
3406 if (err_code != NULL)
3407 *err_code = EMAIL_ERROR_INVALID_PARAM;
3411 int rc = -1, ret = false;
3412 int error = EMAIL_ERROR_NONE;
3414 DB_STMT hStmt = NULL;
3415 char sql_query_string[QUERY_SIZE] = {0, };
3418 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3419 EMSTORAGE_START_READ_TRANSACTION(transaction);
3420 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3421 EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3422 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3423 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3424 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3425 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3426 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3428 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3429 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3430 ("sqlite3_step fail:%d", rc));
3432 *count = sqlite3_column_int(hStmt, 0);
3438 if (hStmt != NULL) {
3439 rc = sqlite3_finalize(hStmt);
3441 if (rc != SQLITE_OK) {
3442 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3443 error = EMAIL_ERROR_DB_FAILURE;
3447 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3449 if (err_code != NULL)
3452 EM_DEBUG_FUNC_END("ret [%d]", ret);
3456 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)
3458 EM_DEBUG_FUNC_BEGIN();
3460 int i = 0, count = 0, rc = -1, ret = false;
3461 int field_index = 0;
3463 int error = EMAIL_ERROR_NONE;
3464 emstorage_account_tbl_t *p_data_tbl = NULL;
3465 char sql_query_string[QUERY_SIZE] = {0, };
3466 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3468 sqlite3 *local_db_handle = NULL;
3469 DB_STMT hStmt = NULL;
3471 if (!select_num || !account_list) {
3472 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3473 if (err_code != NULL)
3474 *err_code = EMAIL_ERROR_INVALID_PARAM;
3478 local_db_handle = emstorage_get_db_connection(multi_user_name);
3480 EMSTORAGE_START_READ_TRANSACTION(transaction);
3482 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3483 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3484 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3486 count = atoi(result[1]);
3487 sqlite3_free_table(result);
3489 EM_DEBUG_LOG_DEV("count = %d", rc);
3492 EM_DEBUG_LOG("no account found...");
3493 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3497 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3498 sql_len = EM_SAFE_STRLEN(sql_query_string);
3500 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3505 "user_data_length, "
3509 "default_mail_slot_size, "
3512 "user_display_name, "
3513 "user_email_address, "
3514 "reply_to_address, "
3516 "incoming_server_type, "
3517 "incoming_server_address, "
3518 "incoming_server_port_number, "
3519 "incoming_server_user_name, "
3520 "incoming_server_password, "
3521 "incoming_server_secure_connection, "
3522 "incoming_server_authentication_method, "
3524 "keep_mails_on_pop_server_after_download, "
3526 "auto_download_size, "
3531 "outgoing_server_type, "
3532 "outgoing_server_address, "
3533 "outgoing_server_port_number, "
3534 "outgoing_server_user_name, "
3535 "outgoing_server_password, "
3536 "outgoing_server_secure_connection, "
3537 "outgoing_server_need_authentication, "
3538 "outgoing_server_use_same_authenticator, "
3541 "req_delivery_receipt, "
3542 "req_read_receipt, "
3546 "display_name_from, "
3548 "forward_with_files, "
3552 "add_my_address_to_bcc, "
3553 "notification_status, "
3555 "display_content_status, "
3556 "default_ringtone_status, "
3557 "alert_ringtone_path, "
3558 "auto_resend_times, "
3559 "outgoing_server_size_limit, "
3560 "wifi_auto_download, "
3562 "incoming_server_requires_apop, "
3564 "certificate_path, "
3569 sql_len = EM_SAFE_STRLEN(sql_query_string);
3571 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3573 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3575 EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3576 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3577 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3579 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3580 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3581 ("sqlite3_step fail:%d", rc));
3582 if (rc == SQLITE_DONE) {
3583 EM_DEBUG_EXCEPTION("no account found...");
3585 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3591 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3592 EM_DEBUG_EXCEPTION("malloc failed...");
3593 error = EMAIL_ERROR_OUT_OF_MEMORY;
3596 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3597 for (i = 0; i < count; i++) {
3600 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3601 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3602 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3603 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3604 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3605 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3606 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3607 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3608 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3609 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3610 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3611 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3612 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3613 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3614 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3615 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3616 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3617 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3618 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3619 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3620 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3621 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3622 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3623 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3624 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3625 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3626 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3627 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3628 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3629 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3630 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3631 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3632 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3633 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3634 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3635 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3636 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3637 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3638 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3639 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3640 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3641 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3642 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3643 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3644 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3645 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3646 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3647 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3648 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3649 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3650 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3651 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3652 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3653 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3654 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3655 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3656 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3657 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3658 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3659 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3660 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3661 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3662 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3663 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3664 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3665 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3667 /* EAS passwd is not accessible */
3668 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3669 /* get password from the secure storage */
3670 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3671 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3673 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3674 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3676 /* get password file name */
3677 error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3678 recv_password_file_name,
3679 send_password_file_name);
3680 if (error != EMAIL_ERROR_NONE) {
3681 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3682 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3686 /* read password from secure storage */
3687 error = _read_password_from_secure_storage(recv_password_file_name,
3688 &(p_data_tbl[i].incoming_server_password));
3690 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3691 recv_password_file_name, error);
3692 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3695 error = _read_password_from_secure_storage(send_password_file_name,
3696 &(p_data_tbl[i].outgoing_server_password));
3698 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3699 send_password_file_name, error);
3700 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3705 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3706 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3707 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3708 ("sqlite3_step fail:%d", rc));
3715 *account_list = p_data_tbl;
3716 *select_num = count;
3717 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3718 } else if (p_data_tbl != NULL)
3719 emstorage_free_account(&p_data_tbl, count, NULL);
3721 if (hStmt != NULL) {
3722 rc = sqlite3_finalize(hStmt);
3724 if (rc != SQLITE_OK) {
3725 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3726 error = EMAIL_ERROR_DB_FAILURE;
3730 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3732 if (err_code != NULL)
3735 EM_DEBUG_FUNC_END("ret [%d]", ret);
3739 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3740 char *server_mail_id,
3742 emstorage_mail_tbl_t **mail,
3746 EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3747 mailbox_id, server_mail_id, mail, transaction, err_code);
3749 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3750 char conditional_clause[QUERY_SIZE] = {0, };
3751 emstorage_mail_tbl_t* p_data_tbl = NULL;
3753 if (!server_mail_id || !mail) {
3754 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3755 error = EMAIL_ERROR_INVALID_PARAM;
3759 sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3761 if (mailbox_id > 0) {
3762 SNPRINTF(conditional_clause + strlen(conditional_clause),
3763 QUERY_SIZE - strlen(conditional_clause),
3764 " AND mailbox_id = %d",
3768 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3769 if (!emstorage_query_mail_tbl(multi_user_name,
3775 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3786 if (err_code != NULL)
3789 EM_DEBUG_FUNC_END("ret [%d]", ret);
3794 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)
3796 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3798 if (account_id < ALL_ACCOUNT) {
3799 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3800 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3804 if (account_id != ALL_ACCOUNT) {
3805 cur_clause += (conditional_clause_count++ == 0) ?
3806 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3807 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3810 if (mailbox_id > 0) {
3811 cur_clause += (conditional_clause_count++ == 0) ?
3812 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3813 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3816 if (thread_id > 0) {
3817 cur_clause += (conditional_clause_count++ == 0) ?
3818 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3819 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3820 } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3821 cur_clause += (conditional_clause_count++ == 0) ?
3822 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3823 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3824 } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3825 cur_clause += (conditional_clause_count++ == 0) ?
3826 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3827 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3828 } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3829 cur_clause += (conditional_clause_count++ == 0) ?
3830 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3831 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3834 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3835 if (addr_list && addr_list->address_count > 0) {
3836 if (!addr_list->address_type) {
3837 cur_clause += (conditional_clause_count++ == 0) ?
3838 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3839 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3841 for (i = 1; i < addr_list->address_count; i++)
3842 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3844 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3846 cur_clause += (conditional_clause_count++ == 0) ?
3847 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]) :
3848 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]);
3850 for (i = 1; i < addr_list->address_count; i++)
3851 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3853 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3857 if (input_except_delete_flagged_mails) {
3858 cur_clause += (conditional_clause_count++ == 0) ?
3859 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3860 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3864 switch (search_type) {
3865 case EMAIL_SEARCH_FILTER_SUBJECT:
3866 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3867 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3869 case EMAIL_SEARCH_FILTER_SENDER:
3870 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3871 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3872 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3874 case EMAIL_SEARCH_FILTER_RECIPIENT:
3875 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3876 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3877 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3878 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3879 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3881 case EMAIL_SEARCH_FILTER_ALL:
3882 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3883 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3884 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3885 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3886 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3887 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3890 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3895 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3896 static char sorting_str[][50] = {
3897 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3898 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3899 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3900 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3901 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3902 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3903 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3904 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3905 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3906 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3907 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3908 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3909 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3910 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3911 " ORDER BY mailbox_id DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3912 " ORDER BY mailbox_id ASC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3913 " ORDER BY flags_flagged_field DESC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3914 " ORDER BY flags_flagged_field ASC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3915 " ORDER BY flags_seen_field DESC, date_time DESC", /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3916 " ORDER BY flags_seen_field ASC, date_time DESC" /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3919 if (sorting < EMAIL_SORT_END)
3920 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3922 EM_DEBUG_LOG(" Invalid Sorting order ");
3925 if (start_index != -1 && limit_count != -1)
3926 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3933 * emstorage_get_mail_list - Get the mail list information.
3937 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)
3939 EM_DEBUG_FUNC_BEGIN();
3940 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3942 int ret = false, error = EMAIL_ERROR_NONE;
3943 char conditional_clause_string[QUERY_SIZE] = { 0, };
3945 if (account_id < ALL_ACCOUNT) {
3946 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3947 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3949 EM_IF_NULL_RETURN_VALUE(result_count, false);
3951 _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);
3953 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3955 if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3956 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3963 if (err_code != NULL)
3966 EM_PROFILE_END(emstorage_get_mail_list_func);
3967 EM_DEBUG_FUNC_END("ret [%d]", ret);
3973 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3977 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)
3979 EM_DEBUG_FUNC_BEGIN();
3980 EM_PROFILE_BEGIN(emStorageGetMails);
3982 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3983 emstorage_mail_tbl_t *p_data_tbl = NULL;
3984 char conditional_clause_string[QUERY_SIZE] = {0, };
3986 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3987 EM_IF_NULL_RETURN_VALUE(result_count, false);
3989 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3990 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3991 if (err_code != NULL)
3992 *err_code = EMAIL_ERROR_INVALID_PARAM;
3996 _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);
3998 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4000 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
4001 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
4010 *mail_list = p_data_tbl;
4011 *result_count = count;
4012 EM_DEBUG_LOG("COUNT : %d", count);
4013 } else if (p_data_tbl != NULL)
4014 emstorage_free_mail(&p_data_tbl, count, NULL);
4016 if (err_code != NULL)
4019 EM_PROFILE_END(emStorageGetMails);
4020 EM_DEBUG_FUNC_END("ret [%d]", ret);
4027 * emstorage_get_searched_mail_list - Get the mail list information after filtering
4031 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)
4033 EM_DEBUG_FUNC_BEGIN();
4035 int ret = false, error = EMAIL_ERROR_NONE;
4036 char conditional_clause[QUERY_SIZE] = {0, };
4037 char *temp_search_value = NULL;
4038 char *temp_search_value2 = NULL;
4040 EM_IF_NULL_RETURN_VALUE(mail_list, false);
4041 EM_IF_NULL_RETURN_VALUE(result_count, false);
4043 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4044 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4045 error = EMAIL_ERROR_INVALID_PARAM;
4049 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4050 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4052 _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);
4054 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4056 if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4057 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4064 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4066 if (err_code != NULL)
4069 EM_SAFE_FREE(temp_search_value);
4070 EM_SAFE_FREE(temp_search_value2);
4072 EM_DEBUG_FUNC_END("ret [%d]", ret);
4077 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4079 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4081 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4082 EM_DEBUG_EXCEPTION("Invalid parameter");
4083 return EMAIL_ERROR_INVALID_PARAM;
4086 EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4088 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4089 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4090 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4092 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4093 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4096 EM_DEBUG_FUNC_END();
4097 return EMAIL_ERROR_NONE;
4100 static int _read_password_from_secure_storage(char *file_name, char **password)
4102 EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4104 if (!file_name || !password) {
4105 EM_DEBUG_EXCEPTION("Invalid Parameter");
4106 return EMAIL_ERROR_INVALID_PARAM;
4109 int ret = EMAIL_ERROR_NONE;
4111 ret = emcore_get_password_in_key_manager(file_name, password);
4112 if (ret != EMAIL_ERROR_NONE) {
4113 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4119 EM_DEBUG_FUNC_END("ret [%d]", ret);
4123 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)
4125 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4128 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4129 if (err_code != NULL)
4130 *err_code = EMAIL_ERROR_INVALID_PARAM;
4135 int error = EMAIL_ERROR_NONE;
4136 emstorage_account_tbl_t* p_data_tbl = NULL;
4137 DB_STMT hStmt = NULL;
4138 char sql_query_string[QUERY_SIZE] = {0, };
4141 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4142 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4144 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4146 EMSTORAGE_START_READ_TRANSACTION(transaction);
4148 /* Make query string */
4149 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4150 sql_len = EM_SAFE_STRLEN(sql_query_string);
4152 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4153 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4154 "incoming_server_type,"
4155 "incoming_server_address,"
4156 "user_email_address,"
4157 "incoming_server_user_name,"
4159 "incoming_server_port_number,"
4160 "incoming_server_secure_connection,"
4161 "incoming_server_authentication_method,"
4162 "outgoing_server_type,"
4163 "outgoing_server_address,"
4164 "outgoing_server_port_number,"
4165 "outgoing_server_need_authentication,"
4166 "outgoing_server_secure_connection,"
4167 "outgoing_server_user_name,"
4168 "user_display_name,"
4172 "keep_mails_on_pop_server_after_download,"
4173 "auto_download_size,"
4178 "outgoing_server_use_same_authenticator,"
4179 "auto_resend_times,"
4180 "outgoing_server_size_limit,"
4181 "wifi_auto_download,"
4183 "incoming_server_requires_apop,"
4190 sql_len = EM_SAFE_STRLEN(sql_query_string);
4193 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4194 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4195 sql_len = EM_SAFE_STRLEN(sql_query_string);
4198 /* get from secure storage, not from db */
4199 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4200 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4203 "req_delivery_receipt,"
4208 "display_name_from,"
4210 "forward_with_files,"
4214 "add_my_address_to_bcc,"
4215 "notification_status,"
4217 "display_content_status,"
4218 "default_ringtone_status,"
4219 "alert_ringtone_path,"
4222 "default_mail_slot_size,"
4229 sql_len = EM_SAFE_STRLEN(sql_query_string);
4232 /* dummy value, FROM WHERE clause */
4233 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4236 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4238 /* execute a sql and count rows */
4239 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4240 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4241 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4243 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4244 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4245 ("sqlite3_step fail:%d", rc));
4247 if (rc == SQLITE_DONE) {
4248 EM_DEBUG_EXCEPTION("no matched account found...");
4249 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4253 /* Assign query result to structure */
4254 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4255 EM_DEBUG_EXCEPTION("malloc failed...");
4256 error = EMAIL_ERROR_OUT_OF_MEMORY;
4260 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4263 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4264 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4265 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
4266 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4267 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4268 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4269 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4270 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4271 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4272 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4273 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4274 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4275 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4276 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4277 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4278 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4279 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4280 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4281 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4282 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4283 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4284 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4285 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4286 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4287 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4288 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4289 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4290 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4291 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4292 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4293 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4294 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4295 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4296 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4297 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4298 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4299 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4302 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4303 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4305 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4306 /* get password file name */
4307 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) {
4308 EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4312 /* read password from secure storage */
4313 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4314 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4318 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
4320 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4321 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4322 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4325 EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
4329 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4330 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4331 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4332 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4333 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4334 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4335 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4336 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4337 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4338 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4339 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4340 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4341 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4342 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4343 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4344 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4345 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4346 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4347 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4348 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4349 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4350 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4351 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4352 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4353 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4354 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4355 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4356 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4363 *account = p_data_tbl;
4366 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4368 if (hStmt != NULL) {
4369 rc = sqlite3_finalize(hStmt);
4370 if (rc != SQLITE_OK) {
4371 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4372 error = EMAIL_ERROR_DB_FAILURE;
4376 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4378 if (err_code != NULL)
4381 EM_DEBUG_FUNC_END("ret [%d]", ret);
4385 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)
4387 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4389 if (account_id <= 0 || password_length == NULL) {
4390 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4391 if (err_code != NULL)
4392 *err_code = EMAIL_ERROR_INVALID_PARAM;
4397 int error = EMAIL_ERROR_NONE;
4398 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4399 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4400 char *temp_password = NULL;
4403 /* get password file name */
4404 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4405 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4409 /* read password from secure storage */
4410 if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4411 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4412 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4415 } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4416 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4417 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4421 EM_DEBUG_LOG("Invalid password type");
4422 if (err_code != NULL)
4423 *err_code = EMAIL_ERROR_INVALID_PARAM;
4427 *password_length = EM_SAFE_STRLEN(temp_password);
4429 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
4434 EM_SAFE_FREE(temp_password);
4436 if (err_code != NULL)
4439 EM_DEBUG_FUNC_END("ret [%d]", ret);
4443 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)
4445 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);
4447 int err = EMAIL_ERROR_NONE;
4448 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4449 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4451 if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4452 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4453 err = EMAIL_ERROR_INVALID_PARAM;
4457 /* get password file name */
4458 if ((err = _get_password_file_name(multi_user_name,
4460 recv_password_file_name,
4461 send_password_file_name)) != EMAIL_ERROR_NONE) {
4462 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4466 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4467 recv_password_file_name, input_incoming_server_password);
4468 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4469 send_password_file_name, input_outgoing_server_password);
4471 if (input_incoming_server_password) {
4472 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4473 if (err != EMAIL_ERROR_NONE) {
4474 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4478 /* save recv passwords to the secure storage */
4479 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4480 if (err != EMAIL_ERROR_NONE) {
4481 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4486 if (input_outgoing_server_password) {
4487 err = emcore_remove_password_in_key_manager(send_password_file_name);
4488 if (err != EMAIL_ERROR_NONE) {
4489 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4493 /* save send passwords to the secure storage */
4494 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4495 if (err != EMAIL_ERROR_NONE) {
4496 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4502 EM_DEBUG_FUNC_END("err [%d]", err);
4506 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4508 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4510 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4511 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4512 if (err_code != NULL)
4513 *err_code = EMAIL_ERROR_INVALID_PARAM;
4517 int error = EMAIL_ERROR_NONE;
4518 int rc, ret = false;
4520 DB_STMT hStmt = NULL;
4521 char sql_query_string[QUERY_SIZE] = {0, };
4523 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4525 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4527 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4528 "UPDATE mail_account_tbl SET"
4530 ", logo_icon_path = ?"
4532 ", user_data_length = ?"
4533 ", account_svc_id = ?"
4535 ", sync_disabled = ?"
4536 ", default_mail_slot_size = ?"
4537 ", roaming_option = ?"
4539 ", user_display_name = ?"
4540 ", user_email_address = ?"
4541 ", reply_to_address = ?"
4542 ", return_address = ?"
4543 ", incoming_server_type = ?"
4544 ", incoming_server_address = ?"
4545 ", incoming_server_port_number = ?"
4546 ", incoming_server_user_name = ?"
4547 ", incoming_server_secure_connection = ?"
4548 ", incoming_server_authentication_method = ?"
4549 ", retrieval_mode = ?"
4550 ", keep_mails_on_pop_server_after_download = ?"
4551 ", check_interval = ?"
4552 ", auto_download_size = ?"
4553 ", peak_interval = ?"
4555 ", peak_start_time = ?"
4556 ", peak_end_time = ?"
4557 ", outgoing_server_type = ?"
4558 ", outgoing_server_address = ?"
4559 ", outgoing_server_port_number = ?"
4560 ", outgoing_server_user_name = ?"
4561 ", outgoing_server_secure_connection = ?"
4562 ", outgoing_server_need_authentication = ?"
4563 ", outgoing_server_use_same_authenticator = ?"
4565 ", keep_local_copy = ?"
4566 ", req_delivery_receipt = ?"
4567 ", req_read_receipt = ?"
4568 ", download_limit = ?"
4569 ", block_address = ?"
4570 ", block_subject = ?"
4571 ", display_name_from = ?"
4572 ", reply_with_body = ?"
4573 ", forward_with_files = ?"
4574 ", add_myname_card = ?"
4575 ", add_signature = ?"
4577 ", add_my_address_to_bcc = ?"
4578 ", notification_status = ?"
4579 ", vibrate_status = ?"
4580 ", display_content_status = ?"
4581 ", default_ringtone_status = ?"
4582 ", alert_ringtone_path = ?"
4583 ", auto_resend_times = ?"
4584 ", outgoing_server_size_limit = ?"
4585 ", wifi_auto_download = ?"
4586 ", pop_before_smtp = ?"
4587 ", incoming_server_requires_apop = ?"
4589 ", certificate_path = ?"
4593 " WHERE account_id = ?");
4595 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4596 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4597 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4598 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4599 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4603 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4604 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4605 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4606 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4607 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4608 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4609 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4610 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4611 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4612 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4613 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4614 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4615 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4616 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4617 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4618 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4619 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4620 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4621 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4622 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4623 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4624 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4625 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4626 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4627 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4628 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4629 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4630 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4631 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4632 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4633 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4634 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4635 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4636 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4637 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4638 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4639 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4640 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4641 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4642 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4643 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4644 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4645 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4646 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4647 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4648 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4649 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4650 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4651 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4652 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4653 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4654 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4655 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4656 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4657 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4658 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4659 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4660 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4661 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4662 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4663 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4664 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4665 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4666 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4667 _bind_stmt_field_data_int(hStmt, i++, account_id);
4669 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4670 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4671 ("sqlite3_step fail:%d", rc));
4672 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4673 ("sqlite3_step fail:%d", rc));
4675 /* validate account existence */
4676 rc = sqlite3_changes(local_db_handle);
4678 EM_DEBUG_EXCEPTION("no matched account found...");
4679 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4683 if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4684 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4685 EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4693 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4695 if (hStmt != NULL) {
4696 rc = sqlite3_finalize(hStmt);
4697 if (rc != SQLITE_OK) {
4698 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4699 error = EMAIL_ERROR_DB_FAILURE;
4703 if (error == EMAIL_ERROR_NONE) {
4704 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4705 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4708 if (err_code != NULL)
4711 EM_DEBUG_FUNC_END("ret [%d]", ret);
4715 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)
4717 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4718 int error = EMAIL_ERROR_NONE;
4720 char sql_query_string[QUERY_SIZE] = {0, };
4721 sqlite3 *local_db_handle = NULL;
4723 if (!account_id || !field_name) {
4724 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4725 return EMAIL_ERROR_INVALID_PARAM;
4728 local_db_handle = emstorage_get_db_connection(multi_user_name);
4730 /* Write query string */
4731 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4733 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4736 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4738 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4739 if (error != EMAIL_ERROR_NONE) {
4740 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4744 if (sqlite3_changes(local_db_handle) == 0)
4745 EM_DEBUG_LOG("no mail matched...");
4749 result = (error == EMAIL_ERROR_NONE) ? true : false;
4750 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4752 if (error == EMAIL_ERROR_NONE) {
4753 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4754 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4756 EM_DEBUG_FUNC_END("error [%d]", error);
4760 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4762 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4764 if (!result_sync_status) {
4765 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4766 if (err_code != NULL)
4767 *err_code = EMAIL_ERROR_INVALID_PARAM;
4771 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status = 0, count, i, col_index;
4772 char sql_query_string[QUERY_SIZE] = {0, };
4773 char **result = NULL;
4774 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4777 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4779 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4781 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4782 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4783 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4786 EM_DEBUG_EXCEPTION("no matched account found...");
4787 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4792 *result_sync_status = 0;
4794 for (i = 0; i < count; i++) {
4795 _get_table_field_data_int(result, &sync_status, col_index++);
4796 *result_sync_status |= sync_status;
4799 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4801 sqlite3_free_table(result);
4808 if (err_code != NULL)
4811 EM_DEBUG_FUNC_END("ret [%d]", ret);
4815 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)
4817 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);
4819 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4820 char sql_query_string[QUERY_SIZE] = {0, };
4821 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4823 if (set_operator != SET_TYPE_SET && account_id) {
4824 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4825 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4826 if (err_code != NULL)
4830 switch (set_operator) {
4831 case SET_TYPE_UNION:
4832 set_value = result_sync_status | set_value;
4834 case SET_TYPE_MINUS:
4835 set_value = result_sync_status & (~set_value);
4838 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4839 error = EMAIL_ERROR_NOT_SUPPORTED;
4842 EM_DEBUG_LOG("set_value [%d]", set_value);
4845 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4848 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4850 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4852 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4854 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4855 if (error != EMAIL_ERROR_NONE) {
4856 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4860 rc = sqlite3_changes(local_db_handle);
4863 EM_DEBUG_EXCEPTION("no matched account found...");
4864 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4871 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4873 if (error == EMAIL_ERROR_NONE) {
4874 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4875 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4878 if (err_code != NULL)
4881 EM_DEBUG_FUNC_END("ret [%d]", ret);
4885 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4887 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4890 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4891 if (err_code != NULL)
4892 *err_code = EMAIL_ERROR_INVALID_PARAM;
4898 int rc = -1, ret = false;
4899 int error = EMAIL_ERROR_NONE;
4900 DB_STMT hStmt = NULL;
4901 char sql_query_string[QUERY_SIZE] = {0, };
4902 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4903 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4905 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4907 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4909 char *sql = "SELECT rowid FROM mail_account_tbl;";
4910 char **result = NULL;
4912 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4913 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4914 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4916 if (NULL == result[1]) rc = 1;
4918 for (i = 1; i <= row_count; i++) {
4919 if (i != atoi(result[i]))
4925 sqlite3_free_table(result);
4928 if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
4929 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4930 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4934 account_tbl->account_id = rc;
4936 if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4937 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4941 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4942 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4943 "INSERT INTO mail_account_tbl VALUES "
4945 " ? " /* account_id */
4946 " , ? " /* account_name */
4947 " , ? " /* logo_icon_path */
4948 " , ? " /* user_data */
4949 " , ? " /* user_data_length */
4950 " , ? " /* account_svc_id */
4951 " , ? " /* sync_status */
4952 " , ? " /* sync_disabled */
4953 " , ? " /* default_mail_slot_size */
4954 " , ? " /* roaming_option */
4955 " , ? " /* color_label */
4956 " , ? " /* user_display_name */
4957 " , ? " /* user_email_address */
4958 " , ? " /* reply_to_address */
4959 " , ? " /* return_address */
4960 " , ? " /* incoming_server_type */
4961 " , ? " /* incoming_server_address */
4962 " , ? " /* incoming_server_port_number */
4963 " , ? " /* incoming_server_user_name */
4964 " , ? " /* incoming_server_password */
4965 " , ? " /* incoming_server_secure_connection */
4966 " , ? " /* incoming_server_authentication_method */
4967 " , ? " /* retrieval_mode */
4968 " , ? " /* keep_mails_on_pop_server_after_download */
4969 " , ? " /* check_interval */
4970 " , ? " /* auto_download_size */
4971 " , ? " /* peak_interval */
4972 " , ? " /* peak_days */
4973 " , ? " /* peak_start_time */
4974 " , ? " /* peak_end_time */
4975 " , ? " /* outgoing_server_type */
4976 " , ? " /* outgoing_server_address */
4977 " , ? " /* outgoing_server_port_number */
4978 " , ? " /* outgoing_server_user_name */
4979 " , ? " /* outgoing_server_password */
4980 " , ? " /* outgoing_server_secure_connection */
4981 " , ? " /* outgoing_server_need_authentication */
4982 " , ? " /* outgoing_server_use_same_authenticator */
4983 " , ? " /* priority */
4984 " , ? " /* keep_local_copy */
4985 " , ? " /* req_delivery_receipt */
4986 " , ? " /* req_read_receipt */
4987 " , ? " /* download_limit */
4988 " , ? " /* block_address */
4989 " , ? " /* block_subject */
4990 " , ? " /* display_name_from */
4991 " , ? " /* reply_with_body */
4992 " , ? " /* forward_with_files */
4993 " , ? " /* add_myname_card */
4994 " , ? " /* add_signature */
4995 " , ? " /* signature */
4996 " , ? " /* add_my_address_to_bcc */
4997 " , ? " /* auto_resend_times */
4998 " , ? " /* outgoing_server_size_limit */
4999 " , ? " /* wifi_auto_download */
5000 " , ? " /* pop_before_smtp */
5001 " , ? " /* incoming_server_requires_apop */
5002 " , ? " /* smime_type */
5003 " , ? " /* certificate_path */
5004 " , ? " /* cipher_type */
5005 " , ? " /* digest_type */
5006 " , ? " /* notification_status */
5007 " , ? " /* vibrate_status */
5008 " , ? " /* display_content_status */
5009 " , ? " /* default_ringtone_status */
5010 " , ? " /* alert_ringtone_path */
5011 " , ? " /* user_name */
5014 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5015 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5016 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5020 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
5021 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5022 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5023 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
5024 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
5025 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
5026 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
5027 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
5028 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
5029 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
5030 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
5031 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5032 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5033 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5034 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5035 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
5036 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5037 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
5038 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5039 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5040 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
5041 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5042 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5043 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5044 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5045 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5046 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5047 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5048 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5049 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5050 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5051 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5052 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5053 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5054 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5055 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5056 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5057 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5058 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5059 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5060 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5061 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5062 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5063 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5064 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5065 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5066 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5067 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5068 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5069 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5070 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5071 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5072 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5073 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5074 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5075 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5076 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5077 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5078 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5079 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5080 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5081 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5082 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5083 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5084 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5085 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5086 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5088 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5090 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5091 ("sqlite3_step fail:%d", rc));
5092 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5093 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5096 /* save passwords to the secure storage */
5097 EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5098 error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5099 if (error != EMAIL_ERROR_NONE) {
5100 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5104 error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5105 if (error != EMAIL_ERROR_NONE) {
5106 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5113 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5115 if (hStmt != NULL) {
5116 rc = sqlite3_finalize(hStmt);
5117 if (rc != SQLITE_OK) {
5118 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5119 error = EMAIL_ERROR_DB_FAILURE;
5123 if (error == EMAIL_ERROR_NONE) {
5124 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5125 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5128 if (err_code != NULL)
5131 EM_DEBUG_FUNC_END("ret [%d]", ret);
5135 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5137 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5139 if (account_id < FIRST_ACCOUNT_ID) {
5140 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5142 if (err_code != NULL)
5143 *err_code = EMAIL_ERROR_INVALID_PARAM;
5147 int rc = -1, ret = false;
5148 int error = EMAIL_ERROR_NONE;
5150 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5152 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5154 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5155 char sql_query_string[QUERY_SIZE] = {0, };
5156 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5157 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5159 /* get password file name */
5160 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5161 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5165 /* delete from db */
5166 memset(sql_query_string, 0x00, sizeof(sql_query_string));
5167 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5168 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5169 if (error != EMAIL_ERROR_NONE) {
5170 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5174 /* validate account existence */
5175 rc = sqlite3_changes(local_db_handle);
5177 EM_DEBUG_EXCEPTION(" no matched account found...");
5178 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5182 /* delete from secure storage */
5183 error = emcore_remove_password_in_key_manager(recv_password_file_name);
5184 if (error != EMAIL_ERROR_NONE) {
5185 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5189 error = emcore_remove_password_in_key_manager(send_password_file_name);
5190 if (error != EMAIL_ERROR_NONE) {
5191 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5198 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5200 if (err_code != NULL)
5203 EM_DEBUG_FUNC_END("ret [%d]", ret);
5208 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5210 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5213 int error = EMAIL_ERROR_NONE;
5216 if (!account_list || !*account_list) {
5217 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5218 error = EMAIL_ERROR_INVALID_PARAM;
5222 emstorage_account_tbl_t* p = *account_list;
5225 for (; i < count; i++) {
5226 EM_SAFE_FREE(p[i].account_name);
5227 EM_SAFE_FREE(p[i].incoming_server_address);
5228 EM_SAFE_FREE(p[i].user_email_address);
5229 EM_SAFE_FREE(p[i].user_data);
5230 EM_SAFE_FREE(p[i].incoming_server_user_name);
5231 EM_SAFE_FREE(p[i].incoming_server_password);
5232 EM_SAFE_FREE(p[i].outgoing_server_address);
5233 EM_SAFE_FREE(p[i].outgoing_server_user_name);
5234 EM_SAFE_FREE(p[i].outgoing_server_password);
5235 EM_SAFE_FREE(p[i].user_display_name);
5236 EM_SAFE_FREE(p[i].reply_to_address);
5237 EM_SAFE_FREE(p[i].return_address);
5238 EM_SAFE_FREE(p[i].logo_icon_path);
5239 EM_SAFE_FREE(p[i].options.display_name_from);
5240 EM_SAFE_FREE(p[i].options.signature);
5241 EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5242 EM_SAFE_FREE(p[i].certificate_path);
5243 EM_SAFE_FREE(p[i].user_name);
5247 *account_list = NULL;
5253 if (err_code != NULL)
5256 EM_DEBUG_FUNC_END("ret [%d]", ret);
5260 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5262 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);
5264 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5265 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5267 if (err_code != NULL)
5268 *err_code = EMAIL_ERROR_INVALID_PARAM;
5272 int rc = -1, ret = false;
5273 int error = EMAIL_ERROR_NONE;
5274 char sql_query_string[QUERY_SIZE] = {0, };
5276 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5277 EMSTORAGE_START_READ_TRANSACTION(transaction);
5279 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);
5283 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5284 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5285 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5287 *count = atoi(result[1]);
5288 sqlite3_free_table(result);
5295 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5297 if (err_code != NULL)
5300 EM_DEBUG_FUNC_END("ret [%d]", ret);
5304 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)
5306 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);
5308 if (!select_num || !mailbox_list) {
5309 EM_DEBUG_EXCEPTION("Invalid parameters");
5311 if (err_code != NULL)
5312 *err_code = EMAIL_ERROR_INVALID_PARAM;
5318 int error = EMAIL_ERROR_NONE;
5319 char conditional_clause_string[QUERY_SIZE] = {0, };
5320 char ordering_clause_string[QUERY_SIZE] = {0, };
5322 if (account_id == ALL_ACCOUNT) {
5323 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5324 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);
5326 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
5327 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5328 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);
5331 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5333 switch (sort_type) {
5334 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5335 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5338 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5339 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5342 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5343 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5346 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5347 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5351 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5353 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5354 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5362 if (err_code != NULL)
5365 EM_DEBUG_FUNC_END("ret [%d]", ret);
5369 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)
5371 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);
5374 int error = EMAIL_ERROR_NONE;
5375 int where_clause_count = 0;
5376 char conditional_clause_string[QUERY_SIZE] = {0, };
5377 char ordering_clause_string[QUERY_SIZE] = {0, };
5379 if (!select_num || !mailbox_list) {
5380 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5381 error = EMAIL_ERROR_INVALID_PARAM;
5385 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5386 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);
5387 where_clause_count++;
5390 if (account_id > 0) {
5391 if (where_clause_count == 0) {
5392 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5393 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);
5395 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);
5398 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5399 " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5400 " THEN 2 ELSE 1 END ASC,"
5401 " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5402 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5403 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5405 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5406 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5414 if (err_code != NULL)
5417 EM_DEBUG_FUNC_END("ret [%d]", ret);
5421 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)
5423 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);
5426 int error = EMAIL_ERROR_NONE;
5427 char conditional_clause_string[QUERY_SIZE] = {0, };
5429 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5430 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);
5431 if (err_code != NULL)
5432 *err_code = EMAIL_ERROR_INVALID_PARAM;
5436 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND (UPPER(mailbox_name) = UPPER('%q') OR UPPER(mailbox_name) LIKE UPPER('%q/%%'))", account_id, parent_mailbox_name, parent_mailbox_name);
5437 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5439 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) {
5440 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5449 if (err_code != NULL)
5452 EM_DEBUG_FUNC_END("ret [%d]", ret);
5456 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)
5458 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);
5459 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5460 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5462 if (err_code != NULL)
5463 *err_code = EMAIL_ERROR_INVALID_PARAM;
5469 int error = EMAIL_ERROR_NONE;
5470 char conditional_clause_string[QUERY_SIZE] = {0, };
5472 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5473 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5475 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) {
5476 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5484 if (err_code != NULL)
5487 EM_DEBUG_FUNC_END("ret [%d]", ret);
5491 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5493 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5495 int result_code = false;
5496 int error = EMAIL_ERROR_NONE;
5497 time_t current_time = 0;
5498 char sql_query_string[QUERY_SIZE] = {0, };
5499 sqlite3 *local_db_handle = NULL;
5501 if (!input_mailbox_id) {
5502 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5503 return EMAIL_ERROR_INVALID_PARAM;
5506 time(¤t_time);
5508 local_db_handle = emstorage_get_db_connection(multi_user_name);
5509 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5511 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5512 "UPDATE mail_box_tbl SET"
5513 " last_sync_time = %d"
5514 " WHERE mailbox_id = %d"
5516 , input_mailbox_id);
5518 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5519 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5520 if (error != EMAIL_ERROR_NONE) {
5521 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5527 if (error == EMAIL_ERROR_NONE)
5530 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5532 EM_DEBUG_FUNC_END("error [%d]", error);
5536 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)
5538 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);
5539 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5541 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5542 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5544 if (err_code != NULL)
5545 *err_code = EMAIL_ERROR_INVALID_PARAM;
5550 int error = EMAIL_ERROR_NONE;
5551 int result_count = 0;
5552 char conditional_clause_string[QUERY_SIZE] = {0, };
5554 if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5555 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5556 EM_DEBUG_EXCEPTION("malloc failed...");
5557 error = EMAIL_ERROR_OUT_OF_MEMORY;
5561 (*result_mailbox)->mailbox_id = 0;
5562 (*result_mailbox)->account_id = account_id;
5563 (*result_mailbox)->local_yn = 1;
5564 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5565 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5566 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
5567 (*result_mailbox)->deleted_flag = 0;
5568 (*result_mailbox)->modifiable_yn = 1;
5569 (*result_mailbox)->total_mail_count_on_server = 1;
5570 (*result_mailbox)->has_archived_mails = 0;
5571 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
5572 (*result_mailbox)->no_select = 0;
5575 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5577 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);
5579 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5581 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5582 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5591 if (err_code != NULL)
5594 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5595 EM_DEBUG_FUNC_END("ret [%d]", ret);
5599 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)
5601 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);
5603 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5604 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5605 if (err_code != NULL)
5606 *err_code = EMAIL_ERROR_INVALID_PARAM;
5611 int error = EMAIL_ERROR_NONE;
5612 int result_count = 0;
5613 emstorage_mailbox_tbl_t *result_mailbox = NULL;
5614 char conditional_clause_string[QUERY_SIZE] = {0,};
5617 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5619 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5621 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5622 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5630 *output_mailbox = result_mailbox;
5632 if (err_code != NULL)
5635 EM_DEBUG_FUNC_END("ret [%d]", ret);
5639 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5641 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5643 if (input_mailbox_id <= 0 || !output_mailbox) {
5644 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5645 return EMAIL_ERROR_INVALID_PARAM;
5648 int ret = EMAIL_ERROR_NONE;
5649 int result_count = 0;
5650 char conditional_clause_string[QUERY_SIZE] = {0, };
5652 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5654 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5656 if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5657 conditional_clause_string,
5658 "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5659 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5665 EM_DEBUG_FUNC_END("ret [%d]", ret);
5669 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)
5671 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);
5673 if (account_id < 0 || !keyword || !result_mailbox) {
5674 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5676 if (err_code != NULL)
5677 *err_code = EMAIL_ERROR_INVALID_PARAM;
5682 int error = EMAIL_ERROR_NONE;
5683 char conditional_clause_string[QUERY_SIZE] = {0, };
5685 if (account_id == 0)
5686 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5687 "WHERE alias LIKE \'%%%q%%\'", keyword);
5688 else if (account_id > 0)
5689 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5690 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5692 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5694 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5695 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5703 if (err_code != NULL)
5706 EM_DEBUG_FUNC_END("ret [%d]", ret);
5710 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)
5712 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);
5713 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5714 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5715 if (err_code != NULL)
5716 *err_code = EMAIL_ERROR_INVALID_PARAM;
5720 int rc, ret = false;
5721 int error = EMAIL_ERROR_NONE;
5722 emstorage_account_tbl_t* account = NULL;
5723 DB_STMT hStmt = NULL;
5724 char sql_query_string[QUERY_SIZE] = {0, };
5726 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5727 EMSTORAGE_START_READ_TRANSACTION(transaction);
5729 /* Check whether the account exists. */
5730 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5731 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5736 emstorage_free_account(&account, 1, NULL);
5738 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);
5740 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5743 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5745 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5746 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5749 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5750 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5751 ("sqlite3_step fail:%d", rc));
5753 if (rc == SQLITE_DONE) {
5754 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5755 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5759 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5764 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5765 if (hStmt != NULL) {
5766 rc = sqlite3_finalize(hStmt);
5767 if (rc != SQLITE_OK) {
5768 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5769 error = EMAIL_ERROR_DB_FAILURE;
5773 if (err_code != NULL)
5776 EM_DEBUG_FUNC_END("ret [%d]", ret);
5780 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)
5782 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);
5783 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5784 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5785 if (err_code != NULL)
5786 *err_code = EMAIL_ERROR_INVALID_PARAM;
5790 int rc, ret = false;
5791 int error = EMAIL_ERROR_NONE;
5792 emstorage_account_tbl_t* account = NULL;
5793 DB_STMT hStmt = NULL;
5794 char sql_query_string[QUERY_SIZE] = {0, };
5796 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5797 EMSTORAGE_START_READ_TRANSACTION(transaction);
5799 /* Check whether the account exists. */
5800 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5801 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5806 emstorage_free_account(&account, 1, NULL);
5808 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);
5810 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5813 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5815 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5816 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5819 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5820 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5821 ("sqlite3_step fail:%d", rc));
5823 if (rc == SQLITE_DONE) {
5824 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5825 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5829 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5834 if (hStmt != NULL) {
5835 rc = sqlite3_finalize(hStmt);
5836 if (rc != SQLITE_OK) {
5837 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5838 error = EMAIL_ERROR_DB_FAILURE;
5842 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5844 if (err_code != NULL)
5847 EM_DEBUG_FUNC_END("ret [%d]", ret);
5851 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)
5853 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);
5855 int error = EMAIL_ERROR_NONE;
5856 char sql_query_string[QUERY_SIZE] = {0, };
5857 char *replaced_mailbox_name = NULL;
5860 if (strstr(mailbox_name, "'"))
5861 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5863 replaced_mailbox_name = strdup(mailbox_name);
5866 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5868 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5870 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5871 "UPDATE mail_box_tbl SET"
5872 " modifiable_yn = %d"
5873 " WHERE account_id = %d"
5874 " AND local_yn = %d"
5875 " AND mailbox_name = '%s'"
5879 , replaced_mailbox_name);
5881 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5882 if (error != EMAIL_ERROR_NONE) {
5883 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5890 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5892 EM_SAFE_FREE(replaced_mailbox_name);
5894 if (err_code != NULL)
5897 EM_DEBUG_FUNC_END("ret [%d]", ret);
5902 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5904 int input_mailbox_id,
5905 int total_count_on_server,
5909 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5910 "transaction[%d], err_code[%p]",
5911 account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
5914 int error = EMAIL_ERROR_NONE;
5915 char sql_query_string[QUERY_SIZE] = {0, };
5917 if (account_id <= 0 || input_mailbox_id <= 0) {
5918 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5919 if (err_code != NULL)
5920 *err_code = EMAIL_ERROR_INVALID_PARAM;
5921 EM_DEBUG_FUNC_END("ret [%d]", ret);
5925 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5927 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5929 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5930 "UPDATE mail_box_tbl SET"
5931 " total_mail_count_on_server = %d"
5932 " WHERE account_id = %d"
5933 " AND mailbox_id = %d"
5934 , total_count_on_server
5936 , input_mailbox_id);
5938 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5939 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5940 if (error != EMAIL_ERROR_NONE) {
5941 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5949 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5951 if (err_code != NULL)
5954 EM_DEBUG_FUNC_END("ret [%d]", ret);
5959 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)
5961 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);
5963 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5964 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);
5966 if (err_code != NULL)
5967 *err_code = EMAIL_ERROR_INVALID_PARAM;
5968 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5972 int rc, ret = false;
5973 int error = EMAIL_ERROR_NONE;
5974 char sql_query_string[QUERY_SIZE] = {0, };
5975 DB_STMT hStmt = NULL;
5978 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5979 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5981 if (local_yn != -1) {
5982 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5983 "UPDATE mail_box_tbl SET"
5985 ", mailbox_name = ?"
5986 ", mailbox_type = ?"
5988 ", deleted_flag = ?"
5989 ", modifiable_yn= ?"
5990 ", mail_slot_size= ?"
5991 ", total_mail_count_on_server = ?"
5992 " WHERE account_id = %d"
5993 " AND local_yn = %d"
5994 " AND mailbox_id = '%d'"
5997 , input_mailbox_id);
5999 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6000 "UPDATE mail_box_tbl SET"
6002 ", mailbox_name = ?"
6003 ", mailbox_type = ?"
6005 ", deleted_flag = ?"
6006 ", modifiable_yn= ?"
6007 ", mail_slot_size= ?"
6008 ", total_mail_count_on_server = ?"
6009 " WHERE account_id = %d"
6010 " AND mailbox_id = '%d'"
6012 , input_mailbox_id);
6017 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6018 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6019 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6021 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
6022 _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);
6023 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
6024 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6025 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
6026 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
6027 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
6028 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
6031 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6032 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6033 ("sqlite3_step fail:%d", rc));
6034 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6035 ("sqlite3_step fail:%d", rc));
6040 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6042 if (hStmt != NULL) {
6043 rc = sqlite3_finalize(hStmt);
6044 if (rc != SQLITE_OK) {
6045 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6046 error = EMAIL_ERROR_DB_FAILURE;
6050 if (err_code != NULL)
6053 EM_DEBUG_FUNC_END("ret [%d]", ret);
6057 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)
6059 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);
6061 if (account_id < FIRST_ACCOUNT_ID) {
6062 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6064 if (err_code != NULL)
6065 *err_code = EMAIL_ERROR_INVALID_PARAM;
6069 int rc, ret = false;
6070 int error = EMAIL_ERROR_NONE;
6071 char sql_query_string[QUERY_SIZE] = {0, };
6073 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6074 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6076 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6078 DB_STMT hStmt_box_tbl = NULL;
6079 DB_STMT hStmt_mail_tbl = NULL;
6082 /* Update mail_box_tbl */
6083 if (local_yn != -1) {
6084 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6085 "UPDATE mail_box_tbl SET"
6087 " WHERE account_id = %d"
6088 " AND local_yn = %d"
6089 " AND mailbox_id = '%d'"
6092 , input_mailbox_id);
6094 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6095 "UPDATE mail_box_tbl SET"
6097 " WHERE account_id = %d"
6098 " AND mailbox_id = '%d'"
6100 , input_mailbox_id);
6103 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6105 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6107 if (SQLITE_OK != rc) {
6108 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6109 error = EMAIL_ERROR_DB_FAILURE;
6113 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6116 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6118 if (rc == SQLITE_FULL) {
6119 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6120 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6124 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6125 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6126 error = EMAIL_ERROR_DB_FAILURE;
6131 /* Update mail_tbl */
6133 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6134 "UPDATE mail_tbl SET"
6136 " WHERE account_id = %d"
6137 " AND mailbox_id = '%d'"
6139 , input_mailbox_id);
6141 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6143 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6144 if (SQLITE_OK != rc) {
6145 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6146 error = EMAIL_ERROR_DB_FAILURE;
6150 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6152 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6153 if (rc == SQLITE_FULL) {
6154 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6155 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6159 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6160 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6161 error = EMAIL_ERROR_DB_FAILURE;
6168 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6170 if (hStmt_box_tbl != NULL) {
6171 rc = sqlite3_finalize(hStmt_box_tbl);
6172 if (rc != SQLITE_OK) {
6173 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6174 error = EMAIL_ERROR_DB_FAILURE;
6178 if (hStmt_mail_tbl != NULL) {
6179 rc = sqlite3_finalize(hStmt_mail_tbl);
6180 if (rc != SQLITE_OK) {
6181 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6182 error = EMAIL_ERROR_DB_FAILURE;
6186 if (err_code != NULL)
6189 EM_DEBUG_FUNC_END("ret [%d]", ret);
6193 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6195 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);
6197 int rc, ret = false;
6198 int error = EMAIL_ERROR_NONE;
6199 char sql_query_string[QUERY_SIZE] = {0, };
6201 if (input_mailbox_id < 0) {
6202 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6203 return EMAIL_ERROR_INVALID_PARAM;
6206 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6208 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6210 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6212 DB_STMT hStmt = NULL;
6215 /* Update mail_box_tbl */
6216 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6217 "UPDATE mail_box_tbl SET"
6219 " WHERE mailbox_id = %d"
6220 , input_mailbox_id);
6222 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6224 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6225 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6226 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6228 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6230 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6231 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6232 ("sqlite3_step fail:%d", rc));
6233 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6234 ("sqlite3_step fail:%d", rc));
6236 if (hStmt != NULL) {
6237 rc = sqlite3_finalize(hStmt);
6238 if (rc != SQLITE_OK) {
6239 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6240 error = EMAIL_ERROR_DB_FAILURE;
6248 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6250 if (hStmt != NULL) {
6251 rc = sqlite3_finalize(hStmt);
6252 if (rc != SQLITE_OK) {
6253 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6254 error = EMAIL_ERROR_DB_FAILURE;
6258 EM_DEBUG_FUNC_END("error [%d]", error);
6262 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)
6264 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);
6266 int err = EMAIL_ERROR_NONE;
6268 int cur_mailbox_id_string = 0;
6269 int mailbox_id_string_buffer_length = 0;
6270 char sql_query_string[QUERY_SIZE] = {0, };
6271 char *mailbox_id_string_buffer = NULL;
6272 char *parameter_string = NULL;
6273 sqlite3 *local_db_handle = NULL;
6275 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6276 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6277 return EMAIL_ERROR_INVALID_PARAM;
6280 local_db_handle = emstorage_get_db_connection(multi_user_name);
6282 /* Generating mail id list string */
6283 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6285 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6287 if (!mailbox_id_string_buffer) {
6288 EM_DEBUG_EXCEPTION("em_mallocfailed");
6289 err = EMAIL_ERROR_OUT_OF_MEMORY;
6293 for (i = 0; i < input_mailbox_id_count; i++)
6294 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]);
6296 if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6297 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6299 /* Generating notification parameter string */
6300 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6302 if (!parameter_string) {
6303 EM_DEBUG_EXCEPTION("em_mallocfailed");
6304 err = EMAIL_ERROR_OUT_OF_MEMORY;
6308 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6310 /* Write query string */
6311 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);
6313 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6316 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6317 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6318 if (err != EMAIL_ERROR_NONE) {
6319 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6323 if (sqlite3_changes(local_db_handle) == 0)
6324 EM_DEBUG_LOG("no mail matched...");
6329 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6331 if (err == EMAIL_ERROR_NONE && parameter_string) {
6332 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6333 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6334 input_field_name, input_value);
6337 EM_SAFE_FREE(mailbox_id_string_buffer);
6338 EM_SAFE_FREE(parameter_string);
6340 EM_DEBUG_FUNC_END("err [%d]", err);
6344 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6346 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6349 if (err_code != NULL)
6350 *err_code = EMAIL_ERROR_INVALID_PARAM;
6354 int rc, ret = false;
6355 int error = EMAIL_ERROR_NONE;
6356 DB_STMT hStmt = NULL;
6357 char sql_query_string[QUERY_SIZE] = {0,};
6358 char **result = NULL;
6359 time_t current_time;
6360 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6361 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6363 EM_SAFE_STRNCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;", sizeof(sql_query_string));
6365 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6366 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6367 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6369 time(¤t_time);
6371 if (NULL == result[1])
6374 rc = atoi(result[1]) + 1;
6375 sqlite3_free_table(result);
6377 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6379 mailbox_tbl->mailbox_id = rc;
6381 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6382 "INSERT INTO mail_box_tbl VALUES "
6383 "(?" /* mailbox_id */
6384 ", ?" /* account_id */
6385 ", ?" /* local_yn */
6386 ", ?" /* mailbox_name */
6387 ", ?" /* mailbox_type */
6389 ", ?" /* deleted_flag */
6390 ", ?" /* modifiable_yn */
6391 ", ?" /* total_mail_count_on_server */
6392 ", ?" /* has_archived_mails */
6393 ", ?" /* mail_slot_size */
6394 ", ?" /* no_select */
6395 ", ?" /* last_sync_time */
6396 ", ?" /* eas_data_length */
6397 ", ?" /* eas_data */
6401 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6402 EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6403 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6404 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6408 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6409 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6410 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6411 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6412 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6413 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6414 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6415 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6416 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6417 _bind_stmt_field_data_int(hStmt, col_index++, 0);
6418 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6419 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6420 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6421 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6422 _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6425 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6426 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6427 ("sqlite3_step fail:%dn", rc));
6432 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6433 if (hStmt != NULL) {
6434 rc = sqlite3_finalize(hStmt);
6435 if (rc != SQLITE_OK) {
6436 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6437 error = EMAIL_ERROR_DB_FAILURE;
6441 if (error == EMAIL_ERROR_NONE) {
6442 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6443 mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6444 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6447 if (err_code != NULL)
6450 EM_DEBUG_FUNC_END("ret [%d]", ret);
6454 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6456 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6458 if (account_id < FIRST_ACCOUNT_ID) {
6460 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6462 if (err_code != NULL)
6463 *err_code = EMAIL_ERROR_INVALID_PARAM;
6466 int rc, ret = false;
6467 int error = EMAIL_ERROR_NONE;
6468 char sql_query_string[QUERY_SIZE] = {0,};
6469 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6470 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6473 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6475 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6476 if (error != EMAIL_ERROR_NONE) {
6477 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6481 rc = sqlite3_changes(local_db_handle);
6483 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6489 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6491 if (err_code != NULL)
6493 EM_DEBUG_FUNC_END("ret [%d]", ret);
6499 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)
6501 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);
6503 if (account_id < FIRST_ACCOUNT_ID) {
6504 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6506 if (err_code != NULL)
6507 *err_code = EMAIL_ERROR_INVALID_PARAM;
6511 int rc, ret = false;
6512 int error = EMAIL_ERROR_NONE;
6513 char sql_query_string[QUERY_SIZE] = {0, };
6514 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6515 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6518 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6520 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);
6522 if (input_mailbox_id > 0) { /* 0 means all mailbox */
6523 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);
6526 EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6527 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6528 if (error != EMAIL_ERROR_NONE) {
6529 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6533 rc = sqlite3_changes(local_db_handle);
6535 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6536 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6543 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6545 if (error == EMAIL_ERROR_NONE) {
6546 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6547 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6550 if (err_code != NULL)
6553 EM_DEBUG_FUNC_END("ret [%d]", ret);
6557 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6559 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6562 int error = EMAIL_ERROR_NONE;
6565 if (!mailbox_list || !*mailbox_list) {
6566 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6568 error = EMAIL_ERROR_INVALID_PARAM;
6572 emstorage_mailbox_tbl_t* p = *mailbox_list;
6575 for (; i < count; i++) {
6576 EM_SAFE_FREE(p[i].mailbox_name);
6577 EM_SAFE_FREE(p[i].alias);
6578 EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6581 EM_SAFE_FREE(p); *mailbox_list = NULL;
6587 if (err_code != NULL)
6590 EM_DEBUG_FUNC_END("ret [%d]", ret);
6594 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)
6596 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);
6598 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6599 EM_DEBUG_EXCEPTION("Invalid params");
6601 if (err_code != NULL)
6602 *err_code = EMAIL_ERROR_INVALID_PARAM;
6606 int rc = -1, ret = false;
6607 int error = EMAIL_ERROR_NONE;
6608 char sql_query_string[QUERY_SIZE] = {0, };
6609 char *replaced_mailbox_name = NULL;
6611 if (strstr(mailbox_name, "'"))
6612 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6614 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6616 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6619 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6620 EMSTORAGE_START_READ_TRANSACTION(transaction);
6621 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);
6622 EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6626 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6627 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6628 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6630 *count = atoi(result[1]);
6631 sqlite3_free_table(result);
6636 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6638 EM_SAFE_FREE(replaced_mailbox_name);
6640 if (err_code != NULL)
6643 EM_DEBUG_FUNC_END("ret [%d]", ret);
6647 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)
6649 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);
6651 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6652 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6654 if (err_code != NULL)
6655 *err_code = EMAIL_ERROR_INVALID_PARAM;
6659 int rc = -1, ret = false;
6660 int error = EMAIL_ERROR_NONE;
6661 char sql_query_string[QUERY_SIZE] = {0, };
6662 char *replaced_mailbox_name = NULL;
6664 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6666 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6667 EMSTORAGE_START_READ_TRANSACTION(transaction);
6670 if (strstr(mailbox_name, "'"))
6671 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6673 replaced_mailbox_name = strdup(mailbox_name);
6675 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);
6677 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);
6680 char **result = NULL;
6682 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6683 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6684 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6686 *exist = atoi(result[1]);
6687 sqlite3_free_table(result);
6697 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6699 EM_SAFE_FREE(replaced_mailbox_name);
6701 if (err_code != NULL)
6704 EM_DEBUG_FUNC_END("ret [%d]", ret);
6708 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6710 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6712 if (!mail || mail_id <= 0) {
6713 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6714 if (err_code != NULL)
6715 *err_code = EMAIL_ERROR_INVALID_PARAM;
6719 int rc, ret = false;
6720 int error = EMAIL_ERROR_NONE;
6721 DB_STMT hStmt = NULL;
6722 char sql_query_string[QUERY_SIZE] = {0, };
6724 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6725 EMSTORAGE_START_READ_TRANSACTION(transaction);
6727 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6729 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6730 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6732 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6733 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6736 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6737 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6738 ("sqlite3_step fail:%d", rc));
6740 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6741 if (*mail == NULL) {
6742 error = EMAIL_ERROR_OUT_OF_MEMORY;
6743 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6747 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6749 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6750 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6751 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6752 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6753 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6754 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6755 _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6757 (*mail)->server_mail_status = 1;
6763 if (hStmt != NULL) {
6764 rc = sqlite3_finalize(hStmt);
6765 if (rc != SQLITE_OK) {
6766 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6767 error = EMAIL_ERROR_DB_FAILURE;
6771 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6773 if (err_code != NULL)
6776 EM_DEBUG_FUNC_END("ret [%d]", ret);
6780 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)
6782 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6783 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);
6784 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6785 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6787 if (err_code != NULL)
6788 *err_code = EMAIL_ERROR_INVALID_PARAM;
6792 int rc, ret = false;
6793 int error = EMAIL_ERROR_NONE;
6795 DB_STMT hStmt = NULL;
6796 char sql_query_string[QUERY_SIZE] = {0, };
6798 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6801 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6802 EMSTORAGE_START_READ_TRANSACTION(transaction);
6805 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);
6807 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6809 EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6813 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6814 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6815 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6816 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6819 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6820 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6821 ("sqlite3_step fail:%d", rc));
6824 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6825 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6826 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6827 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6829 sqlite3_free_table(result);
6831 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6837 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6838 EM_DEBUG_EXCEPTION(" malloc failed...");
6839 error = EMAIL_ERROR_OUT_OF_MEMORY;
6843 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6845 for (i = 0; i < *count; ++i) {
6846 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6847 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6848 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6849 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6850 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6851 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6852 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6853 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6855 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6856 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6857 ("sqlite3_step fail:%d", rc));
6864 *read_mail_uid = p_data_tbl;
6865 else if (p_data_tbl)
6866 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6868 if (hStmt != NULL) {
6869 rc = sqlite3_finalize(hStmt);
6870 if (rc != SQLITE_OK) {
6871 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6872 error = EMAIL_ERROR_DB_FAILURE;
6876 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6878 if (err_code != NULL)
6881 EM_PROFILE_END(emStorageGetDownloadList);
6882 EM_DEBUG_FUNC_END("ret [%d]", ret);
6886 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)
6888 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);
6890 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6891 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);
6893 if (err_code != NULL)
6894 *err_code = EMAIL_ERROR_INVALID_PARAM;
6898 int rc, ret = false;
6899 int error = EMAIL_ERROR_NONE;
6900 DB_STMT hStmt = NULL;
6901 char sql_query_string[QUERY_SIZE] = {0, };
6902 char *replaced_mailbox_name = NULL;
6904 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6905 EMSTORAGE_START_READ_TRANSACTION(transaction);
6908 if (strstr(mailbox_name, "'"))
6909 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6911 replaced_mailbox_name = strdup(mailbox_name);
6913 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6915 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6916 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6917 "WHERE account_id = %d "
6918 "AND mailbox_id = '%s' "
6919 "AND local_uid = %d "
6920 "AND mailbox_name = '%s' "
6921 "AND server_uid = '%s'",
6922 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6924 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6925 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6926 "WHERE account_id = %d "
6927 "AND mailbox_id = '%s' "
6928 "AND local_uid = %d "
6929 "AND server_uid = '%s'",
6930 account_id, mailbox_id, local_uid, uid);
6934 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6935 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6936 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6939 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6940 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6941 ("sqlite3_step fail:%d", rc));
6943 if (rc == SQLITE_DONE) {
6944 EM_DEBUG_LOG("no matched mail found....");
6945 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6949 _get_stmt_field_data_int(hStmt, mail_size, 0);
6954 EM_SAFE_FREE(replaced_mailbox_name);
6956 if (hStmt != NULL) {
6957 rc = sqlite3_finalize(hStmt);
6958 if (rc != SQLITE_OK) {
6959 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6960 error = EMAIL_ERROR_DB_FAILURE;
6964 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6966 if (err_code != NULL)
6969 EM_DEBUG_FUNC_END("ret [%d]", ret);
6973 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)
6975 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6977 if (!read_mail_uid) {
6978 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6979 if (err_code != NULL)
6980 *err_code = EMAIL_ERROR_INVALID_PARAM;
6984 int rc, rc2, ret = false;
6985 int error = EMAIL_ERROR_NONE;
6986 DB_STMT hStmt = NULL;
6987 char sql_query_string[QUERY_SIZE] = {0, };
6989 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6990 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6992 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6993 char **result = NULL;
6996 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6997 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6998 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7000 if (NULL == result[1]) rc = 1;
7001 else rc = atoi(result[1])+1;
7002 sqlite3_free_table(result);
7004 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7005 "INSERT INTO mail_read_mail_uid_tbl VALUES "
7006 "(?" /* account_id */
7007 ", ?" /* mailbox_id */
7008 ", ?" /* mailbox_name */
7009 ", ?" /* local_uid */
7010 ", ?" /* server_uid */
7011 ", ?" /* rfc822_size */
7012 ", ?" /* sync_status */
7013 ", ?" /* flags_seen_field */
7014 ", ?" /* flags_flagged_field */
7018 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7019 if (rc2 != SQLITE_OK) {
7020 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7021 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7023 error = EMAIL_ERROR_DB_FAILURE;
7027 EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
7028 "server_uid[%s] rfc822_size[%d] rc[%d]",
7029 read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
7030 read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
7032 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
7033 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
7034 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
7035 _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);
7036 _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);
7037 _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
7038 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
7039 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
7040 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7043 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7044 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7045 ("sqlite3_step fail:%d", rc));
7046 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7047 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7053 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7054 if (hStmt != NULL) {
7055 rc = sqlite3_finalize(hStmt);
7056 if (rc != SQLITE_OK) {
7057 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7058 error = EMAIL_ERROR_DB_FAILURE;
7062 if (err_code != NULL)
7065 EM_DEBUG_FUNC_END("ret [%d]", ret);
7069 #ifdef __FEATURE_BODY_SEARCH__
7070 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7072 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7075 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7076 if (err_code != NULL)
7077 *err_code = EMAIL_ERROR_INVALID_PARAM;
7081 int rc, rc2, ret = false;
7082 int error = EMAIL_ERROR_NONE;
7083 DB_STMT hStmt = NULL;
7084 char sql_query_string[QUERY_SIZE] = {0, };
7086 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7087 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7089 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7090 char **result = NULL;
7092 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7093 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7094 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7095 sqlite3_free_table(result);
7097 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7098 "INSERT INTO mail_text_tbl VALUES "
7104 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7105 if (rc2 != SQLITE_OK) {
7106 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7107 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7109 error = EMAIL_ERROR_DB_FAILURE;
7113 EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7114 mail_text->account_id, mail_text->mailbox_id);
7115 EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7117 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7118 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7119 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7120 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7122 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7123 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7124 ("sqlite3_step fail:%d", rc));
7125 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7126 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7131 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7132 if (hStmt != NULL) {
7133 rc = sqlite3_finalize(hStmt);
7134 if (rc != SQLITE_OK) {
7135 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7136 error = EMAIL_ERROR_DB_FAILURE;
7140 if (err_code != NULL)
7143 EM_DEBUG_FUNC_END("ret [%d]", ret);
7148 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)
7150 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);
7152 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7153 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);
7155 if (err_code != NULL)
7156 *err_code = EMAIL_ERROR_INVALID_PARAM;
7160 int rc, ret = false;
7161 int error = EMAIL_ERROR_NONE;
7162 DB_STMT hStmt = NULL;
7163 char sql_query_string[QUERY_SIZE] = {0, };
7165 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7167 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7169 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7170 "UPDATE mail_read_mail_uid_tbl SET"
7173 ", mailbox_name = ?"
7177 ", flags_seen_field = ?"
7178 ", flags_flagged_field = ?"
7179 " WHERE account_id = ?"
7180 " AND mailbox_id = ?"
7181 " AND local_uid = ?"
7182 " AND mailbox_name= ?"
7183 " AND server_uid = ?");
7186 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7187 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7188 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7189 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7194 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7195 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7196 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7197 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7198 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7199 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7200 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7201 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7202 _bind_stmt_field_data_int(hStmt, i++, account_id);
7203 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7204 _bind_stmt_field_data_int(hStmt, i++, local_uid);
7205 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7206 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7209 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7210 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7211 ("sqlite3_step fail:%d", rc));
7216 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7217 if (hStmt != NULL) {
7218 rc = sqlite3_finalize(hStmt);
7219 if (rc != SQLITE_OK) {
7220 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7221 error = EMAIL_ERROR_DB_FAILURE;
7225 if (err_code != NULL)
7228 EM_DEBUG_FUNC_END("ret [%d]", ret);
7232 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7240 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7241 "uid[%s], transaction[%d], err_code[%p]",
7242 account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7244 if (account_id < FIRST_ACCOUNT_ID) {
7245 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7247 if (err_code != NULL)
7248 *err_code = EMAIL_ERROR_INVALID_PARAM;
7253 int error = EMAIL_ERROR_NONE;
7254 char sql_query_string[QUERY_SIZE] = {0, };
7255 char *replaced_mailbox_name = NULL;
7256 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7258 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7260 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7261 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7263 if (mailbox_id > 0) {
7264 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7265 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7266 "AND mailbox_id = %d ", mailbox_id);
7269 if (mailbox_name) { /* NULL means all mailbox_name */
7270 if (strstr(mailbox_name, "'"))
7271 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7273 replaced_mailbox_name = strdup(mailbox_name);
7275 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7276 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7277 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7280 if (uid) { /* NULL means all mail */
7281 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7282 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7286 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7287 if (error != EMAIL_ERROR_NONE) {
7288 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7295 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7297 EM_SAFE_FREE(replaced_mailbox_name);
7299 if (err_code != NULL)
7302 EM_DEBUG_FUNC_END("ret [%d]", ret);
7306 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7308 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7311 int error = EMAIL_ERROR_NONE;
7314 if (!read_mail_uid || !*read_mail_uid) {
7315 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7317 error = EMAIL_ERROR_INVALID_PARAM;
7321 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7324 for (i = 0; i < count; i++) {
7325 EM_SAFE_FREE(p[i].mailbox_name);
7326 EM_SAFE_FREE(p[i].server_uid);
7329 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7335 if (err_code != NULL)
7338 EM_DEBUG_FUNC_END("ret [%d]", ret);
7342 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7344 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7347 EM_DEBUG_EXCEPTION("count[%p]", count);
7349 if (err_code != NULL)
7350 *err_code = EMAIL_ERROR_INVALID_PARAM;
7354 int rc = -1, ret = false;
7355 int error = EMAIL_ERROR_NONE;
7356 char sql_query_string[QUERY_SIZE] = {0, };
7358 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7359 EMSTORAGE_START_READ_TRANSACTION(transaction);
7361 if (account_id != ALL_ACCOUNT)
7362 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7364 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7368 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7369 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7370 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7372 *count = atoi(result[1]);
7373 sqlite3_free_table(result);
7378 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7380 if (err_code != NULL)
7383 EM_DEBUG_FUNC_END("ret [%d]", ret);
7387 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)
7389 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);
7391 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
7392 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);
7394 if (err_code != NULL)
7395 *err_code = EMAIL_ERROR_INVALID_PARAM;
7400 int error = EMAIL_ERROR_NONE;
7402 emstorage_rule_tbl_t* p_data_tbl = NULL;
7403 int i = 0, count = 0;
7404 DB_STMT hStmt = NULL;
7405 char sql_query_string[QUERY_SIZE] = {0, };
7409 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7410 EMSTORAGE_START_READ_TRANSACTION(transaction);
7412 if (account_id != ALL_ACCOUNT) {
7414 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7416 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7419 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7421 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7424 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7425 /* EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7426 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7427 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7430 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7431 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7432 ("sqlite3_step fail:%d", rc));
7435 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7436 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7437 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7438 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7440 sqlite3_free_table(result);
7443 EM_DEBUG_LOG_DEV("No matching rule found...");
7445 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7450 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7451 EM_DEBUG_EXCEPTION(" malloc failed...");
7453 error = EMAIL_ERROR_OUT_OF_MEMORY;
7457 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7459 for (i = 0; i < count; i++) {
7460 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7461 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7462 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7463 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7464 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7465 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7466 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7467 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7468 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7469 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7471 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7472 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7473 ("sqlite3_step fail:%d", rc));
7480 EM_DEBUG_LOG("[%d] rules found.", count);
7483 *rule_list = p_data_tbl;
7484 *select_num = count;
7485 } else if (p_data_tbl != NULL)
7486 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7488 if (hStmt != NULL) {
7489 rc = sqlite3_finalize(hStmt);
7490 if (rc != SQLITE_OK) {
7491 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7492 error = EMAIL_ERROR_DB_FAILURE;
7496 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7498 if (err_code != NULL)
7501 EM_DEBUG_FUNC_END("ret [%d]", ret);
7505 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)
7507 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7508 int error = EMAIL_ERROR_NONE;
7510 DB_STMT hStmt = NULL;
7513 EM_DEBUG_EXCEPTION("Invalid parameter");
7514 error = EMAIL_ERROR_INVALID_PARAM;
7519 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7520 error = EMAIL_ERROR_INVALID_PARAM;
7524 emstorage_rule_tbl_t* p_data_tbl = NULL;
7527 char sql_query_string[QUERY_SIZE] = {0, };
7528 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7529 EMSTORAGE_START_READ_TRANSACTION(transaction);
7531 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7534 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7535 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7538 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7539 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7540 ("sqlite3_step fail:%d", rc));
7542 if (rc == SQLITE_DONE) {
7543 EM_DEBUG_EXCEPTION(" no matched rule found...");
7544 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7548 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7549 EM_DEBUG_EXCEPTION(" malloc failed...");
7550 error = EMAIL_ERROR_OUT_OF_MEMORY;
7554 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7555 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7556 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7557 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7558 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7559 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7560 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7561 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7562 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7563 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7564 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7573 if (hStmt != NULL) {
7574 rc = sqlite3_finalize(hStmt);
7575 if (rc != SQLITE_OK) {
7576 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7577 error = EMAIL_ERROR_DB_FAILURE;
7581 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7583 if (err_code != NULL)
7586 EM_DEBUG_FUNC_END("ret [%d]", ret);
7590 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7592 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7594 if (!new_rule) { /* only global rule supported. */
7595 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7597 if (err_code != NULL)
7598 *err_code = EMAIL_ERROR_INVALID_PARAM;
7602 int rc, ret = false;
7603 int error = EMAIL_ERROR_NONE;
7605 DB_STMT hStmt = NULL;
7606 char sql_query_string[QUERY_SIZE] = {0, };
7607 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7608 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7610 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7611 "UPDATE mail_rule_tbl SET"
7617 ", target_mailbox_id = ?"
7622 " WHERE rule_id = %d"
7626 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7627 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7628 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7629 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7633 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7634 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7635 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7636 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7637 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7638 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7639 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7640 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7641 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7642 _bind_stmt_field_data_int(hStmt, i++, rule_id);
7645 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7646 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7647 ("sqlite3_step fail:%d", rc));
7652 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7653 if (hStmt != NULL) {
7654 rc = sqlite3_finalize(hStmt);
7655 if (rc != SQLITE_OK) {
7656 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7657 error = EMAIL_ERROR_DB_FAILURE;
7661 if (err_code != NULL)
7664 EM_DEBUG_FUNC_END("ret [%d]", ret);
7668 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7670 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7673 EM_DEBUG_LOG("rule is NULL");
7674 if (err_code != NULL)
7675 *err_code = EMAIL_ERROR_INVALID_PARAM;
7679 DB_STMT hStmt = NULL;
7680 char sql_query_string[QUERY_SIZE] = {0,};
7681 int error = EMAIL_ERROR_NONE;
7685 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7686 EMSTORAGE_START_READ_TRANSACTION(transaction);
7688 switch (rule->action_type) {
7689 case EMAIL_FILTER_MOVE:
7690 if (rule->type == EMAIL_PRIORITY_SENDER) {
7691 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7692 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7693 rule->action_type, rule->type, rule->value2);
7695 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7696 "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);
7699 case EMAIL_FILTER_BLOCK:
7700 if (rule->type == EMAIL_FILTER_FROM)
7701 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7702 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7703 rule->action_type, rule->type, rule->value2);
7704 else if (rule->type == EMAIL_FILTER_SUBJECT)
7705 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7706 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7707 rule->action_type, rule->type, rule->value);
7708 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7709 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7710 "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\'))",
7711 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7715 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7716 error = EMAIL_ERROR_INVALID_PARAM;
7721 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7722 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7723 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7725 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7726 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7727 ("sqlite3_step fail:%d", rc));
7729 if (rc == SQLITE_DONE) {
7730 EM_DEBUG_EXCEPTION(" no matched rule found...");
7731 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7738 if (hStmt != NULL) {
7739 rc = sqlite3_finalize(hStmt);
7740 if (rc != SQLITE_OK) {
7741 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7742 error = EMAIL_ERROR_DB_FAILURE;
7746 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7751 EM_DEBUG_FUNC_END("ret [%d]", ret);
7755 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7757 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7759 if (!rule) { /* only global rule supported. */
7760 EM_DEBUG_LOG("rule is NULL");
7761 if (err_code != NULL)
7762 *err_code = EMAIL_ERROR_INVALID_PARAM;
7766 int rc, rc_2, ret = false;
7767 int error = EMAIL_ERROR_NONE;
7768 DB_STMT hStmt = NULL;
7769 char sql_query_string[QUERY_SIZE] = {0, };
7771 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7773 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7777 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7779 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7780 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7781 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7783 if (NULL == result[1])
7786 rc = atoi(result[1])+1;
7788 sqlite3_free_table(result);
7792 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7793 "INSERT INTO mail_rule_tbl VALUES "
7794 "(?" /* account id */
7796 ", ?" /* filter_name */
7800 ", ?" /* action_type */
7801 ", ?" /* target_mailbox_id */
7803 ", ?)"); /* flag2 */
7805 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7806 if (rc_2 != SQLITE_OK) {
7807 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7808 error = EMAIL_ERROR_DB_FAILURE;
7812 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7813 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7814 _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);
7815 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7816 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7817 _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7818 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7819 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7820 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7821 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7823 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7824 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7825 ("sqlite3_step fail:%d", rc));
7830 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7831 if (hStmt != NULL) {
7832 rc = sqlite3_finalize(hStmt);
7833 if (rc != SQLITE_OK) {
7834 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7835 error = EMAIL_ERROR_DB_FAILURE;
7839 if (err_code != NULL)
7842 EM_DEBUG_FUNC_END("ret [%d]", ret);
7846 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7848 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7850 if (rule_id <= 0) { /* only global rule supported. */
7851 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7853 if (err_code != NULL)
7854 *err_code = EMAIL_ERROR_INVALID_PARAM;
7858 int rc, ret = false;
7859 int error = EMAIL_ERROR_NONE;
7860 char sql_query_string[QUERY_SIZE] = {0, };
7861 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7862 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7864 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7865 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7866 if (error != EMAIL_ERROR_NONE) {
7867 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7871 rc = sqlite3_changes(local_db_handle);
7873 EM_DEBUG_EXCEPTION(" no matched rule found...");
7875 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7882 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7884 if (err_code != NULL)
7887 EM_DEBUG_FUNC_END("ret [%d]", ret);
7891 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7893 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7896 int error = EMAIL_ERROR_NONE;
7899 if (!rule_list || !*rule_list) {
7900 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7902 error = EMAIL_ERROR_INVALID_PARAM;
7906 emstorage_rule_tbl_t* p = *rule_list;
7909 for (; i < count; i++)
7910 EM_SAFE_FREE(p[i].value);
7912 EM_SAFE_FREE(p); *rule_list = NULL;
7916 if (err_code != NULL)
7919 EM_DEBUG_FUNC_END("ret [%d]", ret);
7923 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)
7925 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);
7927 if (!total && !unseen) {
7928 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7929 if (err_code != NULL)
7930 *err_code = EMAIL_ERROR_INVALID_PARAM;
7934 int rc = -1, ret = false;
7935 int error = EMAIL_ERROR_NONE;
7936 DB_STMT hStmt = NULL;
7937 char sql_query_string[QUERY_SIZE] = {0, };
7938 char *replaced_mailbox_name = NULL;
7940 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7941 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7942 EMSTORAGE_START_READ_TRANSACTION(transaction);
7945 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7947 if (account_id != ALL_ACCOUNT) {
7948 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);
7950 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);
7951 } else if (mailbox_id)
7952 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);
7954 #ifdef USE_GET_RECORD_COUNT_API
7957 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7958 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7959 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7961 *total = atoi(result[1]);
7962 sqlite3_free_table(result);
7965 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7966 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7967 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7969 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7970 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7971 ("sqlite3_step fail:%d", rc));
7972 _get_stmt_field_data_int(hStmt, total, 0);
7973 #endif /* USE_GET_RECORD_COUNT_API */
7977 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7979 if (account_id != ALL_ACCOUNT) {
7980 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7981 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7985 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7986 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7988 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7989 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7992 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7993 EM_DEBUG_DB_EXEC(SQLITE_OK != rc,
7994 { error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7995 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7997 *unseen = atoi(result[1]);
7998 sqlite3_free_table(result);
8006 #ifndef USE_PREPARED_QUERY_
8007 if (hStmt != NULL) {
8008 rc = sqlite3_finalize(hStmt);
8009 if (rc != SQLITE_OK) {
8010 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8011 error = EMAIL_ERROR_DB_FAILURE;
8016 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8018 EM_SAFE_FREE(replaced_mailbox_name);
8020 if (err_code != NULL)
8023 EM_DEBUG_FUNC_END("ret [%d]", ret);
8027 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)
8029 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8031 if (mail_id <= 0 || !mail) {
8032 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8033 if (err_code != NULL)
8034 *err_code = EMAIL_ERROR_INVALID_PARAM;
8039 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
8041 if (p_data_tbl == NULL) {
8042 EM_DEBUG_EXCEPTION("malloc failed...");
8043 if (err_code != NULL)
8044 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8048 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8049 DB_STMT hStmt = NULL;
8050 char sql_query_string[QUERY_SIZE] = {0, };
8052 int rc, ret = false;
8053 int error = EMAIL_ERROR_NONE;
8055 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8056 EMSTORAGE_START_READ_TRANSACTION(transaction);
8059 case RETRIEVE_SUMMARY:
8060 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8061 "SELECT account_id, "
8064 "server_mail_status, "
8065 "server_mailbox_name, "
8069 "file_path_mime_entity, "
8070 "flags_seen_field, "
8074 "thread_item_count "
8075 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8078 case RETRIEVE_FIELDS_FOR_DELETE:
8079 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8080 "SELECT account_id, "
8082 "server_mail_status, "
8083 "server_mailbox_name, "
8085 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8088 case RETRIEVE_ACCOUNT:
8089 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8090 "SELECT account_id "
8091 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8095 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8096 "SELECT account_id, "
8097 "flags_seen_field, "
8100 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8104 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8105 error = EMAIL_ERROR_INVALID_PARAM;
8109 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8111 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8112 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8113 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8116 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8117 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8118 ("sqlite3_step fail:%d", rc));
8120 if (rc == SQLITE_DONE) {
8121 EM_DEBUG_LOG("no matched mail found...");
8122 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8126 case RETRIEVE_SUMMARY:
8127 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8128 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8129 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8130 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8131 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8132 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8133 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8134 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8135 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8136 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8137 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8138 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8139 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8140 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8143 case RETRIEVE_FIELDS_FOR_DELETE:
8144 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8145 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8146 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8147 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8148 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8151 case RETRIEVE_ACCOUNT:
8152 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8156 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8157 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8158 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8159 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8168 else if (p_data_tbl != NULL)
8169 emstorage_free_mail(&p_data_tbl, 1, NULL);
8171 if (hStmt != NULL) {
8172 rc = sqlite3_finalize(hStmt);
8173 if (rc != SQLITE_OK) {
8174 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8175 error = EMAIL_ERROR_DB_FAILURE;
8180 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8182 if (err_code != NULL)
8185 EM_DEBUG_FUNC_END("ret [%d]", ret);
8189 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)
8191 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);
8194 int error = EMAIL_ERROR_NONE;
8195 int query_string_length = 0;
8196 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8197 char **result = NULL;
8198 char *sql_query_string = NULL;
8199 emstorage_mail_tbl_t* p_data_tbl = NULL;
8200 sqlite3 *local_db_handle = NULL;
8202 if (number_of_mails <= 0 || !mail_ids) {
8203 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8204 if (err_code != NULL)
8205 *err_code = EMAIL_ERROR_INVALID_PARAM;
8209 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8211 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8212 sql_query_string = (char*)em_malloc(query_string_length);
8214 if (p_data_tbl == NULL || sql_query_string == NULL) {
8215 EM_DEBUG_EXCEPTION("malloc failed...");
8217 EM_SAFE_FREE(p_data_tbl);
8218 EM_SAFE_FREE(sql_query_string);
8220 if (err_code != NULL)
8221 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8225 local_db_handle = emstorage_get_db_connection(multi_user_name);
8227 EMSTORAGE_START_READ_TRANSACTION(transaction);
8230 case RETRIEVE_SUMMARY:
8231 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8232 "SELECT account_id, "
8235 "server_mail_status, "
8236 "server_mailbox_name, "
8240 "file_path_mime_entity, "
8242 "flags_seen_field, "
8246 "thread_item_count "
8247 "FROM mail_tbl WHERE mail_id in (");
8251 case RETRIEVE_FIELDS_FOR_DELETE:
8252 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8253 "SELECT account_id, "
8255 "server_mail_status, "
8256 "server_mailbox_name, "
8258 "FROM mail_tbl WHERE mail_id in (");
8262 case RETRIEVE_ACCOUNT:
8263 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8264 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8269 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8270 "SELECT account_id, "
8273 "flags_seen_field, "
8275 "FROM mail_tbl WHERE mail_id in (");
8280 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8281 error = EMAIL_ERROR_INVALID_PARAM;
8285 for (i = 0; i < number_of_mails; i++)
8286 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8287 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8289 EM_DEBUG_LOG_SEC("Query [%s], Length [%zu]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8291 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8292 if (SQLITE_OK != rc && -1 != rc) {
8293 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8294 error = EMAIL_ERROR_DB_FAILURE;
8297 EM_DEBUG_LOG("item_count [%d]", item_count);
8299 if (number_of_mails != item_count) {
8300 EM_DEBUG_EXCEPTION("Can't find all emails");
8301 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8305 col_index = field_count;
8307 for (i = 0; i < item_count; i++) {
8309 case RETRIEVE_SUMMARY:
8310 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8311 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8312 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8313 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8314 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8315 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8316 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8317 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8318 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8319 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8320 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8321 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8322 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8323 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8324 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8327 case RETRIEVE_FIELDS_FOR_DELETE:
8328 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8329 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8330 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8331 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8332 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8335 case RETRIEVE_ACCOUNT:
8336 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8340 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8341 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8342 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8343 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8344 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8355 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8358 sqlite3_free_table(result);
8360 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8362 EM_SAFE_FREE(sql_query_string);
8364 if (err_code != NULL)
8367 EM_DEBUG_FUNC_END("ret [%d]", ret);
8371 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)
8373 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8375 if (mail_id <= 0 || !mail) {
8376 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8377 if (err_code != NULL)
8378 *err_code = EMAIL_ERROR_INVALID_PARAM;
8382 int ret = false, error = EMAIL_ERROR_NONE, count;
8383 char conditional_clause[QUERY_SIZE] = {0, };
8384 emstorage_mail_tbl_t* p_data_tbl = NULL;
8386 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8387 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8389 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8390 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8399 else if (p_data_tbl != NULL)
8400 emstorage_free_mail(&p_data_tbl, 1, &error);
8402 if (err_code != NULL)
8405 EM_DEBUG_FUNC_END("ret [%d]", ret);
8409 #ifdef __FEATURE_BODY_SEARCH__
8410 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)
8412 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8414 if (mail_id <= 0 || !mail_text) {
8415 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8416 if (err_code != NULL)
8417 *err_code = EMAIL_ERROR_INVALID_PARAM;
8422 int error = EMAIL_ERROR_NONE;
8424 char conditional_clause[QUERY_SIZE] = {0, };
8425 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8427 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8428 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8430 if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8431 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8439 *mail_text = p_data_tbl;
8440 else if (p_data_tbl != NULL)
8441 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8443 if (err_code != NULL)
8446 EM_DEBUG_FUNC_END("ret [%d]", ret);
8451 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8452 emstorage_search_filter_t *search,
8456 DB_STMT *search_handle,
8461 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8462 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8463 search, account_id, mailbox_id, sorting, search_handle,
8464 searched, transaction, err_code);
8466 if (!search_handle || !searched) {
8467 if (err_code != NULL)
8468 *err_code = EMAIL_ERROR_INVALID_PARAM;
8469 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8470 EM_DEBUG_FUNC_END("false");
8474 emstorage_search_filter_t* p = search;
8475 int error = EMAIL_ERROR_NONE;
8476 DB_STMT hStmt = NULL;
8477 char sql_query_string[QUERY_SIZE] = {0, };
8478 int rc, ret = false;
8479 int and = false, mail_count = 0;
8481 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8482 EMSTORAGE_START_READ_TRANSACTION(transaction);
8484 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8486 if (account_id != ALL_ACCOUNT) {
8487 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);
8492 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);
8498 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8499 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);
8501 } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8502 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);
8504 } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8505 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);
8507 } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8508 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);
8516 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");
8518 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8521 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8522 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8523 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8526 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8527 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8528 ("sqlite3_step fail:%d", rc));
8532 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8533 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8534 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8536 sqlite3_free_table(result);
8542 *search_handle = hStmt;
8543 *searched = mail_count;
8544 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8546 if (hStmt != NULL) {
8547 rc = sqlite3_finalize(hStmt);
8548 if (rc != SQLITE_OK) {
8549 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8550 error = EMAIL_ERROR_DB_FAILURE;
8554 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8557 if (err_code != NULL)
8560 EM_DEBUG_FUNC_END("ret [%d]", ret);
8564 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8566 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8568 if (search_handle == 0 || !data) {
8569 EM_DEBUG_EXCEPTION("No Search_handle type[%d], data[%p]", type, data);
8571 if (err_code != NULL)
8572 *err_code = EMAIL_ERROR_INVALID_PARAM;
8576 emstorage_mail_tbl_t* p_data_tbl = NULL;
8577 DB_STMT hStmt = search_handle;
8578 int rc, ret = false;
8579 int error = EMAIL_ERROR_NONE;
8583 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8586 case RETRIEVE_ENVELOPE:
8588 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8589 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8590 error = EMAIL_ERROR_OUT_OF_MEMORY;
8594 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8595 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8596 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8597 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8598 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8599 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8600 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8601 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8602 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8603 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8604 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8605 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8606 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8607 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8608 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8609 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8610 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8611 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8612 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8613 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8614 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8615 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8616 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8617 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8618 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8619 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8620 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8621 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8623 if (type == RETRIEVE_ALL) {
8624 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8625 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8626 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8627 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8628 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8629 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8630 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8631 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8632 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8633 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8634 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8635 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8636 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8637 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8640 if (p_data_tbl->body_download_status) {
8643 if (p_data_tbl->file_path_html) {
8644 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8645 p_data_tbl->body_download_status = 0;
8646 } else if (p_data_tbl->file_path_plain) {
8647 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8648 p_data_tbl->body_download_status = 0;
8650 p_data_tbl->body_download_status = 0;
8653 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8656 case RETRIEVE_SUMMARY:
8657 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8658 EM_DEBUG_EXCEPTION(" malloc failed...");
8660 error = EMAIL_ERROR_OUT_OF_MEMORY;
8664 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8666 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8667 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8668 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8669 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8670 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8672 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8675 case RETRIEVE_ADDRESS:
8676 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8677 EM_DEBUG_EXCEPTION(" malloc failed...");
8678 error = EMAIL_ERROR_OUT_OF_MEMORY;
8682 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8683 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8684 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8685 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8686 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8693 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8694 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8695 ("sqlite3_step fail:%d", rc));
8701 if (err_code != NULL)
8704 if (ret == false && p_data_tbl)
8705 emstorage_free_mail(&p_data_tbl, 1, NULL);
8707 EM_DEBUG_FUNC_END("ret [%d]", ret);
8711 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8713 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8715 int error = EMAIL_ERROR_NONE;
8716 int rc, ret = false;
8718 if (search_handle == 0) {
8719 EM_DEBUG_EXCEPTION("No search_handle");
8720 error = EMAIL_ERROR_INVALID_PARAM;
8724 DB_STMT hStmt = search_handle;
8726 rc = sqlite3_finalize(hStmt);
8727 if (rc != SQLITE_OK) {
8728 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8729 error = EMAIL_ERROR_DB_FAILURE;
8735 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8737 if (err_code != NULL)
8740 EM_DEBUG_FUNC_END("ret [%d]", ret);
8744 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8746 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8748 if (mail_id <= 0 || !mail) {
8749 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8751 if (err_code != NULL)
8752 *err_code = EMAIL_ERROR_INVALID_PARAM;
8756 DB_STMT hStmt = NULL;
8757 char sql_query_string[QUERY_SIZE] = {0, };
8760 int error = EMAIL_ERROR_NONE;
8762 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8763 char mailbox_id_param_string[10] = {0,};
8765 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8767 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8768 "UPDATE mail_tbl SET"
8773 ", server_mail_status = ?"
8774 ", server_mailbox_name = ?"
8775 ", server_mail_id = ?"
8776 ", reference_mail_id = ?"
8777 ", full_address_from = ?"
8778 ", full_address_reply = ?" /* 10 */
8779 ", full_address_to = ?"
8780 ", full_address_cc = ?"
8781 ", full_address_bcc = ?"
8782 ", full_address_return = ?"
8784 ", body_download_status = ?"
8785 ", file_path_plain = ?"
8786 ", file_path_html = ?"
8787 ", file_path_mime_entity = ?"
8789 ", flags_seen_field = ?"
8790 ", flags_deleted_field = ?"
8791 ", flags_flagged_field = ?"
8792 ", flags_answered_field = ?"
8793 ", flags_recent_field = ?"
8794 ", flags_draft_field = ?"
8795 ", flags_forwarded_field = ?"
8801 ", report_status = ?"
8802 ", preview_text = ?"
8804 ", scheduled_sending_time = ?"
8805 ", remaining_resend_times = ?"
8807 ", replied_time = ?"
8808 ", forwarded_time = ?"
8809 ", default_charset = ?"
8810 ", eas_data_length = ?"
8812 " WHERE mail_id = %d AND account_id != 0 "
8816 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8817 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8818 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8820 _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8821 _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8822 _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8823 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8824 _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8825 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8826 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8827 _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8828 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8829 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8830 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8831 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8832 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8833 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8834 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8835 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8836 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8837 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8838 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8839 _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8840 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8841 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8842 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8843 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8844 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8845 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8846 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8847 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8848 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8849 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8850 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8851 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8852 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8853 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8854 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8855 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8856 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8857 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8858 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8859 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8860 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8861 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8862 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8864 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8865 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8866 ("sqlite3_step fail:%d", rc));
8868 rc = sqlite3_changes(local_db_handle);
8870 EM_DEBUG_LOG(" no matched mail found...");
8871 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8878 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8879 if (hStmt != NULL) {
8880 rc = sqlite3_finalize(hStmt);
8881 if (rc != SQLITE_OK) {
8882 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8883 error = EMAIL_ERROR_DB_FAILURE;
8888 if (error == EMAIL_ERROR_NONE && mail) {
8889 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8890 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8891 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8894 if (err_code != NULL)
8897 EM_DEBUG_FUNC_END("ret [%d]", ret);
8902 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8906 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int *err_code)
8908 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8910 EM_IF_NULL_RETURN_VALUE(err_code, false);
8913 int error = EMAIL_ERROR_NONE;
8915 char sql_query_string[QUERY_SIZE] = {0, };
8916 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8918 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8919 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);
8921 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8922 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8923 if (error != EMAIL_ERROR_NONE) {
8924 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8928 rc = sqlite3_changes(local_db_handle);
8930 EM_DEBUG_LOG(" No Matched Mail Exists ");
8931 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8938 if (err_code != NULL)
8941 EM_DEBUG_FUNC_END("ret [%d]", ret);
8945 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)
8947 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);
8949 int error = EMAIL_ERROR_NONE;
8952 int cur_mail_id_string = 0;
8953 int mail_id_string_buffer_length = 0;
8954 int parameter_string_length = 0;
8955 char *sql_query_string = NULL;
8956 char *mail_id_string_buffer = NULL;
8957 char *parameter_string = NULL;
8958 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8959 email_mail_attribute_type target_mail_attribute_type = 0;
8961 if (!mail_ids || !field_name || account_id == 0) {
8962 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8963 if (err_code != NULL)
8964 *err_code = EMAIL_ERROR_INVALID_PARAM;
8968 if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8969 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8970 if (err_code != NULL)
8975 /* Generating mail id list string */
8976 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8978 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8980 if (!mail_id_string_buffer) {
8981 EM_DEBUG_EXCEPTION("em_mallocfailed");
8982 if (err_code != NULL)
8983 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8987 for (i = 0; i < mail_ids_count; i++)
8988 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8990 if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8991 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8993 /* Generating notification parameter string */
8994 parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8995 parameter_string = em_malloc(parameter_string_length);
8997 if (!parameter_string) {
8998 EM_DEBUG_EXCEPTION("em_mallocfailed");
8999 if (err_code != NULL)
9000 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
9001 EM_SAFE_FREE(mail_id_string_buffer);
9005 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
9006 query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
9008 sql_query_string = em_malloc(query_size);
9009 if (sql_query_string == NULL) {
9010 EM_DEBUG_EXCEPTION("em_mallocfailed");
9011 error = EMAIL_ERROR_OUT_OF_MEMORY;
9014 /* Write query string */
9015 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);
9017 EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
9020 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9021 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9022 if (error != EMAIL_ERROR_NONE) {
9023 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9027 if (sqlite3_changes(local_db_handle) == 0)
9028 EM_DEBUG_LOG("no mail matched...");
9033 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9035 if (error == EMAIL_ERROR_NONE && parameter_string) {
9036 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
9037 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9040 EM_SAFE_FREE(mail_id_string_buffer);
9041 EM_SAFE_FREE(parameter_string);
9042 EM_SAFE_FREE(sql_query_string);
9045 if (err_code != NULL)
9048 EM_DEBUG_FUNC_END("error [%d]", error);
9052 #ifdef __FEATURE_BODY_SEARCH__
9053 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)
9055 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9057 if (mail_id <= 0 || !mail_text) {
9058 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9059 if (err_code != NULL)
9060 *err_code = EMAIL_ERROR_INVALID_PARAM;
9065 int error = EMAIL_ERROR_NONE;
9066 DB_STMT hStmt = NULL;
9067 char sql_query_string[QUERY_SIZE] = {0, };
9072 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9073 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9075 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9076 "UPDATE mail_text_tbl SET"
9078 " WHERE mail_id = %d AND account_id != 0"
9080 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9082 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9083 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9084 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9087 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9089 if (hStmt != NULL) {
9090 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9091 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9092 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9094 rc = sqlite3_changes(local_db_handle);
9096 EM_DEBUG_LOG(" no matched mail found...");
9097 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9105 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9107 if (hStmt != NULL) {
9108 rc = sqlite3_finalize(hStmt);
9109 if (rc != SQLITE_OK) {
9110 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9111 error = EMAIL_ERROR_DB_FAILURE;
9116 if (err_code != NULL)
9119 EM_DEBUG_FUNC_END("ret [%d]", ret);
9124 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)
9126 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9129 int error = EMAIL_ERROR_NONE;
9134 DB_STMT hStmt = NULL;
9135 char sql_query_string[QUERY_SIZE] = {0, };
9136 char mailbox_id_param_string[10] = {0,};
9137 sqlite3 *local_db_handle = NULL;
9139 if (mail_id <= 0 || !mail) {
9140 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9141 if (err_code != NULL)
9142 *err_code = EMAIL_ERROR_INVALID_PARAM;
9146 local_db_handle = emstorage_get_db_connection(multi_user_name);
9148 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9152 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9153 "UPDATE mail_tbl SET"
9154 " body_download_status = ?"
9155 ", file_path_plain = ?"
9156 ", file_path_html = ?"
9157 ", file_path_mime_entity = ?"
9158 ", flags_seen_field = ?"
9159 ", flags_deleted_field = ?"
9160 ", flags_flagged_field = ?"
9161 ", flags_answered_field = ?"
9162 ", flags_recent_field = ?"
9163 ", flags_draft_field = ?"
9164 ", flags_forwarded_field = ?"
9166 ", attachment_count = ?"
9168 ", meeting_request_status = ? "
9169 ", message_class = ? "
9170 ", digest_type = ? "
9172 " WHERE mail_id = %d AND account_id != 0"
9176 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9177 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9178 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9181 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9182 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9183 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9184 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9185 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9186 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9187 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9188 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9189 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9190 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9191 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9192 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9193 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9194 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9195 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9196 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9197 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9198 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9201 case UPDATE_MAILBOX: {
9203 emstorage_mailbox_tbl_t *mailbox_tbl;
9205 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9206 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9210 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9211 "UPDATE mail_tbl SET"
9212 " mailbox_id = '%d'"
9213 ",mailbox_type = '%d'"
9214 " WHERE mail_id = %d AND account_id != 0"
9215 , mailbox_tbl->mailbox_id
9216 , mailbox_tbl->mailbox_type
9221 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9222 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9223 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9226 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9228 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9234 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9235 "UPDATE mail_tbl SET"
9236 " flags_seen_field = %d"
9237 ",flags_deleted_field = %d"
9238 ",flags_flagged_field = %d"
9239 ",flags_answered_field = %d"
9240 ",flags_recent_field = %d"
9241 ",flags_draft_field = %d"
9242 ",flags_forwarded_field = %d"
9243 " WHERE mail_id = %d AND account_id != 0"
9244 , mail->flags_seen_field
9245 , mail->flags_deleted_field
9246 , mail->flags_flagged_field
9247 , mail->flags_answered_field
9248 , mail->flags_recent_field
9249 , mail->flags_draft_field
9250 , mail->flags_forwarded_field
9252 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9255 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9256 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9257 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9261 case UPDATE_EXTRA_FLAG:
9262 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9263 "UPDATE mail_tbl SET"
9265 ", save_status = %d"
9266 ", lock_status = %d"
9267 ", report_status = %d"
9269 " WHERE mail_id = %d AND account_id != 0"
9273 , mail->report_status
9276 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9279 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9280 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9281 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9284 case UPDATE_STICKY_EXTRA_FLAG:
9285 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9286 "UPDATE mail_tbl SET"
9288 " WHERE mail_id = %d AND account_id != 0"
9291 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9294 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9295 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9296 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9300 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9301 "UPDATE mail_tbl SET"
9302 " full_address_from = ?"
9303 ", full_address_reply = ?"
9304 ", full_address_to = ?"
9305 ", full_address_cc = ?"
9306 ", full_address_bcc = ?"
9307 ", full_address_return = ?"
9309 ", file_path_plain = ?"
9311 ", flags_seen_field = ?"
9312 ", flags_deleted_field = ?"
9313 ", flags_flagged_field = ?"
9314 ", flags_answered_field = ?"
9315 ", flags_recent_field = ?"
9316 ", flags_draft_field = ?"
9317 ", flags_forwarded_field = ?"
9321 ", report_status = ?"
9323 ", file_path_html = ?"
9324 ", file_path_mime_entity = ?"
9326 ", preview_text = ?"
9327 ", body_download_status = ?"
9328 ", attachment_count = ?"
9329 ", inline_content_count = ?"
9330 ", meeting_request_status = ?"
9331 ", message_class = ?"
9334 ", scheduled_sending_time = ?"
9335 ", remaining_resend_times = ?"
9337 ", replied_time = ?"
9338 ", forwarded_time = ?"
9339 ", default_charset = ?"
9340 ", eas_data_length = ?"
9342 " WHERE mail_id = %d AND account_id != 0"
9346 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9347 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
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->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9352 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9353 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9354 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9355 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9356 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9357 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9358 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9359 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9360 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9361 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9362 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9363 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9364 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9365 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9366 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9367 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9368 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9369 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9370 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9371 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9372 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9373 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9374 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9375 _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9376 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9377 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9378 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9379 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9380 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9381 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9382 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9383 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9384 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9385 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9386 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9387 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9388 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9389 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9390 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9393 case UPDATE_DATETIME: {
9394 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9395 "UPDATE mail_tbl SET"
9396 " date_time = '%ld'"
9397 " WHERE mail_id = %d AND account_id != 0"
9401 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9402 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9403 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9407 case UPDATE_FROM_CONTACT_INFO:
9408 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9409 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9410 "UPDATE mail_tbl SET"
9411 " email_address_sender = ?,"
9412 " WHERE mail_id = %d",
9417 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9418 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9419 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9421 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9424 case UPDATE_TO_CONTACT_INFO:
9425 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9426 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9427 "UPDATE mail_tbl SET"
9428 " email_address_recipient = ?,"
9429 " WHERE mail_id = %d",
9434 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9435 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9436 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9438 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9441 case UPDATE_ALL_CONTACT_INFO:
9442 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9443 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9444 "UPDATE mail_tbl SET"
9445 " email_address_sender = ?,"
9446 " email_address_recipient = ?,"
9447 " WHERE mail_id = %d",
9452 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9453 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9454 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9456 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9457 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9461 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9462 case UPDATE_PARTIAL_BODY_DOWNLOAD:
9464 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9465 "UPDATE mail_tbl SET"
9466 " body_download_status = ?"
9467 ", file_path_plain = ?"
9468 ", file_path_html = ?"
9469 ", file_path_mime_entity = ?"
9470 ", attachment_count = ?"
9471 ", inline_content_count = ?"
9472 ", preview_text = ?"
9475 " WHERE mail_id = %d"
9479 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9480 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9481 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9484 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9485 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9486 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9487 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9488 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9489 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9490 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9491 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9492 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9497 case UPDATE_FILE_PATH:
9498 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9499 "UPDATE mail_tbl SET"
9500 ", file_path_plain = ?"
9501 ", file_path_html = ?"
9502 ", file_path_mime_entity = ?"
9503 " WHERE mail_id = %d"
9507 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9508 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9509 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9510 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9512 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9513 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9514 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9518 EM_DEBUG_LOG(" type[%d]", type);
9520 error = EMAIL_ERROR_INVALID_PARAM;
9524 if (hStmt != NULL) {
9526 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9527 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9528 ("sqlite3_step fail:%d", rc));
9529 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9530 ("sqlite3_step fail:%d", rc));
9531 rc = sqlite3_changes(local_db_handle);
9533 EM_DEBUG_LOG(" no matched mail found...");
9534 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9539 if (mail->account_id == 0) {
9540 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9541 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9542 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9545 mail->account_id = mail_for_account_tbl->account_id;
9546 if (mail_for_account_tbl)
9547 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9553 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9555 if (hStmt != NULL) {
9556 rc = sqlite3_finalize(hStmt);
9557 if (rc != SQLITE_OK) {
9558 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9559 error = EMAIL_ERROR_DB_FAILURE;
9564 if (error == EMAIL_ERROR_NONE && move_flag != 1 && transaction) {
9565 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9566 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9568 if (mail->mailbox_id == mailbox_id) {
9569 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9570 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9571 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9573 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9574 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9575 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9579 if (err_code != NULL)
9582 EM_DEBUG_FUNC_END("ret [%d]", ret);
9586 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9588 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9590 int rc, ret = false;
9591 int error = EMAIL_ERROR_NONE;
9592 int latest_mail_id = 0;
9593 sqlite3 *local_db_handle = NULL;
9594 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9595 char **result = NULL;
9597 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9598 _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9599 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9601 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9602 if (ret < 0 || latest_mail_id == 0) {
9603 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9605 local_db_handle = emstorage_get_db_connection(multi_user_name);
9607 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9608 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9609 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9610 if (NULL == result[1])
9613 rc = atoi(result[1]) + 1;
9615 sqlite3_free_table(result);
9616 latest_mail_id = rc;
9621 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9624 *mail_id = latest_mail_id;
9630 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9631 _unlockshm_mutex(mapped_for_generating_mail_id);
9632 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9634 if (err_code != NULL)
9637 EM_DEBUG_FUNC_END("ret [%d]", ret);
9641 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)
9643 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9644 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9646 if (!mail_tbl_data) {
9647 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9648 if (err_code != NULL)
9649 *err_code = EMAIL_ERROR_INVALID_PARAM;
9653 int rc, ret = false;
9654 int error = EMAIL_ERROR_NONE;
9655 char sql_query_string[QUERY_SIZE] = {0, };
9656 DB_STMT hStmt = NULL;
9658 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9660 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9663 /* increase unique id */
9664 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9667 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9668 /* EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9669 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9670 if (rc != SQLITE_OK) {
9671 EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9672 error = EMAIL_ERROR_DB_FAILURE;
9673 sqlite3_free_table(result);
9677 if (NULL == result[1])
9680 rc = atoi(result[1])+1;
9682 sqlite3_free_table(result);
9684 mail_tbl_data->mail_id = rc;
9685 mail_tbl_data->thread_id = rc;
9688 if (mail_tbl_data->date_time == 0)
9689 mail_tbl_data->date_time = time(NULL);
9691 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9692 "INSERT INTO mail_tbl VALUES "
9694 ", ?" /* account_id */
9695 ", ?" /* mailbox_id */
9696 ", ?" /* mailbox_type */
9699 ", ?" /* date_time */
9700 ", ?" /* server_mail_status */
9701 ", ?" /* server_mailbox_name */
9702 ", ?" /* server_mail_id */
9703 ", ?" /* message_id */
9705 ", ?" /* reference_mail_id */
9706 ", ?" /* full_address_from */
9707 ", ?" /* full_address_reply */
9708 ", ?" /* full_address_to */
9709 ", ?" /* full_address_cc */
9711 ", ?" /* full_address_bcc */
9712 ", ?" /* full_address_return */
9713 ", ?" /* email_address_sender */
9714 ", ?" /* email_address_recipient */
9715 ", ?" /* alias_sender */
9717 ", ?" /* alias_recipient */
9718 ", ?" /* body_download_status */
9719 ", ?" /* file_path_plain */
9720 ", ?" /* file_path_html */
9721 ", ?" /* file_path_mime_entity */
9723 ", ?" /* mail_size */
9724 ", ?" /* flags_seen_field */
9725 ", ?" /* flags_deleted_field */
9726 ", ?" /* flags_flagged_field */
9727 ", ?" /* flags_answered_field */
9729 ", ?" /* flags_recent_field */
9730 ", ?" /* flags_draft_field */
9731 ", ?" /* flags_forwarded_field */
9732 ", ?" /* DRM_status */
9733 ", ?" /* priority */
9735 ", ?" /* save_status */
9736 ", ?" /* lock_status */
9737 ", ?" /* report_status */
9738 ", ?" /* attachment_count */
9739 ", ?" /* inline_content_count */
9741 ", ?" /* thread_id */
9742 ", ?" /* thread_item_count */
9743 ", ?" /* preview_text */
9744 ", ?" /* meeting_request_status */
9745 ", ?" /* message_class */
9747 ", ?" /* digest_type */
9748 ", ?" /* smime_type */
9749 ", ?" /* scheduled_sending_time */
9750 ", ?" /* remaining_resend_times */
9753 ", ?" /* replied_time */
9754 ", ?" /* forwarded_time */
9755 ", ?" /* default charset */
9756 ", ?" /* eas_data_length */
9757 ", ?" /* eas_data */
9758 ", ?" /* user_name */
9761 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9763 EM_SAFE_STRLEN(sql_query_string),
9767 if (rc != SQLITE_OK) {
9768 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9769 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9770 error = EMAIL_ERROR_DB_FAILURE;
9774 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9775 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9776 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9777 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9778 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9780 _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9781 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9782 _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);
9783 _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);
9784 _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);
9786 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9787 _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);
9788 _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);
9789 _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);
9790 _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);
9792 _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);
9793 _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);
9794 _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);
9795 _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);
9796 _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);
9798 _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);
9799 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9800 _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);
9801 _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);
9802 _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);
9804 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9805 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9806 _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9807 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9808 _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9810 _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9811 _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9812 _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9813 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9814 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9816 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9817 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9818 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9819 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9820 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9822 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9823 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9824 _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9825 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9826 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9828 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9829 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9830 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9831 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9832 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9834 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9835 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9836 _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);
9837 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9838 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9839 _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);
9841 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9842 if (rc == SQLITE_FULL) {
9843 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9844 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9847 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9848 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9849 error = EMAIL_ERROR_DB_FAILURE;
9855 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9857 if (hStmt != NULL) {
9858 rc = sqlite3_finalize(hStmt);
9859 if (rc != SQLITE_OK) {
9860 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9861 error = EMAIL_ERROR_DB_FAILURE;
9865 if (err_code != NULL)
9868 EM_PROFILE_END(profile_emstorage_add_mail);
9869 EM_DEBUG_FUNC_END("ret [%d]", ret);
9873 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)
9875 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);
9877 int ret = false, i, cur_conditional_clause = 0;
9878 int error = EMAIL_ERROR_NONE;
9879 int target_account_id;
9880 int conditional_clause_len = 0;
9881 char *sql_query_string = NULL, *conditional_clause = NULL;
9882 emstorage_mailbox_tbl_t *result_mailbox = NULL;
9883 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9884 char* target_mailbox_name = NULL;
9886 if (!mail_ids || input_mailbox_id <= 0) {
9887 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9888 if (err_code != NULL)
9889 *err_code = EMAIL_ERROR_INVALID_PARAM;
9893 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9895 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9896 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9897 if (err_code != NULL)
9902 if (result_mailbox->mailbox_name) {
9903 if (strstr(result_mailbox->mailbox_name, "'"))
9904 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9906 target_mailbox_name = strdup(result_mailbox->mailbox_name);
9909 target_mailbox_type = result_mailbox->mailbox_type;
9910 target_account_id = result_mailbox->account_id;
9911 emstorage_free_mailbox(&result_mailbox, 1, NULL);
9913 conditional_clause_len = (sizeof(char) * 8 * number_of_mails) + 512;
9914 conditional_clause = em_malloc(conditional_clause_len);
9915 if (conditional_clause == NULL) {
9916 EM_DEBUG_EXCEPTION("em_mallocfailed");
9917 error = EMAIL_ERROR_OUT_OF_MEMORY;
9920 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9922 for (i = 0; i < number_of_mails; i++)
9923 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9926 char *last_comma = rindex(conditional_clause, ',');
9927 if (last_comma) *last_comma = ')'; /* replace , with) */
9929 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9931 /* Updating a mail_tbl */
9933 sql_query_string = em_malloc(conditional_clause_len);
9934 if (sql_query_string == NULL) {
9935 EM_DEBUG_EXCEPTION("em_mallocfailed");
9936 error = EMAIL_ERROR_OUT_OF_MEMORY;
9939 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);
9940 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9942 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9943 if (error != EMAIL_ERROR_NONE) {
9944 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9948 /* Updating a mail_attachment_tbl */
9949 memset(sql_query_string, 0x00, conditional_clause_len);
9950 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);
9951 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9952 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9953 if (error != EMAIL_ERROR_NONE) {
9954 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9958 /* Updating a mail_meeting_tbl */
9959 memset(sql_query_string, 0x00, conditional_clause_len);
9960 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);
9961 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9962 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9963 if (error != EMAIL_ERROR_NONE) {
9964 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9968 #ifdef __FEATURE_BODY_SEARCH__
9969 /* Updating mail_text_tbl */
9970 memset(sql_query_string, 0x00, conditional_clause_len);
9971 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);
9972 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9973 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9974 if (error != EMAIL_ERROR_NONE) {
9975 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9980 /* Updating a mail_read_mail_uid_tbl */
9981 memset(conditional_clause, 0x00, conditional_clause_len);
9982 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9984 for (i = 0; i < number_of_mails; i++)
9985 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9988 last_comma = rindex(conditional_clause, ',');
9989 if (last_comma) *last_comma = ')'; /* replace , with) */
9991 memset(sql_query_string, 0x00, conditional_clause_len);
9992 SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = ? mailbox_id = ? account_id = ? %s", conditional_clause);
9994 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9995 error = emstorage_exec_query_by_prepare_v2_stmt(local_db_handle, sql_query_string, target_mailbox_name, input_mailbox_id, target_account_id);
9996 if (error != EMAIL_ERROR_NONE) {
9997 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2_stmt failed:[%d]", error);
10004 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10006 EM_SAFE_FREE(target_mailbox_name);
10007 EM_SAFE_FREE(conditional_clause);
10008 EM_SAFE_FREE(sql_query_string);
10010 if (err_code != NULL)
10013 EM_DEBUG_FUNC_END("ret [%d]", ret);
10017 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
10019 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10022 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10023 if (err_code != NULL)
10024 *err_code = EMAIL_ERROR_INVALID_PARAM;
10029 int error = EMAIL_ERROR_NONE;
10030 char sql_query_string[QUERY_SIZE] = {0, };
10031 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10032 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10034 memset(sql_query_string, 0x00, sizeof(sql_query_string));
10036 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
10037 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10038 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10039 if (error != EMAIL_ERROR_NONE) {
10040 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10047 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10049 if (err_code != NULL)
10052 EM_DEBUG_FUNC_END("ret [%d]", ret);
10056 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10058 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10060 int ret = false, i, cur_sql_query_string = 0;
10061 int error = EMAIL_ERROR_NONE;
10062 int query_size = 0;
10063 char *sql_query_string = NULL;
10066 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10067 if (err_code != NULL)
10068 *err_code = EMAIL_ERROR_INVALID_PARAM;
10072 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10073 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10075 query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10076 sql_query_string = em_malloc(query_size);
10077 if (sql_query_string == NULL) {
10078 EM_DEBUG_EXCEPTION("em_mallocfailed");
10079 error = EMAIL_ERROR_OUT_OF_MEMORY;
10083 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10085 for (i = 0; i < number_of_mails; i++)
10086 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10088 /* prevent 34414 */
10089 char *last_comma = rindex(sql_query_string, ',');
10090 if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10092 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10093 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10094 if (error != EMAIL_ERROR_NONE) {
10095 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10099 #ifdef __FEATURE_BODY_SEARCH__
10100 /* delete mail_text from mail_text_tbl */
10101 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10103 for (i = 0; i < number_of_mails; i++)
10104 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10106 last_comma = rindex(sql_query_string, ',');
10107 *last_comma = ')'; /* replace , with) */
10109 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10110 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10111 if (error != EMAIL_ERROR_NONE) {
10112 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10120 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10122 EM_SAFE_FREE(sql_query_string);
10123 if (err_code != NULL)
10126 EM_DEBUG_FUNC_END("ret [%d]", ret);
10130 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10132 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10134 if (account_id < FIRST_ACCOUNT_ID) {
10135 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10137 if (err_code != NULL)
10138 *err_code = EMAIL_ERROR_INVALID_PARAM;
10142 int rc, ret = false;
10143 int error = EMAIL_ERROR_NONE;
10144 char sql_query_string[QUERY_SIZE] = {0, };
10146 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10147 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10149 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10150 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10151 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10152 if (error != EMAIL_ERROR_NONE) {
10153 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10157 rc = sqlite3_changes(local_db_handle);
10159 EM_DEBUG_LOG(" no mail found...");
10160 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10163 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
10164 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10165 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10166 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10167 if (error != EMAIL_ERROR_NONE) {
10168 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10172 rc = sqlite3_changes(local_db_handle);
10174 EM_DEBUG_LOG("No mail found...");
10175 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10178 #ifdef __FEATURE_BODY_SEARCH__
10179 /* Delete all mail_text in mail_text_tbl table based on account id */
10180 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10181 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10182 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10183 if (error != EMAIL_ERROR_NONE) {
10184 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10188 rc = sqlite3_changes(local_db_handle);
10190 EM_DEBUG_LOG("No mail found...");
10191 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10198 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10200 if (error == EMAIL_ERROR_NONE) {
10201 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10202 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10205 if (err_code != NULL)
10208 EM_DEBUG_FUNC_END("ret [%d]", ret);
10212 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10214 EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10216 if (mailbox == NULL) {
10217 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10218 if (err_code != NULL)
10219 *err_code = EMAIL_ERROR_INVALID_PARAM;
10224 int error = EMAIL_ERROR_NONE;
10225 char sql_query_string[QUERY_SIZE] = {0, };
10226 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10227 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10229 if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10230 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);
10231 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10232 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10233 if (error != EMAIL_ERROR_NONE) {
10234 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10238 /* Delete Mails from mail_read_mail_uid_tbl */
10239 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);
10240 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10241 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10242 if (error != EMAIL_ERROR_NONE) {
10243 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10247 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);
10248 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10249 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10250 if (error != EMAIL_ERROR_NONE) {
10251 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10255 /* Delete Mails from mail_read_mail_uid_tbl */
10256 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);
10257 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10258 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10259 if (error != EMAIL_ERROR_NONE) {
10260 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10264 #ifdef __FEATURE_BODY_SEARCH__
10265 /* Delete Mails from mail_text_tbl */
10266 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);
10267 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10268 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10269 if (error != EMAIL_ERROR_NONE) {
10270 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10278 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10280 if (error == EMAIL_ERROR_NONE) {
10281 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10282 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10285 if (err_code != NULL)
10288 EM_DEBUG_FUNC_END("ret [%d]", ret);
10292 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10294 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10297 if ((mail_list == NULL) || (*mail_list == NULL)) {
10298 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10301 *err_code = EMAIL_ERROR_INVALID_PARAM;
10305 emstorage_mail_tbl_t* p = *mail_list;
10308 for (; i < count; i++, p++) {
10309 EM_SAFE_FREE(p->server_mailbox_name);
10310 EM_SAFE_FREE(p->server_mail_id);
10311 EM_SAFE_FREE(p->full_address_from);
10312 EM_SAFE_FREE(p->full_address_reply);
10313 EM_SAFE_FREE(p->full_address_to);
10314 EM_SAFE_FREE(p->full_address_cc);
10315 EM_SAFE_FREE(p->full_address_bcc);
10316 EM_SAFE_FREE(p->full_address_return);
10317 EM_SAFE_FREE(p->subject);
10318 EM_SAFE_FREE(p->file_path_plain);
10319 EM_SAFE_FREE(p->file_path_html);
10320 EM_SAFE_FREE(p->file_path_mime_entity);
10321 EM_SAFE_FREE(p->message_id);
10322 EM_SAFE_FREE(p->email_address_sender);
10323 EM_SAFE_FREE(p->email_address_recipient);
10324 EM_SAFE_FREE(p->preview_text);
10325 EM_SAFE_FREE(p->alias_sender);
10326 EM_SAFE_FREE(p->alias_recipient);
10327 EM_SAFE_FREE(p->default_charset);
10328 EM_SAFE_FREE(p->pgp_password);
10329 EM_SAFE_FREE(p->eas_data);
10330 EM_SAFE_FREE(p->user_name);
10332 EM_SAFE_FREE(*mail_list);
10335 if (err_code != NULL)
10336 *err_code = EMAIL_ERROR_NONE;
10338 EM_DEBUG_FUNC_END();
10342 #ifdef __FEATURE_BODY_SEARCH__
10343 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10345 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10348 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10349 EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10353 emstorage_mail_text_tbl_t *p = *mail_text_list;
10356 for (; i < count; i++, p++)
10357 EM_SAFE_FREE(p->body_text);
10359 EM_SAFE_FREE(*mail_text_list);
10362 EM_DEBUG_FUNC_END();
10366 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10368 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10370 if (mail_id <= 0 || !count) {
10371 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10372 if (err_code != NULL)
10373 *err_code = EMAIL_ERROR_INVALID_PARAM;
10377 int rc = -1, ret = false;
10378 int error = EMAIL_ERROR_NONE;
10379 char sql_query_string[QUERY_SIZE] = {0, };
10381 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10382 EMSTORAGE_START_READ_TRANSACTION(transaction);
10384 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10388 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10389 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10390 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10392 *count = atoi(result[1]);
10393 sqlite3_free_table(result);
10398 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10400 if (err_code != NULL)
10403 EM_DEBUG_FUNC_END("ret [%d]", ret);
10407 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)
10409 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);
10411 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10412 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10413 return EMAIL_ERROR_INVALID_PARAM;
10416 int error = EMAIL_ERROR_NONE;
10419 char **result = NULL;
10420 char sql_query_string[QUERY_SIZE] = {0, };
10421 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10422 DB_STMT hStmt = NULL;
10423 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10425 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10426 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10427 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10428 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10429 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10431 *output_attachment_count = atoi(result[1]);
10432 sqlite3_free_table(result);
10434 if (*output_attachment_count == 0) {
10435 error = EMAIL_ERROR_NONE;
10439 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10442 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10443 error = EMAIL_ERROR_OUT_OF_MEMORY;
10447 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10448 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10450 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10451 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)));
10453 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10454 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10456 if (rc == SQLITE_DONE) {
10457 EM_DEBUG_EXCEPTION("no matched attachment found...");
10458 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10461 for (i = 0; i < *output_attachment_count; i++) {
10462 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10463 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10464 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10465 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10466 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10467 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10468 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10469 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10470 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10471 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10472 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10473 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10474 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10475 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10476 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10477 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10478 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10483 if (error == EMAIL_ERROR_NONE)
10484 *output_attachment_list = p_data_tbl;
10485 else if (p_data_tbl != NULL)
10486 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10489 rc = sqlite3_finalize(hStmt);
10490 if (rc != SQLITE_OK) {
10491 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10492 error = EMAIL_ERROR_DB_FAILURE;
10493 if (*output_attachment_list)
10494 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10498 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10500 EM_DEBUG_FUNC_END("error [%d]", error);
10504 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10506 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10508 if (attachment_id <= 0 || !attachment) {
10509 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10510 if (err_code != NULL)
10511 *err_code = EMAIL_ERROR_INVALID_PARAM;
10515 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10516 int rc, ret = false;
10517 int error = EMAIL_ERROR_NONE;
10518 char sql_query_string[QUERY_SIZE] = {0, };
10520 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10521 EMSTORAGE_START_READ_TRANSACTION(transaction);
10523 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10525 sqlite3_stmt* hStmt = NULL;
10527 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10528 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10530 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10531 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10534 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10535 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10536 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10538 if (rc == SQLITE_DONE) {
10539 EM_DEBUG_LOG("no matched attachment found...");
10540 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10544 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10545 EM_DEBUG_EXCEPTION("malloc failed...");
10546 error = EMAIL_ERROR_OUT_OF_MEMORY;
10550 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10551 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10552 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10553 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10554 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10555 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10556 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10557 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10558 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10559 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10560 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10561 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10562 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10564 #ifdef __ATTACHMENT_OPTI__
10565 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10566 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10573 *attachment = p_data_tbl;
10575 if (hStmt != NULL) {
10576 rc = sqlite3_finalize(hStmt);
10577 if (rc != SQLITE_OK) {
10578 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10579 error = EMAIL_ERROR_DB_FAILURE;
10583 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10585 if (err_code != NULL)
10588 EM_DEBUG_FUNC_END("ret [%d]", ret);
10592 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)
10594 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);
10596 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10597 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10599 if (err_code != NULL)
10600 *err_code = EMAIL_ERROR_INVALID_PARAM;
10604 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10606 int rc, ret = false;
10607 int error = EMAIL_ERROR_NONE;
10608 char sql_query_string[QUERY_SIZE] = {0, };
10610 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10611 EMSTORAGE_START_READ_TRANSACTION(transaction);
10613 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));
10614 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10616 DB_STMT hStmt = NULL;
10618 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10619 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10620 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10623 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10624 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10625 ("sqlite3_step fail:%d", rc));
10627 if (rc == SQLITE_DONE) {
10628 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10629 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10633 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10634 EM_DEBUG_EXCEPTION(" malloc failed...");
10635 error = EMAIL_ERROR_OUT_OF_MEMORY;
10639 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10640 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10641 p_data_tbl->attachment_name = cpy_str(p);
10642 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10643 p_data_tbl->attachment_path = cpy_str(p);
10644 if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10645 p_data_tbl->content_id = cpy_str(p);
10646 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10647 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10648 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10649 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10650 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10651 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10652 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10653 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10654 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10655 p_data_tbl->attachment_mime_type = cpy_str(p);
10656 #ifdef __ATTACHMENT_OPTI__
10657 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10658 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10659 p_data_tbl->section = cpy_str(p);
10665 *attachment_tbl = p_data_tbl;
10667 if (hStmt != NULL) {
10668 rc = sqlite3_finalize(hStmt);
10669 if (rc != SQLITE_OK) {
10670 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10671 error = EMAIL_ERROR_DB_FAILURE;
10675 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10677 if (err_code != NULL)
10680 EM_DEBUG_FUNC_END("ret [%d]", ret);
10684 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)
10686 EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10688 if (attachment_path == NULL || !attachment) {
10689 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10690 if (err_code != NULL)
10691 *err_code = EMAIL_ERROR_INVALID_PARAM;
10695 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10696 int rc, ret = false;
10697 int error = EMAIL_ERROR_NONE;
10698 char sql_query_string[QUERY_SIZE] = {0, };
10700 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10701 EMSTORAGE_START_READ_TRANSACTION(transaction);
10703 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10705 sqlite3_stmt* hStmt = NULL;
10707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10708 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10710 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10711 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10714 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10715 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10716 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10718 if (rc == SQLITE_DONE) {
10719 EM_DEBUG_LOG("no matched attachment found...");
10720 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10724 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10725 EM_DEBUG_EXCEPTION("malloc failed...");
10726 error = EMAIL_ERROR_OUT_OF_MEMORY;
10730 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10731 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10732 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10733 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10734 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10735 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10736 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10737 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10738 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10739 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10740 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10741 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10742 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10744 #ifdef __ATTACHMENT_OPTI__
10745 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10746 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10753 *attachment = p_data_tbl;
10755 if (hStmt != NULL) {
10756 rc = sqlite3_finalize(hStmt);
10757 if (rc != SQLITE_OK) {
10758 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10759 error = EMAIL_ERROR_DB_FAILURE;
10763 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10765 if (err_code != NULL)
10768 EM_DEBUG_FUNC_END("ret [%d]", ret);
10772 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)
10774 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10776 if (mail_id <= 0 || !attachment) {
10777 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10778 if (err_code != NULL)
10779 *err_code = EMAIL_ERROR_INVALID_PARAM;
10783 int rc, ret = false;
10784 int error = EMAIL_ERROR_NONE;
10785 DB_STMT hStmt = NULL;
10786 char sql_query_string[QUERY_SIZE] = {0, };
10790 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10792 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10795 case UPDATE_MAILBOX:
10796 EM_DEBUG_LOG("UPDATE_MAILBOX");
10797 if (!attachment->mailbox_id) {
10798 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10799 error = EMAIL_ERROR_INVALID_PARAM;
10802 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10803 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10805 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10806 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10807 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10808 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10810 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10813 case UPDATE_SAVENAME:
10814 EM_DEBUG_LOG("UPDATE_SAVENAME");
10815 if (!attachment->attachment_path) {
10816 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10817 error = EMAIL_ERROR_INVALID_PARAM;
10821 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10822 "UPDATE mail_attachment_tbl SET"
10823 " attachment_size = ?"
10824 ", attachment_save_status = ?"
10825 ", attachment_path = ?"
10826 " WHERE mail_id = %d"
10827 " AND attachment_id = %d"
10828 , attachment->mail_id
10829 , attachment->attachment_id);
10832 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10833 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10834 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10835 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10837 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10838 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10839 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10843 EM_DEBUG_LOG("type[%d]", type);
10844 error = EMAIL_ERROR_INVALID_PARAM;
10847 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10849 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10850 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10851 ("sqlite3_step fail:%d", rc));
10856 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10858 if (hStmt != NULL) {
10859 rc = sqlite3_finalize(hStmt);
10860 if (rc != SQLITE_OK) {
10861 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10862 error = EMAIL_ERROR_DB_FAILURE;
10866 if (err_code != NULL)
10868 EM_DEBUG_FUNC_END("ret [%d]", ret);
10872 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)
10874 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);
10878 int error = EMAIL_ERROR_NONE;
10880 int account_id = 0;
10881 char sql_query_string[QUERY_SIZE] = {0, };
10882 char *replaced_mailbox_name = NULL;
10883 char *replaced_alias = NULL;
10884 sqlite3 *local_db_handle = NULL;
10885 DB_STMT hStmt = NULL;
10886 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10888 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10889 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10890 return EMAIL_ERROR_INVALID_PARAM;
10893 if (strstr(input_new_mailbox_name, "'"))
10894 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10896 replaced_mailbox_name = strdup(input_new_mailbox_name);
10898 if (strstr(input_new_mailbox_alias, "'"))
10899 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10901 replaced_alias = strdup(input_new_mailbox_alias);
10903 local_db_handle = emstorage_get_db_connection(multi_user_name);
10905 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10906 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10907 EM_SAFE_FREE(replaced_mailbox_name);
10908 EM_SAFE_FREE(replaced_alias);
10912 if (old_mailbox_data == NULL) {
10913 EM_DEBUG_LOG("old_mailbox_data is NULL");
10914 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10918 account_id = old_mailbox_data->account_id;
10920 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10922 if (input_eas_data && input_eas_data_length > 0) {
10923 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10924 "UPDATE mail_box_tbl SET"
10925 " mailbox_name = ?"
10928 ",eas_data_length = ?"
10929 " WHERE mailbox_id = ?");
10931 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10933 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10934 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10936 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10937 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10938 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10939 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10940 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10942 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10943 "UPDATE mail_box_tbl SET"
10944 " mailbox_name = ?"
10946 " WHERE mailbox_id = ?");
10948 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10950 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10951 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10953 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10954 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10955 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10959 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10960 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10961 ("sqlite3_step fail:%d", rc));
10962 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10963 ("sqlite3_step fail:%d", rc));
10965 if (sqlite3_changes(local_db_handle) == 0)
10966 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10971 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10972 if (error == EMAIL_ERROR_NONE) {
10973 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10974 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10976 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10977 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10980 EM_SAFE_FREE(replaced_mailbox_name);
10981 EM_SAFE_FREE(replaced_alias);
10983 if (old_mailbox_data)
10984 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10986 if (hStmt != NULL) {
10987 rc = sqlite3_finalize(hStmt);
10988 if (rc != SQLITE_OK) {
10989 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10990 error = EMAIL_ERROR_DB_FAILURE;
10994 EM_DEBUG_FUNC_END("error [%d]", error);
10998 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
11000 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
11001 int rc, ret = false;
11002 int error = EMAIL_ERROR_NONE;
11003 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11006 if (!attachment_no) {
11007 EM_DEBUG_EXCEPTION("Invalid attachment");
11008 if (err_code != NULL)
11009 *err_code = EMAIL_ERROR_INVALID_PARAM;
11013 *attachment_no = -1;
11015 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11018 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11019 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11020 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11022 if (NULL == result[1])
11025 rc = atoi(result[1])+1;
11027 sqlite3_free_table(result);
11029 *attachment_no = rc;
11030 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
11035 if (err_code != NULL)
11038 EM_DEBUG_FUNC_END("ret [%d]", ret);
11042 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11044 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11048 int rc, ret = false;
11049 int error = EMAIL_ERROR_NONE;
11050 char sql_query_string[QUERY_SIZE] = {0, };
11052 DB_STMT hStmt = NULL;
11053 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11055 if (!attachment_tbl) {
11056 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11057 if (err_code != NULL)
11058 *err_code = EMAIL_ERROR_INVALID_PARAM;
11062 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11064 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11066 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11067 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11068 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11070 if (NULL == result[1]) rc = 1;
11071 else rc = atoi(result[1]) + 1;
11072 sqlite3_free_table(result);
11074 attachment_tbl->attachment_id = rc;
11076 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11077 "INSERT INTO mail_attachment_tbl VALUES "
11078 "(?" /* attachment_id */
11079 ", ?" /* attachment_name */
11080 ", ?" /* attachment_path */
11081 ", ?" /* content_id */
11082 ", ?" /* attachment_size */
11083 ", ?" /* mail_id */
11084 ", ?" /* account_id */
11085 ", ?" /* mailbox_id */
11086 ", ?" /* attachment_save_status */
11087 ", ?" /* attachment_drm_type */
11088 ", ?" /* attachment_drm_method */
11089 ", ?" /* attachment_inline_content_status */
11090 ", ?" /* attachment_mime_type */
11091 #ifdef __ATTACHMENT_OPTI__
11098 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11099 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11100 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11102 _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11103 _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);
11104 _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);
11105 _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);
11106 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11107 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11108 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11109 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11110 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11111 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11112 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11113 _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11114 _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);
11115 #ifdef __ATTACHMENT_OPTI__
11116 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11117 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11121 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11122 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11123 ("sqlite3_step fail:%d", rc));
11124 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11125 ("sqlite3_step fail:%d", rc));
11127 rc = sqlite3_changes(local_db_handle);
11129 EM_DEBUG_LOG(" no matched mail found...");
11130 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11137 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11139 if (hStmt != NULL) {
11140 rc = sqlite3_finalize(hStmt);
11141 if (rc != SQLITE_OK) {
11142 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11143 error = EMAIL_ERROR_DB_FAILURE;
11147 if (err_code != NULL)
11150 EM_DEBUG_FUNC_END("ret [%d]", ret);
11154 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11156 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11158 int rc, ret = false, field_idx = 0;
11159 int error = EMAIL_ERROR_NONE;
11160 DB_STMT hStmt = NULL;
11161 char sql_query_string[QUERY_SIZE] = {0, };
11163 if (!attachment_tbl) {
11164 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11166 *err_code = EMAIL_ERROR_INVALID_PARAM;
11170 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11172 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11174 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11175 "UPDATE mail_attachment_tbl SET "
11176 " attachment_name = ?"
11177 ", attachment_path = ?"
11179 ", attachment_size = ?"
11183 ", attachment_save_status = ?"
11184 ", attachment_drm_type = ?"
11185 ", attachment_drm_method = ?"
11186 ", attachment_inline_content_status = ? "
11187 ", attachment_mime_type = ? "
11188 " WHERE attachment_id = ?;");
11191 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11193 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11194 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11196 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11197 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11198 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11199 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11200 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11201 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11202 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11203 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11204 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11205 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11206 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11207 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11208 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11211 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11212 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11213 ("sqlite3_step fail:%d", rc));
11214 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11215 ("sqlite3_step fail:%d", rc));
11217 rc = sqlite3_changes(local_db_handle);
11219 EM_DEBUG_LOG(" no matched mail found...");
11220 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11227 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11228 if (hStmt != NULL) {
11229 rc = sqlite3_finalize(hStmt);
11230 if (rc != SQLITE_OK) {
11231 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11232 error = EMAIL_ERROR_DB_FAILURE;
11236 if (err_code != NULL)
11239 EM_DEBUG_FUNC_END("ret [%d]", ret);
11243 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11245 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11247 if (attachment_id < 0) {
11248 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11249 if (err_code != NULL)
11250 *err_code = EMAIL_ERROR_INVALID_PARAM;
11255 int error = EMAIL_ERROR_NONE;
11256 char sql_query_string[QUERY_SIZE] = {0, };
11257 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11259 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11261 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11263 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11264 if (error != EMAIL_ERROR_NONE) {
11265 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11272 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11277 EM_DEBUG_FUNC_END("ret [%d]", ret);
11281 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11283 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11285 int error = EMAIL_ERROR_NONE;
11286 char sql_query_string[QUERY_SIZE] = {0, };
11287 sqlite3 *local_db_handle = NULL;
11289 if (mail_id <= 0) {
11290 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11291 if (err_code != NULL)
11292 *err_code = EMAIL_ERROR_INVALID_PARAM;
11296 local_db_handle = emstorage_get_db_connection(multi_user_name);
11298 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11300 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11301 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11302 if (error != EMAIL_ERROR_NONE) {
11303 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11310 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11315 EM_DEBUG_FUNC_END("ret [%d]", ret);
11319 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11321 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11323 int error = EMAIL_ERROR_NONE;
11325 char sql_query_string[QUERY_SIZE] = {0, };
11326 char *replaced_mailbox = NULL;
11327 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11329 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11331 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11333 if (account_id != ALL_ACCOUNT) /* '0' means all account */
11334 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);
11336 if (mailbox) /* NULL means all mailbox_name */ {
11337 if (strstr(mailbox, "'"))
11338 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11340 replaced_mailbox = strdup(mailbox);
11342 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);
11343 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11346 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11347 if (error != EMAIL_ERROR_NONE) {
11348 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11355 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11357 if (err_code != NULL)
11360 EM_DEBUG_FUNC_END("ret [%d]", ret);
11364 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11366 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11369 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11370 EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11371 if (err_code != NULL)
11372 *err_code = EMAIL_ERROR_INVALID_PARAM;
11376 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11379 for (i = 0; i < count; i++) {
11380 EM_SAFE_FREE(p[i].attachment_name);
11381 EM_SAFE_FREE(p[i].attachment_path);
11382 EM_SAFE_FREE(p[i].content_id);
11383 EM_SAFE_FREE(p[i].attachment_mime_type);
11384 #ifdef __ATTACHMENT_OPTI__
11385 EM_SAFE_FREE(p[i].section);
11390 *attachment_tbl_list = NULL;
11393 if (err_code != NULL)
11394 *err_code = EMAIL_ERROR_NONE;
11395 EM_DEBUG_FUNC_END();
11399 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11401 EM_DEBUG_FUNC_BEGIN();
11402 EM_PROFILE_BEGIN(emStorageBeginTransaction);
11405 _timedlock_shm_mutex(mapped_for_db_lock, 2);
11407 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11411 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11412 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11414 if (ret == false) {
11415 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11418 EM_PROFILE_END(emStorageBeginTransaction);
11419 EM_DEBUG_FUNC_END("ret [%d]", ret);
11423 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11425 EM_DEBUG_FUNC_BEGIN();
11429 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11431 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11432 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11434 if (ret == false && err_code != NULL)
11435 *err_code = EMAIL_ERROR_DB_FAILURE;
11437 _unlockshm_mutex(mapped_for_db_lock);
11439 EM_DEBUG_FUNC_END("ret [%d]", ret);
11443 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11445 EM_DEBUG_FUNC_BEGIN();
11448 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11450 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11451 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11452 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11454 if (ret == false && err_code != NULL)
11455 *err_code = EMAIL_ERROR_DB_FAILURE;
11457 _unlockshm_mutex(mapped_for_db_lock);
11459 EM_DEBUG_FUNC_END("ret [%d]", ret);
11463 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11465 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11467 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11469 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11471 if (err_code != NULL)
11472 *err_code = EMAIL_ERROR_INVALID_PARAM;
11478 int error = EMAIL_ERROR_NONE;
11479 int mail_count = 0;
11481 if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11482 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11487 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11488 if (mail_count >= mailbox->mail_slot_size)
11499 if (err_code != NULL)
11502 EM_DEBUG_FUNC_END("ret [%d]", ret);
11506 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11508 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11511 int error = EMAIL_ERROR_NONE;
11512 char sql_query_string[QUERY_SIZE] = {0, };
11513 const email_db_object_t* tables = _g_db_tables;
11514 const email_db_object_t* indexes = _g_db_indexes;
11516 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11517 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11519 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11520 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11525 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11526 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11527 chmod(MAIL_TEMP, 0777);
11529 /* first clear index. */
11530 while (indexes->object_name) {
11531 if (indexes->data_flag) {
11532 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11533 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11534 if (error != EMAIL_ERROR_NONE) {
11535 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11542 while (tables->object_name) {
11543 if (tables->data_flag) {
11544 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11545 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11546 if (error != EMAIL_ERROR_NONE) {
11547 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11557 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11559 if (err_code != NULL)
11562 EM_DEBUG_FUNC_END("ret [%d]", ret);
11565 /*======================= DB File Utils =============================================*/
11566 #include <dirent.h>
11567 #include <sys/types.h>
11568 #define DIR_SEPERATOR "/"
11570 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11572 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11573 char delims[] = "/";
11574 char *result = NULL;
11575 gchar **token = NULL;
11577 token = g_strsplit_set(file_name, delims, 1);
11579 if (token && token[0]) {
11580 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11581 result = EM_SAFE_STRDUP(token[0]);
11583 EM_DEBUG_LOG(">>>> No Need to create Directory");
11590 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11591 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11593 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);
11594 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11597 int error = EMAIL_ERROR_NONE;
11598 char *dir_name = NULL;
11599 char create_dir[1024] = {0};
11600 char *temp_file = NULL;
11601 char *prefix_path = NULL;
11602 char *modified_fname = NULL;
11604 if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11605 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);
11606 error = EMAIL_ERROR_INVALID_PARAM;
11610 snprintf(path_buf, 512, "%s", MAIL_HOME);
11611 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11614 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11617 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11620 temp_file = EM_SAFE_STRDUP(fname);
11621 if (temp_file && strstr(temp_file, "/"))
11622 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11626 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11627 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11628 ret = mkdir(create_dir, DIRECTORY_PERMISSION);
11629 EM_DEBUG_LOG("mkdir result = [%d]", ret);
11630 EM_SAFE_FREE(dir_name);
11634 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11636 /* Did not allow the slash */
11637 modified_fname = reg_replace_new(fname, "/", "_");
11638 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11640 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11642 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11643 char *modified_name = NULL;
11644 int remain_len = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11646 if (remain_len <= 0) {
11647 error = EMAIL_ERROR_MAX_EXCEEDED;
11651 if (remain_len > MAX_FILENAME)
11652 remain_len = MAX_FILENAME;
11654 modified_name = em_shrink_filename(modified_fname, remain_len);
11656 if (!modified_name) {
11657 error = EMAIL_ERROR_MAX_EXCEEDED;
11661 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11662 "%s%s", DIR_SEPERATOR, modified_name);
11663 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11664 EM_SAFE_FREE(modified_name);
11666 if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11667 char *modified_name = NULL;
11669 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11670 if (!modified_name) {
11671 error = EMAIL_ERROR_MAX_EXCEEDED;
11675 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11676 "%s%s", DIR_SEPERATOR, modified_name);
11677 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11678 EM_SAFE_FREE(modified_name);
11680 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11681 "%s%s", DIR_SEPERATOR, modified_fname);
11686 EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11688 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11689 error = emcore_get_container_path(multi_user_name, &prefix_path);
11690 if (error != EMAIL_ERROR_NONE) {
11691 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11694 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11695 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11697 snprintf(move_buf, 512, "%s", path_buf);
11698 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11705 EM_SAFE_FREE(temp_file);
11706 EM_SAFE_FREE(prefix_path);
11707 EM_SAFE_FREE(modified_fname);
11709 if (err_code != NULL)
11712 EM_PROFILE_END(profile_emstorage_get_save_name);
11713 EM_DEBUG_FUNC_END("ret [%d]", ret);
11718 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)
11720 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);
11722 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11723 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);
11724 if (err_code != NULL)
11725 *err_code = EMAIL_ERROR_INVALID_PARAM;
11729 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11732 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11737 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11742 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11744 EM_DEBUG_FUNC_END();
11749 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11751 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11752 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11754 int error = EMAIL_ERROR_NONE;
11757 char *prefix_path = NULL;
11758 char errno_buf[ERRNO_BUF_SIZE] = {0};
11760 memset(buf, 0x00, sizeof(buf));
11762 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11763 error = emcore_get_container_path(multi_user_name, &prefix_path);
11764 if (error != EMAIL_ERROR_NONE) {
11765 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11769 prefix_path = strdup("");
11772 if (account_id >= FIRST_ACCOUNT_ID) {
11773 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
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 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11789 error = EMAIL_ERROR_SYSTEM_FAILURE;
11791 error = EMAIL_ERROR_OUT_OF_MEMORY;
11795 if (account_id == EML_FOLDER)
11801 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11803 if (account_id < FIRST_ACCOUNT_ID) {
11804 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11805 error = EMAIL_ERROR_INVALID_PARAM;
11809 if (space_left_in_buffer + 10 > sizeof(buf)) {
11810 EM_DEBUG_EXCEPTION("Buffer overflowed");
11811 error = EMAIL_ERROR_OUT_OF_MEMORY;
11815 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11817 if (stat(buf, &sbuf) == 0) {
11818 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11819 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11820 error = EMAIL_ERROR_SYSTEM_FAILURE;
11824 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11825 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11826 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11827 error = EMAIL_ERROR_SYSTEM_FAILURE;
11829 error = EMAIL_ERROR_OUT_OF_MEMORY;
11833 if (account_id == EML_FOLDER)
11839 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11840 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11841 error = EMAIL_ERROR_INVALID_PARAM;
11845 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11847 if (stat(buf, &sbuf) == 0) {
11848 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11849 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11850 error = EMAIL_ERROR_SYSTEM_FAILURE;
11854 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11855 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11856 error = EMAIL_ERROR_SYSTEM_FAILURE;
11858 error = EMAIL_ERROR_OUT_OF_MEMORY;
11862 if (account_id == EML_FOLDER)
11871 EM_SAFE_FREE(prefix_path);
11873 if (err_code != NULL)
11876 EM_PROFILE_END(profile_emcore_save_create_dir);
11877 EM_DEBUG_FUNC_END("ret [%d]", ret);
11881 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11883 EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11884 EM_DEBUG_LOG("Using the fsync function");
11886 int error = EMAIL_ERROR_NONE;
11887 struct stat st_buf;
11892 char buf[FILE_MAX_BUFFER_SIZE] = {0};
11893 char errno_buf[ERRNO_BUF_SIZE] = {0};
11895 if (!src_file || !dst_file) {
11896 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11898 error = EMAIL_ERROR_INVALID_PARAM;
11902 if (stat(src_file, &st_buf) < 0) {
11903 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11905 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
11909 error = em_open(src_file, O_RDONLY, 0, &fp_src);
11910 if (error != EMAIL_ERROR_NONE) {
11911 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11915 error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11916 if (error != EMAIL_ERROR_NONE) {
11917 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s: %d", dst_file, error);
11921 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11923 while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11924 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11925 EM_DEBUG_LOG("Nread Value [%d]", nread);
11927 ssize_t byte_written = 0;
11928 size_t remain_byte = nread;
11932 while (remain_byte > 0 && buf_ptr && errno == 0) {
11933 byte_written = write(fp_dst, buf_ptr, remain_byte);
11935 if (byte_written < 0) {
11936 /* interrupted by a signal */
11937 if (errno == EINTR) {
11942 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11943 error = EMAIL_ERROR_UNKNOWN;
11946 EM_DEBUG_LOG("NWRITTEN [%zu]", byte_written);
11947 remain_byte -= byte_written;
11948 buf_ptr += byte_written;
11952 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11958 EM_SAFE_CLOSE(fp_src);
11960 if (fp_dst >= 0) { /*prevent 24474*/
11962 EM_DEBUG_LOG("Before fsync");
11968 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11971 if (err_code != NULL)
11973 EM_DEBUG_FUNC_END("ret [%d]", ret);
11976 /* create Directory if user has deleted [deepam.p@samsung.com] */
11977 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11979 EM_DEBUG_FUNC_BEGIN();
11981 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11982 mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
11983 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11984 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11985 chmod(MAIL_TEMP, 0777);
11987 EM_DEBUG_FUNC_END();
11990 static int _get_temp_file_name(char **filename, int *err_code)
11992 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11995 int error = EMAIL_ERROR_NONE;
11997 if (filename == NULL) {
11998 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11999 error = EMAIL_ERROR_INVALID_PARAM;
12003 char tempname[512] = {0x00, };
12006 gettimeofday(&tv, NULL);
12008 unsigned int seed = time(NULL);
12009 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
12011 char *p = EM_SAFE_STRDUP(tempname);
12013 EM_DEBUG_EXCEPTION(" strdup failed...");
12014 error = EMAIL_ERROR_OUT_OF_MEMORY;
12023 if (err_code != NULL)
12026 EM_DEBUG_FUNC_END("ret [%d]", ret);
12030 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
12032 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
12034 EM_IF_NULL_RETURN_VALUE(file_path, false);
12035 EM_IF_NULL_RETURN_VALUE(char_set, false);
12036 EM_IF_NULL_RETURN_VALUE(err_code, false);
12040 struct stat st_buf;
12042 char *low_char_set = NULL;
12043 char *match_str = NULL;
12046 int error = EMAIL_ERROR_NONE;
12047 int data_count_to_written = 0;
12048 char *temp_file_name = NULL;
12050 FILE* fp_src = NULL;
12051 FILE* fp_dest = NULL;
12055 if (stat(file_path, &st_buf) < 0) {
12056 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12057 error = EMAIL_ERROR_SYSTEM_FAILURE;
12061 buf_size = st_buf.st_size;
12062 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12063 buf = (char *)calloc(1, buf_size+1);
12066 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12070 error = em_fopen(file_path, "rb", &fp_src);
12071 if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12072 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12076 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12077 if (nread > 0 && nread <= buf_size) {
12078 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12081 * 1.Add check for whether content type is there.
12082 * 2. If not based on the character set, Append it in File
12085 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12086 if (low_char_set == NULL) {
12087 EM_DEBUG_EXCEPTION("calloc failed");
12088 error = EMAIL_ERROR_OUT_OF_MEMORY;
12092 strncat(low_char_set, char_set, sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1);
12093 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12094 strncat(low_char_set, " \" /></head>", sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1); /*prevent 34359*/
12095 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12098 match_str = strstr(buf, CONTENT_TYPE_DATA);
12100 if (match_str == NULL) {
12101 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12102 if (fp_src != NULL) {
12106 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12107 buf1 = (char *)calloc(1, data_count_to_written);
12110 strncat(buf1, CONTENT_DATA, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1); /*prevent 34359*/
12111 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12112 strncat(buf1, low_char_set, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1);
12113 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
12115 /* 1. Create a temporary file name */
12116 if (!_get_temp_file_name(&temp_file_name, &err)) {
12117 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12118 if (err_code != NULL) *err_code = err;
12121 EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12123 /* Open the Temp file in Append mode */
12124 error = em_fopen(temp_file_name, "ab", &fp_dest);
12125 if (error != EMAIL_ERROR_NONE) {
12126 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12130 /* 2. write the Latest data */
12131 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12133 if (nwritten > 0) {
12134 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
12136 /* 3. Append old data */
12137 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12139 if (nwritten <= 0) {
12140 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12141 error = EMAIL_ERROR_SYSTEM_FAILURE;
12144 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12146 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12147 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12153 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12154 error = EMAIL_ERROR_SYSTEM_FAILURE;
12166 EM_SAFE_FREE(buf1);
12167 EM_SAFE_FREE(low_char_set);
12168 EM_SAFE_FREE(temp_file_name);
12170 if (fp_src != NULL) {
12175 if (fp_dest != NULL) {
12183 EM_DEBUG_FUNC_END("ret [%d]", ret);
12188 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12190 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12193 int error = EMAIL_ERROR_NONE;
12194 char errno_buf[ERRNO_BUF_SIZE] = {0};
12196 if (src_file == NULL || dst_file == NULL) {
12197 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12198 error = EMAIL_ERROR_INVALID_PARAM;
12202 EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12204 if (strcmp(src_file, dst_file) != 0) {
12205 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12206 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12207 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12209 struct stat temp_file_stat;
12210 if (stat(src_file, &temp_file_stat) < 0)
12211 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12213 if (stat(dst_file, &temp_file_stat) < 0)
12214 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12216 error = EMAIL_ERROR_FILE_NOT_FOUND;
12222 EM_DEBUG_LOG("src[%s] removed", src_file);
12224 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12230 if (err_code != NULL)
12233 EM_DEBUG_FUNC_END("ret [%d]", ret);
12237 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12239 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12242 int error = EMAIL_ERROR_NONE;
12244 if (src_file == NULL) {
12245 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12247 error = EMAIL_ERROR_INVALID_PARAM;
12251 if (remove(src_file) != 0) {
12252 if (errno != ENOENT) {
12253 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12255 error = EMAIL_ERROR_SYSTEM_FAILURE;
12258 EM_DEBUG_EXCEPTION(" no file found...");
12260 error = EMAIL_ERROR_FILE_NOT_FOUND;
12267 if (err_code != NULL)
12270 EM_DEBUG_FUNC_END("ret [%d]", ret);
12275 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12277 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12279 if (src_dir == NULL) {
12280 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12282 if (err_code != NULL)
12283 *err_code = EMAIL_ERROR_INVALID_PARAM;
12287 int error = EMAIL_ERROR_NONE;
12294 dirp = opendir(src_dir);
12296 if (dirp == NULL) {
12297 if (errno == ENOENT) {
12298 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12299 if (err_code != NULL)
12300 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12303 EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12304 if (err_code != NULL)
12305 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12310 while ((dp = readdir(dirp))) {
12311 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12314 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12316 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12317 /* check directory */
12318 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
12319 /* recursive call */
12320 if (!emstorage_delete_dir(buf, &error)) {
12322 if (err_code != NULL)
12326 } else { /* file */
12327 if (remove(buf) < 0) {
12328 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12330 if (err_code != NULL)
12331 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12336 EM_DEBUG_EXCEPTION("content does not exist...");
12341 EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12343 /* EM_DEBUG_FUNC_BEGIN(); */
12345 if (remove(src_dir) < 0) {
12346 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12347 if (err_code != NULL)
12348 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12352 if (err_code != NULL)
12358 /* faizan.h@samsung.com */
12359 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12361 char *old_server_uid,
12362 char *new_server_uid,
12365 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12367 int error = EMAIL_ERROR_NONE;
12368 int transaction = true;
12369 int temp_strlen = 0;
12370 int and_operation = 0;
12371 char sql_query_string[QUERY_SIZE] = {0, };
12372 char conditional_clause_string[QUERY_SIZE] = {0};
12374 if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12375 EM_DEBUG_EXCEPTION("Invalid parameters");
12377 *err_code = EMAIL_ERROR_INVALID_PARAM;
12381 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12382 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12384 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12387 temp_strlen = strlen(conditional_clause_string);
12388 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12389 "mail_id = %d ", mail_id);
12393 if (old_server_uid) {
12394 temp_strlen = strlen(conditional_clause_string);
12395 if (!and_operation) {
12396 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12397 "server_mail_id = '%q'", old_server_uid);
12399 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12400 "and server_mail_id = '%q'", old_server_uid);
12404 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12405 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12407 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12408 if (error != EMAIL_ERROR_NONE) {
12409 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12417 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12419 if (err_code != NULL)
12422 EM_DEBUG_FUNC_END("ret [%d]", ret);
12427 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)
12429 EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12432 int error = EMAIL_ERROR_NONE;
12433 char sql_query_string[QUERY_SIZE] = {0, };
12434 int transaction = true;
12436 if (!mail_id || !new_server_uid || !mbox_name) {
12437 EM_DEBUG_EXCEPTION("Invalid parameters");
12438 if (err_code != NULL)
12439 *err_code = EMAIL_ERROR_INVALID_PARAM;
12443 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12444 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12447 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12448 "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);
12450 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12451 if (error != EMAIL_ERROR_NONE) {
12452 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12460 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12462 if (err_code != NULL)
12465 EM_DEBUG_FUNC_END("ret [%d]", ret);
12470 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12472 EM_DEBUG_FUNC_BEGIN();
12475 int transaction = true;
12476 int error = EMAIL_ERROR_NONE;
12477 char sql_query_string[QUERY_SIZE] = {0,};
12479 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12481 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12483 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12485 if (account_id <= ALL_ACCOUNT)
12486 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);
12488 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);
12490 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12491 if (error != EMAIL_ERROR_NONE) {
12492 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12499 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12501 if (err_code != NULL)
12504 EM_DEBUG_FUNC_END("ret [%d]", ret);
12509 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12511 EM_DEBUG_FUNC_BEGIN();
12513 if ((!mail_ids) || (account_id <= 0 && account_id != -1)) {
12514 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12515 if (err_code != NULL)
12516 *err_code = EMAIL_ERROR_INVALID_PARAM;
12522 int error = EMAIL_ERROR_NONE;
12526 int *p_mail_ids = NULL;
12527 int transaction = false;
12528 char **result = NULL;
12529 char sql_query_string[QUERY_SIZE] = {0, };
12530 char temp_query_string[QUERY_SIZE] = {0,};
12531 char sql_select_query_string[QUERY_SIZE] = {0, };
12533 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12534 EMSTORAGE_START_READ_TRANSACTION(transaction);
12536 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12538 if (account_id == -1) {
12539 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12540 "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12541 EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12543 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12544 "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12545 account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12549 SNPRINTF(temp_query_string, sizeof(temp_query_string),
12550 "%s AND tag_id < 0", sql_select_query_string);
12552 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12555 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12557 EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12559 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12560 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12561 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12563 EM_DEBUG_LOG("Count : %d", count);
12566 EM_DEBUG_EXCEPTION("no Mails found...");
12568 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12572 p_mail_ids = em_malloc(count * sizeof(int));
12573 if (p_mail_ids == NULL) {
12574 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12575 error = EMAIL_ERROR_OUT_OF_MEMORY;
12581 for (i = 0; i < count; i++)
12582 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12589 sqlite3_free_table(result);
12591 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12593 // sqlite3_db_release_memory(local_db_handle);
12596 if (mail_ids != NULL)
12597 *mail_ids = p_mail_ids;
12599 if (mail_number != NULL)
12600 *mail_number = count;
12602 EM_SAFE_FREE(p_mail_ids);
12605 if (err_code != NULL)
12608 EM_DEBUG_FUNC_END("ret [%d]", ret);
12612 int setting_system_command(const char *command)
12615 int pid = 0, status = 0;
12616 char *const environ[] = { NULL };
12631 argv[2] = (char *)command;
12634 execve("/bin/sh", argv, environ);
12639 if (waitpid(pid, &status, 0) == -1) {
12640 if (errno != EINTR)
12651 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
12653 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12655 if (!total_usage) {
12656 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12658 if (err_code != NULL)
12659 *err_code = EMAIL_ERROR_INVALID_PARAM;
12664 int error = EMAIL_ERROR_NONE;
12665 char syscmd[256] = {0, };
12666 char line[256] = {0, };
12667 char *line_from_file = NULL;
12669 unsigned long total_diskusage = 0;
12671 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12672 if (setting_system_command(syscmd) == -1) {
12673 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12675 error = EMAIL_ERROR_SYSTEM_FAILURE;
12679 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12680 EM_DEBUG_LOG(" cmd : %s", syscmd);
12681 if (setting_system_command(syscmd) == -1) {
12682 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12684 error = EMAIL_ERROR_SYSTEM_FAILURE;
12688 error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12689 if (error != EMAIL_ERROR_NONE) {
12690 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12694 line_from_file = fgets(line, sizeof(line), fp);
12696 if (line_from_file == NULL) {
12697 EM_DEBUG_EXCEPTION("fgets failed");
12698 error = EMAIL_ERROR_SYSTEM_FAILURE;
12701 total_diskusage = strtoul(line, NULL, 10);
12703 memset(syscmd, 0, sizeof(syscmd));
12704 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12705 if (setting_system_command(syscmd) == -1) {
12706 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12707 error = EMAIL_ERROR_SYSTEM_FAILURE;
12711 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12716 if (err_code != NULL)
12720 *total_usage = total_diskusage;
12724 if (fp) fclose(fp); /* prevent 32730 */
12726 EM_DEBUG_FUNC_END("ret [%d]", ret);
12729 #define MAILHOME_UTF8 tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12731 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)
12733 DB_STMT hStmt = NULL;
12735 int error = EMAIL_ERROR_NONE;
12737 char sql_query_string[QUERY_SIZE] = {0, };
12739 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12740 "INSERT INTO mail_tbl VALUES "
12742 ", ?" /* account_id */
12743 ", ?" /* mail_size */
12744 ", ?" /* server_mail_status */
12745 ", ?" /* server_mailbox_name */
12746 ", ?" /* server_mail_id */
12747 ", ?" /* reference_mail_id */
12748 ", ?" /* full_address_from */
12749 ", ?" /* full_address_reply */
12750 ", ?" /* full_address_to */
12751 ", ?" /* full_address_cc */
12752 ", ?" /* full_address_bcc */
12753 ", ?" /* full_address_return */
12754 ", ?" /* subject */
12755 ", ?" /* body_download_status */
12756 ", ?" /* file_path_plain */
12757 ", ?" /* file_path_html */
12758 ", ?" /* date_time */
12759 ", ?" /* flags_seen_field */
12760 ", ?" /* flags_deleted_field */
12761 ", ?" /* flags_flagged_field */
12762 ", ?" /* flags_answered_field */
12763 ", ?" /* flags_recent_field */
12764 ", ?" /* flags_draft_field */
12765 ", ?" /* flags_forwarded_field */
12766 ", ?" /* DRM_status */
12767 ", ?" /* priority */
12768 ", ?" /* save_status */
12769 ", ?" /* lock_status */
12770 ", ?" /* message_id */
12771 ", ?" /* report_status */
12772 ", ?" /* email_address_sender */
12773 ", ?" /* email_address_recipient */
12774 ", ?" /* attachment_count */
12775 ", ?" /* inline_content_count */
12776 ", ?" /* preview_text */
12777 ", ?" /* thread_id */
12778 ", ?" /* mailbox_type */
12779 ", ?" /* alias_sender */
12780 ", ?" /* alias_recipient */
12781 ", ?" /* thread_item_count */
12782 ", ?" /* meeting_request_status */
12783 ", ?" /* message_class */
12784 ", ?" /* digest_type */
12785 ", ?" /* smime_type */
12786 ", ?" /* scheduled_sending_time */
12787 ", ?" /* remaining_resend_times */
12789 ", ?" /* replied_time */
12790 ", ?" /* forwarded_time */
12791 ", ?" /* default_charset */
12792 ", ?" /* eas_data_length */
12793 ", ?" /* eas_data */
12796 int transaction = true;
12797 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12799 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12802 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12803 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12804 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12806 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12807 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12808 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12809 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12810 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12811 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12812 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12813 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12814 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12815 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12816 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12817 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12818 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12819 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12820 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12821 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12822 _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);
12823 _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);
12824 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12825 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12826 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12827 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12828 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12829 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12830 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12831 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12832 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12833 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12834 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12835 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12836 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12837 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12838 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12839 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12840 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12841 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12842 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12843 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12844 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12845 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12846 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12847 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12848 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12849 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12850 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12851 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12852 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12853 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12854 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12855 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12856 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12857 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12858 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12859 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12861 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12862 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12863 ("sqlite3_step fail:%d", rc));
12864 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12865 ("sqlite3_step fail:%d", rc));
12869 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12870 if (hStmt != NULL) {
12871 rc = sqlite3_finalize(hStmt);
12872 if (rc != SQLITE_OK) {
12873 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12874 error = EMAIL_ERROR_DB_FAILURE;
12878 if (err_code != NULL)
12881 EM_DEBUG_FUNC_END("ret [%d]", ret);
12886 INTERNAL_FUNC int emstorage_get_max_mail_count()
12888 return EMAIL_MAIL_MAX_COUNT;
12891 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12893 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12894 emstorage_mail_tbl_t *mail_tbl,
12896 int *result_latest_mail_id_in_thread,
12897 int *thread_item_count)
12899 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12900 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12901 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12902 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12904 int rc = 0, query_size = 0, query_size_account = 0;
12906 int search_thread = false;
12907 int account_id = 0;
12908 int err_code = EMAIL_ERROR_NONE;
12909 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12910 time_t latest_date_time = 0;
12911 char *subject = NULL;
12912 char *p_subject = NULL;
12913 char *sql_query_string = NULL, *sql_account = NULL;
12915 int temp_thread_id = -1;
12916 char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12917 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12918 char *sql_format_account = " AND account_id = %d ";
12919 char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12920 char **result = NULL;
12921 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12922 char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12924 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12926 EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12928 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12929 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12930 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12931 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12933 account_id = mail_tbl->account_id;
12934 subject = mail_tbl->subject;
12936 EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12938 if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12939 result_thread_id = mail_tbl->mail_id;
12944 if (em_find_pos_stripped_subject_for_thread_view(subject,
12946 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12947 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12948 err_code = EMAIL_ERROR_UNKNOWN;
12949 result_thread_id = -1;
12953 EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12955 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12956 result_thread_id = -1;
12960 EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s",
12961 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12963 if (account_id > 0) {
12964 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12965 sql_account = malloc(query_size_account);
12966 if (sql_account == NULL) {
12967 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
12968 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12971 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12974 /* prevent 34362 */
12975 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
12976 sql_query_string = malloc(query_size);
12978 if (sql_query_string == NULL) {
12979 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
12980 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12984 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12986 if (account_id > 0)
12987 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12989 EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12990 EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12992 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12994 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12997 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12998 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13000 EM_DEBUG_LOG("Result rows count : %d", count);
13003 result_thread_id = -1;
13005 for (i = 0; i < count; i++) {
13006 EM_SAFE_FREE(p_subject);
13008 _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
13009 _get_table_field_data_int(result, &result_thread_id, col_index++);
13010 _get_table_field_data_time_t(result, &latest_date_time, col_index++);
13011 _get_table_field_data_string(result, &p_subject, 0, col_index++);
13013 if (temp_thread_id == result_thread_id)
13016 temp_thread_id = result_thread_id;
13018 if (em_find_pos_stripped_subject_for_thread_view(p_subject,
13020 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13021 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13022 err_code = EMAIL_ERROR_UNKNOWN;
13023 result_thread_id = -1;
13027 if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
13028 if (latest_date_time < mail_tbl->date_time)
13029 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
13031 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
13033 search_thread = true;
13036 if (search_thread) {
13037 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13038 latest_mail_id_in_thread, mail_tbl->mail_id);
13041 result_thread_id = -1;
13050 *thread_id = result_thread_id;
13051 *thread_item_count = count;
13053 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13054 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13055 EM_DEBUG_LOG("err_code : %d", err_code);
13057 EM_SAFE_FREE(sql_account);
13058 EM_SAFE_FREE(sql_query_string);
13059 EM_SAFE_FREE(p_subject);
13061 sqlite3_free_table(result);
13063 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13068 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)
13070 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);
13071 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13074 int query_size = 0;
13075 int query_size_account = 0;
13076 int err_code = EMAIL_ERROR_NONE;
13078 int result_thread_id = -1;
13079 char *sql_query_string = NULL;
13080 char *sql_account = NULL;
13081 char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13082 char *sql_format_account = " AND account_id = %d ";
13083 char *sql_format_order_by = " ORDER BY date_time DESC ";
13084 char **result = NULL;
13085 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13086 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13088 EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13089 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13090 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13092 EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13094 if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13095 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13096 err_code = EMAIL_ERROR_UNKNOWN;
13097 result_thread_id = -1;
13101 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13102 result_thread_id = -1;
13106 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13108 if (account_id > 0) {
13109 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13110 sql_account = malloc(query_size_account);
13111 if (sql_account == NULL) {
13112 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
13113 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13116 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13119 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; */
13120 sql_query_string = malloc(query_size);
13122 if (sql_query_string == NULL) {
13123 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
13124 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13128 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13130 if (account_id > 0)
13131 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13132 EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13133 EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13135 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13137 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13139 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13140 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13142 EM_DEBUG_LOG("Result rows count : %d", count);
13145 result_thread_id = -1;
13147 _get_table_field_data_int(result, &result_thread_id, 1);
13150 *thread_id = result_thread_id;
13151 *thread_item_count = count;
13153 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13154 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13155 EM_DEBUG_LOG("err_code : %d", err_code);
13157 EM_SAFE_FREE(sql_account);
13158 EM_SAFE_FREE(sql_query_string);
13160 sqlite3_free_table(result);
13162 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13167 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)
13169 EM_DEBUG_FUNC_BEGIN();
13171 int count = 0, ret = false;
13172 int error = EMAIL_ERROR_NONE;
13173 emstorage_mail_tbl_t *p_data_tbl = NULL;
13174 char conditional_clause[QUERY_SIZE] = {0, };
13176 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13178 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13179 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13181 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13182 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13187 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13193 *mail_tbl = p_data_tbl;
13194 else if (p_data_tbl != NULL)
13195 emstorage_free_mail(&p_data_tbl, 1, NULL);
13197 if (err_code != NULL)
13200 EM_DEBUG_FUNC_END("ret [%d]", ret);
13204 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)
13206 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]"
13207 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13209 if ((!sender_list) || (!sender_count)) {
13210 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13211 if (err_code != NULL)
13212 *err_code = EMAIL_ERROR_INVALID_PARAM;
13216 int rc = -1, ret = false;
13217 int error = EMAIL_ERROR_NONE;
13219 int i, col_index = 0;
13220 int read_count = 0;
13221 email_sender_list_t *p_sender_list = NULL;
13222 char sql_query_string[QUERY_SIZE] = {0, };
13223 char **result = NULL;
13224 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13226 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13227 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13232 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);
13233 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13234 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) ");
13237 /* '0' (ALL_ACCOUNT) means all account */
13238 if (account_id > ALL_ACCOUNT)
13239 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);
13241 if (search_value) {
13242 switch (search_type) {
13243 case EMAIL_SEARCH_FILTER_SUBJECT:
13244 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13245 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13247 case EMAIL_SEARCH_FILTER_SENDER:
13248 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13249 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13250 ") ", search_value);
13252 case EMAIL_SEARCH_FILTER_RECIPIENT:
13253 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13254 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13255 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13256 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13257 ") ", search_value, search_value, search_value);
13259 case EMAIL_SEARCH_FILTER_ALL:
13260 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13261 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13262 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13263 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13264 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13265 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13268 ")", search_value, search_value, search_value, search_value, search_value);
13274 /* sorting option is not available now. The order of sender list is ascending order by display name */
13275 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13276 "GROUP BY email_address_sender "
13277 "ORDER BY UPPER(alias_sender) ");
13279 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13281 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13282 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13283 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13285 EM_DEBUG_LOG("Count of Sender [%d]", count);
13287 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13288 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13289 error = EMAIL_ERROR_OUT_OF_MEMORY;
13295 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13296 for (i = 0; i < count; i++) {
13297 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13298 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13299 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13300 _get_table_field_data_int(result, &(read_count), col_index++);
13301 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
13303 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13305 sqlite3_free_table(result);
13312 *sender_list = p_sender_list;
13313 *sender_count = count;
13314 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13318 if (err_code != NULL)
13321 EM_DEBUG_FUNC_END("ret [%d]", ret);
13325 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13327 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13329 int err = EMAIL_ERROR_NONE;
13332 if (!sender_list || !*sender_list) {
13333 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13334 err = EMAIL_ERROR_INVALID_PARAM;
13338 email_sender_list_t* p = *sender_list;
13341 for (; i < count; i++) {
13342 EM_SAFE_FREE(p[i].address);
13343 EM_SAFE_FREE(p[i].display_name);
13347 *sender_list = NULL;
13354 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13356 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13358 int err = EMAIL_ERROR_NONE;
13359 email_address_info_t *p_address_info = NULL;
13360 GList *list = NULL;
13361 GList *node = NULL;
13364 if (!address_info_list || !*address_info_list) {
13365 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13366 err = EMAIL_ERROR_INVALID_PARAM;
13370 /* delete GLists */
13371 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13373 case EMAIL_ADDRESS_TYPE_FROM:
13374 list = (*address_info_list)->from;
13376 case EMAIL_ADDRESS_TYPE_TO:
13377 list = (*address_info_list)->to;
13379 case EMAIL_ADDRESS_TYPE_CC:
13380 list = (*address_info_list)->cc;
13382 case EMAIL_ADDRESS_TYPE_BCC:
13383 list = (*address_info_list)->bcc;
13387 /* delete dynamic-allocated memory for each item */
13388 node = g_list_first(list);
13389 while (node != NULL) {
13390 p_address_info = (email_address_info_t*)node->data;
13391 EM_SAFE_FREE(p_address_info->address);
13392 EM_SAFE_FREE(p_address_info->display_name);
13393 EM_SAFE_FREE(node->data);
13395 node = g_list_next(node);
13400 EM_SAFE_FREE(*address_info_list);
13401 *address_info_list = NULL;
13403 EM_DEBUG_FUNC_END("err [%d]", err);
13406 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13408 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)
13410 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13412 if (!local_activity || !activity_id) {
13413 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, transaction, activity_id, err_code);
13414 if (err_code != NULL)
13415 *err_code = EMAIL_ERROR_INVALID_PARAM;
13421 int error = EMAIL_ERROR_NONE;
13424 char sql_query_string[QUERY_SIZE] = {0, };
13425 DB_STMT hStmt = NULL;
13426 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13428 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13429 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13430 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13431 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13433 "? " /* Account ID */
13434 ",?" /* Local Mail ID */
13435 ",?" /* Server mail ID */
13436 ",?" /* Activity ID */
13437 ",?" /* Activity type*/
13438 ",?" /* Mailbox ID*/
13439 ",?" /* Mailbox name*/
13440 ",?" /* Multi User Name */
13443 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13444 char **result = NULL;
13447 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13449 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13450 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13452 if (NULL == result[1]) rc = 1;
13453 else rc = atoi(result[1])+1;
13454 sqlite3_free_table(result);
13457 *activity_id = local_activity->activity_id = rc;
13459 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13460 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13462 if (local_activity->mailbox_id)
13463 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13466 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13467 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13468 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13471 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13472 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13473 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13474 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13475 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13476 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13477 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13478 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13481 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13483 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13484 ("sqlite3_step fail:%d", rc));
13485 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13486 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13491 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13492 if (hStmt != NULL) {
13493 rc = sqlite3_finalize(hStmt);
13495 if (rc != SQLITE_OK) {
13496 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13497 error = EMAIL_ERROR_DB_FAILURE;
13501 if (err_code != NULL)
13504 EM_DEBUG_FUNC_END("ret [%d]", ret);
13508 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)
13510 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13512 if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13513 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13514 if (err_code != NULL)
13515 *err_code = EMAIL_ERROR_INVALID_PARAM;
13520 int error = EMAIL_ERROR_NONE;
13522 int i = 0, rc = -1;
13523 int *mbox_list = NULL;
13524 DB_STMT hStmt = NULL;
13525 char sql_query_string[QUERY_SIZE] = {0, };
13527 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13528 EMSTORAGE_START_READ_TRANSACTION(transaction);
13530 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13531 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);
13534 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13535 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13536 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13538 *count = atoi(result[1]);
13539 sqlite3_free_table(result);
13542 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13543 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13547 EM_DEBUG_LOG("Mailbox count = %d", *count);
13549 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13551 /* 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); */
13552 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);
13554 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13557 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13560 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13561 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13564 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13565 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13566 ("sqlite3_step fail:%d", rc));
13568 mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13569 if (NULL == mbox_list) {
13570 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13571 error = EMAIL_ERROR_OUT_OF_MEMORY;
13575 memset(mbox_list, 0x00, sizeof(int) * (*count));
13577 for (i = 0; i < (*count); i++) {
13578 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13579 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13580 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13581 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13582 ("sqlite3_step fail:%d", rc));
13583 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13590 *mailbox_list = mbox_list;
13592 EM_SAFE_FREE(mbox_list);
13594 if (hStmt != NULL) {
13595 rc = sqlite3_finalize(hStmt);
13597 if (rc != SQLITE_OK) {
13598 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13599 error = EMAIL_ERROR_DB_FAILURE;
13603 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13604 if (err_code != NULL)
13606 EM_DEBUG_FUNC_END("ret [%d]", ret);
13610 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13612 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13614 if (NULL == account_list || NULL == count) {
13615 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13616 if (err_code != NULL)
13617 *err_code = EMAIL_ERROR_INVALID_PARAM;
13622 int error = EMAIL_ERROR_NONE;
13623 char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13625 int i = 0, rc = -1;
13626 int *result_account_list = NULL;
13627 DB_STMT hStmt = NULL;
13628 char sql_query_string[QUERY_SIZE] = {0, };
13630 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13632 EMSTORAGE_START_READ_TRANSACTION(transaction);
13634 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13635 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13636 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13638 *count = atoi(result[1]);
13639 sqlite3_free_table(result);
13642 EM_DEBUG_EXCEPTION("no account found...");
13643 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13648 EM_DEBUG_LOG("Account count [%d]", *count);
13650 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13652 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13654 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13657 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13659 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13660 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13661 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13664 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13665 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13666 ("sqlite3_step fail:%d", rc));
13668 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13669 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13670 error = EMAIL_ERROR_OUT_OF_MEMORY;
13674 memset(result_account_list, 0x00, sizeof(int) * (*count));
13676 for (i = 0; i < (*count); i++) {
13677 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13679 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13680 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13681 ("sqlite3_step fail:%d", rc));
13682 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13689 *account_list = result_account_list;
13691 EM_SAFE_FREE(result_account_list);
13693 if (hStmt != NULL) {
13694 rc = sqlite3_finalize(hStmt);
13696 if (rc != SQLITE_OK) {
13697 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13698 error = EMAIL_ERROR_DB_FAILURE;
13702 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13703 if (err_code != NULL)
13705 EM_DEBUG_FUNC_END("ret [%d]", ret);
13709 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)
13711 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13713 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13714 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);
13716 if (err_code != NULL)
13717 *err_code = EMAIL_ERROR_INVALID_PARAM;
13724 int error = EMAIL_ERROR_NONE;
13726 DB_STMT hStmt = NULL;
13727 email_event_partial_body_thd* event_list = NULL;
13728 char sql_query_string[QUERY_SIZE] = {0, };
13730 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13732 EMSTORAGE_START_READ_TRANSACTION(transaction);
13734 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13735 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);
13738 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13739 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13740 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13742 *count = atoi(result[1]);
13743 sqlite3_free_table(result);
13745 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13748 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13749 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13753 EM_DEBUG_LOG("Activity Count = %d", *count);
13755 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13756 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);
13758 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13761 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13763 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13764 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13765 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13768 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13769 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13770 ("sqlite3_step fail:%d", rc));
13772 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13773 EM_DEBUG_EXCEPTION("Malloc failed");
13775 error = EMAIL_ERROR_OUT_OF_MEMORY;
13778 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13780 for (i = 0; i < (*count); i++) {
13781 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13782 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13783 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13784 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13785 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13786 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13787 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13788 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13790 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13791 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13792 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13793 ("sqlite3_step fail:%d", rc));
13795 event_list[i].event_type = 0;
13802 *event_start = event_list;
13804 for (i = 0; i < (*count); i++)
13805 emcore_free_partial_body_thd_event(event_list, NULL);
13806 EM_SAFE_FREE(event_list); /*prevent 54559*/
13807 *event_start = NULL;
13811 if (hStmt != NULL) {
13812 rc = sqlite3_finalize(hStmt);
13814 if (rc != SQLITE_OK) {
13815 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13816 error = EMAIL_ERROR_DB_FAILURE;
13820 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13821 if (err_code != NULL)
13824 EM_DEBUG_FUNC_END("ret [%d]", ret);
13828 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)
13830 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);
13833 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13834 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);
13836 if (err_code != NULL)
13837 *err_code = EMAIL_ERROR_INVALID_PARAM;
13843 int error = EMAIL_ERROR_NONE;
13844 char sql_query_string[QUERY_SIZE] = {0, };
13845 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13847 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13848 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13850 if (activity_id == 0)
13851 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);
13853 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);
13855 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13856 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13857 if (error != EMAIL_ERROR_NONE) {
13858 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13862 /* validate activity existence */
13863 rc = sqlite3_changes(local_db_handle);
13865 EM_DEBUG_EXCEPTION("No matching activity found");
13866 error = EMAIL_ERROR_DATA_NOT_FOUND;
13874 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13876 if (err_code != NULL)
13879 EM_DEBUG_FUNC_END("ret [%d]", ret);
13883 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)
13885 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13887 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13888 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13889 if (err_code != NULL)
13890 *err_code = EMAIL_ERROR_INVALID_PARAM;
13895 int error = EMAIL_ERROR_NONE;
13896 char sql_query_string[QUERY_SIZE] = {0, };
13898 DB_STMT hStmt = NULL;
13900 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13902 EMSTORAGE_START_READ_TRANSACTION(transaction);
13903 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13905 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);
13907 EM_DEBUG_LOG_SEC(" 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("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)));
13916 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13917 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13918 ("sqlite3_step fail:%d", rc));
13920 _get_stmt_field_data_int(hStmt, activity_count, 0);
13922 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);
13939 if (err_code != NULL)
13942 EM_DEBUG_FUNC_END("ret [%d]", ret);
13946 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13948 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13950 if (NULL == activity_count || NULL == err_code) {
13951 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13952 if (err_code != NULL)
13953 *err_code = EMAIL_ERROR_INVALID_PARAM;
13958 int error = EMAIL_ERROR_NONE;
13959 DB_STMT hStmt = NULL;
13960 char sql_query_string[QUERY_SIZE] = {0, };
13962 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13964 EMSTORAGE_START_READ_TRANSACTION(transaction);
13965 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13967 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13969 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13971 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13972 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
13973 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13974 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13976 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13977 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13978 ("sqlite3_step fail:%d", rc));
13980 _get_stmt_field_data_int(hStmt, activity_count, 0);
13982 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13989 if (hStmt != NULL) {
13990 rc = sqlite3_finalize(hStmt);
13992 if (rc != SQLITE_OK) {
13993 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13994 error = EMAIL_ERROR_DB_FAILURE;
13998 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13999 if (err_code != NULL)
14002 EM_DEBUG_FUNC_END("ret [%d]", ret);
14006 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
14008 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14009 if (account_id < FIRST_ACCOUNT_ID) {
14010 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14011 if (err_code != NULL)
14012 *err_code = EMAIL_ERROR_INVALID_PARAM;
14018 int error = EMAIL_ERROR_NONE;
14019 char sql_query_string[QUERY_SIZE] = {0, };
14021 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14023 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14024 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14025 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
14027 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14028 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14029 if (error != EMAIL_ERROR_NONE) {
14030 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14034 rc = sqlite3_changes(local_db_handle);
14036 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14037 error = EMAIL_ERROR_DATA_NOT_FOUND;
14046 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14048 if (err_code != NULL)
14051 EM_DEBUG_FUNC_END("ret [%d]", ret);
14055 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14056 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*/
14058 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)
14060 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14062 int rc = -1, ret = false;
14063 int error = EMAIL_ERROR_NONE;
14064 char sql_query_string[QUERY_SIZE] = {0, };
14066 int transaction = true;
14068 if (!old_server_uid || !new_server_uid || !mbox_name) {
14069 EM_DEBUG_EXCEPTION("Invalid parameters");
14070 error = EMAIL_ERROR_INVALID_PARAM;
14074 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14076 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14077 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14078 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14079 "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);
14081 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14082 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14083 if (error != EMAIL_ERROR_NONE) {
14084 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14088 rc = sqlite3_changes(local_db_handle);
14090 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14095 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14097 if (err_code != NULL)
14100 EM_DEBUG_FUNC_END("ret [%d]", ret);
14105 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14107 EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14110 int error = EMAIL_ERROR_NONE;
14111 FILE* fp_dst = NULL;
14112 char errno_buf[ERRNO_BUF_SIZE] = {0};
14114 if (!data_string || !dst_file_name) {
14115 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14116 error = EMAIL_ERROR_INVALID_PARAM;
14120 error = em_fopen(dst_file_name, "w", &fp_dst);
14121 if (error != EMAIL_ERROR_NONE) {
14122 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14126 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14127 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14128 error = EMAIL_ERROR_UNKNOWN;
14136 if (fp_dst != NULL)
14139 if (err_code != NULL)
14142 EM_DEBUG_FUNC_END("ret [%d]", ret);
14146 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14150 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14151 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)
14153 EM_DEBUG_FUNC_BEGIN();
14156 int error = EMAIL_ERROR_NONE;
14157 char sql_query_string[QUERY_SIZE] = {0, };
14159 int transaction = true;
14161 if (!old_server_uid || !new_server_uid || !mbox_name) {
14162 EM_DEBUG_EXCEPTION("Invalid parameters");
14163 if (err_code != NULL)
14164 *err_code = EMAIL_ERROR_INVALID_PARAM;
14168 EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14170 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14171 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14174 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14175 "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);
14177 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14178 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14179 if (error != EMAIL_ERROR_NONE) {
14180 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14184 rc = sqlite3_changes(local_db_handle);
14186 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14187 error = EMAIL_ERROR_DATA_NOT_FOUND;
14194 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14196 if (err_code != NULL)
14199 EM_DEBUG_FUNC_END("ret [%d]", ret);
14206 * @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);
14207 * Prepare an array of mail_id and corresponding server mail id.
14209 * @author h.gahlaut@samsung.com
14210 * @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)
14211 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14212 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14213 * @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
14214 * @param[out] err_code Returns the error code.
14215 * @remarks An Example of Query to be exexuted in this API:
14216 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14217 * @return This function returns true on success or false on failure.
14219 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14221 email_id_set_t** idset,
14225 EM_DEBUG_FUNC_BEGIN();
14226 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14228 int error = EMAIL_ERROR_NONE;
14230 email_id_set_t* p_id_set = NULL;
14232 const int buf_size = QUERY_SIZE;
14233 char sql_query_string[QUERY_SIZE] = {0, };
14234 int space_left_in_query_buffer = buf_size;
14237 char *server_mail_id = NULL;
14238 char **result = NULL;
14242 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14243 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14244 mail_ids, idset, id_set_count);
14245 if (err_code != NULL)
14246 *err_code = EMAIL_ERROR_INVALID_PARAM;
14250 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14252 SNPRINTF(sql_query_string, space_left_in_query_buffer,
14253 "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14256 EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14258 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14259 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14260 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14261 goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s",
14262 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14264 EM_DEBUG_LOG(" Count of mails [%d ]", count);
14267 EM_DEBUG_EXCEPTION("Can't find proper mail");
14268 error = EMAIL_ERROR_DATA_NOT_FOUND;
14272 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14273 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14274 error = EMAIL_ERROR_OUT_OF_MEMORY;
14280 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14281 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14282 for (i = 0; i < count; i++) {
14283 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14284 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14285 if (server_mail_id) {
14286 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14287 EM_SAFE_FREE(server_mail_id);
14290 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14291 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14293 sqlite3_free_table(result);
14302 *id_set_count = count;
14303 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14305 EM_SAFE_FREE(p_id_set);
14308 if (err_code != NULL)
14311 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14313 EM_DEBUG_FUNC_END("ret [%d]", ret);
14321 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14323 EM_DEBUG_FUNC_BEGIN();
14324 int ret = true, transaction = true;
14325 int error = EMAIL_ERROR_NONE;
14326 char sql_query_string[QUERY_SIZE] = {0, };
14327 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14328 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14330 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14331 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14332 if (error != EMAIL_ERROR_NONE) {
14333 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14337 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14338 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14339 if (error != EMAIL_ERROR_NONE) {
14340 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14344 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14345 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14346 if (error != EMAIL_ERROR_NONE) {
14347 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14354 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14356 EM_DEBUG_FUNC_END("ret [%d]", ret);
14360 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14362 EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14364 int error = EMAIL_ERROR_NONE;
14365 char sql_query_string[QUERY_SIZE] = {0, };
14366 int transaction = true;
14368 if (old_filter_id < 0 || new_filter_id < 0) {
14369 EM_DEBUG_EXCEPTION("Invalid parameters");
14370 if (err_code != NULL)
14371 *err_code = EMAIL_ERROR_INVALID_PARAM;
14375 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14377 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14379 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14380 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14382 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14383 if (error != EMAIL_ERROR_NONE) {
14384 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14391 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14393 if (err_code != NULL)
14396 EM_DEBUG_FUNC_END("ret [%d]", ret);
14400 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)
14402 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);
14404 if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14405 EM_DEBUG_EXCEPTION("Invalid Parameter");
14407 if (err_code != NULL)
14408 *err_code = EMAIL_ERROR_INVALID_PARAM;
14412 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14413 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14414 int tag_id = rule->rule_id;
14415 char **result = NULL, *where_pararaph = NULL;
14416 char sql_query_string[QUERY_SIZE] = {0, };
14417 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14419 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14421 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14422 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14423 if (where_pararaph == NULL) {
14424 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14425 error = EMAIL_ERROR_OUT_OF_MEMORY;
14429 if (account_id != ALL_ACCOUNT)
14430 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14432 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14434 if (rule->type & EMAIL_FILTER_SUBJECT) {
14435 if (rule->flag2 == RULE_TYPE_INCLUDES)
14436 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14437 else /* RULE_TYPE_EXACTLY */
14438 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14441 if (rule->type & EMAIL_FILTER_FROM) {
14442 if (rule->flag2 == RULE_TYPE_INCLUDES)
14443 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);
14444 #ifdef __FEATURE_COMPARE_DOMAIN__
14445 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14446 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);
14447 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14448 else /* RULE_TYPE_EXACTLY */
14449 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);
14452 if (rule->type == EMAIL_PRIORITY_SENDER) {
14453 if (rule->flag2 == RULE_TYPE_INCLUDES)
14454 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);
14455 else /* RULE_TYPE_EXACTLY */
14456 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);
14458 tag_id = PRIORITY_SENDER_TAG_ID;
14461 /* prevent 34361 */
14462 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14463 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14464 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14466 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14467 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14468 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14469 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14471 EM_DEBUG_LOG("Count of mails [%d]", count);
14474 mail_list = malloc(sizeof(int) * count);
14475 if (mail_list == NULL) {
14476 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14477 error = EMAIL_ERROR_OUT_OF_MEMORY;
14483 for (i = 0; i < count; i++)
14484 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14486 memset(sql_query_string, 0x00, QUERY_SIZE);
14488 switch (rule->action_type) {
14489 case EMAIL_FILTER_MOVE:
14490 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14492 case EMAIL_FILTER_BLOCK:
14494 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14499 switch (rule->action_type) {
14500 case EMAIL_FILTER_MOVE:
14501 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14503 case EMAIL_FILTER_BLOCK:
14504 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14507 EM_DEBUG_LOG("Not support");
14512 /* prevent 34361 */
14513 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14514 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14516 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14517 if (error != EMAIL_ERROR_NONE) {
14518 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14522 #ifdef __FEATURE_BODY_SEARCH__
14523 /* Updating mail_text_tbl */
14524 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14525 memset(sql_query_string, 0x00, QUERY_SIZE);
14526 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14527 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14528 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14530 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14531 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14532 if (error != EMAIL_ERROR_NONE) {
14533 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14545 if (filtered_mail_id_list)
14546 *filtered_mail_id_list = mail_list;
14548 if (count_of_mails)
14549 *count_of_mails = count;
14551 EM_SAFE_FREE(mail_list);
14553 sqlite3_free_table(result);
14557 EM_SAFE_FREE(where_pararaph);
14559 if (err_code != NULL)
14562 EM_DEBUG_FUNC_END("ret [%d]", ret);
14566 #define EMAIL_SLOT_UNIT 25
14567 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)
14569 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);
14570 int ret = false, err = EMAIL_ERROR_NONE;
14571 int where_pararaph_length = 0;
14572 char *where_pararaph = NULL;
14573 char sql_query_string[QUERY_SIZE] = {0, };
14575 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14577 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14579 if (new_slot_size > 0)
14580 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14581 else if (new_slot_size == 0)
14582 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14584 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14588 where_pararaph_length = 80;
14590 where_pararaph_length = 50;
14592 if (new_slot_size == 0)
14593 where_pararaph_length += 70;
14595 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14596 if (where_pararaph == NULL) {
14597 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14598 err = EMAIL_ERROR_OUT_OF_MEMORY;
14601 memset(where_pararaph, 0x00, where_pararaph_length);
14603 if (account_id > ALL_ACCOUNT) {
14606 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14608 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14611 if (new_slot_size == 0)
14612 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"));
14614 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14615 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14617 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14618 err = EMAIL_ERROR_OUT_OF_MEMORY;
14622 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14623 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14624 if (err != EMAIL_ERROR_NONE) {
14625 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14632 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14634 EM_SAFE_FREE(where_pararaph);
14636 if (err_code != NULL)
14639 EM_DEBUG_FUNC_END("ret [%d]", ret);
14643 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)
14645 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);
14647 if (!meeting_req || meeting_req->mail_id <= 0) {
14649 EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
14651 if (err_code != NULL)
14652 *err_code = EMAIL_ERROR_INVALID_PARAM;
14659 int error = EMAIL_ERROR_NONE;
14660 DB_STMT hStmt = NULL;
14661 char sql_query_string[QUERY_SIZE] = {0, };
14663 time_t temp_unix_time = 0;
14665 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14666 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14668 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14669 "INSERT INTO mail_meeting_tbl VALUES "
14671 ", ?" /* account_id */
14672 ", ?" /* mailbox_id */
14673 ", ?" /* meeting_response */
14674 ", ?" /* start_time */
14675 ", ?" /* end_time */
14676 ", ?" /* location */
14677 ", ?" /* global_object_id */
14679 ", ?" /* standard_name */
14680 ", ?" /* standard_time_start_date */
14681 ", ?" /* standard_biad */
14682 ", ?" /* daylight_name */
14683 ", ?" /* daylight_time_start_date */
14684 ", ?" /* daylight_bias */
14687 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14688 if (rc != SQLITE_OK) {
14689 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14690 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14692 error = EMAIL_ERROR_DB_FAILURE;
14698 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14699 EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14700 EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14701 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14702 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14703 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14704 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14705 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14706 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14707 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14708 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14709 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14710 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14711 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14712 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14714 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14715 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14716 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14717 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14719 temp_unix_time = timegm(&(meeting_req->start_time));
14720 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14721 temp_unix_time = timegm(&(meeting_req->end_time));
14722 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14724 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14725 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14727 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14728 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14729 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14730 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14731 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14733 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14734 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14735 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14736 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14739 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14740 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14741 ("sqlite3_step fail:%d", rc));
14742 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14743 ("sqlite3_step fail:%d", rc));
14748 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14749 if (hStmt != NULL) {
14750 rc = sqlite3_finalize(hStmt);
14751 if (rc != SQLITE_OK) {
14752 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14753 error = EMAIL_ERROR_DB_FAILURE;
14757 if (err_code != NULL)
14760 EM_DEBUG_FUNC_END("ret [%d]", ret);
14764 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)
14766 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);
14773 int err = EMAIL_ERROR_NONE;
14774 char **result = NULL;
14775 char sql_query_string[QUERY_SIZE] = {0, };
14776 email_meeting_request_t* p_temp_meeting_req = NULL;
14777 sqlite3 *local_db_handle = NULL;
14778 time_t temp_unix_time;
14780 if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14781 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14782 err = EMAIL_ERROR_INVALID_PARAM;
14786 col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14787 EM_DEBUG_LOG("col_index [%d]", col_index);
14789 local_db_handle = emstorage_get_db_connection(multi_user_name);
14791 EMSTORAGE_START_READ_TRANSACTION(transaction);
14793 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14795 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14797 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14798 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14799 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14802 EM_DEBUG_EXCEPTION("No meeting_request found...");
14803 err = EMAIL_ERROR_DATA_NOT_FOUND;
14807 EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14808 if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14809 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14810 err = EMAIL_ERROR_OUT_OF_MEMORY;
14814 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14816 for (i = 0; i < count; i++) {
14817 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14818 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14819 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14820 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14821 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14822 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14823 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14824 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14825 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14826 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14827 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14828 _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++);
14829 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14830 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14831 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14832 _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++);
14833 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14834 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14835 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14841 sqlite3_free_table(result);
14843 if (err == EMAIL_ERROR_NONE) {
14844 if (p_temp_meeting_req)
14845 *output_meeting_req = p_temp_meeting_req;
14846 *output_result_count = count;
14848 EM_SAFE_FREE(p_temp_meeting_req);
14850 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14852 // sqlite3_db_release_memory(local_db_handle);
14854 EM_DEBUG_FUNC_END("err [%d]", err);
14858 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)
14860 EM_DEBUG_FUNC_BEGIN();
14864 int error = EMAIL_ERROR_NONE;
14865 char conditional_clause[QUERY_SIZE] = {0, };
14867 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14870 SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14871 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14873 if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14874 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14878 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14879 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14880 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14881 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14882 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14883 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14884 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14885 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14886 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14887 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14888 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14889 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14890 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14895 if (err_code != NULL)
14898 EM_DEBUG_FUNC_END("ret [%d]", ret);
14902 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14904 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14907 int error = EMAIL_ERROR_NONE;
14909 DB_STMT hStmt = NULL;
14910 char sql_query_string[QUERY_SIZE] = {0, };
14911 time_t temp_unix_time = 0;
14913 if (!meeting_req) {
14914 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14915 if (err_code != NULL)
14916 *err_code = EMAIL_ERROR_INVALID_PARAM;
14920 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14921 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14923 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14924 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14925 "UPDATE mail_meeting_tbl "
14927 /* " account_id = ?, " // not update here, this can be changed when move or copy */
14928 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
14929 " meeting_response = ?, "
14930 " start_time = ?, "
14933 " global_object_id = ?, "
14935 " standard_name = ?, "
14936 " standard_time_start_date = ?, "
14937 " standard_bias = ?, "
14938 " daylight_name = ?, "
14939 " daylight_time_start_date = ?, "
14940 " daylight_bias = ? "
14941 "WHERE mail_id = %d",
14942 meeting_req->mail_id);
14944 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14946 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14947 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14948 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14950 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14951 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14952 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14953 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14954 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14955 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14956 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14957 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14958 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14959 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14960 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14961 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14962 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14966 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14967 temp_unix_time = timegm(&(meeting_req->start_time));
14968 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14969 temp_unix_time = timegm(&(meeting_req->end_time));
14970 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14971 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14972 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14973 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14974 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14975 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14976 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14977 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14978 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14979 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14980 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14981 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14984 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14985 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14986 ("sqlite3_step fail:%d", rc));
14991 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14993 if (hStmt != NULL) {
14994 rc = sqlite3_finalize(hStmt);
14995 if (rc != SQLITE_OK) {
14996 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14997 error = EMAIL_ERROR_DB_FAILURE;
15001 if (err_code != NULL)
15004 EM_DEBUG_FUNC_END("ret [%d]", ret);
15008 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)
15010 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);
15012 if (account_id < ALL_ACCOUNT || mail_id < 0) {
15013 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
15015 if (err_code != NULL)
15016 *err_code = EMAIL_ERROR_INVALID_PARAM;
15021 int error = EMAIL_ERROR_NONE;
15023 char sql_query_string[QUERY_SIZE] = {0, };
15025 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15026 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15028 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15030 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
15031 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);
15035 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);
15038 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
15039 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);
15042 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15043 if (error != EMAIL_ERROR_NONE) {
15044 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15051 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15056 EM_DEBUG_FUNC_END("ret [%d]", ret);
15061 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15063 EM_DEBUG_FUNC_BEGIN();
15065 if (!meeting_req) return;
15067 EM_SAFE_FREE(meeting_req->location);
15068 EM_SAFE_FREE(meeting_req->global_object_id);
15070 EM_DEBUG_FUNC_END();
15073 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)
15075 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);
15076 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15077 char sql_query_string[QUERY_SIZE] = {0, };
15078 char **result = NULL;
15079 int rc = -1, ret = false;
15080 int error = EMAIL_ERROR_NONE;
15081 int counter = 0, col_index = 0;
15082 int result_mail_id_count = 0;
15083 int *result_mail_id_list = NULL;
15085 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15086 EM_DEBUG_EXCEPTION("Invalid Parameter");
15088 *err_code = EMAIL_ERROR_INVALID_PARAM;
15092 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);
15094 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15096 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15097 EMSTORAGE_START_READ_TRANSACTION(transaction);
15099 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15100 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15101 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15103 if (!result_mail_id_count) {
15104 EM_DEBUG_LOG("No mail found...");
15106 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15110 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15112 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15113 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15114 error = EMAIL_ERROR_OUT_OF_MEMORY;
15115 sqlite3_free_table(result);
15119 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15123 for (counter = 0; counter < result_mail_id_count; counter++)
15124 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15129 EM_DEBUG_LOG("finish off [%d]", ret);
15132 sqlite3_free_table(result);
15135 *mail_id_list = result_mail_id_list;
15136 *mail_id_count = result_mail_id_count;
15138 EM_SAFE_FREE(result_mail_id_list);
15140 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15142 if (err_code != NULL)
15145 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15146 EM_DEBUG_FUNC_END("ret [%d]", ret);
15150 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15152 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15154 int rc = -1, ret = false;
15155 int err = EMAIL_ERROR_NONE;
15156 char sql_query_string[QUERY_SIZE] = {0, };
15158 int result_count = 0;
15160 if (mail_id == 0 || thread_id == NULL) {
15161 EM_DEBUG_EXCEPTION("Invalid Parameter");
15163 *err_code = EMAIL_ERROR_INVALID_PARAM;
15167 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15169 memset(sql_query_string, 0, QUERY_SIZE);
15170 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15172 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15173 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15174 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15175 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15177 if (!result_count) {
15178 EM_DEBUG_LOG("No mail found...");
15180 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15181 /* sqlite3_free_table(result); */
15185 _get_table_field_data_int(result, thread_id, 1);
15187 sqlite3_free_table(result);
15193 if (err_code != NULL)
15196 EM_DEBUG_FUNC_END("ret [%d]", ret);
15200 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15205 int *updated_thread_id,
15206 int latest_mail_id,
15207 int thread_item_count,
15212 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15213 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15214 account_id, mailbox_id, thread_id, updated_thread_id,
15215 latest_mail_id, thread_item_count, err_code);
15217 int rc = -1, ret = false;
15218 int err = EMAIL_ERROR_NONE;
15219 char sql_query_string[QUERY_SIZE] = {0, };
15220 char **result = NULL;
15221 int result_count = 0;
15223 if (thread_id == 0) {
15224 EM_DEBUG_EXCEPTION("Invalid Parameter");
15226 *err_code = EMAIL_ERROR_INVALID_PARAM;
15230 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15232 if (thread_item_count == 0 && latest_mail_id == 0) {
15233 memset(sql_query_string, 0, QUERY_SIZE);
15234 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);
15236 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15237 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15238 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15239 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15240 EM_DEBUG_LOG("result_count[%d]", result_count);
15241 if (result_count == 0) {
15242 EM_DEBUG_LOG("No mail found...");
15245 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND;
15246 sqlite3_free_table(result);
15250 _get_table_field_data_int(result, &latest_mail_id, 2);
15251 _get_table_field_data_int(result, &thread_item_count, 3);
15253 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15254 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15256 sqlite3_free_table(result);
15259 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15261 if (thread_item_count < 0) {
15262 memset(sql_query_string, 0, QUERY_SIZE);
15263 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);
15264 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15265 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15266 if (err != EMAIL_ERROR_NONE) {
15267 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15270 } else if (thread_id != latest_mail_id) {
15271 /* Initialize the thread id */
15272 memset(sql_query_string, 0, QUERY_SIZE);
15273 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);
15274 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15275 if (err != EMAIL_ERROR_NONE) {
15276 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15280 /* update the thread item count */
15281 memset(sql_query_string, 0, QUERY_SIZE);
15282 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);
15283 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15284 if (err != EMAIL_ERROR_NONE) {
15285 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15289 memset(sql_query_string, 0, QUERY_SIZE);
15290 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);
15291 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15292 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15293 if (err != EMAIL_ERROR_NONE) {
15294 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15301 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15303 if (thread_id != latest_mail_id) {
15304 if (err == EMAIL_ERROR_NONE) {
15305 EM_DEBUG_LOG("noti_type[%d]", noti_type);
15307 if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15308 char mailbox_id_str[25] = {0,};
15309 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15310 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15311 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15313 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15316 } else if (thread_item_count >= 0) {
15317 if (err == EMAIL_ERROR_NONE) {
15318 char parameter_string[500] = {0,};
15319 SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15320 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15321 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15325 if (err_code != NULL)
15328 EM_DEBUG_FUNC_END("ret [%d]", ret);
15332 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)
15334 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);
15337 int err = EMAIL_ERROR_NONE;
15338 char sql_query_string[QUERY_SIZE] = {0, };
15340 if (thread_id == 0) {
15341 EM_DEBUG_EXCEPTION("Invalid Parameter");
15343 *err_code = EMAIL_ERROR_INVALID_PARAM;
15347 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15349 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15351 memset(sql_query_string, 0, QUERY_SIZE);
15352 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);
15353 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15354 if (err != EMAIL_ERROR_NONE) {
15355 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15362 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15364 if (err_code != NULL)
15367 EM_DEBUG_FUNC_END("ret [%d]", ret);
15371 #ifdef __FEATURE_LOCAL_ACTIVITY__
15373 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15376 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15378 EM_DEBUG_FUNC_BEGIN();
15380 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15382 int rc = -1, ret = false;
15383 int error = EMAIL_ERROR_NONE;
15384 DB_STMT hStmt = NULL;
15385 char sql_query_string[8192] = { 0x00, };
15388 if (!local_activity) {
15389 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15390 if (err_code != NULL)
15391 *err_code = EMAIL_ERROR_INVALID_PARAM;
15395 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15397 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15399 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15400 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15402 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15403 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15404 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15405 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15406 EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15407 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15408 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
15410 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15413 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15414 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15415 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15417 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15419 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15420 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15421 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15422 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15423 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15424 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15425 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15428 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15430 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15431 ("sqlite3_step fail:%d", rc));
15432 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15433 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15438 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15440 if (hStmt != NULL) {
15441 rc = sqlite3_finalize(hStmt);
15442 if (rc != SQLITE_OK) {
15443 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15444 error = EMAIL_ERROR_DB_FAILURE;
15448 if (err_code != NULL)
15451 EM_DEBUG_FUNC_END("ret [%d]", ret);
15456 * emstorage_get_activity - Get the Local activity Information
15460 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)
15462 EM_DEBUG_FUNC_BEGIN();
15464 int i = 0, count = 0, rc = -1, ret = false;
15465 int error = EMAIL_ERROR_NONE;
15466 emstorage_activity_tbl_t *p_activity_tbl = NULL;
15467 char sql_query_string[1024] = {0x00, };
15468 char **result = NULL;
15471 EM_IF_NULL_RETURN_VALUE(activity_list, false);
15472 EM_IF_NULL_RETURN_VALUE(select_num, false);
15475 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15476 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15478 *err_code = EMAIL_ERROR_INVALID_PARAM;
15482 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15484 EMSTORAGE_START_READ_TRANSACTION(transaction);
15486 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15488 if (activityid == ALL_ACTIVITIES) {
15489 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15490 "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15492 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15493 "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15496 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15500 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15501 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15502 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15506 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15507 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15508 error = EMAIL_ERROR_OUT_OF_MEMORY;
15513 for (i = 0; i < count; i++) {
15514 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15515 if (result[col_index])
15516 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15518 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15519 if (result[col_index])
15520 p_activity_tbl[i].account_id = atoi(result[col_index++]);
15522 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15523 if (result[col_index])
15524 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15526 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15527 if (result[col_index])
15528 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15531 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15532 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15533 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15537 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15538 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15539 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15543 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15544 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15545 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15552 sqlite3_free_table(result);
15558 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15561 *activity_list = p_activity_tbl;
15562 *select_num = count;
15563 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15564 } else if (p_activity_tbl != NULL) {
15565 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15569 if (err_code != NULL)
15572 EM_DEBUG_FUNC_END("ret [%d]", ret);
15577 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15580 EM_DEBUG_FUNC_BEGIN();
15583 int err = EMAIL_ERROR_NONE;
15586 char **result = NULL;
15588 if (NULL == activity_id) {
15589 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15591 *err_code = EMAIL_ERROR_INVALID_PARAM;
15595 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15597 /* increase unique id */
15599 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15601 /* 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); */
15602 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15603 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15605 if (NULL == result[1])
15608 rc = atoi(result[1])+1;
15613 sqlite3_free_table(result);
15622 EM_DEBUG_FUNC_END("ret [%d]", ret);
15627 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)
15630 EM_DEBUG_FUNC_BEGIN();
15632 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);
15634 if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15635 if (err_code != NULL)
15636 *err_code = EMAIL_ERROR_INVALID_PARAM;
15641 int error = EMAIL_ERROR_NONE;
15642 int i = 0, rc = -1, count = 0;
15643 char sql_query_string[1024] = {0x00, };
15644 int *activity_ids = NULL;
15646 char **result = NULL;
15647 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15648 EMSTORAGE_START_READ_TRANSACTION(transaction);
15650 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15652 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);
15654 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15657 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15658 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15659 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15663 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15665 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15666 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15667 error = EMAIL_ERROR_OUT_OF_MEMORY;
15671 for (i = 0; i < count; i++) {
15672 activity_ids[i] = atoi(result[col_index]);
15674 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15683 *activity_id_count = count;
15684 *activity_id_list = activity_ids;
15685 } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15686 EM_SAFE_FREE(activity_ids);
15689 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15690 if (err_code != NULL)
15693 EM_DEBUG_FUNC_END("ret [%d]", ret);
15697 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15699 EM_DEBUG_FUNC_BEGIN();
15701 int error = EMAIL_ERROR_NONE;
15704 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15706 if (NULL == activity_id_list) {
15707 error = EMAIL_ERROR_INVALID_PARAM;
15710 EM_SAFE_FREE(activity_id_list);
15718 if (NULL != error_code)
15719 *error_code = error;
15721 EM_DEBUG_FUNC_END("ret [%d]", ret);
15726 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15727 * or based on server mail id
15730 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15732 EM_DEBUG_FUNC_BEGIN();
15735 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15737 if (!local_activity) {
15738 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15739 if (err_code != NULL)
15740 *err_code = EMAIL_ERROR_INVALID_PARAM;
15744 int rc = -1, ret = false; /* Prevent_FIX */
15745 int err = EMAIL_ERROR_NONE;
15747 int query_where = 0;
15748 char sql_query_string[8192] = { 0x00, };
15749 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15750 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15752 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15754 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15756 EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15758 if (local_activity->account_id) {
15759 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15760 " WHERE account_id = %d ", local_activity->account_id);
15765 if (local_activity->server_mailid) {
15766 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15767 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15773 if (local_activity->mail_id) {
15774 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15776 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15777 " %s %s mail_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15784 if (local_activity->activity_type > 0) {
15785 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15786 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15789 if (local_activity->activity_id > 0) {
15790 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15791 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15795 EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15796 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15797 if (error != EMAIL_ERROR_NONE) {
15798 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15802 rc = sqlite3_changes(local_db_handle);
15804 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15805 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15811 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15813 if (hStmt != NULL) {
15814 rc = sqlite3_finalize(hStmt);
15815 if (rc != SQLITE_OK) {
15816 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15817 error = EMAIL_ERROR_DB_FAILURE;
15821 if (err_code != NULL)
15823 EM_DEBUG_FUNC_END("ret [%d]", ret);
15828 * emstorage_free_local_activity - Free the Local Activity data
15830 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15832 EM_DEBUG_FUNC_BEGIN();
15834 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15837 int error = EMAIL_ERROR_INVALID_PARAM;
15840 if (!local_activity_list || !*local_activity_list) {
15841 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15843 error = EMAIL_ERROR_INVALID_PARAM;
15847 emstorage_activity_tbl_t* p = *local_activity_list;
15850 for (; i < count; i++) {
15851 EM_SAFE_FREE(p[i].dest_mbox);
15852 EM_SAFE_FREE(p[i].src_mbox);
15853 EM_SAFE_FREE(p[i].server_mailid);
15857 *local_activity_list = NULL;
15865 if (err_code != NULL)
15868 EM_DEBUG_FUNC_END("ret [%d]", ret);
15872 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15875 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15877 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15879 int ret = EMAIL_ERROR_NONE;
15880 char key_value_string[QUERY_SIZE] = { 0, };
15881 char *temp_key_value_1 = NULL;
15882 char *temp_key_value_2 = NULL;
15884 if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15885 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15886 return EMAIL_ERROR_INVALID_PARAM;
15889 switch (input_list_filter_rule->target_attribute) {
15890 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15891 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15892 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15893 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15894 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15895 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15896 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15897 case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15898 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15899 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15900 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15901 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15902 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15903 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15904 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15905 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15906 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15907 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15908 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15909 case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15910 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15911 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15912 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15913 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15914 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15915 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15916 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15917 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15918 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15919 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15920 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15921 case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15922 case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15923 case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15924 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15927 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15928 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15929 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15930 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15931 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15932 case EMAIL_MAIL_ATTRIBUTE_FROM:
15933 case EMAIL_MAIL_ATTRIBUTE_TO:
15934 case EMAIL_MAIL_ATTRIBUTE_CC:
15935 case EMAIL_MAIL_ATTRIBUTE_BCC:
15936 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15937 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15938 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15939 ret = EMAIL_ERROR_INVALID_PARAM;
15943 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15945 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15946 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15948 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15949 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15951 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15954 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15955 case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15956 case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15957 case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15958 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15962 ret = EMAIL_ERROR_INVALID_PARAM;
15963 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15967 if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0)
15968 *output_key_value_string = strdup(key_value_string);
15972 EM_SAFE_FREE(temp_key_value_1);
15973 EM_SAFE_FREE(temp_key_value_2);
15975 EM_DEBUG_FUNC_END("ret [%d]", ret);
15979 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15980 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15982 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15984 if (index <= 0 || !cert_password_file_name) {
15985 EM_DEBUG_EXCEPTION("Invalid parameter");
15986 return EMAIL_ERROR_INVALID_PARAM;
15989 sprintf(cert_password_file_name, ".email_cert_%d", index);
15991 EM_DEBUG_FUNC_END();
15992 return EMAIL_ERROR_NONE;
15996 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15998 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16000 int ret = EMAIL_ERROR_NONE;
16002 int length_field_name = 0;
16003 int length_value = 0;
16004 char result_rule_string[QUERY_SIZE] = { 0 , };
16005 char *mod_field_name_string = NULL;
16006 char *mod_value_string = NULL;
16007 char *temp_field_name_string = NULL;
16008 char *temp_key_value_string = NULL;
16010 if (input_list_filter_rule == NULL || output_string == NULL) {
16011 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16012 return EMAIL_ERROR_INVALID_PARAM;
16015 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
16017 if (temp_field_name_string == NULL) {
16018 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
16019 return EMAIL_ERROR_INVALID_PARAM;
16022 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) {
16023 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
16024 return EMAIL_ERROR_INVALID_PARAM;
16027 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16028 length_value = EM_SAFE_STRLEN(temp_key_value_string);
16030 switch (input_list_filter_rule->target_attribute) {
16031 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16032 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16033 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16034 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16035 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16036 case EMAIL_MAIL_ATTRIBUTE_FROM:
16037 case EMAIL_MAIL_ATTRIBUTE_TO:
16038 case EMAIL_MAIL_ATTRIBUTE_CC:
16039 case EMAIL_MAIL_ATTRIBUTE_BCC:
16040 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16048 if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16049 length_field_name += strlen("UPPER() ");
16050 length_value += strlen("UPPER() ");
16051 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16052 if (mod_field_name_string == NULL) {
16053 EM_DEBUG_EXCEPTION("em_mallocfailed");
16054 EM_SAFE_FREE(temp_field_name_string);
16055 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16059 mod_value_string = em_malloc(sizeof(char) * length_value);
16060 if (mod_value_string == NULL) {
16061 EM_DEBUG_EXCEPTION("em_mallocfailed");
16062 EM_SAFE_FREE(temp_field_name_string);
16063 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16067 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16068 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
16069 EM_SAFE_FREE(temp_key_value_string);
16071 mod_field_name_string = strdup(temp_field_name_string);
16072 mod_value_string = temp_key_value_string;
16075 switch (input_list_filter_rule->rule_type) {
16076 case EMAIL_LIST_FILTER_RULE_EQUAL:
16077 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16079 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16080 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16082 case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16083 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16085 case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16086 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16088 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16089 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16091 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16092 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16094 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16095 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16097 case EMAIL_LIST_FILTER_RULE_IN:
16098 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16100 case EMAIL_LIST_FILTER_RULE_NOT_IN:
16101 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16104 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16105 ret = EMAIL_ERROR_INVALID_PARAM;
16109 *output_string = strdup(result_rule_string);
16112 EM_SAFE_FREE(mod_field_name_string);
16113 EM_SAFE_FREE(mod_value_string);
16115 EM_DEBUG_FUNC_END("ret [%d]", ret);
16119 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16121 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16123 char *field_name_string = NULL;
16124 char key_value_string[QUERY_SIZE] = {0,};
16125 char result_rule_string[QUERY_SIZE] = {0,};
16128 int query_size = 0;
16131 int error = EMAIL_ERROR_NONE;
16132 char **result = NULL;
16133 char sql_query_string[QUERY_SIZE] = {0,};
16134 char *sql_query_string2 = NULL;
16135 sqlite3 *local_db_handle = NULL;
16136 int *mail_ids = NULL;
16138 if (input_list_filter_rule == NULL || output_string == NULL) {
16139 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16140 return EMAIL_ERROR_INVALID_PARAM;
16143 field_name_string = EM_SAFE_STRDUP("attachment_name");
16144 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16146 switch (input_list_filter_rule->rule_type) {
16148 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16149 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16152 case EMAIL_LIST_FILTER_RULE_MATCH:
16153 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16157 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16158 error = EMAIL_ERROR_INVALID_PARAM;
16162 local_db_handle = emstorage_get_db_connection(multi_user_name);
16164 EMSTORAGE_START_READ_TRANSACTION(true);
16165 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16167 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16168 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16169 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16174 EM_DEBUG_LOG("No mail found...");
16175 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16176 *output_string = strdup("mail_id IN () ");
16180 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16182 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16183 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16184 error = EMAIL_ERROR_OUT_OF_MEMORY;
16188 for (i = 0; i < count; i++) {
16189 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16190 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16193 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16194 sqlite3_free_table(result);
16195 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16197 // sqlite3_db_release_memory(local_db_handle);
16200 query_size = (10 * count) + strlen("mail_id IN () ");
16202 sql_query_string2 = em_malloc(query_size);
16203 if (sql_query_string2 == NULL) {
16204 EM_DEBUG_EXCEPTION("em_mallocfailed");
16205 error = EMAIL_ERROR_OUT_OF_MEMORY;
16209 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16210 for (i = 0; i < count-1; i++)
16211 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16213 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16215 *output_string = strdup(sql_query_string2);
16218 EM_SAFE_FREE(mail_ids); /* prevent */
16219 EM_SAFE_FREE(sql_query_string2);
16220 EM_SAFE_FREE(field_name_string);
16221 EM_DEBUG_FUNC_END("error [%d]", error);
16225 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16227 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16228 char *field_name_string = NULL;
16229 char key_value_string[QUERY_SIZE] = {0,};
16230 char result_rule_string[QUERY_SIZE] = {0,};
16234 int query_size = 0;
16235 int error = EMAIL_ERROR_NONE;
16236 char **result = NULL;
16237 char sql_query_string[QUERY_SIZE] = {0,};
16238 char *sql_query_string2 = NULL;
16239 sqlite3 *local_db_handle = NULL;
16240 int *mail_ids = NULL;
16242 if (input_list_filter_rule == NULL || output_string == NULL) {
16243 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16244 return EMAIL_ERROR_INVALID_PARAM;
16247 field_name_string = EM_SAFE_STRDUP("body_text");
16248 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16250 switch (input_list_filter_rule->rule_type) {
16252 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16253 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16256 case EMAIL_LIST_FILTER_RULE_MATCH:
16257 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16261 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16262 error = EMAIL_ERROR_INVALID_PARAM;
16266 local_db_handle = emstorage_get_db_connection(multi_user_name);
16268 EMSTORAGE_START_READ_TRANSACTION(true);
16269 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16271 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16272 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16273 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16278 EM_DEBUG_LOG("No mail found...");
16279 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16280 *output_string = strdup("mail_id IN () ");
16284 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16287 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16288 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16289 error = EMAIL_ERROR_OUT_OF_MEMORY;
16293 for (i = 0; i < count; i++) {
16294 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16295 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16298 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16299 sqlite3_free_table(result);
16300 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16302 // sqlite3_db_release_memory(local_db_handle);
16305 query_size = (10 * count) + strlen("mail_id IN () ");
16306 sql_query_string2 = em_malloc(query_size);
16307 if (sql_query_string2 == NULL) {
16308 EM_DEBUG_EXCEPTION("em_mallocfailed");
16309 error = EMAIL_ERROR_OUT_OF_MEMORY;
16313 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16314 for (i = 0; i < count-1; i++)
16315 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16317 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16319 *output_string = strdup(sql_query_string2);
16322 EM_SAFE_FREE(mail_ids); /* prevent */
16323 EM_SAFE_FREE(sql_query_string2);
16324 EM_SAFE_FREE(field_name_string);
16325 EM_DEBUG_FUNC_END("error [%d]", error);
16329 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16331 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16333 char result_rule_string[QUERY_SIZE] = { 0 , };
16334 int ret = EMAIL_ERROR_NONE;
16336 emstorage_account_tbl_t *account_tbl_array = NULL;
16339 char *result_str = NULL;
16340 char *tmp_str1 = NULL;
16341 char *tmp_str2 = NULL;
16342 char query_per_account[QUERY_SIZE] = { 0 , };
16344 if (input_sorting_rule->force_boolean_check) {
16345 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ",
16346 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16348 EM_SAFE_STRNCPY(result_rule_string,
16349 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16350 QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16353 switch (input_sorting_rule->sort_order) {
16354 case EMAIL_SORT_ORDER_ASCEND:
16355 EM_SAFE_STRNCAT(result_rule_string, " ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16358 case EMAIL_SORT_ORDER_DESCEND:
16359 EM_SAFE_STRNCAT(result_rule_string, " DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16362 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16363 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16366 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16367 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16370 case EMAIL_SORT_ORDER_TO_CCBCC:
16371 memset(result_rule_string, 0, QUERY_SIZE);
16372 if (input_sorting_rule->key_value.string_type_value)
16373 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16374 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16375 input_sorting_rule->key_value.string_type_value);
16378 case EMAIL_SORT_ORDER_TO_CC_BCC:
16379 memset(result_rule_string, 0, QUERY_SIZE);
16380 if (input_sorting_rule->key_value.string_type_value)
16381 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16382 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16383 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16386 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16387 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16388 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16393 EM_DEBUG_LOG("No account exist");
16394 ret = EMAIL_ERROR_INVALID_PARAM;
16398 for (i = 0; i < count; i++) {
16399 if (i > 0 && result_str) {
16400 tmp_str2 = result_str;
16401 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16402 EM_SAFE_FREE(tmp_str2);
16405 memset(query_per_account, 0, QUERY_SIZE);
16406 snprintf(query_per_account, QUERY_SIZE,
16407 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16408 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16410 tmp_str1 = result_str;
16412 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16414 result_str = g_strdup(query_per_account);
16415 EM_SAFE_FREE(tmp_str1);
16418 snprintf(result_rule_string, QUERY_SIZE,
16419 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16421 EM_SAFE_FREE(result_str);
16422 if (account_tbl_array)
16423 emstorage_free_account(&account_tbl_array, count, NULL);
16426 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16427 memset(result_rule_string, 0, QUERY_SIZE);
16428 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16429 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16430 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16431 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16434 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16435 memset(result_rule_string, 0, QUERY_SIZE);
16436 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16437 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16438 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16439 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16443 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16444 ret = EMAIL_ERROR_INVALID_PARAM;
16448 *output_string = strdup(result_rule_string);
16451 EM_DEBUG_FUNC_END("ret [%d]", ret);
16455 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)
16457 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);
16458 int ret = EMAIL_ERROR_NONE;
16460 int string_offset = 0;
16461 int query_size = 0;
16462 int new_query_size = 0;
16463 char *conditional_clause_string = NULL;
16464 char *result_string_for_a_item = NULL;
16466 if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16467 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16468 return EMAIL_ERROR_INVALID_PARAM;
16471 conditional_clause_string = em_malloc(QUERY_SIZE);
16472 if (conditional_clause_string == NULL) {
16473 EM_DEBUG_EXCEPTION("Memory is full");
16474 return EMAIL_ERROR_OUT_OF_MEMORY;
16477 if (input_filter_count > 0) {
16478 query_size = QUERY_SIZE;
16479 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16481 for (i = 0; i < input_filter_count; i++) {
16482 switch (input_filter_list[i].list_filter_item_type) {
16483 case EMAIL_LIST_FILTER_ITEM_RULE:
16484 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16485 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16488 case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16489 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16490 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16493 case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16494 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16495 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16498 case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16499 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16500 switch (input_filter_list[i].list_filter_item.operator_type) {
16501 case EMAIL_LIST_FILTER_OPERATOR_AND:
16502 result_string_for_a_item = strdup("AND ");
16504 case EMAIL_LIST_FILTER_OPERATOR_OR:
16505 result_string_for_a_item = strdup("OR ");
16507 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16508 result_string_for_a_item = strdup(" (");
16510 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16511 result_string_for_a_item = strdup(") ");
16517 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16518 ret = EMAIL_ERROR_INVALID_PARAM;
16522 if (result_string_for_a_item == NULL) {
16523 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16524 ret = EMAIL_ERROR_INVALID_PARAM;
16528 if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16529 EM_DEBUG_LOG("QUERY is too long");
16530 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16531 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16532 if (conditional_clause_string == NULL) {
16533 EM_DEBUG_EXCEPTION("realloc failed");
16534 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16538 query_size = new_query_size;
16541 EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16542 EM_SAFE_FREE(result_string_for_a_item);
16546 if (input_sorting_rule_count > 0) {
16547 EM_SAFE_STRNCAT(conditional_clause_string, "ORDER BY ", QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16549 for (i = 0; i < input_sorting_rule_count; i++) {
16550 if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16551 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16555 EM_SAFE_STRNCAT(conditional_clause_string, ", " , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16556 EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16557 EM_SAFE_FREE(result_string_for_a_item);
16561 if (input_start_index != -1 && input_limit_count != -1) {
16562 string_offset = strlen(conditional_clause_string);
16563 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16566 *output_conditional_clause = strdup(conditional_clause_string);
16569 EM_SAFE_FREE(result_string_for_a_item);
16570 EM_SAFE_FREE(conditional_clause_string);
16572 EM_DEBUG_FUNC_END("ret [%d]", ret);
16576 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16578 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16579 int err = EMAIL_ERROR_NONE;
16581 email_list_filter_t *temp_filter_list = NULL;
16583 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16585 for (i = 0; i < input_filter_count; i++) {
16586 temp_filter_list = (*input_filter_list) + i;
16587 if (!temp_filter_list)
16590 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16591 switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16592 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16593 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16594 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16595 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16596 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16597 case EMAIL_MAIL_ATTRIBUTE_FROM:
16598 case EMAIL_MAIL_ATTRIBUTE_TO:
16599 case EMAIL_MAIL_ATTRIBUTE_CC:
16600 case EMAIL_MAIL_ATTRIBUTE_BCC:
16601 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16602 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16603 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16604 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16609 } 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) {
16610 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16611 } 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) {
16612 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16616 EM_SAFE_FREE(*input_filter_list);
16618 EM_DEBUG_FUNC_END("err [%d]", err);
16622 /* Tasks --------------------------------------------------------------------------*/
16623 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)
16625 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);
16629 int err = EMAIL_ERROR_NONE;
16631 DB_STMT hStmt = NULL;
16632 char sql_query_string[QUERY_SIZE] = {0, };
16633 sqlite3 *local_db_handle = NULL;
16634 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16635 char **result = NULL;
16637 if (input_task_parameter == NULL || output_task_id == NULL) {
16638 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16639 return EMAIL_ERROR_INVALID_PARAM;
16642 local_db_handle = emstorage_get_db_connection(multi_user_name);
16643 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16645 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16646 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16647 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16649 if (NULL == result[1])
16652 task_id = atoi(result[1])+1;
16654 *output_task_id = task_id;
16656 sqlite3_free_table(result);
16659 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16660 "INSERT INTO mail_task_tbl VALUES "
16662 " ? " /* task_id */
16663 " , ? " /* task_type */
16664 " , ? " /* task_status */
16665 " , ? " /* task_priority */
16666 " , ? " /* task_parameter_length */
16667 " , ? " /* task_parameter */
16668 " , ? " /* date_time */
16671 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16672 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16673 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16675 EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16678 _bind_stmt_field_data_int(hStmt, i++, task_id);
16679 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16680 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16681 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16682 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16683 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16684 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16686 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16688 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16689 ("sqlite3_step fail:%d", rc));
16690 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16691 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16693 ret = (err == EMAIL_ERROR_NONE);
16696 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16698 if (hStmt != NULL) {
16699 rc = sqlite3_finalize(hStmt);
16700 if (rc != SQLITE_OK) {
16701 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16702 err = EMAIL_ERROR_DB_FAILURE;
16706 EM_DEBUG_FUNC_END("err [%d]", err);
16710 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16712 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16714 int err = EMAIL_ERROR_NONE;
16715 char sql_query_string[QUERY_SIZE] = {0, };
16717 sqlite3 *local_db_handle = NULL;
16720 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16721 return EMAIL_ERROR_INVALID_PARAM;
16724 local_db_handle = emstorage_get_db_connection(multi_user_name);
16726 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16728 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16729 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16730 if (err != EMAIL_ERROR_NONE) {
16731 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16738 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16740 EM_DEBUG_FUNC_END("err [%d]", err);
16744 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16746 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16748 int err = EMAIL_ERROR_NONE;
16749 char sql_query_string[QUERY_SIZE] = {0, };
16751 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16752 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16754 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16755 "UPDATE mail_task_tbl SET"
16756 " task_status = %d"
16757 " WHERE task_id = %d"
16760 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16761 if (err != EMAIL_ERROR_NONE) {
16762 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16769 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16771 EM_DEBUG_FUNC_END("err [%d]", err);
16775 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16776 const char *input_conditional_clause,
16777 const char *input_ordering_clause,
16778 email_task_t **output_task_list,
16779 int *output_task_count)
16781 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16782 "output_task_list[%p], output_task_count[%d]",
16783 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16784 int i = 0, count = 0, rc = -1;
16786 int field_index = 0;
16787 int err = EMAIL_ERROR_NONE;
16788 email_task_t *task_item_from_tbl = NULL;
16789 char sql_query_string[QUERY_SIZE] = {0, };
16790 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16792 sqlite3 *local_db_handle = NULL;
16793 DB_STMT hStmt = NULL;
16795 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16796 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16798 local_db_handle = emstorage_get_db_connection(multi_user_name);
16800 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16801 "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16802 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16804 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16805 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16806 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16808 count = atoi(result[1]);
16809 sqlite3_free_table(result);
16811 EM_DEBUG_LOG("count = %d", rc);
16814 EM_DEBUG_EXCEPTION("no task found...");
16815 err = EMAIL_ERROR_TASK_NOT_FOUND;
16819 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16820 "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16821 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16823 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16825 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16826 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16827 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16829 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16830 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16831 ("sqlite3_step fail:%d", rc));
16833 if (rc == SQLITE_DONE) {
16834 EM_DEBUG_EXCEPTION("no task found...");
16835 err = EMAIL_ERROR_TASK_NOT_FOUND;
16840 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16841 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16842 err = EMAIL_ERROR_OUT_OF_MEMORY;
16846 for (i = 0; i < count; i++) {
16847 /* get recordset */
16850 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16851 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16852 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16853 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16854 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16855 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16857 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16858 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
16859 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16860 ("sqlite3_step fail:%d", rc));
16865 if (err == EMAIL_ERROR_NONE && output_task_list) {
16866 *output_task_list = task_item_from_tbl;
16867 *output_task_count = count;
16869 if (task_item_from_tbl) {
16870 for (i = 0; i < count; i++)
16871 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16873 free(task_item_from_tbl);
16877 if (hStmt != NULL) {
16878 rc = sqlite3_finalize(hStmt);
16880 if (rc != SQLITE_OK) {
16881 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16882 err = EMAIL_ERROR_DB_FAILURE;
16886 EM_DEBUG_FUNC_END("err [%d]", err);
16890 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)
16892 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);
16893 int err = EMAIL_ERROR_NONE;
16895 if (message_id == NULL) {
16896 EM_DEBUG_EXCEPTION("Invalid parameter");
16897 err = EMAIL_ERROR_INVALID_PARAM;
16903 int temp_mail_id = 0;
16904 int where_pararaph_length = 0;
16905 char *where_pararaph = NULL;
16906 char sql_query_string[QUERY_SIZE] = {0, };
16907 char **result = NULL;
16908 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16910 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16912 where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16913 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16914 if (where_pararaph == NULL) {
16915 EM_DEBUG_EXCEPTION("em_mallocfailed");
16916 err = EMAIL_ERROR_OUT_OF_MEMORY;
16920 if (account_id != ALL_ACCOUNT)
16921 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);
16923 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16925 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16926 EM_SAFE_STRNCAT(sql_query_string, where_pararaph , QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16928 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16930 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16931 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16932 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16935 EM_DEBUG_LOG("Count of mails [%d]", count);
16938 _get_table_field_data_int(result, &temp_mail_id, 1);
16939 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16941 memset(sql_query_string, 0x00, QUERY_SIZE);
16942 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16944 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE) {
16945 EM_SAFE_STRNCAT(sql_query_string, where_pararaph,
16946 QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16949 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16950 if (err != EMAIL_ERROR_NONE) {
16951 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16956 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16961 sqlite3_free_table(result);
16965 EM_SAFE_FREE(where_pararaph);
16967 if (mail_id != NULL)
16968 *mail_id = temp_mail_id;
16970 EM_DEBUG_FUNC_END("err : [%d]", err);
16973 /* Tasks --------------------------------------------------------------------------*/
16975 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16976 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)
16978 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16980 if (!local_activity || !activity_id) {
16981 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16982 if (err_code != NULL)
16983 *err_code = EMAIL_ERROR_INVALID_PARAM;
16989 int error = EMAIL_ERROR_NONE;
16992 char sql_query_string[QUERY_SIZE] = {0, };
16993 DB_STMT hStmt = NULL;
16995 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16996 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16998 memset(sql_query_string, 0x00, sizeof(sql_query_string));
16999 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17000 "INSERT INTO mail_auto_download_activity_tbl VALUES "
17002 "? " /* Activity ID */
17004 ",?" /* Account ID */
17005 ",?" /* Local Mail ID */
17006 ",?" /* Server mail ID */
17007 ",?" /* Mailbox ID*/
17008 ",?" /* Multi USER NAME */
17011 char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
17012 char **result = NULL;
17015 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17017 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17018 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17020 if (NULL == result[1]) rc = 1;
17021 else rc = atoi(result[1])+1;
17022 sqlite3_free_table(result);
17025 *activity_id = local_activity->activity_id = rc;
17027 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
17028 local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
17030 if (local_activity->mailbox_id)
17031 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17034 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17035 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17036 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17039 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17040 _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17041 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17042 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17043 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17044 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17045 _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17048 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17050 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17051 ("sqlite3_step fail:%d", rc));
17052 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17053 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17058 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17059 if (hStmt != NULL) {
17060 rc = sqlite3_finalize(hStmt);
17062 if (rc != SQLITE_OK) {
17063 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17064 error = EMAIL_ERROR_DB_FAILURE;
17068 if (err_code != NULL)
17071 EM_DEBUG_FUNC_END("ret [%d]", ret);
17076 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)
17078 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);
17080 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17081 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);
17083 if (err_code != NULL)
17084 *err_code = EMAIL_ERROR_INVALID_PARAM;
17090 int error = EMAIL_ERROR_NONE;
17091 char sql_query_string[QUERY_SIZE] = {0, };
17092 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17094 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17095 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17097 if (activity_id == 0)
17098 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);
17100 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);
17102 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17103 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17104 if (error != EMAIL_ERROR_NONE) {
17105 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17109 /* validate activity existence */
17110 rc = sqlite3_changes(local_db_handle);
17112 EM_DEBUG_EXCEPTION("No matching activity found");
17113 error = EMAIL_ERROR_DATA_NOT_FOUND;
17121 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17123 if (err_code != NULL)
17126 EM_DEBUG_FUNC_END("ret [%d]", ret);
17130 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17132 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17134 if (account_id < FIRST_ACCOUNT_ID) {
17135 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17136 if (err_code != NULL)
17137 *err_code = EMAIL_ERROR_INVALID_PARAM;
17143 int error = EMAIL_ERROR_NONE;
17144 char sql_query_string[QUERY_SIZE] = {0, };
17146 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17148 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17149 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17150 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17152 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17153 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17154 if (error != EMAIL_ERROR_NONE) {
17155 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17159 rc = sqlite3_changes(local_db_handle);
17161 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17162 error = EMAIL_ERROR_DATA_NOT_FOUND;
17170 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17172 if (err_code != NULL)
17175 EM_DEBUG_FUNC_END("ret [%d]", ret);
17180 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)
17182 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17184 if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17185 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17186 if (err_code != NULL)
17187 *err_code = EMAIL_ERROR_INVALID_PARAM;
17193 int error = EMAIL_ERROR_NONE;
17194 char sql_query_string[QUERY_SIZE] = {0, };
17196 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17198 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17199 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17200 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);
17202 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17203 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17204 if (error != EMAIL_ERROR_NONE) {
17205 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17209 rc = sqlite3_changes(local_db_handle);
17211 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17212 error = EMAIL_ERROR_DATA_NOT_FOUND;
17220 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17222 if (err_code != NULL)
17225 EM_DEBUG_FUNC_END("ret [%d]", ret);
17230 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)
17232 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17234 if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17235 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);
17237 if (err_code != NULL)
17238 *err_code = EMAIL_ERROR_INVALID_PARAM;
17245 int error = EMAIL_ERROR_NONE;
17247 DB_STMT hStmt = NULL;
17248 email_event_auto_download *event_list = NULL;
17249 char sql_query_string[QUERY_SIZE] = {0, };
17251 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17253 EMSTORAGE_START_READ_TRANSACTION(transaction);
17255 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17256 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);
17259 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17260 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17261 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17263 *count = atoi(result[1]);
17264 sqlite3_free_table(result);
17266 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17269 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17270 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17274 EM_DEBUG_LOG("Activity Count = %d", *count);
17276 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17277 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);
17279 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17282 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17284 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17285 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17286 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17289 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17290 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17291 ("sqlite3_step fail:%d", rc));
17293 if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17294 EM_DEBUG_EXCEPTION("Malloc failed");
17296 error = EMAIL_ERROR_OUT_OF_MEMORY;
17300 for (i = 0; i < (*count); i++) {
17301 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17302 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17303 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17304 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17305 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17306 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17308 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17309 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17310 ("sqlite3_step fail:%d", rc));
17318 *event_start = event_list;
17320 EM_SAFE_FREE(event_list);
17321 *event_start = NULL;
17325 if (hStmt != NULL) {
17326 rc = sqlite3_finalize(hStmt);
17328 if (rc != SQLITE_OK) {
17329 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17330 error = EMAIL_ERROR_DB_FAILURE;
17334 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17335 if (err_code != NULL)
17338 EM_DEBUG_FUNC_END("ret [%d]", ret);
17343 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17345 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17347 if (!activity_count || !err_code) {
17348 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17349 if (err_code != NULL)
17350 *err_code = EMAIL_ERROR_INVALID_PARAM;
17356 int error = EMAIL_ERROR_NONE;
17357 DB_STMT hStmt = NULL;
17358 char sql_query_string[QUERY_SIZE] = {0, };
17360 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17362 EMSTORAGE_START_READ_TRANSACTION(transaction);
17363 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17365 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17367 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17370 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17371 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17372 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17373 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17376 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17377 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17378 ("sqlite3_step fail:%d", rc));
17380 _get_stmt_field_data_int(hStmt, activity_count, 0);
17382 EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17388 if (hStmt != NULL) {
17389 rc = sqlite3_finalize(hStmt);
17391 if (rc != SQLITE_OK) {
17392 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17393 error = EMAIL_ERROR_DB_FAILURE;
17397 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17398 if (err_code != NULL)
17401 EM_DEBUG_FUNC_END("ret [%d]", ret);
17406 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17408 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17410 if (!account_list || !count) {
17411 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17412 if (err_code != NULL)
17413 *err_code = EMAIL_ERROR_INVALID_PARAM;
17418 int error = EMAIL_ERROR_NONE;
17419 char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17421 int i = 0, rc = -1;
17422 int *result_account_list = NULL;
17423 DB_STMT hStmt = NULL;
17424 char sql_query_string[QUERY_SIZE] = {0, };
17425 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17427 EMSTORAGE_START_READ_TRANSACTION(transaction);
17429 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17430 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17431 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17433 *count = atoi(result[1]);
17434 sqlite3_free_table(result);
17437 EM_DEBUG_EXCEPTION("no account found...");
17438 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17443 EM_DEBUG_LOG("Account count [%d]", *count);
17445 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17447 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17449 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17452 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17454 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17455 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17456 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17459 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17460 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17461 ("sqlite3_step fail:%d", rc));
17463 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17464 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17465 error = EMAIL_ERROR_OUT_OF_MEMORY;
17469 for (i = 0; i < (*count); i++) {
17470 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17472 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17473 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17474 ("sqlite3_step fail:%d", rc));
17475 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17483 *account_list = result_account_list;
17485 EM_SAFE_FREE(result_account_list);
17487 if (hStmt != NULL) {
17488 rc = sqlite3_finalize(hStmt);
17490 if (rc != SQLITE_OK) {
17491 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17492 error = EMAIL_ERROR_DB_FAILURE;
17496 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17497 if (err_code != NULL)
17499 EM_DEBUG_FUNC_END("ret [%d]", ret);
17504 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)
17506 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17508 if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17509 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17510 if (err_code != NULL)
17511 *err_code = EMAIL_ERROR_INVALID_PARAM;
17516 int error = EMAIL_ERROR_NONE;
17518 int i = 0, rc = -1;
17519 int *mbox_list = NULL;
17520 DB_STMT hStmt = NULL;
17521 char sql_query_string[QUERY_SIZE] = {0, };
17523 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17525 EMSTORAGE_START_READ_TRANSACTION(transaction);
17527 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17528 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);
17531 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17532 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17533 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17535 *count = atoi(result[1]);
17536 sqlite3_free_table(result);
17539 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17540 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17544 EM_DEBUG_LOG("Mailbox count = %d", *count);
17546 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17548 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);
17550 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17553 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17556 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17557 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17560 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17561 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17562 ("sqlite3_step fail:%d", rc));
17564 mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17565 if (mbox_list == NULL) {
17566 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17567 error = EMAIL_ERROR_OUT_OF_MEMORY;
17571 for (i = 0; i < (*count); i++) {
17572 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17573 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17575 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17576 ("sqlite3_step fail:%d", rc));
17577 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17585 *mailbox_list = mbox_list;
17587 EM_SAFE_FREE(mbox_list);
17589 if (hStmt != NULL) {
17590 rc = sqlite3_finalize(hStmt);
17592 if (rc != SQLITE_OK) {
17593 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17594 error = EMAIL_ERROR_DB_FAILURE;
17598 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17599 if (err_code != NULL)
17601 EM_DEBUG_FUNC_END("ret [%d]", ret);
17606 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)
17608 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17610 if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17611 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17612 if (err_code != NULL)
17613 *err_code = EMAIL_ERROR_INVALID_PARAM;
17619 int error = EMAIL_ERROR_NONE;
17620 char sql_query_string[QUERY_SIZE] = {0, };
17621 DB_STMT hStmt = NULL;
17623 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17625 EMSTORAGE_START_READ_TRANSACTION(transaction);
17626 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17627 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);
17629 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17631 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17632 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17633 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17634 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17636 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17637 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17638 ("sqlite3_step fail:%d", rc));
17640 _get_stmt_field_data_int(hStmt, activity_count, 0);
17642 EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17648 if (hStmt != NULL) {
17649 rc = sqlite3_finalize(hStmt);
17651 if (rc != SQLITE_OK) {
17652 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17653 error = EMAIL_ERROR_DB_FAILURE;
17657 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17659 if (err_code != NULL)
17662 EM_DEBUG_FUNC_END("ret [%d]", ret);
17667 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)
17669 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17671 int rc = -1, ret = false;
17672 int error = EMAIL_ERROR_NONE;
17673 char sql_query_string[QUERY_SIZE] = {0, };
17674 int transaction = true;
17676 if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17677 EM_DEBUG_EXCEPTION("Invalid parameters");
17678 error = EMAIL_ERROR_INVALID_PARAM;
17682 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17684 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17685 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17687 if (mailbox_id > 0)
17688 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17689 "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);
17690 else if (mailbox_name)
17691 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17692 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17694 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17695 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17696 if (error != EMAIL_ERROR_NONE) {
17697 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17701 rc = sqlite3_changes(local_db_handle);
17703 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17709 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17711 if (err_code != NULL)
17714 EM_DEBUG_FUNC_END("ret [%d]", ret);
17720 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17725 } email_column_info_t;
17727 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17729 EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17733 char *column_name = NULL;
17734 char *column_type = NULL;
17735 GList *new_list = *((GList**)arg1);
17736 email_column_info_t *column_info_item = NULL;
17738 for (i = 0; i < argc; ++i) {
17739 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17740 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17742 EM_SAFE_FREE(column_name);
17744 column_name = EM_SAFE_STRDUP(argv[i]);
17746 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17748 EM_SAFE_FREE(column_type);
17750 column_type = EM_SAFE_STRDUP(argv[i]);
17755 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17756 column_info_item = em_malloc(sizeof(email_column_info_t));
17757 if (column_info_item == NULL) {
17758 EM_DEBUG_EXCEPTION("em_mallocfailed");
17762 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17763 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17764 new_list = g_list_append(new_list, (gpointer)column_info_item);
17765 *((GList**)arg1) = new_list;
17770 EM_SAFE_FREE(column_name);
17771 EM_SAFE_FREE(column_type);
17773 EM_DEBUG_FUNC_END();
17777 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17779 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17780 int err = EMAIL_ERROR_NONE;
17781 int result_from_sqlite = 0;
17782 char *error_message_from_sqlite = NULL;
17783 char sql_query_string[QUERY_SIZE] = {0, };
17784 GList *new_list = NULL;
17785 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17787 SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17789 result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17791 if (result_from_sqlite != SQLITE_OK)
17792 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17794 EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17796 if (new_list && output_column_info) {
17797 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17798 *output_column_info = new_list;
17801 EM_DEBUG_FUNC_END("err [%d]", err);
17805 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)
17807 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);
17808 int error = EMAIL_ERROR_NONE;
17810 sqlite3 *local_db_handle = NULL;
17811 char sql_query_string[QUERY_SIZE] = {0, };
17813 local_db_handle = emstorage_get_db_connection(multi_user_name);
17815 if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17816 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17817 error = EMAIL_ERROR_INVALID_PARAM;
17821 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17822 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17824 EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17826 EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17827 reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17829 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17832 if (error == EMAIL_ERROR_NONE) {
17833 EMSTORAGE_PROTECTED_FUNC_CALL(
17834 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17836 EMSTORAGE_PROTECTED_FUNC_CALL(
17837 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17840 EM_DEBUG_FUNC_END("error [%d]", error);
17844 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17846 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17847 int error = EMAIL_ERROR_NONE;
17849 sqlite3 *local_db_handle = NULL;
17850 char sql_query_string[QUERY_SIZE] = {0, };
17852 local_db_handle = emstorage_get_db_connection(multi_user_name);
17854 if (input_table_name == NULL || input_new_column == NULL) {
17855 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17856 error = EMAIL_ERROR_INVALID_PARAM;
17860 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17861 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17862 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);
17863 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17866 if (error == EMAIL_ERROR_NONE) {
17867 EMSTORAGE_PROTECTED_FUNC_CALL(
17868 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17870 EMSTORAGE_PROTECTED_FUNC_CALL(
17871 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17874 EM_DEBUG_FUNC_END("error [%d]", error);
17878 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17880 EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17881 int error = EMAIL_ERROR_NONE;
17883 sqlite3 *local_db_handle = NULL;
17884 char sql_query_string[QUERY_SIZE] = {0, };
17886 local_db_handle = emstorage_get_db_connection(multi_user_name);
17888 if (input_table_name == NULL) {
17889 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17890 error = EMAIL_ERROR_INVALID_PARAM;
17894 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17895 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17896 SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17897 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17900 if (error == EMAIL_ERROR_NONE) {
17901 EMSTORAGE_PROTECTED_FUNC_CALL(
17902 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17904 EMSTORAGE_PROTECTED_FUNC_CALL(
17905 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17908 EM_DEBUG_FUNC_END("error [%d]", error);
17912 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17914 EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17915 email_column_info_t *left_one = (email_column_info_t*)old_column_info;
17916 email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17918 if (old_column_info == NULL || new_column_info == NULL)
17921 return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17924 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17926 EM_DEBUG_FUNC_BEGIN();
17929 int error = EMAIL_ERROR_NONE;
17931 email_column_info_t *new_column_info = NULL;
17932 email_column_info_t *p_column_info = NULL;
17933 char **create_table_query = NULL;
17934 GList *found_data = NULL;
17935 GList *column_list_of_old_table = NULL;
17936 GList *column_list_of_new_table = NULL;
17937 char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17938 { "mail_box_tbl", "mail_box_tbl_new" },
17939 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17940 { "mail_rule_tbl", "mail_rule_tbl_new" },
17941 { "mail_tbl", "mail_tbl_new" },
17942 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17943 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17944 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17948 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17949 #ifdef __FEATURE_LOCAL_ACTIVITY__
17950 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17954 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17955 { "mail_task_tbl", "mail_task_tbl_new" },
17956 #ifdef __FEATURE_BODY_SEARCH__
17957 { "mail_text_tbl", "mail_text_tbl_new" },
17962 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17963 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17970 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17972 if (error != EMAIL_ERROR_NONE) {
17973 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17977 if (query_len < CREATE_TABLE_MAX) {
17978 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17979 error = EMAIL_ERROR_SYSTEM_FAILURE;
17983 for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17984 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17985 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17986 /* Check existing of _new table */
17987 emstorage_drop_table(multi_user_name, table_names[i][1]);
17988 error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17989 if (error != EMAIL_ERROR_NONE) {
17990 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17994 emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17995 emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17997 /* Compare fields and add new field */
17998 for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17999 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
18000 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
18001 if (found_data == NULL) {
18003 emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
18007 emstorage_drop_table(multi_user_name, table_names[i][1]);
18009 EM_DEBUG_LOG("Skipped");
18014 if (create_table_query) {
18016 for (i = 0; i < query_len; i++) {
18017 if (create_table_query[i])
18018 EM_SAFE_FREE(create_table_query[i]);
18020 EM_SAFE_FREE(create_table_query);
18023 found_data = g_list_first(column_list_of_old_table);
18024 while (found_data != NULL) {
18025 p_column_info = (email_column_info_t *)found_data->data;
18026 EM_SAFE_FREE(p_column_info->column_name);
18027 EM_SAFE_FREE(p_column_info->column_type);
18028 EM_SAFE_FREE(p_column_info);
18030 found_data = g_list_next(found_data);
18032 g_list_free(column_list_of_old_table);
18034 found_data = g_list_first(column_list_of_new_table);
18035 while (found_data != NULL) {
18036 p_column_info = (email_column_info_t *)found_data->data;
18037 EM_SAFE_FREE(p_column_info->column_name);
18038 EM_SAFE_FREE(p_column_info->column_type);
18039 EM_SAFE_FREE(p_column_info);
18041 found_data = g_list_next(found_data);
18043 g_list_free(column_list_of_new_table);
18045 EM_DEBUG_FUNC_END("error [%d]", error);
18048 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */